list.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const app = require('../../app')
  2. const {editIcon, createIcon} = require('../../assets')
  3. const list = ({
  4. typeName,
  5. typePlural,
  6. camelName,
  7. camelPlural,
  8. paramName,
  9. paramPlural,
  10. apiPrefix,
  11. appPrefix,
  12. columns
  13. }) => {
  14. const defaultHeader = column => html`<th md-column>${column.fieldName}</th>`
  15. const defaultCell = column => html`<td md-cell>{{${raw(camelName)}.${raw(column.camelName)}}}</td>`
  16. app.component(`app${typePlural}Page`, {
  17. template: html`
  18. <app-user-area>
  19. <h1>${typePlural}</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. ${columns.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="${camelName}" md-select-id="name" md-auto-select ng-repeat="${raw(camelName)} in ctrl.data track by ${raw(camelName)}.id">
  30. ${columns.map(c => c.cell || defaultCell(c))}
  31. <td md-cell>
  32. <md-button ng-href="${appPrefix}/{{${raw(camelName)}.id}}">
  33. <md-icon md-svg-icon="${editIcon}"></md-icon>
  34. Edit
  35. </md-button>
  36. </td>
  37. </tr>
  38. </tbody>
  39. </table>
  40. </md-table-container>
  41. <div layout="row" layout-align="end">
  42. <md-button class="md-fab" aria-label="Add ${typeName}" ng-href="${appPrefix}/new">
  43. <md-icon md-svg-src="${createIcon}"></md-icon>
  44. </md-button>
  45. </div>
  46. </app-user-area>
  47. `,
  48. controllerAs: 'ctrl',
  49. controller: function(api) {
  50. const crud = api.crud(apiPrefix)
  51. this.selected = []
  52. this.data = []
  53. this.getRecords = () => {
  54. this.promise = crud.list().then(data => {
  55. this.data = data
  56. })
  57. }
  58. this.getRecords()
  59. }
  60. })
  61. }
  62. module.exports = list