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