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