trash.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. const app = require('../../app')
  2. const { undoIcon} = require('../../assets')
  3. /**
  4. * @param {CrudPagesOptions} opts
  5. */
  6. const list = (opts) => {
  7. const defaultHeader = column => html`<th md-column>${column.titleName}</th>`
  8. const defaultCell = column => html`<td md-cell>{{${raw(opts.camelName)}.${raw(column.camelName)}}}</td>`
  9. console.log(`crud: app${opts.pascalPlural}TrashPage`)
  10. app.component(`app${opts.pascalPlural}TrashPage`, {
  11. template: html`
  12. <app-user-area title-text="${opts.titlePlural} Trash">
  13. <md-table-container style="width: 100%">
  14. <table md-table md-row-select md-auto-select md-multiple ng-model="ctrl.selected" md-progress="ctrl.promise">
  15. <thead md-head md-order="query.order" md-on-reorder="ctrl.getRecords">
  16. <tr md-row>
  17. ${opts.columns.filter(c => c.inList).map(c => c.header || defaultHeader(c))}
  18. <th md-column>Actions</th>
  19. </tr>
  20. </thead>
  21. <tbody md-body>
  22. <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">
  23. ${opts.columns.filter(c => c.inList).map(c => c.cell || defaultCell(c))}
  24. <td md-cell>
  25. <md-button ng-click="ctrl.undelete(${raw(opts.camelName)})">
  26. <md-icon md-svg-icon="${undoIcon}"></md-icon>
  27. Undelete
  28. </md-button>
  29. </td>
  30. </tr>
  31. </tbody>
  32. </table>
  33. </md-table-container>
  34. </app-user-area>
  35. `,
  36. controllerAs: 'ctrl',
  37. controller: function(api, $mdToast, $location, util, $routeParams) {
  38. this.apiPrefix = util.fillPath(opts.apiPrefix, $routeParams)
  39. this.appPrefix = util.fillPath(opts.appPrefix, $routeParams)
  40. const crud = api.crud(this.apiPrefix)
  41. this.selected = []
  42. this.data = []
  43. this.getRecords = () => {
  44. this.promise = crud.trash().then(data => {
  45. this.data = data
  46. })
  47. }
  48. this.undelete = async rec => {
  49. try {
  50. await crud.undelete(rec.id)
  51. $mdToast.showSimple(`${opts.titleName} undeleted.`)
  52. $location.url(this.appPrefix)
  53. } catch (err) {
  54. console.error(err)
  55. $mdToast.showSimple(`Could not undelete ${opts.titleName}: ${err.message || err}`)
  56. }
  57. }
  58. this.getRecords()
  59. }
  60. })
  61. }
  62. module.exports = list