Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 1 | import {IXosTableCfg} from '../../core/table/table'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 2 | import {IModelStoreService} from '../../datasources/stores/model.store'; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 3 | import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 4 | import * as _ from 'lodash'; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 5 | import {IXosFormConfig} from '../../core/form/form'; |
| 6 | import {IXosResourceService} from '../../datasources/rest/model.rest'; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 7 | export interface IXosCrudData { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 8 | model: string; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 9 | related: string[]; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 10 | xosTableCfg: IXosTableCfg; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 11 | xosFormCfg: IXosFormConfig; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | class CrudController { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 15 | static $inject = ['$scope', '$state', '$stateParams', 'ModelStore', 'ConfigHelpers', 'ModelRest']; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 16 | |
| 17 | public data: IXosCrudData; |
| 18 | public tableCfg: IXosTableCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 19 | public formCfg: any; |
| 20 | public stateName: string; |
| 21 | public baseUrl: string; |
| 22 | public list: boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 23 | public title: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 24 | public tableData: any[]; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 25 | public model: any; |
| 26 | public related: string[]; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 27 | |
| 28 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 29 | private $scope: angular.IScope, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 30 | private $state: angular.ui.IStateService, |
| 31 | private $stateParams: ng.ui.IStateParamsService, |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 32 | private store: IModelStoreService, |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 33 | private ConfigHelpers: IXosConfigHelpersService, |
| 34 | private ModelRest: IXosResourceService |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 35 | ) { |
| 36 | this.data = this.$state.current.data; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 37 | this.tableCfg = this.data.xosTableCfg; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 38 | this.title = this.ConfigHelpers.pluralize(this.data.model); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 39 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 40 | this.list = true; |
| 41 | this.stateName = $state.current.name; |
| 42 | this.baseUrl = '#/core' + $state.current.url.toString().replace(':id?', ''); |
| 43 | |
| 44 | this.related = $state.current.data.related; |
| 45 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 46 | this.formCfg = $state.current.data.xosFormCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 47 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 48 | this.store.query(this.data.model) |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 49 | .subscribe( |
| 50 | (event) => { |
| 51 | // NOTE Observable mess with $digest cycles, we need to schedule the expression later |
| 52 | $scope.$evalAsync(() => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 53 | this.title = this.ConfigHelpers.pluralize(this.data.model, event.length); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 54 | this.tableData = event; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 56 | // if it is a detail page for an existing model |
| 57 | if ($stateParams['id'] && $stateParams['id'] !== 'add') { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 58 | this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)}); |
| 59 | } |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 60 | }); |
| 61 | } |
| 62 | ); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 63 | |
| 64 | // if it is a detail page |
| 65 | if ($stateParams['id']) { |
| 66 | this.list = false; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 67 | |
| 68 | // if it is the create page |
| 69 | if ($stateParams['id'] === 'add') { |
| 70 | // generate a resource for an empty model |
| 71 | const endpoint = this.ConfigHelpers.urlFromCoreModel(this.data.model); |
| 72 | const resource = this.ModelRest.getResource(endpoint); |
| 73 | this.model = new resource({}); |
| 74 | } |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 75 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 76 | } |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 77 | |
| 78 | public getRelatedItem(relation: string, item: any): number { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 79 | if (item && angular.isDefined(item[relation.toLowerCase()])) { |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 80 | return item[relation.toLowerCase()]; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | export const xosCrud: angular.IComponentOptions = { |
| 87 | template: require('./crud.html'), |
| 88 | controllerAs: 'vm', |
| 89 | controller: CrudController |
| 90 | }; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame^] | 91 | |