const app = require('../../app') const {editIcon, createIcon, deleteIcon, viewIcon} = require('../../assets') /** * @param {CrudPagesOptions} opts */ const list = (opts) => { const hideCriteria = column => column.routeParam ? `$ctrl.$routeParams.${column.routeParam} !== '${opts.paramAll}'` : 'false' const defaultHeader = column => html`${column.titleName}` const defaultCell = column => { if (column.type === 'autocomplete') { return html`{{${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)}))}}` } else { return html`{{${raw(opts.camelName)}.${raw(column.camelName)}}}` } } const template = html` ${opts.columns.filter(c => c.inList).map(c => c.header || defaultHeader(c))} ${opts.columns.filter(c => c.inList).map(c => c.cell || defaultCell(c))}
Actions
Edit View Delete
` console.log(`crud: app${opts.pascalPlural}Page`) app.component(`app${opts.pascalPlural}Page`, { template, controller: function(api, $mdToast, $routeParams, util, $interpolate, $scope) { this.api = api this.titleFn = opts.titles && opts.titles.list this.$routeParams = $routeParams this.apiPrefix = util.fillPath(opts.apiPrefix, $routeParams) this.appPrefix = util.fillPath(opts.appPrefix, $routeParams) const crud = api.crud(this.apiPrefix) this.template = template this.selected = [] this.data = [] this.getRecords = () => { this.promise = crud.list().then(data => { this.data = data }) } this.delete = async (record) => { try { await crud.delete(record.id) $mdToast.showSimple(`${opts.titleName} deleted.`) this.data = this.data.filter(x => x.id !== record.id) } catch (err) { console.error(err) $mdToast.showSimple(`Could not delete ${opts.titleName}: ${err.message || err}`) } } this.getRecords() /* Autocomplete fields */ this.autocomplete = {} opts.columns.filter(c => c.type === 'autocomplete').forEach(c => { const crud = api.crud(util.fillPath(c.apiPrefix, $routeParams)) let timer = null let ids = [] const load = () => { timer = null console.log('Loading', ids) crud.lookup(ids).then(records => { records.forEach(rec => { ac.lookup[rec.id] = rec.name || rec.key || rec.id }) }) } const ac = this.autocomplete[c.camelName] = { lookup: {}, load(id) { if (!ac.lookup.hasOwnProperty(id)) { console.log('Queuing to load', id) ac.lookup[id] = '...' if (!ids.includes(id)) ids.push(id) if (!timer) timer = setTimeout(load) } return ac.lookup[id] } } }) } }) } module.exports = list