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 | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 30 | |
| 31 | export interface IXosModelRelation { |
| 32 | model: string; |
| 33 | type: string; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 34 | on_field: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | class CrudController { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 38 | static $inject = [ |
| 39 | '$scope', |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 40 | '$log', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 41 | '$state', |
| 42 | '$stateParams', |
| 43 | 'XosModelStore', |
| 44 | 'ConfigHelpers', |
| 45 | 'ModelRest', |
| 46 | 'StoreHelpers', |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 47 | 'XosModelDiscoverer', |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 48 | 'XosCrudRelation', |
| 49 | 'XosDebug', |
| 50 | 'XosKeyboardShortcut' |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 51 | ]; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 52 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 53 | // bindings |
| 54 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 55 | public data: {model: string}; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 56 | public tableCfg: IXosTableCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 57 | public formCfg: any; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 58 | public baseUrl: string; |
| 59 | public list: boolean; |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 60 | public modelName: string; |
| 61 | public pluralTitle: string; |
| 62 | public singularTitle: string; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 63 | public tableData: any[]; |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 64 | public model: any; // holds the real model |
| 65 | public modelDef: IXosModel; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 66 | public related: {manytoone: IXosModelRelation[], onetomany: IXosModelRelation[]} = { |
| 67 | manytoone: [], |
| 68 | onetomany: [] |
| 69 | }; |
| 70 | public relatedModels: {manytoone: any, onetomany: any} = { |
| 71 | manytoone: {}, |
| 72 | onetomany: {} |
| 73 | }; |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 74 | public debugTab: boolean; |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 75 | |
| 76 | constructor( |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 77 | private $scope: angular.IScope, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 78 | private $log: angular.ILogService, |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 79 | private $state: angular.ui.IStateService, |
| 80 | private $stateParams: ng.ui.IStateParamsService, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 81 | private store: IXosModelStoreService, |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 82 | private ConfigHelpers: IXosConfigHelpersService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 83 | private ModelRest: IXosResourceService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 84 | private StoreHelpers: IStoreHelpersService, |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 85 | private XosModelDiscovererService: IXosModelDiscovererService, |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 86 | private XosCrudRelation: IXosCrudRelationService, |
| 87 | private XosDebug: IXosDebugService, |
| 88 | private XosKeyboardShortcut: IXosKeyboardShortcutService |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 89 | ) { |
Matteo Scandolo | a471859 | 2017-08-10 14:54:51 -0700 | [diff] [blame] | 90 | this.$log.info('[XosCrud] Setup', $state.current.data); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 91 | |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 92 | this.data = this.$state.current.data; |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 93 | this.modelDef = this.XosModelDiscovererService.get(this.data.model); |
| 94 | this.modelName = this.modelDef.verbose_name ? this.modelDef.verbose_name : this.modelDef.name; |
| 95 | this.pluralTitle = this.ConfigHelpers.pluralize(this.modelName); |
| 96 | this.singularTitle = this.ConfigHelpers.pluralize(this.modelName, 1); |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 97 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 98 | this.list = true; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 99 | |
| 100 | // TODO get the proper URL from model discoverer |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 101 | this.baseUrl = '#/' + this.modelDef.clientUrl.replace(':id?', ''); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 102 | |
Matteo Scandolo | 580033a | 2017-08-17 11:16:39 -0700 | [diff] [blame] | 103 | this.tableCfg = this.modelDef.tableCfg; |
| 104 | this.formCfg = this.modelDef.formCfg; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 105 | |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 106 | this.debugTab = this.XosDebug.status.modelsTab; |
| 107 | this.$scope.$on('xos.debug.status', (e, status: IXosDebugStatus) => { |
| 108 | this.debugTab = status.modelsTab; |
| 109 | this.$scope.$apply(); |
| 110 | }); |
| 111 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 112 | this.store.query(this.data.model) |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 113 | .subscribe( |
| 114 | (event) => { |
| 115 | // NOTE Observable mess with $digest cycles, we need to schedule the expression later |
| 116 | $scope.$evalAsync(() => { |
| 117 | this.tableData = event; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 118 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 119 | // if it is a detail page for an existing model |
| 120 | if ($stateParams['id'] && $stateParams['id'] !== 'add') { |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 121 | this.related.onetomany = _.filter($state.current.data.relations, {type: 'onetomany'}); |
| 122 | this.related.manytoone = _.filter($state.current.data.relations, {type: 'manytoone'}); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 123 | this.model = _.find(this.tableData, {id: parseInt($stateParams['id'], 10)}); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 124 | this.getRelatedModels(this.related, this.model); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 125 | } |
Matteo Scandolo | 035c593 | 2016-12-14 09:55:15 -0800 | [diff] [blame] | 126 | }); |
| 127 | } |
| 128 | ); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 129 | |
| 130 | // if it is a detail page |
| 131 | if ($stateParams['id']) { |
| 132 | this.list = false; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 133 | |
| 134 | // if it is the create page |
| 135 | if ($stateParams['id'] === 'add') { |
| 136 | // generate a resource for an empty model |
Matteo Scandolo | 47c53fc | 2017-03-23 14:11:32 -0700 | [diff] [blame] | 137 | const endpoint = this.XosModelDiscovererService.getApiUrlFromModel(this.XosModelDiscovererService.get(this.data.model)); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 138 | const resource = this.ModelRest.getResource(endpoint); |
| 139 | this.model = new resource({}); |
| 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 | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 198 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 199 | } |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 200 | |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 201 | public iterateItems() { |
| 202 | const rowCount = this.tableCfg.filteredData.length > this.tableCfg.pagination.pageSize ? this.tableCfg.pagination.pageSize : this.tableCfg.filteredData.length; |
| 203 | if ((this.tableCfg.selectedRow + 1) < rowCount) { |
| 204 | this.tableCfg.selectedRow++; |
| 205 | } |
| 206 | else { |
| 207 | this.tableCfg.selectedRow = 0; |
| 208 | } |
| 209 | this.$scope.$apply(); |
| 210 | } |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 211 | |
| 212 | public getRelatedItemId(relation: IXosModelRelation, item: any): boolean { |
| 213 | return this.XosCrudRelation.existsRelatedItem(relation, item); |
| 214 | } |
| 215 | |
| 216 | public getHumanReadableOnField(r: IXosModelRelation) { |
| 217 | return this.XosCrudRelation.getHumanReadableOnField(r, this.data.model); |
| 218 | } |
| 219 | |
| 220 | public getRelatedModels(relations: {manytoone: IXosModelRelation[], onetomany: IXosModelRelation[]}, item: any) { |
Matteo Scandolo | a471859 | 2017-08-10 14:54:51 -0700 | [diff] [blame] | 221 | this.$log.debug(`[XosCrud] Managing relation for ${this.data.model}:`, relations); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 222 | |
| 223 | // loading many to one relations (you'll get a model) |
| 224 | _.forEach(relations.manytoone, (r: IXosModelRelation) => { |
| 225 | if (!item || !item[`${r.on_field.toLowerCase()}_id`]) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | this.$log.debug(`[XosCrud] Loading manytoone relation with ${r.model} on ${r.on_field}`); |
| 230 | |
| 231 | if (!angular.isDefined(this.relatedModels.manytoone[r.model])) { |
| 232 | this.relatedModels.manytoone[r.model] = {}; |
| 233 | } |
| 234 | |
| 235 | this.XosCrudRelation.getModel(r, item[`${r.on_field.toLowerCase()}_id`]) |
| 236 | .then(res => { |
| 237 | this.relatedModels.manytoone[r.model][r.on_field] = res; |
| 238 | }) |
| 239 | .catch(err => { |
| 240 | this.$log.error(`[XosCrud] Error loading manytoone relation with ${r.model} on ${r.on_field}`, err); |
| 241 | }); |
| 242 | }); |
| 243 | |
| 244 | // loading onetomany relations (you'll get a list of models) |
| 245 | _.forEach(relations.onetomany, (r: IXosModelRelation) => { |
| 246 | if (!item) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | this.$log.debug(`[XosCrud] Loading onetomany relation with ${r.model} on ${r.on_field}`); |
| 251 | |
| 252 | if (!angular.isDefined(this.relatedModels.onetomany[r.model])) { |
| 253 | this.relatedModels.onetomany[r.model] = {}; |
| 254 | } |
| 255 | |
| 256 | this.XosCrudRelation.getModels(r, item.id) |
| 257 | .then(res => { |
| 258 | this.relatedModels.onetomany[r.model][r.on_field] = res; |
| 259 | }) |
| 260 | .catch(err => { |
| 261 | this.$log.error(`[XosCrud] Error loading onetomany relation with ${r.model} on ${r.on_field}`, err); |
| 262 | }); |
| 263 | }); |
Matteo Scandolo | 00d9789 | 2016-12-23 17:53:12 -0800 | [diff] [blame] | 264 | } |
Matteo Scandolo | 9f87f30 | 2016-12-13 18:11:10 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | export const xosCrud: angular.IComponentOptions = { |
| 268 | template: require('./crud.html'), |
| 269 | controllerAs: 'vm', |
| 270 | controller: CrudController |
| 271 | }; |