blob: d19a9429c1be320d986ea0042b7a9d23604857bd [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 Scandolo9f87f302016-12-13 18:11:10 -08005export interface IXosCrudData {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08006 model: string;
Matteo Scandoloee655a12016-12-19 15:38:43 -08007 related: string[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -08008 xosTableCfg: IXosTableCfg;
9}
10
11class CrudController {
Matteo Scandoloee655a12016-12-19 15:38:43 -080012 static $inject = ['$scope', '$state', '$stateParams', 'ModelStore', 'ConfigHelpers'];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080013
14 public data: IXosCrudData;
15 public tableCfg: IXosTableCfg;
Matteo Scandoloee655a12016-12-19 15:38:43 -080016 public formCfg: any;
17 public stateName: string;
18 public baseUrl: string;
19 public list: boolean;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080020 public title: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080021 public tableData: any[];
Matteo Scandoloee655a12016-12-19 15:38:43 -080022 public model: any;
23 public related: string[];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080024
25 constructor(
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080026 private $scope: angular.IScope,
Matteo Scandoloee655a12016-12-19 15:38:43 -080027 private $state: angular.ui.IStateService,
28 private $stateParams: ng.ui.IStateParamsService,
Matteo Scandolod58d5042016-12-16 16:59:21 -080029 private store: IModelStoreService,
30 private ConfigHelpers: IXosConfigHelpersService
Matteo Scandolo9f87f302016-12-13 18:11:10 -080031 ) {
32 this.data = this.$state.current.data;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080033 this.tableCfg = this.data.xosTableCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080034 this.title = this.ConfigHelpers.pluralize(this.data.model);
Matteo Scandolo9f87f302016-12-13 18:11:10 -080035
Matteo Scandoloee655a12016-12-19 15:38:43 -080036 this.list = true;
37 this.stateName = $state.current.name;
38 this.baseUrl = '#/core' + $state.current.url.toString().replace(':id?', '');
39
40 this.related = $state.current.data.related;
41
42 this.formCfg = {
43 formName: 'sampleForm',
44 actions: [
45 {
46 label: 'Save',
47 icon: 'ok', // refers to bootstraps glyphicon
48 cb: (item) => { // receive the model
49 console.log(item);
50 },
51 class: 'success'
52 }
53 ]
54 };
55
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080056 this.store.query(this.data.model)
Matteo Scandolo035c5932016-12-14 09:55:15 -080057 .subscribe(
58 (event) => {
59 // NOTE Observable mess with $digest cycles, we need to schedule the expression later
60 $scope.$evalAsync(() => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080061 this.title = this.ConfigHelpers.pluralize(this.data.model, event.length);
Matteo Scandolo035c5932016-12-14 09:55:15 -080062 this.tableData = event;
Matteo Scandoloee655a12016-12-19 15:38:43 -080063
64 // if it is a detail page
65 if ($stateParams['id']) {
66 this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)});
67 }
Matteo Scandolo035c5932016-12-14 09:55:15 -080068 });
69 }
70 );
Matteo Scandoloee655a12016-12-19 15:38:43 -080071
72 // if it is a detail page
73 if ($stateParams['id']) {
74 this.list = false;
75 }
Matteo Scandolo9f87f302016-12-13 18:11:10 -080076 }
77}
78
79export const xosCrud: angular.IComponentOptions = {
80 template: require('./crud.html'),
81 controllerAs: 'vm',
82 controller: CrudController
83};