const _ = require('lodash') const app = require('../../app') const { dollarIcon } = require('../../assets') /** * @param {CrudPagesOptions} opts */ const details = (opts) => { const hideCriteria = column => column.routeParam ? `ctrl.$routeParams.${column.routeParam} !== '${opts.paramAll}'` : 'false' const autocompleteInput = column => { if (!column.apiPrefix) throw new Error('apiPrefix is required for autocomplete fields') return html` {{item.name || item.key || item.id}} ` } const attrs = obj => obj ? _.toPairs().map((key, value) => html`${key}="${value}"`).join(' ') : '' const standardInput = column => html` ${column.titleName} ` const currencyInput = column => html` ${column.titleName} ` const defaultField = column => column.type === 'autocomplete' ? autocompleteInput(column) : column.type === 'currency' ? currencyInput(column) : standardInput(column) const layout = () => { if (opts.layout) { const cols = _.fromPairs(opts.columns.map(col => [col.camelName, col])) return opts.layout.map(section => html` ${section.section ? html`${section.section}`:''} ${raw(section.rows ? section.rows.map( row => html` ${raw(row .map(field => cols[field]) .map(c => c.field || defaultField(c)) .join('\n') )} `).join('\n') : '')} `).join('\n') } else { return html` ${opts.columns.map(c => html` ${c.field || defaultField(c)} `)} ` } } const template = html` New ${opts.titleName} ${opts.titleName} Details ${raw(layout())} Submit ` console.log(`crud: app${opts.pascalName}DetailsPage`) app.component(`app${opts.pascalName}DetailsPage`, { template, controllerAs: 'ctrl', controller: function(api, $scope, $routeParams, $mdToast, $location, $q, util) { this.$routeParams = $routeParams this.template = template // For inspection purposes this.apiPrefix = util.fillPath(opts.apiPrefix, $routeParams) this.appPrefix = util.fillPath(opts.appPrefix, $routeParams) this.isNew = $routeParams[opts.routeParam] === opts.paramNew const crud = api.crud(this.apiPrefix) let original if (this.isNew) { original = {} $scope.model = Object.create(original) this.loadingPromise = $q.resolve($scope.model) } else { this.loadingPromise = crud.read($routeParams[opts.routeParam]).then(model => { original = model $scope.model = Object.create(original) return $scope.model }) } this.submit = async () => { try { if (this.isNew) { await crud.create($scope.model) } else { const obj = {} for (var key in $scope.model) { if ($scope.model.hasOwnProperty(key)) { obj[key] = $scope.model[key] } } await crud.update(original.id, obj) } $mdToast.showSimple(`${opts.titleName} saved.`) $location.url(this.appPrefix) } catch (err) { console.error(err) $mdToast.showSimple(`Could not save ${opts.titleName}: ${err.message || err.statusText || err}`) } } /* Autocomplete fields */ this.searchText = {} this.autocomplete = {} opts.columns.filter(c => c.type === 'autocomplete').forEach(c => { const crud = api.crud(util.fillPath(c.apiPrefix, $routeParams)) const ac = this.autocomplete[c.camelName] = { onChange() { $scope.model[c.camelName] = ac.selectedItem ? ac.selectedItem.id : null }, getItems(searchText) { return crud.autocomplete(searchText) } } this.loadingPromise.then(model => { if (model[c.camelName]) { crud.read(model[c.camelName]).then(record => { ac.selectedItem = record }) } }) }) } }) } module.exports = details