blob: 846a840c5d043fe517f65e8a1b085026d601ae68 [file] [log] [blame]
Matteo Scandolo9f87f302016-12-13 18:11:10 -08001import {IXosTableCfg} from '../../core/table/table';
Matteo Scandolo47860fe2017-02-02 12:05:55 -08002import {IXosModelStoreService} from '../../datasources/stores/model.store';
Matteo Scandolod58d5042016-12-16 16:59:21 -08003import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers';
Matteo Scandoloee655a12016-12-19 15:38:43 -08004import * as _ from 'lodash';
Matteo Scandolo80c3a652017-01-06 10:48:31 -08005import {IXosFormConfig} from '../../core/form/form';
6import {IXosResourceService} from '../../datasources/rest/model.rest';
Matteo Scandolo04964232017-01-07 12:53:46 -08007import {IStoreHelpersService} from '../../datasources/helpers/store.helpers';
Matteo Scandolo9f87f302016-12-13 18:11:10 -08008export interface IXosCrudData {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08009 model: string;
Matteo Scandoloee655a12016-12-19 15:38:43 -080010 related: string[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080011 xosTableCfg: IXosTableCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080012 xosFormCfg: IXosFormConfig;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080013}
14
15class CrudController {
Matteo Scandolo04964232017-01-07 12:53:46 -080016 static $inject = ['$scope', '$state', '$stateParams', 'ModelStore', 'ConfigHelpers', 'ModelRest', 'StoreHelpers'];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080017
18 public data: IXosCrudData;
19 public tableCfg: IXosTableCfg;
Matteo Scandoloee655a12016-12-19 15:38:43 -080020 public formCfg: any;
21 public stateName: string;
22 public baseUrl: string;
23 public list: boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080024 public title: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080025 public tableData: any[];
Matteo Scandoloee655a12016-12-19 15:38:43 -080026 public model: any;
27 public related: string[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080028
29 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080030 private $scope: angular.IScope,
Matteo Scandoloee655a12016-12-19 15:38:43 -080031 private $state: angular.ui.IStateService,
32 private $stateParams: ng.ui.IStateParamsService,
Matteo Scandolo47860fe2017-02-02 12:05:55 -080033 private store: IXosModelStoreService,
Matteo Scandolo80c3a652017-01-06 10:48:31 -080034 private ConfigHelpers: IXosConfigHelpersService,
Matteo Scandolo04964232017-01-07 12:53:46 -080035 private ModelRest: IXosResourceService,
36 private StoreHelpers: IStoreHelpersService
Matteo Scandolo9f87f302016-12-13 18:11:10 -080037 ) {
38 this.data = this.$state.current.data;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080039 this.tableCfg = this.data.xosTableCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080040 this.title = this.ConfigHelpers.pluralize(this.data.model);
Matteo Scandolo9f87f302016-12-13 18:11:10 -080041
Matteo Scandoloee655a12016-12-19 15:38:43 -080042 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 Scandolo80c3a652017-01-06 10:48:31 -080048 this.formCfg = $state.current.data.xosFormCfg;
Matteo Scandoloee655a12016-12-19 15:38:43 -080049
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080050 this.store.query(this.data.model)
Matteo Scandolo035c5932016-12-14 09:55:15 -080051 .subscribe(
52 (event) => {
53 // NOTE Observable mess with $digest cycles, we need to schedule the expression later
54 $scope.$evalAsync(() => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080055 this.title = this.ConfigHelpers.pluralize(this.data.model, event.length);
Matteo Scandolo035c5932016-12-14 09:55:15 -080056 this.tableData = event;
Matteo Scandoloee655a12016-12-19 15:38:43 -080057
Matteo Scandolo80c3a652017-01-06 10:48:31 -080058 // if it is a detail page for an existing model
59 if ($stateParams['id'] && $stateParams['id'] !== 'add') {
Matteo Scandoloee655a12016-12-19 15:38:43 -080060 this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)});
61 }
Matteo Scandolo035c5932016-12-14 09:55:15 -080062 });
63 }
64 );
Matteo Scandoloee655a12016-12-19 15:38:43 -080065
66 // if it is a detail page
67 if ($stateParams['id']) {
68 this.list = false;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080069
70 // if it is the create page
71 if ($stateParams['id'] === 'add') {
72 // generate a resource for an empty model
Matteo Scandolo04964232017-01-07 12:53:46 -080073 const endpoint = this.StoreHelpers.urlFromCoreModel(this.data.model);
Matteo Scandolo80c3a652017-01-06 10:48:31 -080074 const resource = this.ModelRest.getResource(endpoint);
75 this.model = new resource({});
76 }
Matteo Scandoloee655a12016-12-19 15:38:43 -080077 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080078 }
Matteo Scandolo00d97892016-12-23 17:53:12 -080079
80 public getRelatedItem(relation: string, item: any): number {
Matteo Scandolo80c3a652017-01-06 10:48:31 -080081 if (item && angular.isDefined(item[relation.toLowerCase()])) {
Matteo Scandolo00d97892016-12-23 17:53:12 -080082 return item[relation.toLowerCase()];
83 }
84 return 0;
85 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080086}
87
88export const xosCrud: angular.IComponentOptions = {
89 template: require('./crud.html'),
90 controllerAs: 'vm',
91 controller: CrudController
92};
Matteo Scandolo80c3a652017-01-06 10:48:31 -080093