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 | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 4 | import * as _ from 'lodash'; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 5 | export interface IXosCrudData { |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 6 | model: string; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 7 | related: string[]; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 8 | xosTableCfg: IXosTableCfg; |
| 9 | } |
| 10 | |
| 11 | class CrudController { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 12 | static $inject = ['$scope', '$state', '$stateParams', 'ModelStore', 'ConfigHelpers']; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 13 | |
| 14 | public data: IXosCrudData; |
| 15 | public tableCfg: IXosTableCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 16 | public formCfg: any; |
| 17 | public stateName: string; |
| 18 | public baseUrl: string; |
| 19 | public list: boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 20 | public title: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 21 | public tableData: any[]; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 22 | public model: any; |
| 23 | public related: string[]; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 24 | |
| 25 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 26 | private $scope: angular.IScope, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 27 | private $state: angular.ui.IStateService, |
| 28 | private $stateParams: ng.ui.IStateParamsService, |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 29 | private store: IModelStoreService, |
| 30 | private ConfigHelpers: IXosConfigHelpersService |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 31 | ) { |
| 32 | this.data = this.$state.current.data; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 33 | this.tableCfg = this.data.xosTableCfg; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 34 | this.title = this.ConfigHelpers.pluralize(this.data.model); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 35 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 36 | 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 Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 56 | this.store.query(this.data.model) |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 57 | .subscribe( |
| 58 | (event) => { |
| 59 | // NOTE Observable mess with $digest cycles, we need to schedule the expression later |
| 60 | $scope.$evalAsync(() => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 61 | this.title = this.ConfigHelpers.pluralize(this.data.model, event.length); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 62 | this.tableData = event; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 63 | |
| 64 | // if it is a detail page |
| 65 | if ($stateParams['id']) { |
| 66 | this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)}); |
| 67 | } |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 68 | }); |
| 69 | } |
| 70 | ); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 71 | |
| 72 | // if it is a detail page |
| 73 | if ($stateParams['id']) { |
| 74 | this.list = false; |
| 75 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 76 | } |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame^] | 77 | |
| 78 | public getRelatedItem(relation: string, item: any): number { |
| 79 | if (angular.isDefined(item[relation.toLowerCase()])) { |
| 80 | return item[relation.toLowerCase()]; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | export const xosCrud: angular.IComponentOptions = { |
| 87 | template: require('./crud.html'), |
| 88 | controllerAs: 'vm', |
| 89 | controller: CrudController |
| 90 | }; |