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