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