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