list.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const app = require('../../app')
  2. const {editIcon, createIcon, deleteIcon} = require('../../assets')
  3. /**
  4. * @param {CrudPagesOptions} opts
  5. */
  6. const list = (opts) => {
  7. const hideCriteria = column => column.routeParam ? `ctrl.$routeParams.${column.routeParam} !== '${opts.paramAll}'` : 'false'
  8. const defaultHeader = column =>
  9. html`<th ng-hide="${hideCriteria(column)}" md-column>${column.titleName}</th>`
  10. const defaultCell = column => {
  11. if (column.type === 'autocomplete') {
  12. return html`<td ng-hide="${hideCriteria(column)}" md-cell>{{${raw(opts.camelName)}.${raw(column.camelName)} && (ctrl.autocomplete.${raw(column.camelName)}.lookup[${raw(opts.camelName)}.${raw(column.camelName)}] || ctrl.autocomplete.${raw(column.camelName)}.load(${raw(opts.camelName)}.${raw(column.camelName)}))}}</td>`
  13. } else {
  14. return html`<td ng-hide="${hideCriteria(column)}" md-cell>{{${raw(opts.camelName)}.${raw(column.camelName)}}}</td>`
  15. }
  16. }
  17. const template = html`
  18. <app-user-area>
  19. <h1>${opts.titlePlural}</h1>
  20. <md-table-container>
  21. <table md-table md-row-select md-auto-select ng-model="ctrl.selected" md-progress="ctrl.promise">
  22. <thead md-head md-order="query.order" md-on-reorder="ctrl.getRecords">
  23. <tr md-row>
  24. ${opts.columns.filter(c => c.inList).map(c => c.header || defaultHeader(c))}
  25. <th md-column>Actions</th>
  26. </tr>
  27. </thead>
  28. <tbody md-body>
  29. <tr md-row md-select="${opts.camelName}" md-select-id="name" md-auto-select ng-repeat="${raw(opts.camelName)} in ctrl.data track by ${raw(opts.camelName)}.id">
  30. ${opts.columns.filter(c => c.inList).map(c => c.cell || defaultCell(c))}
  31. <td md-cell>
  32. <md-button ng-href="{{::ctrl.appPrefix}}/{{${raw(opts.camelName)}.id}}">
  33. <md-icon md-svg-icon="${editIcon}"></md-icon>
  34. Edit
  35. </md-button>
  36. <md-button ng-click="ctrl.delete(${raw(opts.camelName)})">
  37. <md-icon md-svg-src="${deleteIcon}"></md-icon>
  38. Delete
  39. </md-button>
  40. </td>
  41. </tr>
  42. </tbody>
  43. </table>
  44. </md-table-container>
  45. <div layout="row" layout-align="end">
  46. <md-button class="md-fab" aria-label="Add ${opts.titleName}" ng-href="{{::ctrl.appPrefix}}/new">
  47. <md-icon md-svg-src="${createIcon}"></md-icon>
  48. </md-button>
  49. </div>
  50. </app-user-area>
  51. `
  52. console.log(`crud: app${opts.pascalPlural}Page`)
  53. app.component(`app${opts.pascalPlural}Page`, {
  54. template,
  55. controllerAs: 'ctrl',
  56. controller: function(api, $mdToast, $routeParams, util, $interpolate, $scope) {
  57. this.$routeParams = $routeParams
  58. this.apiPrefix = util.fillPath(opts.apiPrefix, $routeParams)
  59. this.appPrefix = util.fillPath(opts.appPrefix, $routeParams)
  60. const crud = api.crud(this.apiPrefix)
  61. this.template = template
  62. this.selected = []
  63. this.data = []
  64. this.getRecords = () => {
  65. this.promise = crud.list().then(data => {
  66. this.data = data
  67. })
  68. }
  69. this.delete = async (record) => {
  70. try {
  71. await crud.delete(record.id)
  72. $mdToast.showSimple(`${opts.titleName} deleted.`)
  73. this.data = this.data.filter(x => x.id !== record.id)
  74. } catch (err) {
  75. console.error(err)
  76. $mdToast.showSimple(`Could not delete ${opts.titleName}: ${err.message || err}`)
  77. }
  78. }
  79. this.getRecords()
  80. /* Autocomplete fields */
  81. this.autocomplete = {}
  82. opts.columns.filter(c => c.type === 'autocomplete').forEach(c => {
  83. const crud = api.crud(util.fillPath(c.apiPrefix, $routeParams))
  84. let timer = null
  85. let ids = []
  86. const load = () => {
  87. timer = null
  88. console.log('Loading', ids)
  89. crud.lookup(ids).then(records => {
  90. records.forEach(rec => {
  91. ac.lookup[rec.id] = rec.name || rec.key || rec.id
  92. })
  93. })
  94. }
  95. const ac = this.autocomplete[c.camelName] = {
  96. lookup: {},
  97. load(id) {
  98. if (!ac.lookup.hasOwnProperty(id)) {
  99. console.log('Queuing to load', id)
  100. ac.lookup[id] = '...'
  101. if (!ids.includes(id)) ids.push(id)
  102. if (!timer) timer = setTimeout(load)
  103. }
  104. return ac.lookup[id]
  105. }
  106. }
  107. })
  108. }
  109. })
  110. }
  111. module.exports = list