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