Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 1 | import {IXosTableCfg} from '../../core/table/table'; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 2 | import {IXosModelStoreService} 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 | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 5 | import {IXosResourceService} from '../../datasources/rest/model.rest'; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 6 | import {IStoreHelpersService} from '../../datasources/helpers/store.helpers'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 7 | import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service'; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 8 | import './crud.scss'; |
| 9 | import {IXosCrudRelationService} from './crud.relations.service'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 10 | |
| 11 | export interface IXosModelRelation { |
| 12 | model: string; |
| 13 | type: string; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 14 | on_field: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | class CrudController { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 18 | static $inject = [ |
| 19 | '$scope', |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 20 | '$log', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 21 | '$state', |
| 22 | '$stateParams', |
| 23 | 'XosModelStore', |
| 24 | 'ConfigHelpers', |
| 25 | 'ModelRest', |
| 26 | 'StoreHelpers', |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 27 | 'XosModelDiscoverer', |
| 28 | 'XosCrudRelation' |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 29 | ]; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 30 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 31 | // bindings |
| 32 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 33 | public data: {model: string}; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 34 | public tableCfg: IXosTableCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 35 | public formCfg: any; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 36 | public baseUrl: string; |
| 37 | public list: boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 38 | public title: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 39 | public tableData: any[]; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 40 | public model: any; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 41 | public related: {manytoone: IXosModelRelation[], onetomany: IXosModelRelation[]} = { |
| 42 | manytoone: [], |
| 43 | onetomany: [] |
| 44 | }; |
| 45 | public relatedModels: {manytoone: any, onetomany: any} = { |
| 46 | manytoone: {}, |
| 47 | onetomany: {} |
| 48 | }; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 49 | |
| 50 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 51 | private $scope: angular.IScope, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 52 | private $log: angular.ILogService, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 53 | private $state: angular.ui.IStateService, |
| 54 | private $stateParams: ng.ui.IStateParamsService, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 55 | private store: IXosModelStoreService, |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 56 | private ConfigHelpers: IXosConfigHelpersService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 57 | private ModelRest: IXosResourceService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 58 | private StoreHelpers: IStoreHelpersService, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 59 | private XosModelDiscovererService: IXosModelDiscovererService, |
| 60 | private XosCrudRelation: IXosCrudRelationService |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 61 | ) { |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 62 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 63 | this.data = this.$state.current.data; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 64 | this.model = this.XosModelDiscovererService.get(this.data.model); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 65 | this.title = this.ConfigHelpers.pluralize(this.data.model); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 66 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 67 | this.list = true; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 68 | |
| 69 | // TODO get the proper URL from model discoverer |
| 70 | this.baseUrl = '#/' + this.model.clientUrl.replace(':id?', ''); |
| 71 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 72 | this.$log.debug('[XosCrud]', $state.current.data); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 73 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 74 | this.tableCfg = this.model.tableCfg; |
| 75 | this.formCfg = this.model.formCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 76 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 77 | this.store.query(this.data.model) |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 78 | .subscribe( |
| 79 | (event) => { |
| 80 | // NOTE Observable mess with $digest cycles, we need to schedule the expression later |
| 81 | $scope.$evalAsync(() => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 82 | this.title = this.ConfigHelpers.pluralize(this.data.model, event.length); |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 83 | this.tableData = event; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 84 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 85 | // if it is a detail page for an existing model |
| 86 | if ($stateParams['id'] && $stateParams['id'] !== 'add') { |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 87 | this.related.onetomany = _.filter($state.current.data.relations, {type: 'onetomany'}); |
| 88 | this.related.manytoone = _.filter($state.current.data.relations, {type: 'manytoone'}); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 89 | this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)}); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 90 | this.getRelatedModels(this.related, this.model); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 91 | } |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 92 | }); |
| 93 | } |
| 94 | ); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 95 | |
| 96 | // if it is a detail page |
| 97 | if ($stateParams['id']) { |
| 98 | this.list = false; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 99 | |
| 100 | // if it is the create page |
| 101 | if ($stateParams['id'] === 'add') { |
| 102 | // generate a resource for an empty model |
Matteo Scandolo | 47c53fc | 2017-03-23 14:11:32 -0700 | [diff] [blame] | 103 | const endpoint = this.XosModelDiscovererService.getApiUrlFromModel(this.XosModelDiscovererService.get(this.data.model)); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 104 | const resource = this.ModelRest.getResource(endpoint); |
| 105 | this.model = new resource({}); |
| 106 | } |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 107 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 108 | } |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 109 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame^] | 110 | |
| 111 | public getRelatedItemId(relation: IXosModelRelation, item: any): boolean { |
| 112 | return this.XosCrudRelation.existsRelatedItem(relation, item); |
| 113 | } |
| 114 | |
| 115 | public getHumanReadableOnField(r: IXosModelRelation) { |
| 116 | return this.XosCrudRelation.getHumanReadableOnField(r, this.data.model); |
| 117 | } |
| 118 | |
| 119 | public getRelatedModels(relations: {manytoone: IXosModelRelation[], onetomany: IXosModelRelation[]}, item: any) { |
| 120 | this.$log.info(`[XosCrud] Managing relation for ${this.data.model}:`, relations); |
| 121 | |
| 122 | // loading many to one relations (you'll get a model) |
| 123 | _.forEach(relations.manytoone, (r: IXosModelRelation) => { |
| 124 | if (!item || !item[`${r.on_field.toLowerCase()}_id`]) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | this.$log.debug(`[XosCrud] Loading manytoone relation with ${r.model} on ${r.on_field}`); |
| 129 | |
| 130 | if (!angular.isDefined(this.relatedModels.manytoone[r.model])) { |
| 131 | this.relatedModels.manytoone[r.model] = {}; |
| 132 | } |
| 133 | |
| 134 | this.XosCrudRelation.getModel(r, item[`${r.on_field.toLowerCase()}_id`]) |
| 135 | .then(res => { |
| 136 | this.relatedModels.manytoone[r.model][r.on_field] = res; |
| 137 | }) |
| 138 | .catch(err => { |
| 139 | this.$log.error(`[XosCrud] Error loading manytoone relation with ${r.model} on ${r.on_field}`, err); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | // loading onetomany relations (you'll get a list of models) |
| 144 | _.forEach(relations.onetomany, (r: IXosModelRelation) => { |
| 145 | if (!item) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | this.$log.debug(`[XosCrud] Loading onetomany relation with ${r.model} on ${r.on_field}`); |
| 150 | |
| 151 | if (!angular.isDefined(this.relatedModels.onetomany[r.model])) { |
| 152 | this.relatedModels.onetomany[r.model] = {}; |
| 153 | } |
| 154 | |
| 155 | this.XosCrudRelation.getModels(r, item.id) |
| 156 | .then(res => { |
| 157 | this.relatedModels.onetomany[r.model][r.on_field] = res; |
| 158 | }) |
| 159 | .catch(err => { |
| 160 | this.$log.error(`[XosCrud] Error loading onetomany relation with ${r.model} on ${r.on_field}`, err); |
| 161 | }); |
| 162 | }); |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 163 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | export const xosCrud: angular.IComponentOptions = { |
| 167 | template: require('./crud.html'), |
| 168 | controllerAs: 'vm', |
| 169 | controller: CrudController |
| 170 | }; |