blob: 90d3342fe932205acc41750b225f75ebd4a48c5c [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 Scandolo1aee1982017-02-17 08:33:23 -08005import {IXosFormCfg} from '../../core/form/form';
Matteo Scandolo80c3a652017-01-06 10:48:31 -08006import {IXosResourceService} from '../../datasources/rest/model.rest';
Matteo Scandolo04964232017-01-07 12:53:46 -08007import {IStoreHelpersService} from '../../datasources/helpers/store.helpers';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08008import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
9
Matteo Scandolo9f87f302016-12-13 18:11:10 -080010export interface IXosCrudData {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080011 model: string;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080012 related: IXosModelRelation[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080013 xosTableCfg: IXosTableCfg;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080014 xosFormCfg: IXosFormCfg;
15}
16
17export interface IXosModelRelation {
18 model: string;
19 type: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080020}
21
22class CrudController {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080023 static $inject = [
24 '$scope',
25 '$state',
26 '$stateParams',
27 'XosModelStore',
28 'ConfigHelpers',
29 'ModelRest',
30 'StoreHelpers',
31 'XosModelDiscoverer'
32 ];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080033
Matteo Scandolo1aee1982017-02-17 08:33:23 -080034 public data: {model: string};
Matteo Scandolo9f87f302016-12-13 18:11:10 -080035 public tableCfg: IXosTableCfg;
Matteo Scandoloee655a12016-12-19 15:38:43 -080036 public formCfg: any;
Matteo Scandoloee655a12016-12-19 15:38:43 -080037 public baseUrl: string;
38 public list: boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080039 public title: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080040 public tableData: any[];
Matteo Scandoloee655a12016-12-19 15:38:43 -080041 public model: any;
42 public related: string[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080043
44 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080045 private $scope: angular.IScope,
Matteo Scandoloee655a12016-12-19 15:38:43 -080046 private $state: angular.ui.IStateService,
47 private $stateParams: ng.ui.IStateParamsService,
Matteo Scandolo47860fe2017-02-02 12:05:55 -080048 private store: IXosModelStoreService,
Matteo Scandolo80c3a652017-01-06 10:48:31 -080049 private ConfigHelpers: IXosConfigHelpersService,
Matteo Scandolo04964232017-01-07 12:53:46 -080050 private ModelRest: IXosResourceService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080051 private StoreHelpers: IStoreHelpersService,
52 private XosModelDiscovererService: IXosModelDiscovererService
Matteo Scandolo9f87f302016-12-13 18:11:10 -080053 ) {
54 this.data = this.$state.current.data;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080055 this.model = this.XosModelDiscovererService.get(this.data.model);
Matteo Scandolod58d5042016-12-16 16:59:21 -080056 this.title = this.ConfigHelpers.pluralize(this.data.model);
Matteo Scandolo9f87f302016-12-13 18:11:10 -080057
Matteo Scandoloee655a12016-12-19 15:38:43 -080058 this.list = true;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080059
60 // TODO get the proper URL from model discoverer
61 this.baseUrl = '#/' + this.model.clientUrl.replace(':id?', '');
62
Matteo Scandoloee655a12016-12-19 15:38:43 -080063
64 this.related = $state.current.data.related;
65
Matteo Scandolo1aee1982017-02-17 08:33:23 -080066 this.tableCfg = this.model.tableCfg;
67 this.formCfg = this.model.formCfg;
Matteo Scandoloee655a12016-12-19 15:38:43 -080068
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080069 this.store.query(this.data.model)
Matteo Scandolo035c5932016-12-14 09:55:15 -080070 .subscribe(
71 (event) => {
72 // NOTE Observable mess with $digest cycles, we need to schedule the expression later
73 $scope.$evalAsync(() => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080074 this.title = this.ConfigHelpers.pluralize(this.data.model, event.length);
Matteo Scandolo035c5932016-12-14 09:55:15 -080075 this.tableData = event;
Matteo Scandoloee655a12016-12-19 15:38:43 -080076
Matteo Scandolo80c3a652017-01-06 10:48:31 -080077 // if it is a detail page for an existing model
78 if ($stateParams['id'] && $stateParams['id'] !== 'add') {
Matteo Scandoloee655a12016-12-19 15:38:43 -080079 this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)});
80 }
Matteo Scandolo035c5932016-12-14 09:55:15 -080081 });
82 }
83 );
Matteo Scandoloee655a12016-12-19 15:38:43 -080084
85 // if it is a detail page
86 if ($stateParams['id']) {
87 this.list = false;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080088
89 // if it is the create page
90 if ($stateParams['id'] === 'add') {
91 // generate a resource for an empty model
Matteo Scandolo47c53fc2017-03-23 14:11:32 -070092 const endpoint = this.XosModelDiscovererService.getApiUrlFromModel(this.XosModelDiscovererService.get(this.data.model));
Matteo Scandolo80c3a652017-01-06 10:48:31 -080093 const resource = this.ModelRest.getResource(endpoint);
94 this.model = new resource({});
95 }
Matteo Scandoloee655a12016-12-19 15:38:43 -080096 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080097 }
Matteo Scandolo00d97892016-12-23 17:53:12 -080098
Matteo Scandolo1aee1982017-02-17 08:33:23 -080099 public getRelatedItem(relation: IXosModelRelation, item: any): number {
100 if (item && angular.isDefined(item[`${relation.model.toLowerCase()}_id`])) {
101 return item[`${relation.model.toLowerCase()}_id`];
Matteo Scandolo00d97892016-12-23 17:53:12 -0800102 }
103 return 0;
104 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -0800105}
106
107export const xosCrud: angular.IComponentOptions = {
108 template: require('./crud.html'),
109 controllerAs: 'vm',
110 controller: CrudController
111};