Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 1 | import {IXosTableCfg} from '../../core/table/table'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 2 | import {IModelStoreService} from '../../datasources/stores/model.store'; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 3 | import {IXosConfigHelpersService} from '../../core/services/helpers/config.helpers'; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 4 | export interface IXosCrudData { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 5 | model: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 6 | xosTableCfg: IXosTableCfg; |
| 7 | } |
| 8 | |
| 9 | class CrudController { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 10 | static $inject = ['$state', '$scope', 'ModelStore', 'ConfigHelpers']; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 11 | |
| 12 | public data: IXosCrudData; |
| 13 | public tableCfg: IXosTableCfg; |
| 14 | public title: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 15 | public tableData: any[]; |
| 16 | |
| 17 | constructor( |
| 18 | private $state: angular.ui.IStateService, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 19 | private $scope: angular.IScope, |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 20 | private store: IModelStoreService, |
| 21 | private ConfigHelpers: IXosConfigHelpersService |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 22 | ) { |
| 23 | this.data = this.$state.current.data; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 24 | this.tableCfg = this.data.xosTableCfg; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 25 | this.title = this.ConfigHelpers.pluralize(this.data.model); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 26 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 27 | this.store.query(this.data.model) |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 28 | .subscribe( |
| 29 | (event) => { |
| 30 | // NOTE Observable mess with $digest cycles, we need to schedule the expression later |
| 31 | $scope.$evalAsync(() => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 32 | this.title = this.ConfigHelpers.pluralize(this.data.model, event.length); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 33 | this.tableData = event; |
| 34 | }); |
| 35 | } |
| 36 | ); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | export const xosCrud: angular.IComponentOptions = { |
| 41 | template: require('./crud.html'), |
| 42 | controllerAs: 'vm', |
| 43 | controller: CrudController |
| 44 | }; |