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 | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 25 | import {IXosModelDiscovererService, IXosModel} 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 | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 28 | import {IXosDebugService, IXosDebugStatus} from '../../core/debug/debug.service'; |
| 29 | import {IXosKeyboardShortcutService} from '../../core/services/keyboard-shortcut'; |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 30 | import {Subscription} from 'rxjs'; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 31 | import {IXosModeldefsCache} from '../../datasources/helpers/modeldefs.service'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 32 | |
| 33 | export interface IXosModelRelation { |
| 34 | model: string; |
| 35 | type: string; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 36 | on_field: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | class CrudController { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 40 | static $inject = [ |
| 41 | '$scope', |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 42 | '$log', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 43 | '$state', |
| 44 | '$stateParams', |
| 45 | 'XosModelStore', |
| 46 | 'ConfigHelpers', |
| 47 | 'ModelRest', |
| 48 | 'StoreHelpers', |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 49 | 'XosModelDiscoverer', |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 50 | 'XosCrudRelation', |
| 51 | 'XosDebug', |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 52 | 'XosKeyboardShortcut', |
| 53 | 'XosModeldefsCache' |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 54 | ]; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 56 | // bindings |
| 57 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 58 | public data: {model: string}; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 59 | public tableCfg: IXosTableCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 60 | public formCfg: any; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 61 | public baseUrl: string; |
| 62 | public list: boolean; |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 63 | public modelName: string; |
| 64 | public pluralTitle: string; |
| 65 | public singularTitle: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 66 | public tableData: any[]; |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 67 | public model: any; // holds the real model |
| 68 | public modelDef: IXosModel; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 69 | public related: {manytoone: IXosModelRelation[], onetomany: IXosModelRelation[]} = { |
| 70 | manytoone: [], |
| 71 | onetomany: [] |
| 72 | }; |
| 73 | public relatedModels: {manytoone: any, onetomany: any} = { |
| 74 | manytoone: {}, |
| 75 | onetomany: {} |
| 76 | }; |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 77 | public debugTab: boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 78 | |
Matteo Scandolo | 39e0415 | 2017-11-29 14:24:45 -0800 | [diff] [blame] | 79 | public getRelatedModels = _.memoize(this._getRelatedModels); |
| 80 | |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 81 | private subscription: Subscription; |
| 82 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 83 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 84 | private $scope: angular.IScope, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 85 | private $log: angular.ILogService, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 86 | private $state: angular.ui.IStateService, |
| 87 | private $stateParams: ng.ui.IStateParamsService, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 88 | private store: IXosModelStoreService, |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 89 | private ConfigHelpers: IXosConfigHelpersService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 90 | private ModelRest: IXosResourceService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 91 | private StoreHelpers: IStoreHelpersService, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 92 | private XosModelDiscovererService: IXosModelDiscovererService, |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 93 | private XosCrudRelation: IXosCrudRelationService, |
| 94 | private XosDebug: IXosDebugService, |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 95 | private XosKeyboardShortcut: IXosKeyboardShortcutService, |
| 96 | private XosModeldefsCache: IXosModeldefsCache |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 97 | ) { |
Matteo Scandolo | a471859 | 2017-08-10 14:54:51 -0700 | [diff] [blame] | 98 | this.$log.info('[XosCrud] Setup', $state.current.data); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 99 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 100 | this.data = this.$state.current.data; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 101 | this.modelDef = this.XosModeldefsCache.get(this.data.model); |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 102 | this.modelName = this.modelDef.verbose_name ? this.modelDef.verbose_name : this.modelDef.name; |
| 103 | this.pluralTitle = this.ConfigHelpers.pluralize(this.modelName); |
| 104 | this.singularTitle = this.ConfigHelpers.pluralize(this.modelName, 1); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 105 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 106 | this.list = true; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 107 | |
| 108 | // TODO get the proper URL from model discoverer |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 109 | this.baseUrl = '#/' + this.modelDef.clientUrl.replace(':id?', ''); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 110 | |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 111 | this.tableCfg = this.modelDef.tableCfg; |
| 112 | this.formCfg = this.modelDef.formCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 113 | |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 114 | this.debugTab = this.XosDebug.status.modelsTab; |
| 115 | this.$scope.$on('xos.debug.status', (e, status: IXosDebugStatus) => { |
| 116 | this.debugTab = status.modelsTab; |
| 117 | this.$scope.$apply(); |
| 118 | }); |
| 119 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 120 | // if it is a detail page |
| 121 | if ($stateParams['id']) { |
| 122 | this.list = false; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 123 | |
| 124 | // if it is the create page |
| 125 | if ($stateParams['id'] === 'add') { |
| 126 | // generate a resource for an empty model |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 127 | const endpoint = this.XosModelDiscovererService.getApiUrlFromModel(this.XosModeldefsCache.get(this.data.model)); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 128 | const resource = this.ModelRest.getResource(endpoint); |
| 129 | this.model = new resource({}); |
Matteo Scandolo | 39e0415 | 2017-11-29 14:24:45 -0800 | [diff] [blame] | 130 | |
| 131 | // attach a redirect to the $save method |
| 132 | const originalSave = angular.copy(this.formCfg.actions[0].cb); |
| 133 | this.formCfg.actions[0].cb = (item, form: angular.IFormController) => { |
| 134 | originalSave(item, form) |
| 135 | .then(res => { |
| 136 | this.$state.go(this.$state.current, {id: res.id}); |
| 137 | }) |
| 138 | .catch(err => { |
| 139 | this.$log.error(`[XosCrud] Error while saving:`, item, err); |
| 140 | }); |
| 141 | }; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 142 | } |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 143 | else { |
| 144 | this.subscription = this.store.get(this.data.model, $stateParams['id']) |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 145 | .subscribe(res => { |
| 146 | $scope.$evalAsync(() => { |
| 147 | this.related.onetomany = _.filter($state.current.data.relations, {type: 'onetomany'}); |
| 148 | this.related.manytoone = _.filter($state.current.data.relations, {type: 'manytoone'}); |
| 149 | this.model = res; |
| 150 | this.getRelatedModels(this.related, this.model); |
| 151 | }); |
| 152 | }); |
| 153 | } |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 154 | |
| 155 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 156 | key: 'A', |
| 157 | cb: () => this.XosDebug.toggleDebug('modelsTab'), |
| 158 | description: 'Toggle Debug tab in model details view' |
| 159 | }, 'view'); |
| 160 | |
| 161 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 162 | key: 'delete', |
| 163 | cb: () => { |
| 164 | this.$state.go(this.$state.current.name, {id: null}); |
| 165 | }, |
| 166 | description: 'Go back to the list view' |
| 167 | }, 'view'); |
| 168 | } |
| 169 | // list page |
| 170 | else { |
| 171 | this.tableCfg.selectedRow = -1; |
| 172 | |
| 173 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 174 | key: 'Tab', |
| 175 | cb: () => this.iterateItems(), |
| 176 | description: 'Iterate trough items in the list' |
| 177 | }, 'view'); |
| 178 | |
| 179 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 180 | key: 'Enter', |
| 181 | cb: () => { |
| 182 | if (this.tableCfg.selectedRow < 0) { |
| 183 | return; |
| 184 | } |
| 185 | this.$state.go(this.$state.current.name, {id: this.tableCfg.filteredData[this.tableCfg.selectedRow].id}); |
| 186 | }, |
| 187 | description: 'View details of selected item' |
| 188 | }, 'view'); |
| 189 | |
| 190 | this.XosKeyboardShortcut.registerKeyBinding({ |
| 191 | key: 'Delete', |
| 192 | cb: () => { |
| 193 | if (this.tableCfg.selectedRow < 0) { |
| 194 | return; |
| 195 | } |
| 196 | const deleteFn = _.find(this.tableCfg.actions, {label: 'delete'}); |
| 197 | deleteFn.cb(this.tableCfg.filteredData[this.tableCfg.selectedRow]); |
| 198 | }, |
| 199 | description: 'View details of selected item' |
| 200 | }, 'view'); |
| 201 | |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 202 | this.subscription = this.store.query(this.data.model) |
| 203 | .subscribe( |
| 204 | (event) => { |
| 205 | // NOTE Observable mess with $digest cycles, we need to schedule the expression later |
| 206 | $scope.$evalAsync(() => { |
| 207 | this.tableData = event; |
| 208 | }); |
| 209 | } |
| 210 | ); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 211 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 212 | } |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 213 | |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 214 | $onDestroy() { |
Matteo Scandolo | 39e0415 | 2017-11-29 14:24:45 -0800 | [diff] [blame] | 215 | if (this.subscription) { |
| 216 | this.subscription.unsubscribe(); |
| 217 | } |
Matteo Scandolo | 04f487c | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 218 | this.$log.info(`[XosCrud] Destroying component`); |
| 219 | } |
| 220 | |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 221 | public iterateItems() { |
| 222 | const rowCount = this.tableCfg.filteredData.length > this.tableCfg.pagination.pageSize ? this.tableCfg.pagination.pageSize : this.tableCfg.filteredData.length; |
| 223 | if ((this.tableCfg.selectedRow + 1) < rowCount) { |
| 224 | this.tableCfg.selectedRow++; |
| 225 | } |
| 226 | else { |
| 227 | this.tableCfg.selectedRow = 0; |
| 228 | } |
| 229 | this.$scope.$apply(); |
| 230 | } |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 231 | |
| 232 | public getRelatedItemId(relation: IXosModelRelation, item: any): boolean { |
| 233 | return this.XosCrudRelation.existsRelatedItem(relation, item); |
| 234 | } |
| 235 | |
| 236 | public getHumanReadableOnField(r: IXosModelRelation) { |
| 237 | return this.XosCrudRelation.getHumanReadableOnField(r, this.data.model); |
| 238 | } |
| 239 | |
Matteo Scandolo | 39e0415 | 2017-11-29 14:24:45 -0800 | [diff] [blame] | 240 | private _getRelatedModels(relations: {manytoone: IXosModelRelation[], onetomany: IXosModelRelation[]}, item: any) { |
Matteo Scandolo | a471859 | 2017-08-10 14:54:51 -0700 | [diff] [blame] | 241 | this.$log.debug(`[XosCrud] Managing relation for ${this.data.model}:`, relations); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 242 | |
| 243 | // loading many to one relations (you'll get a model) |
| 244 | _.forEach(relations.manytoone, (r: IXosModelRelation) => { |
| 245 | if (!item || !item[`${r.on_field.toLowerCase()}_id`]) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | this.$log.debug(`[XosCrud] Loading manytoone relation with ${r.model} on ${r.on_field}`); |
| 250 | |
| 251 | if (!angular.isDefined(this.relatedModels.manytoone[r.model])) { |
| 252 | this.relatedModels.manytoone[r.model] = {}; |
| 253 | } |
| 254 | |
| 255 | this.XosCrudRelation.getModel(r, item[`${r.on_field.toLowerCase()}_id`]) |
| 256 | .then(res => { |
| 257 | this.relatedModels.manytoone[r.model][r.on_field] = res; |
| 258 | }) |
| 259 | .catch(err => { |
| 260 | this.$log.error(`[XosCrud] Error loading manytoone relation with ${r.model} on ${r.on_field}`, err); |
| 261 | }); |
| 262 | }); |
| 263 | |
| 264 | // loading onetomany relations (you'll get a list of models) |
| 265 | _.forEach(relations.onetomany, (r: IXosModelRelation) => { |
| 266 | if (!item) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | this.$log.debug(`[XosCrud] Loading onetomany relation with ${r.model} on ${r.on_field}`); |
| 271 | |
| 272 | if (!angular.isDefined(this.relatedModels.onetomany[r.model])) { |
| 273 | this.relatedModels.onetomany[r.model] = {}; |
| 274 | } |
| 275 | |
| 276 | this.XosCrudRelation.getModels(r, item.id) |
| 277 | .then(res => { |
| 278 | this.relatedModels.onetomany[r.model][r.on_field] = res; |
| 279 | }) |
| 280 | .catch(err => { |
| 281 | this.$log.error(`[XosCrud] Error loading onetomany relation with ${r.model} on ${r.on_field}`, err); |
| 282 | }); |
| 283 | }); |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 284 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | export const xosCrud: angular.IComponentOptions = { |
| 288 | template: require('./crud.html'), |
| 289 | controllerAs: 'vm', |
| 290 | controller: CrudController |
| 291 | }; |