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