blob: f3cc1fafbe300d3963b536e1f81aba6c0f54adab [file] [log] [blame]
Matteo Scandolo9f87f302016-12-13 18:11:10 -08001import {IXosTableCfg} from '../../core/table/table';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08002import {IModelStoreService} 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 Scandolo9f87f302016-12-13 18:11:10 -08007export interface IXosCrudData {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08008 model: string;
Matteo Scandoloee655a12016-12-19 15:38:43 -08009 related: string[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080010 xosTableCfg: IXosTableCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080011 xosFormCfg: IXosFormConfig;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080012}
13
14class CrudController {
Matteo Scandolo80c3a652017-01-06 10:48:31 -080015 static $inject = ['$scope', '$state', '$stateParams', 'ModelStore', 'ConfigHelpers', 'ModelRest'];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080016
17 public data: IXosCrudData;
18 public tableCfg: IXosTableCfg;
Matteo Scandoloee655a12016-12-19 15:38:43 -080019 public formCfg: any;
20 public stateName: string;
21 public baseUrl: string;
22 public list: boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080023 public title: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080024 public tableData: any[];
Matteo Scandoloee655a12016-12-19 15:38:43 -080025 public model: any;
26 public related: string[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080027
28 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080029 private $scope: angular.IScope,
Matteo Scandoloee655a12016-12-19 15:38:43 -080030 private $state: angular.ui.IStateService,
31 private $stateParams: ng.ui.IStateParamsService,
Matteo Scandolod58d5042016-12-16 16:59:21 -080032 private store: IModelStoreService,
Matteo Scandolo80c3a652017-01-06 10:48:31 -080033 private ConfigHelpers: IXosConfigHelpersService,
34 private ModelRest: IXosResourceService
Matteo Scandolo9f87f302016-12-13 18:11:10 -080035 ) {
36 this.data = this.$state.current.data;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080037 this.tableCfg = this.data.xosTableCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080038 this.title = this.ConfigHelpers.pluralize(this.data.model);
Matteo Scandolo9f87f302016-12-13 18:11:10 -080039
Matteo Scandoloee655a12016-12-19 15:38:43 -080040 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 Scandolo80c3a652017-01-06 10:48:31 -080046 this.formCfg = $state.current.data.xosFormCfg;
Matteo Scandoloee655a12016-12-19 15:38:43 -080047
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080048 this.store.query(this.data.model)
Matteo Scandolo035c5932016-12-14 09:55:15 -080049 .subscribe(
50 (event) => {
51 // NOTE Observable mess with $digest cycles, we need to schedule the expression later
52 $scope.$evalAsync(() => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080053 this.title = this.ConfigHelpers.pluralize(this.data.model, event.length);
Matteo Scandolo035c5932016-12-14 09:55:15 -080054 this.tableData = event;
Matteo Scandoloee655a12016-12-19 15:38:43 -080055
Matteo Scandolo80c3a652017-01-06 10:48:31 -080056 // if it is a detail page for an existing model
57 if ($stateParams['id'] && $stateParams['id'] !== 'add') {
Matteo Scandoloee655a12016-12-19 15:38:43 -080058 this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)});
59 }
Matteo Scandolo035c5932016-12-14 09:55:15 -080060 });
61 }
62 );
Matteo Scandoloee655a12016-12-19 15:38:43 -080063
64 // if it is a detail page
65 if ($stateParams['id']) {
66 this.list = false;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080067
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 Scandoloee655a12016-12-19 15:38:43 -080075 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080076 }
Matteo Scandolo00d97892016-12-23 17:53:12 -080077
78 public getRelatedItem(relation: string, item: any): number {
Matteo Scandolo80c3a652017-01-06 10:48:31 -080079 if (item && angular.isDefined(item[relation.toLowerCase()])) {
Matteo Scandolo00d97892016-12-23 17:53:12 -080080 return item[relation.toLowerCase()];
81 }
82 return 0;
83 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080084}
85
86export const xosCrud: angular.IComponentOptions = {
87 template: require('./crud.html'),
88 controllerAs: 'vm',
89 controller: CrudController
90};
Matteo Scandolo80c3a652017-01-06 10:48:31 -080091