trash.js 2.3 KB

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