blob: d1e4d0e7bb448ecd4746fc6b2e6f5e852be4e609 [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 Scandolo9f87f302016-12-13 18:11:10 -08004export interface IXosCrudData {
Matteo Scandolof2c3ed62016-12-15 14:32:50 -08005 model: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -08006 xosTableCfg: IXosTableCfg;
7}
8
9class CrudController {
Matteo Scandolod58d5042016-12-16 16:59:21 -080010 static $inject = ['$state', '$scope', 'ModelStore', 'ConfigHelpers'];
Matteo Scandolo9f87f302016-12-13 18:11:10 -080011
12 public data: IXosCrudData;
13 public tableCfg: IXosTableCfg;
14 public title: string;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080015 public tableData: any[];
16
17 constructor(
18 private $state: angular.ui.IStateService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080019 private $scope: angular.IScope,
Matteo Scandolod58d5042016-12-16 16:59:21 -080020 private store: IModelStoreService,
21 private ConfigHelpers: IXosConfigHelpersService
Matteo Scandolo9f87f302016-12-13 18:11:10 -080022 ) {
23 this.data = this.$state.current.data;
Matteo Scandolo9f87f302016-12-13 18:11:10 -080024 this.tableCfg = this.data.xosTableCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080025 this.title = this.ConfigHelpers.pluralize(this.data.model);
Matteo Scandolo9f87f302016-12-13 18:11:10 -080026
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080027 this.store.query(this.data.model)
Matteo Scandolo035c5932016-12-14 09:55:15 -080028 .subscribe(
29 (event) => {
30 // NOTE Observable mess with $digest cycles, we need to schedule the expression later
31 $scope.$evalAsync(() => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080032 this.title = this.ConfigHelpers.pluralize(this.data.model, event.length);
Matteo Scandolo035c5932016-12-14 09:55:15 -080033 this.tableData = event;
34 });
35 }
36 );
Matteo Scandolo9f87f302016-12-13 18:11:10 -080037 }
38}
39
40export const xosCrud: angular.IComponentOptions = {
41 template: require('./crud.html'),
42 controllerAs: 'vm',
43 controller: CrudController
44};