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