blob: 85039fe8537c49e9f4485744882697117bf20852 [file] [log] [blame]
Matteo Scandolod58d5042016-12-16 16:59:21 -08001import * as _ from 'lodash';
2import * as pluralize from 'pluralize';
Matteo Scandolocb466ed2017-01-04 17:16:24 -08003import {IXosTableColumn, IXosTableCfg} from '../../table/table';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08004import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest';
Matteo Scandoloe7e052d2017-07-31 19:54:31 -07005import {IXosFormCfg, IXosFormInput, IXosFormInputValidator, IXosFormInputOptions} from '../../form/form';
Matteo Scandolo47860fe2017-02-02 12:05:55 -08006import {IXosModelStoreService} from '../../../datasources/stores/model.store';
Matteo Scandolo1aee1982017-02-17 08:33:23 -08007import {IXosState} from '../runtime-states';
8
9export interface IXosModelDefsFieldValidators {
10 name: string;
11 bool_value?: boolean;
12 int_value?: number;
13}
Matteo Scandolod58d5042016-12-16 16:59:21 -080014
15export interface IXosModelDefsField {
16 name: string;
17 type: string;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080018 validators?: IXosModelDefsFieldValidators[];
Matteo Scandolo04964232017-01-07 12:53:46 -080019 hint?: string;
20 relation?: {
21 model: string;
22 type: string;
23 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070024 options?: IXosFormInputOptions[];
25 default?: any | null;
Matteo Scandolod58d5042016-12-16 16:59:21 -080026}
27
28export interface IXosConfigHelpersService {
Matteo Scandoloee655a12016-12-19 15:38:43 -080029 excluded_fields: string[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080030 modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[];
31 modelToTableCfg(model: IXosModeldef, modelName: string): IXosTableCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080032 modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080033 modelToFormCfg(model: IXosModeldef): IXosFormCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080034 pluralize(string: string, quantity?: number, count?: boolean): string;
35 toLabel(string: string, pluralize?: boolean): string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080036 toLabels(string: string[], pluralize?: boolean): string[];
Matteo Scandoloa242c872017-01-12 15:13:00 -080037 stateFromCoreModel(name: string): string;
38 stateWithParams(name: string, model: any): string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080039 stateWithParamsForJs(name: string, model: any): any;
Matteo Scandolod58d5042016-12-16 16:59:21 -080040}
41
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080042export class ConfigHelpers implements IXosConfigHelpersService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080043 static $inject = [
44 '$state',
45 'toastr',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080046 'XosModelStore'];
Matteo Scandolod58d5042016-12-16 16:59:21 -080047
Matteo Scandolo1aee1982017-02-17 08:33:23 -080048 public excluded_fields = [
Matteo Scandoloee655a12016-12-19 15:38:43 -080049 'created',
50 'updated',
51 'enacted',
52 'policed',
53 'backend_register',
54 'deleted',
55 'write_protect',
56 'lazy_blocked',
57 'no_sync',
58 'no_policy',
59 'omf_friendly',
60 'enabled',
Matteo Scandolod62ea792016-12-22 14:02:28 -080061 'validators',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080062 'password',
63 'backend_need_delete',
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070064 'backend_need_reap',
65 'leaf_model_name'
Matteo Scandoloee655a12016-12-19 15:38:43 -080066 ];
67
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070068 public form_excluded_fields = this.excluded_fields.concat([
69 'id',
70 'policy_status',
71 'backend_status',
72 ]);
73
Matteo Scandolocb466ed2017-01-04 17:16:24 -080074 constructor(
Matteo Scandoloa242c872017-01-12 15:13:00 -080075 private $state: ng.ui.IStateService,
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080076 private toastr: ng.toastr.IToastrService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080077 private XosModelStore: IXosModelStoreService
Matteo Scandolocb466ed2017-01-04 17:16:24 -080078 ) {
Matteo Scandolo08464e52017-01-17 13:35:27 -080079 pluralize.addIrregularRule('xos', 'xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -080080 pluralize.addPluralRule(/slice$/i, 'slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -080081 pluralize.addSingularRule(/slice$/i, 'slice');
Matteo Scandolo1aee1982017-02-17 08:33:23 -080082 pluralize.addPluralRule(/library$/i, 'librarys');
Matteo Scandolo5d962a32017-08-01 18:16:14 -070083 pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss');
84 pluralize.addPluralRule(/controllerimages/i, 'controllerimagess');
85 pluralize.addPluralRule(/servicedependency/i, 'servicedependencys');
86 pluralize.addPluralRule(/servicemonitoringagentinfo/i, 'servicemonitoringagentinfoes');
Matteo Scandolod58d5042016-12-16 16:59:21 -080087 }
88
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080089 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080090 return pluralize(string, quantity, count);
91 }
92
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080093 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080094 if (angular.isArray(strings)) {
95 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080096 return this.toLabel(s, pluralize);
97 });
98 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080099 }
100
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800101 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800102
103 if (pluralize) {
104 string = this.pluralize(string);
105 }
106
107 string = this.fromCamelCase(string);
108 string = this.fromSnakeCase(string);
109 string = this.fromKebabCase(string);
110
111 return this.capitalizeFirst(string);
112 }
113
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800114 public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800115 const cfg = {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800116 columns: this.modelFieldsToColumnsCfg(model),
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800117 filter: 'fulltext',
118 order: {field: 'id', reverse: false},
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800119 pagination: {
120 pageSize: 10
121 },
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800122 actions: [
123 {
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700124 label: 'details',
125 icon: 'search',
126 cb: (item) => {
127 this.$state.go(this.$state.current.name, {id: item.id});
128 }
129 },
130 {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800131 label: 'delete',
132 icon: 'remove',
133 color: 'red',
134 cb: (item) => {
135 let obj = angular.copy(item);
136
137 item.$delete()
138 .then((res) => {
139 if (res.status === 404) {
140 // TODO understand why it does not go directly in catch
141 throw new Error();
142 }
143 this.toastr.info(`${model.name} ${obj.name} succesfully deleted`);
144 })
145 .catch(() => {
146 this.toastr.error(`Error while deleting ${obj.name}`);
147 });
148 }
149 }
150 ]
151 };
152 return cfg;
153 }
154
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800155 public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] {
156 const fields: IXosModelDefsField[] = model.fields;
157 const modelName: string = model.name;
Matteo Scandolo231de262017-01-04 16:33:14 -0800158 const columns = _.map(fields, (f) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800159 if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800160 return;
161 }
162 const col: IXosTableColumn = {
163 label: this.toLabel(f.name),
164 prop: f.name
165 };
166
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800167 if (f.name === 'id' || f.name === 'name') {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800168 col.link = item => this.stateWithParams(modelName, item);
Matteo Scandoloee655a12016-12-19 15:38:43 -0800169 }
170
Matteo Scandolo04964232017-01-07 12:53:46 -0800171 // if the field identify a relation, create a link
Matteo Scandolo18975142017-08-01 14:48:04 -0700172 if (f.relation && f.relation.type === 'manytoone') {
Matteo Scandolo04964232017-01-07 12:53:46 -0800173 col.type = 'custom';
174 col.formatter = item => {
175 this.populateRelated(item, item[f.name], f);
176 return item[f.name];
177 };
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800178 col.link = item => {
179 return this.stateWithParams(f.relation.model, item);
180 };
Matteo Scandolo04964232017-01-07 12:53:46 -0800181 }
182
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700183 if (f.name === 'backend_status' || f.name === 'policy_status') {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800184 col.type = 'icon';
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800185 col.hover = (item) => {
186 return item[f.name];
187 };
Matteo Scandolod58d5042016-12-16 16:59:21 -0800188 col.formatter = (item) => {
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700189 if (item[f.name].indexOf('1') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800190 return 'check';
191 }
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700192 if (item[f.name].indexOf('2') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800193 return 'exclamation-circle';
194 }
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700195 if (item[f.name].indexOf('0') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800196 return 'clock-o';
197 }
198 };
199 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800200
Matteo Scandolod58d5042016-12-16 16:59:21 -0800201 return col;
202 })
203 .filter(v => angular.isDefined(v));
204
Matteo Scandolo231de262017-01-04 16:33:14 -0800205 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800206 };
207
Matteo Scandoloa242c872017-01-12 15:13:00 -0800208 public stateFromCoreModel(name: string): string {
209 const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => {
210 if (s.data) {
211 return s.data.model === name;
212 }
213 return false;
214 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800215 return state ? state.name : null;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800216 }
217
218 public stateWithParams(name: string, model: any): string {
219 const state = this.stateFromCoreModel(name);
220 return `${state}({id: ${model['id']}})`;
221 }
222
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800223 public stateWithParamsForJs(name: string, model: any): any {
224 // TODO test and interface
225 const state = this.stateFromCoreModel(name);
226 return {name: state, params: {id: model.id}};
227 }
228
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800229 public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] {
230
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800231 return _.map(fields, (f: IXosModelDefsField) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800232 const input: IXosFormInput = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800233 name: f.name,
234 label: this.toLabel(f.name),
235 type: f.type,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800236 validators: this.formatValidators(f.validators),
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700237 hint: f.hint,
238 default: f.default || null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800239 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700240
241 // NOTE populate drop-downs based on relation
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800242 if (f.relation) {
243 input.type = 'select';
244 this.populateSelectField(f, input);
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700245 }
246 // NOTE if static options are defined in modeldefs
247 // the f.options field is already populated,
248 // we just need to move it to the input
249 else if (f.options && f.options.length > 0) {
250 input.options = f.options;
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800251 }
252 return input;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800253 })
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700254 .filter(f => this.form_excluded_fields.indexOf(f.name) === -1);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800255 }
256
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800257 public modelToFormCfg(model: IXosModeldef): IXosFormCfg {
258 const formCfg: IXosFormCfg = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800259 formName: `${model.name}Form`,
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700260 exclude: this.form_excluded_fields,
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800261 actions: [{
262 label: 'Save',
263 class: 'success',
264 icon: 'ok',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800265 cb: null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800266 }],
267 inputs: this.modelFieldToInputCfg(model.fields)
268 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800269
270 formCfg.actions[0].cb = (item, form: angular.IFormController) => {
271
272 if (!form.$valid) {
273 formCfg.feedback = {
274 show: true,
275 message: 'Form is invalid',
276 type: 'danger',
277 closeBtn: true
278 };
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800279
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800280 return;
281 }
282
283 const model = angular.copy(item);
284
285 // TODO remove ManyToMany relations and save them separately (how??)
286 delete item.networks;
287
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800288 // remove field added by xosTable
289 _.forEach(Object.keys(item), prop => {
290 if (prop.indexOf('-formatted') > -1) {
291 delete item[prop];
292 }
293 });
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800294
295 item.$save()
296 .then((res) => {
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800297 formCfg.feedback = {
298 show: true,
299 message: `${model.name} succesfully saved`,
300 type: 'success',
301 closeBtn: true
302 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800303 this.toastr.success(`${model.name} succesfully saved`);
304 })
305 .catch(err => {
Matteo Scandolo42c66922017-05-01 17:24:59 -0700306 formCfg.feedback = {
307 show: true,
308 message: `Error while saving ${model.name}: ${err.error}. ${err.specific_error || ''}`,
309 type: 'danger',
310 closeBtn: true
311 };
312 this.toastr.error(err.specific_error || '', `Error while saving ${model.name}: ${err.error}`);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800313 });
314 };
315
316 return formCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800317 }
318
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800319 private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator {
320 // convert validators as expressed from modelDefs,
321 // to the object required by xosForm
322 return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => {
323 formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value;
324 return formValidators;
325 }, {});
326 }
327
Matteo Scandolod58d5042016-12-16 16:59:21 -0800328 private fromCamelCase(string: string): string {
329 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
330 }
331
332 private fromSnakeCase(string: string): string {
333 return string.split('_').join(' ').trim();
334 }
335
336 private fromKebabCase(string: string): string {
337 return string.split('-').join(' ').trim();
338 }
339
340 private capitalizeFirst(string: string): string {
341 return string.slice(0, 1).toUpperCase() + string.slice(1);
342 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800343
344 private populateRelated(item: any, fk: string, field: IXosModelDefsField): any {
345 // if the relation is not defined return
346 if (!fk || angular.isUndefined(fk) || fk === null) {
347 return;
348 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800349 this.XosModelStore.query(field.relation.model)
Matteo Scandolo04964232017-01-07 12:53:46 -0800350 .subscribe(res => {
351 if (angular.isDefined(res) && angular.isDefined(fk)) {
352 let ri = _.find(res, {id: fk});
353 if (angular.isDefined(ri)) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800354 if (angular.isDefined(ri.name)) {
355 item[`${field.name}-formatted`] = ri.name;
356 }
357 else if (angular.isDefined(ri.humanReadableName)) {
358 item[`${field.name}-formatted`] = ri.humanReadableName;
359 }
360 else {
361 item[`${field.name}-formatted`] = ri.id;
362 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800363 }
364 }
365 });
366 }
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800367
368 // augment a select field with related model informations
369 private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800370 this.XosModelStore.query(field.relation.model)
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800371 .subscribe(res => {
372 input.options = _.map(res, item => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800373 let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name};
374 if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) {
375 opt.label = item.id;
376 }
377 return opt;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800378 });
379 });
380 }
Matteo Scandolod58d5042016-12-16 16:59:21 -0800381}