blob: 16958de5603c4303d3d082ac17c4ff81b57c2a81 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolod58d5042016-12-16 16:59:21 -080019import * as _ from 'lodash';
20import * as pluralize from 'pluralize';
Matteo Scandolocb466ed2017-01-04 17:16:24 -080021import {IXosTableColumn, IXosTableCfg} from '../../table/table';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080022import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest';
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070023import {IXosFormCfg, IXosFormInput, IXosFormInputValidator, IXosFormInputOptions} from '../../form/form';
Matteo Scandolo47860fe2017-02-02 12:05:55 -080024import {IXosModelStoreService} from '../../../datasources/stores/model.store';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080025import {IXosState} from '../runtime-states';
Matteo Scandolo0e171f32017-09-26 17:21:41 -070026import {IXosFormHelpersService} from '../../form/form-helpers';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080027
28export interface IXosModelDefsFieldValidators {
29 name: string;
30 bool_value?: boolean;
31 int_value?: number;
32}
Matteo Scandolod58d5042016-12-16 16:59:21 -080033
34export interface IXosModelDefsField {
35 name: string;
36 type: string;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080037 validators?: IXosModelDefsFieldValidators[];
Matteo Scandolo04964232017-01-07 12:53:46 -080038 hint?: string;
39 relation?: {
40 model: string;
41 type: string;
42 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070043 options?: IXosFormInputOptions[];
44 default?: any | null;
Matteo Scandolod58d5042016-12-16 16:59:21 -080045}
46
47export interface IXosConfigHelpersService {
Matteo Scandoloee655a12016-12-19 15:38:43 -080048 excluded_fields: string[];
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070049 form_excluded_fields: string[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080050 modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[];
51 modelToTableCfg(model: IXosModeldef, modelName: string): IXosTableCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080052 modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080053 modelToFormCfg(model: IXosModeldef): IXosFormCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080054 pluralize(string: string, quantity?: number, count?: boolean): string;
55 toLabel(string: string, pluralize?: boolean): string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080056 toLabels(string: string[], pluralize?: boolean): string[];
Matteo Scandoloa242c872017-01-12 15:13:00 -080057 stateFromCoreModel(name: string): string;
58 stateWithParams(name: string, model: any): string;
Matteo Scandolo8248bca2017-08-09 13:46:04 -070059 relatedStateWithParams(name: string, id: string): string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080060 stateWithParamsForJs(name: string, model: any): any;
Matteo Scandolod58d5042016-12-16 16:59:21 -080061}
62
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080063export class ConfigHelpers implements IXosConfigHelpersService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080064 static $inject = [
Matteo Scandolo0e171f32017-09-26 17:21:41 -070065 '$q',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080066 '$state',
67 'toastr',
Matteo Scandolo0e171f32017-09-26 17:21:41 -070068 'XosModelStore',
69 'XosFormHelpers'
70 ];
Matteo Scandolod58d5042016-12-16 16:59:21 -080071
Matteo Scandolo1aee1982017-02-17 08:33:23 -080072 public excluded_fields = [
Matteo Scandoloee655a12016-12-19 15:38:43 -080073 '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 Scandolod62ea792016-12-22 14:02:28 -080085 'validators',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080086 'password',
87 'backend_need_delete',
Scott Baker63847252017-09-07 10:18:06 -070088 'backend_need_delete_policy',
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070089 'backend_need_reap',
Scott Baker63847252017-09-07 10:18:06 -070090 'leaf_model_name',
91 'link_deleted_count',
Matteo Scandolof363d742017-09-20 10:13:13 +090092 'policy_code',
93 'backend_code',
Matteo Scandoloee655a12016-12-19 15:38:43 -080094 ];
95
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070096 public form_excluded_fields = this.excluded_fields.concat([
97 'id',
98 'policy_status',
99 'backend_status',
100 ]);
101
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800102 constructor(
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700103 private $q: ng.IQService,
Matteo Scandoloa242c872017-01-12 15:13:00 -0800104 private $state: ng.ui.IStateService,
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -0800105 private toastr: ng.toastr.IToastrService,
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700106 private XosModelStore: IXosModelStoreService,
107 private XosFormHelpers: IXosFormHelpersService
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800108 ) {
Matteo Scandolo08464e52017-01-17 13:35:27 -0800109 pluralize.addIrregularRule('xos', 'xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800110 pluralize.addPluralRule(/slice$/i, 'slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800111 pluralize.addSingularRule(/slice$/i, 'slice');
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800112 pluralize.addPluralRule(/library$/i, 'librarys');
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700113 pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss');
114 pluralize.addPluralRule(/controllerimages/i, 'controllerimagess');
115 pluralize.addPluralRule(/servicedependency/i, 'servicedependencys');
116 pluralize.addPluralRule(/servicemonitoringagentinfo/i, 'servicemonitoringagentinfoes');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800117 }
118
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800119 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800120 return pluralize(string, quantity, count);
121 }
122
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800123 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800124 if (angular.isArray(strings)) {
125 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800126 return this.toLabel(s, pluralize);
127 });
128 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800129 }
130
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800131 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800132
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 Scandolo1aee1982017-02-17 08:33:23 -0800144 public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800145 const cfg = {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800146 columns: this.modelFieldsToColumnsCfg(model),
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800147 filter: 'fulltext',
148 order: {field: 'id', reverse: false},
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800149 pagination: {
150 pageSize: 10
151 },
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800152 actions: [
153 {
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700154 label: 'details',
155 icon: 'search',
156 cb: (item) => {
157 this.$state.go(this.$state.current.name, {id: item.id});
158 }
159 },
160 {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800161 label: 'delete',
162 icon: 'remove',
163 color: 'red',
164 cb: (item) => {
165 let obj = angular.copy(item);
Max Chudc6d6882017-09-06 08:32:34 -0700166 const objName = (angular.isUndefined(obj.name)) ? 'instance' : obj.name;
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800167
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 }
Max Chudc6d6882017-09-06 08:32:34 -0700174 this.toastr.info(`${model.name} ${objName} successfully deleted`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800175 })
176 .catch(() => {
Max Chudc6d6882017-09-06 08:32:34 -0700177 this.toastr.error(`Error while deleting ${objName}`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800178 });
179 }
180 }
181 ]
182 };
183 return cfg;
184 }
185
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800186 public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] {
187 const fields: IXosModelDefsField[] = model.fields;
188 const modelName: string = model.name;
Matteo Scandolo231de262017-01-04 16:33:14 -0800189 const columns = _.map(fields, (f) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800190 if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800191 return;
192 }
193 const col: IXosTableColumn = {
194 label: this.toLabel(f.name),
195 prop: f.name
196 };
197
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800198 if (f.name === 'id' || f.name === 'name') {
Matteo Scandolof5739de2017-09-27 08:39:37 -0700199 col.link = item => this.stateWithParamsForJs(modelName, item);
Matteo Scandoloee655a12016-12-19 15:38:43 -0800200 }
201
Matteo Scandolo04964232017-01-07 12:53:46 -0800202 // if the field identify a relation, create a link
Matteo Scandolo18975142017-08-01 14:48:04 -0700203 if (f.relation && f.relation.type === 'manytoone') {
Matteo Scandolo04964232017-01-07 12:53:46 -0800204 col.type = 'custom';
205 col.formatter = item => {
206 this.populateRelated(item, item[f.name], f);
207 return item[f.name];
208 };
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700209 col.link = item => this.relatedStateWithParams(f.relation.model, item[col.prop]);
Matteo Scandolo04964232017-01-07 12:53:46 -0800210 }
211
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700212 if (f.name === 'backend_status' || f.name === 'policy_status') {
Matteo Scandolof363d742017-09-20 10:13:13 +0900213
214 const statusCol = f.name === 'backend_status' ? 'backend_code' : 'policy_code';
215
Matteo Scandolod58d5042016-12-16 16:59:21 -0800216 col.type = 'icon';
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800217 col.hover = (item) => {
218 return item[f.name];
219 };
Matteo Scandolod58d5042016-12-16 16:59:21 -0800220 col.formatter = (item) => {
Matteo Scandolof363d742017-09-20 10:13:13 +0900221 if (parseInt(item[statusCol], 10) === 1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800222 return 'check';
223 }
Matteo Scandolof363d742017-09-20 10:13:13 +0900224 if (parseInt(item[statusCol], 10) === 2) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800225 return 'exclamation-circle';
226 }
Matteo Scandolof363d742017-09-20 10:13:13 +0900227 if (parseInt(item[statusCol], 10) === 0) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800228 return 'clock-o';
229 }
230 };
231 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800232
Matteo Scandolod58d5042016-12-16 16:59:21 -0800233 return col;
234 })
235 .filter(v => angular.isDefined(v));
236
Matteo Scandolo231de262017-01-04 16:33:14 -0800237 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800238 };
239
Matteo Scandoloa242c872017-01-12 15:13:00 -0800240 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 Scandolo1aee1982017-02-17 08:33:23 -0800247 return state ? state.name : null;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800248 }
249
250 public stateWithParams(name: string, model: any): string {
251 const state = this.stateFromCoreModel(name);
252 return `${state}({id: ${model['id']}})`;
253 }
254
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700255 public relatedStateWithParams(name: string, id: string): string {
256 const state = this.stateFromCoreModel(name);
257 return `${state}({id: ${id}})`;
258 }
259
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800260 public stateWithParamsForJs(name: string, model: any): any {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800261 const state = this.stateFromCoreModel(name);
262 return {name: state, params: {id: model.id}};
263 }
264
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800265 public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] {
266
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800267 return _.map(fields, (f: IXosModelDefsField) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800268 const input: IXosFormInput = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800269 name: f.name,
270 label: this.toLabel(f.name),
271 type: f.type,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800272 validators: this.formatValidators(f.validators),
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700273 hint: f.hint,
Matteo Scandolodd9c1152017-09-05 17:30:34 -0700274 default: this.formatDefaultValues(f.default)
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800275 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700276
277 // NOTE populate drop-downs based on relation
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800278 if (f.relation) {
279 input.type = 'select';
280 this.populateSelectField(f, input);
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700281 }
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 Scandolo1aee1982017-02-17 08:33:23 -0800287 }
288 return input;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800289 })
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700290 .filter(f => this.form_excluded_fields.indexOf(f.name) === -1);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800291 }
292
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800293 public modelToFormCfg(model: IXosModeldef): IXosFormCfg {
Matteo Scandolodd9c1152017-09-05 17:30:34 -0700294
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800295 const formCfg: IXosFormCfg = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800296 formName: `${model.name}Form`,
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700297 exclude: this.form_excluded_fields,
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800298 actions: [{
299 label: 'Save',
300 class: 'success',
301 icon: 'ok',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800302 cb: null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800303 }],
304 inputs: this.modelFieldToInputCfg(model.fields)
305 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800306
307 formCfg.actions[0].cb = (item, form: angular.IFormController) => {
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700308 const d = this.$q.defer();
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800309 if (!form.$valid) {
310 formCfg.feedback = {
311 show: true,
312 message: 'Form is invalid',
313 type: 'danger',
314 closeBtn: true
315 };
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800316
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800317 return;
318 }
319
Max Chudc6d6882017-09-06 08:32:34 -0700320 const itemCopy = angular.copy(item);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800321
322 // TODO remove ManyToMany relations and save them separately (how??)
323 delete item.networks;
324
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800325 // remove field added by xosTable
326 _.forEach(Object.keys(item), prop => {
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700327 // FIXME what _ptr fields comes from??
328 if (prop.indexOf('-formatted') > -1 || prop.indexOf('_ptr') > -1) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800329 delete item[prop];
330 }
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700331
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 Scandolo1aee1982017-02-17 08:33:23 -0800336 });
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800337
Max Chudc6d6882017-09-06 08:32:34 -0700338 const itemName = (angular.isUndefined(itemCopy.name)) ? model.name : itemCopy.name;
339
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800340 item.$save()
341 .then((res) => {
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800342 formCfg.feedback = {
343 show: true,
Max Chudc6d6882017-09-06 08:32:34 -0700344 message: `${itemName} successfully saved`,
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800345 type: 'success',
346 closeBtn: true
347 };
Max Chudc6d6882017-09-06 08:32:34 -0700348 this.toastr.success(`${itemName} successfully saved`);
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700349 d.resolve(res);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800350 })
351 .catch(err => {
Matteo Scandolo42c66922017-05-01 17:24:59 -0700352 formCfg.feedback = {
353 show: true,
Max Chudc6d6882017-09-06 08:32:34 -0700354 message: `Error while saving ${itemName}: ${err.error}. ${err.specific_error || ''}`,
Matteo Scandolo42c66922017-05-01 17:24:59 -0700355 type: 'danger',
356 closeBtn: true
357 };
Max Chudc6d6882017-09-06 08:32:34 -0700358 this.toastr.error(err.specific_error || '', `Error while saving ${itemName}: ${err.error}`);
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700359 d.reject(err);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800360 });
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700361
362 return d.promise;
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800363 };
364
365 return formCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800366 }
367
Matteo Scandolodd9c1152017-09-05 17:30:34 -0700368 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 Scandolo1aee1982017-02-17 08:33:23 -0800383 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 Scandolod58d5042016-12-16 16:59:21 -0800392 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 Scandolo04964232017-01-07 12:53:46 -0800407
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 Scandolo1aee1982017-02-17 08:33:23 -0800413 this.XosModelStore.query(field.relation.model)
Matteo Scandolo04964232017-01-07 12:53:46 -0800414 .subscribe(res => {
415 if (angular.isDefined(res) && angular.isDefined(fk)) {
416 let ri = _.find(res, {id: fk});
417 if (angular.isDefined(ri)) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800418 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 Scandolo04964232017-01-07 12:53:46 -0800427 }
428 }
429 });
430 }
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800431
432 // augment a select field with related model informations
433 private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800434 this.XosModelStore.query(field.relation.model)
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800435 .subscribe(res => {
436 input.options = _.map(res, item => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800437 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 Scandolo07e2f622017-01-09 10:54:13 -0800442 });
443 });
444 }
Matteo Scandolod58d5042016-12-16 16:59:21 -0800445}