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 | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 19 | import * as _ from 'lodash'; |
| 20 | import * as pluralize from 'pluralize'; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 21 | import {IXosTableColumn, IXosTableCfg} from '../../table/table'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 22 | import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest'; |
Matteo Scandolo | e7e052d | 2017-07-31 19:54:31 -0700 | [diff] [blame] | 23 | import {IXosFormCfg, IXosFormInput, IXosFormInputValidator, IXosFormInputOptions} from '../../form/form'; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 24 | import {IXosModelStoreService} from '../../../datasources/stores/model.store'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 25 | import {IXosState} from '../runtime-states'; |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 26 | import {IXosFormHelpersService} from '../../form/form-helpers'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 27 | |
| 28 | export interface IXosModelDefsFieldValidators { |
| 29 | name: string; |
| 30 | bool_value?: boolean; |
| 31 | int_value?: number; |
| 32 | } |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 33 | |
| 34 | export interface IXosModelDefsField { |
| 35 | name: string; |
| 36 | type: string; |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame^] | 37 | read_only: boolean; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 38 | validators?: IXosModelDefsFieldValidators[]; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 39 | hint?: string; |
| 40 | relation?: { |
| 41 | model: string; |
| 42 | type: string; |
| 43 | }; |
Matteo Scandolo | e7e052d | 2017-07-31 19:54:31 -0700 | [diff] [blame] | 44 | options?: IXosFormInputOptions[]; |
| 45 | default?: any | null; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | export interface IXosConfigHelpersService { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 49 | excluded_fields: string[]; |
Matteo Scandolo | c8a58c8 | 2017-08-17 17:14:38 -0700 | [diff] [blame] | 50 | form_excluded_fields: string[]; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 51 | modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[]; |
| 52 | modelToTableCfg(model: IXosModeldef, modelName: string): IXosTableCfg; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 53 | modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[]; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 54 | modelToFormCfg(model: IXosModeldef): IXosFormCfg; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 55 | pluralize(string: string, quantity?: number, count?: boolean): string; |
| 56 | toLabel(string: string, pluralize?: boolean): string; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 57 | toLabels(string: string[], pluralize?: boolean): string[]; |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 58 | stateFromCoreModel(name: string): string; |
| 59 | stateWithParams(name: string, model: any): string; |
Matteo Scandolo | 8248bca | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 60 | relatedStateWithParams(name: string, id: string): string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 61 | stateWithParamsForJs(name: string, model: any): any; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 64 | export class ConfigHelpers implements IXosConfigHelpersService { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 65 | static $inject = [ |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 66 | '$q', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 67 | '$state', |
| 68 | 'toastr', |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 69 | 'XosModelStore', |
| 70 | 'XosFormHelpers' |
| 71 | ]; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 72 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 73 | public excluded_fields = [ |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 74 | 'created', |
| 75 | 'updated', |
| 76 | 'enacted', |
| 77 | 'policed', |
| 78 | 'backend_register', |
| 79 | 'deleted', |
| 80 | 'write_protect', |
| 81 | 'lazy_blocked', |
| 82 | 'no_sync', |
| 83 | 'no_policy', |
| 84 | 'omf_friendly', |
| 85 | 'enabled', |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 86 | 'validators', |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 87 | 'password', |
| 88 | 'backend_need_delete', |
Scott Baker | e5dcb9a | 2017-09-07 10:18:06 -0700 | [diff] [blame] | 89 | 'backend_need_delete_policy', |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 90 | 'backend_need_reap', |
Scott Baker | e5dcb9a | 2017-09-07 10:18:06 -0700 | [diff] [blame] | 91 | 'leaf_model_name', |
| 92 | 'link_deleted_count', |
Matteo Scandolo | 8cd21b0 | 2017-09-20 10:13:13 +0900 | [diff] [blame] | 93 | 'policy_code', |
| 94 | 'backend_code', |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 95 | ]; |
| 96 | |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 97 | public form_excluded_fields = this.excluded_fields.concat([ |
| 98 | 'id', |
| 99 | 'policy_status', |
| 100 | 'backend_status', |
| 101 | ]); |
| 102 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 103 | constructor( |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 104 | private $q: ng.IQService, |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 105 | private $state: ng.ui.IStateService, |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 106 | private toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 107 | private XosModelStore: IXosModelStoreService, |
| 108 | private XosFormHelpers: IXosFormHelpersService |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 109 | ) { |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 110 | pluralize.addIrregularRule('xos', 'xoses'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 111 | pluralize.addPluralRule(/slice$/i, 'slices'); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 112 | pluralize.addSingularRule(/slice$/i, 'slice'); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 113 | pluralize.addPluralRule(/library$/i, 'librarys'); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 114 | pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss'); |
| 115 | pluralize.addPluralRule(/controllerimages/i, 'controllerimagess'); |
| 116 | pluralize.addPluralRule(/servicedependency/i, 'servicedependencys'); |
| 117 | pluralize.addPluralRule(/servicemonitoringagentinfo/i, 'servicemonitoringagentinfoes'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 120 | public pluralize(string: string, quantity?: number, count?: boolean): string { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 121 | return pluralize(string, quantity, count); |
| 122 | } |
| 123 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 124 | public toLabels(strings: string[], pluralize?: boolean): string[] { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 125 | if (angular.isArray(strings)) { |
| 126 | return _.map(strings, s => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 127 | return this.toLabel(s, pluralize); |
| 128 | }); |
| 129 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 132 | public toLabel(string: string, pluralize?: boolean): string { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 133 | |
| 134 | if (pluralize) { |
| 135 | string = this.pluralize(string); |
| 136 | } |
| 137 | |
| 138 | string = this.fromCamelCase(string); |
| 139 | string = this.fromSnakeCase(string); |
| 140 | string = this.fromKebabCase(string); |
| 141 | |
| 142 | return this.capitalizeFirst(string); |
| 143 | } |
| 144 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 145 | public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg { |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 146 | const cfg = { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 147 | columns: this.modelFieldsToColumnsCfg(model), |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 148 | filter: 'fulltext', |
| 149 | order: {field: 'id', reverse: false}, |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 150 | pagination: { |
| 151 | pageSize: 10 |
| 152 | }, |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 153 | actions: [ |
| 154 | { |
Matteo Scandolo | cc4bce8 | 2017-08-07 13:11:47 -0700 | [diff] [blame] | 155 | label: 'details', |
| 156 | icon: 'search', |
| 157 | cb: (item) => { |
| 158 | this.$state.go(this.$state.current.name, {id: item.id}); |
| 159 | } |
| 160 | }, |
| 161 | { |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 162 | label: 'delete', |
| 163 | icon: 'remove', |
| 164 | color: 'red', |
| 165 | cb: (item) => { |
| 166 | let obj = angular.copy(item); |
Max Chu | bab0a58 | 2017-09-06 08:32:34 -0700 | [diff] [blame] | 167 | const objName = (angular.isUndefined(obj.name)) ? 'instance' : obj.name; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 168 | |
| 169 | item.$delete() |
| 170 | .then((res) => { |
| 171 | if (res.status === 404) { |
| 172 | // TODO understand why it does not go directly in catch |
| 173 | throw new Error(); |
| 174 | } |
Matteo Scandolo | e9cdf9a | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 175 | this.toastr.info(`Requested removal for ${model.name} ${objName}`); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 176 | }) |
| 177 | .catch(() => { |
Max Chu | bab0a58 | 2017-09-06 08:32:34 -0700 | [diff] [blame] | 178 | this.toastr.error(`Error while deleting ${objName}`); |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 179 | }); |
| 180 | } |
| 181 | } |
| 182 | ] |
| 183 | }; |
| 184 | return cfg; |
| 185 | } |
| 186 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 187 | public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] { |
| 188 | const fields: IXosModelDefsField[] = model.fields; |
| 189 | const modelName: string = model.name; |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 190 | const columns = _.map(fields, (f) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 191 | if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | const col: IXosTableColumn = { |
| 195 | label: this.toLabel(f.name), |
| 196 | prop: f.name |
| 197 | }; |
| 198 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 199 | if (f.name === 'id' || f.name === 'name') { |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 200 | col.link = item => this.stateWithParamsForJs(modelName, item.id); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 203 | // if the field identify a relation, create a link |
Matteo Scandolo | 1897514 | 2017-08-01 14:48:04 -0700 | [diff] [blame] | 204 | if (f.relation && f.relation.type === 'manytoone') { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 205 | col.type = 'custom'; |
| 206 | col.formatter = item => { |
| 207 | this.populateRelated(item, item[f.name], f); |
| 208 | return item[f.name]; |
| 209 | }; |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 210 | col.link = item => this.stateWithParamsForJs(f.relation.model, item[col.prop]); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 213 | if (f.name === 'backend_status' || f.name === 'policy_status') { |
Matteo Scandolo | 8cd21b0 | 2017-09-20 10:13:13 +0900 | [diff] [blame] | 214 | |
| 215 | const statusCol = f.name === 'backend_status' ? 'backend_code' : 'policy_code'; |
| 216 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 217 | col.type = 'icon'; |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 218 | col.hover = (item) => { |
| 219 | return item[f.name]; |
| 220 | }; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 221 | col.formatter = (item) => { |
Matteo Scandolo | 8cd21b0 | 2017-09-20 10:13:13 +0900 | [diff] [blame] | 222 | if (parseInt(item[statusCol], 10) === 1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 223 | return 'check'; |
| 224 | } |
Matteo Scandolo | 8cd21b0 | 2017-09-20 10:13:13 +0900 | [diff] [blame] | 225 | if (parseInt(item[statusCol], 10) === 2) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 226 | return 'exclamation-circle'; |
| 227 | } |
Matteo Scandolo | 8cd21b0 | 2017-09-20 10:13:13 +0900 | [diff] [blame] | 228 | if (parseInt(item[statusCol], 10) === 0) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 229 | return 'clock-o'; |
| 230 | } |
| 231 | }; |
| 232 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 233 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 234 | return col; |
| 235 | }) |
| 236 | .filter(v => angular.isDefined(v)); |
| 237 | |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 238 | return columns; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 239 | }; |
| 240 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 241 | public stateFromCoreModel(name: string): string { |
| 242 | const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => { |
| 243 | if (s.data) { |
| 244 | return s.data.model === name; |
| 245 | } |
| 246 | return false; |
| 247 | }); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 248 | return state ? state.name : null; |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | public stateWithParams(name: string, model: any): string { |
| 252 | const state = this.stateFromCoreModel(name); |
| 253 | return `${state}({id: ${model['id']}})`; |
| 254 | } |
| 255 | |
Matteo Scandolo | 8248bca | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 256 | public relatedStateWithParams(name: string, id: string): string { |
| 257 | const state = this.stateFromCoreModel(name); |
| 258 | return `${state}({id: ${id}})`; |
| 259 | } |
| 260 | |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 261 | public stateWithParamsForJs(name: string, id: number): any { |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 262 | const state = this.stateFromCoreModel(name); |
Matteo Scandolo | 1888b2a | 2018-01-08 16:49:06 -0800 | [diff] [blame] | 263 | return {name: state, params: {id: id}}; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 266 | public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] { |
| 267 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 268 | return _.map(fields, (f: IXosModelDefsField) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 269 | const input: IXosFormInput = { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 270 | name: f.name, |
| 271 | label: this.toLabel(f.name), |
| 272 | type: f.type, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 273 | validators: this.formatValidators(f.validators), |
Matteo Scandolo | e7e052d | 2017-07-31 19:54:31 -0700 | [diff] [blame] | 274 | hint: f.hint, |
Matteo Scandolo | d67adee | 2018-03-08 16:27:05 -0800 | [diff] [blame^] | 275 | default: this.formatDefaultValues(f.default), |
| 276 | read_only: f.read_only |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 277 | }; |
Matteo Scandolo | e7e052d | 2017-07-31 19:54:31 -0700 | [diff] [blame] | 278 | |
| 279 | // NOTE populate drop-downs based on relation |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 280 | if (f.relation) { |
| 281 | input.type = 'select'; |
| 282 | this.populateSelectField(f, input); |
Matteo Scandolo | e7e052d | 2017-07-31 19:54:31 -0700 | [diff] [blame] | 283 | } |
| 284 | // NOTE if static options are defined in modeldefs |
| 285 | // the f.options field is already populated, |
| 286 | // we just need to move it to the input |
| 287 | else if (f.options && f.options.length > 0) { |
| 288 | input.options = f.options; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 289 | } |
| 290 | return input; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 291 | }) |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 292 | .filter(f => this.form_excluded_fields.indexOf(f.name) === -1); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 295 | public modelToFormCfg(model: IXosModeldef): IXosFormCfg { |
Matteo Scandolo | f1e68cd | 2017-09-05 17:30:34 -0700 | [diff] [blame] | 296 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 297 | const formCfg: IXosFormCfg = { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 298 | formName: `${model.name}Form`, |
Matteo Scandolo | d53ac1d | 2017-08-01 15:06:09 -0700 | [diff] [blame] | 299 | exclude: this.form_excluded_fields, |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 300 | actions: [{ |
| 301 | label: 'Save', |
| 302 | class: 'success', |
| 303 | icon: 'ok', |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 304 | cb: null |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 305 | }], |
| 306 | inputs: this.modelFieldToInputCfg(model.fields) |
| 307 | }; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 308 | |
| 309 | formCfg.actions[0].cb = (item, form: angular.IFormController) => { |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 310 | const d = this.$q.defer(); |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 311 | if (!form.$valid) { |
| 312 | formCfg.feedback = { |
| 313 | show: true, |
| 314 | message: 'Form is invalid', |
| 315 | type: 'danger', |
| 316 | closeBtn: true |
| 317 | }; |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 318 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 319 | return; |
| 320 | } |
Matteo Scandolo | 620cb49 | 2017-11-27 16:56:36 -0800 | [diff] [blame] | 321 | // remove fields added by the GUI |
| 322 | item = this.removeExtraFields(item, model); |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 323 | |
Matteo Scandolo | 620cb49 | 2017-11-27 16:56:36 -0800 | [diff] [blame] | 324 | const itemCopy = angular.copy(item); |
Max Chu | bab0a58 | 2017-09-06 08:32:34 -0700 | [diff] [blame] | 325 | const itemName = (angular.isUndefined(itemCopy.name)) ? model.name : itemCopy.name; |
| 326 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 327 | item.$save() |
| 328 | .then((res) => { |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 329 | formCfg.feedback = { |
| 330 | show: true, |
Max Chu | bab0a58 | 2017-09-06 08:32:34 -0700 | [diff] [blame] | 331 | message: `${itemName} successfully saved`, |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 332 | type: 'success', |
| 333 | closeBtn: true |
| 334 | }; |
Max Chu | bab0a58 | 2017-09-06 08:32:34 -0700 | [diff] [blame] | 335 | this.toastr.success(`${itemName} successfully saved`); |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 336 | d.resolve(res); |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 337 | }) |
| 338 | .catch(err => { |
Matteo Scandolo | 39e0415 | 2017-11-29 14:24:45 -0800 | [diff] [blame] | 339 | const errorMsg = err.specific_error || err.error || 'Internal Server Error'; |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 340 | formCfg.feedback = { |
| 341 | show: true, |
Matteo Scandolo | 39e0415 | 2017-11-29 14:24:45 -0800 | [diff] [blame] | 342 | message: `Error while saving ${itemName}: ${errorMsg}.`, |
Matteo Scandolo | 42c6692 | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 343 | type: 'danger', |
| 344 | closeBtn: true |
| 345 | }; |
Matteo Scandolo | 39e0415 | 2017-11-29 14:24:45 -0800 | [diff] [blame] | 346 | this.toastr.error(err.specific_error || '', `Error while saving ${itemName}: ${errorMsg}`); |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 347 | d.reject(err); |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 348 | }); |
Matteo Scandolo | 6349847 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 349 | |
| 350 | return d.promise; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | return formCfg; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Matteo Scandolo | 620cb49 | 2017-11-27 16:56:36 -0800 | [diff] [blame] | 356 | private removeExtraFields(item: any, modelDef: IXosModeldef) { |
| 357 | _.forEach(Object.keys(item), prop => { |
| 358 | const hasField = _.findIndex(modelDef.fields, {name: prop}) > -1; |
| 359 | if (!hasField) { |
| 360 | delete item[prop]; |
| 361 | } |
| 362 | }); |
| 363 | return item; |
| 364 | } |
| 365 | |
Matteo Scandolo | f1e68cd | 2017-09-05 17:30:34 -0700 | [diff] [blame] | 366 | private formatDefaultValues(val: any): any { |
| 367 | |
| 368 | if (angular.isString(val)) { |
| 369 | const unquoted = val.split('"').join('').toLowerCase(); |
| 370 | if (unquoted === 'true') { |
| 371 | return true; |
| 372 | } |
| 373 | else if (unquoted === 'false') { |
| 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | return val || undefined; |
| 379 | } |
| 380 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 381 | private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator { |
| 382 | // convert validators as expressed from modelDefs, |
| 383 | // to the object required by xosForm |
| 384 | return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => { |
| 385 | formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value; |
| 386 | return formValidators; |
| 387 | }, {}); |
| 388 | } |
| 389 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 390 | private fromCamelCase(string: string): string { |
| 391 | return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' '); |
| 392 | } |
| 393 | |
| 394 | private fromSnakeCase(string: string): string { |
| 395 | return string.split('_').join(' ').trim(); |
| 396 | } |
| 397 | |
| 398 | private fromKebabCase(string: string): string { |
| 399 | return string.split('-').join(' ').trim(); |
| 400 | } |
| 401 | |
| 402 | private capitalizeFirst(string: string): string { |
| 403 | return string.slice(0, 1).toUpperCase() + string.slice(1); |
| 404 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 405 | |
| 406 | private populateRelated(item: any, fk: string, field: IXosModelDefsField): any { |
| 407 | // if the relation is not defined return |
| 408 | if (!fk || angular.isUndefined(fk) || fk === null) { |
| 409 | return; |
| 410 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 411 | this.XosModelStore.query(field.relation.model) |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 412 | .subscribe(res => { |
| 413 | if (angular.isDefined(res) && angular.isDefined(fk)) { |
| 414 | let ri = _.find(res, {id: fk}); |
| 415 | if (angular.isDefined(ri)) { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 416 | if (angular.isDefined(ri.name)) { |
| 417 | item[`${field.name}-formatted`] = ri.name; |
| 418 | } |
| 419 | else if (angular.isDefined(ri.humanReadableName)) { |
| 420 | item[`${field.name}-formatted`] = ri.humanReadableName; |
| 421 | } |
| 422 | else { |
| 423 | item[`${field.name}-formatted`] = ri.id; |
| 424 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | }); |
| 428 | } |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 429 | |
| 430 | // augment a select field with related model informations |
| 431 | private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 432 | this.XosModelStore.query(field.relation.model) |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 433 | .subscribe(res => { |
| 434 | input.options = _.map(res, item => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 435 | let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name}; |
| 436 | if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) { |
| 437 | opt.label = item.id; |
| 438 | } |
| 439 | return opt; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 440 | }); |
| 441 | }); |
| 442 | } |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 443 | } |