blob: f60b9b6600a7b3bdb00fc0655e3a9bddb1ecbcc4 [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 Scandolo63498472017-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 Scandolod67adee2018-03-08 16:27:05 -080037 read_only: boolean;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080038 validators?: IXosModelDefsFieldValidators[];
Matteo Scandolo04964232017-01-07 12:53:46 -080039 hint?: string;
40 relation?: {
41 model: string;
42 type: string;
43 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070044 options?: IXosFormInputOptions[];
45 default?: any | null;
Matteo Scandolod58d5042016-12-16 16:59:21 -080046}
47
48export interface IXosConfigHelpersService {
Matteo Scandoloee655a12016-12-19 15:38:43 -080049 excluded_fields: string[];
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070050 form_excluded_fields: string[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080051 modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[];
52 modelToTableCfg(model: IXosModeldef, modelName: string): IXosTableCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080053 modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080054 modelToFormCfg(model: IXosModeldef): IXosFormCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080055 pluralize(string: string, quantity?: number, count?: boolean): string;
56 toLabel(string: string, pluralize?: boolean): string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080057 toLabels(string: string[], pluralize?: boolean): string[];
Matteo Scandoloa242c872017-01-12 15:13:00 -080058 stateFromCoreModel(name: string): string;
59 stateWithParams(name: string, model: any): string;
Matteo Scandolo8248bca2017-08-09 13:46:04 -070060 relatedStateWithParams(name: string, id: string): string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080061 stateWithParamsForJs(name: string, model: any): any;
Matteo Scandolod58d5042016-12-16 16:59:21 -080062}
63
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080064export class ConfigHelpers implements IXosConfigHelpersService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080065 static $inject = [
Matteo Scandolo63498472017-09-26 17:21:41 -070066 '$q',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080067 '$state',
68 'toastr',
Matteo Scandolo63498472017-09-26 17:21:41 -070069 'XosModelStore',
70 'XosFormHelpers'
71 ];
Matteo Scandolod58d5042016-12-16 16:59:21 -080072
Matteo Scandolo1aee1982017-02-17 08:33:23 -080073 public excluded_fields = [
Matteo Scandoloee655a12016-12-19 15:38:43 -080074 'created',
75 'updated',
76 'enacted',
77 'policed',
78 'backend_register',
79 'deleted',
80 'write_protect',
81 'lazy_blocked',
82 'no_sync',
83 'no_policy',
84 'omf_friendly',
85 'enabled',
Matteo Scandolod62ea792016-12-22 14:02:28 -080086 'validators',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080087 'password',
88 'backend_need_delete',
Scott Bakere5dcb9a2017-09-07 10:18:06 -070089 'backend_need_delete_policy',
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070090 'backend_need_reap',
Scott Bakere5dcb9a2017-09-07 10:18:06 -070091 'leaf_model_name',
92 'link_deleted_count',
Matteo Scandolo8cd21b02017-09-20 10:13:13 +090093 'policy_code',
94 'backend_code',
Matteo Scandoloee655a12016-12-19 15:38:43 -080095 ];
96
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070097 public form_excluded_fields = this.excluded_fields.concat([
98 'id',
99 'policy_status',
Matteo Scandoloa9fe46b2018-07-16 14:58:19 -0400100 'backend_status'
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700101 ]);
102
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800103 constructor(
Matteo Scandolo63498472017-09-26 17:21:41 -0700104 private $q: ng.IQService,
Matteo Scandoloa242c872017-01-12 15:13:00 -0800105 private $state: ng.ui.IStateService,
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -0800106 private toastr: ng.toastr.IToastrService,
Matteo Scandolo63498472017-09-26 17:21:41 -0700107 private XosModelStore: IXosModelStoreService,
108 private XosFormHelpers: IXosFormHelpersService
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800109 ) {
Matteo Scandolo08464e52017-01-17 13:35:27 -0800110 pluralize.addIrregularRule('xos', 'xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800111 pluralize.addPluralRule(/slice$/i, 'slices');
Matteo Scandolo8bd01ad2018-05-14 13:26:35 -0700112 pluralize.addPluralRule(/data/i, 'datas');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800113 pluralize.addSingularRule(/slice$/i, 'slice');
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800114 pluralize.addPluralRule(/library$/i, 'librarys');
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700115 pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss');
116 pluralize.addPluralRule(/controllerimages/i, 'controllerimagess');
117 pluralize.addPluralRule(/servicedependency/i, 'servicedependencys');
118 pluralize.addPluralRule(/servicemonitoringagentinfo/i, 'servicemonitoringagentinfoes');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800119 }
120
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800121 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800122 return pluralize(string, quantity, count);
123 }
124
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800125 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800126 if (angular.isArray(strings)) {
127 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800128 return this.toLabel(s, pluralize);
129 });
130 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800131 }
132
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800133 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800134
135 if (pluralize) {
136 string = this.pluralize(string);
137 }
138
139 string = this.fromCamelCase(string);
140 string = this.fromSnakeCase(string);
141 string = this.fromKebabCase(string);
142
143 return this.capitalizeFirst(string);
144 }
145
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800146 public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800147 const cfg = {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800148 columns: this.modelFieldsToColumnsCfg(model),
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800149 filter: 'fulltext',
150 order: {field: 'id', reverse: false},
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800151 pagination: {
152 pageSize: 10
153 },
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800154 actions: [
155 {
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700156 label: 'details',
157 icon: 'search',
158 cb: (item) => {
159 this.$state.go(this.$state.current.name, {id: item.id});
160 }
161 },
162 {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800163 label: 'delete',
164 icon: 'remove',
165 color: 'red',
166 cb: (item) => {
167 let obj = angular.copy(item);
Max Chubab0a582017-09-06 08:32:34 -0700168 const objName = (angular.isUndefined(obj.name)) ? 'instance' : obj.name;
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800169
170 item.$delete()
171 .then((res) => {
172 if (res.status === 404) {
173 // TODO understand why it does not go directly in catch
174 throw new Error();
175 }
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800176 this.toastr.info(`Requested removal for ${model.name} ${objName}`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800177 })
178 .catch(() => {
Max Chubab0a582017-09-06 08:32:34 -0700179 this.toastr.error(`Error while deleting ${objName}`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800180 });
181 }
182 }
183 ]
184 };
185 return cfg;
186 }
187
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800188 public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] {
189 const fields: IXosModelDefsField[] = model.fields;
190 const modelName: string = model.name;
Matteo Scandolo231de262017-01-04 16:33:14 -0800191 const columns = _.map(fields, (f) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800192 if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800193 return;
194 }
195 const col: IXosTableColumn = {
196 label: this.toLabel(f.name),
197 prop: f.name
198 };
199
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800200 if (f.name === 'id' || f.name === 'name') {
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800201 col.link = item => this.stateWithParamsForJs(modelName, item.id);
Matteo Scandoloee655a12016-12-19 15:38:43 -0800202 }
203
Matteo Scandolo04964232017-01-07 12:53:46 -0800204 // if the field identify a relation, create a link
Matteo Scandolo18975142017-08-01 14:48:04 -0700205 if (f.relation && f.relation.type === 'manytoone') {
Matteo Scandolo04964232017-01-07 12:53:46 -0800206 col.type = 'custom';
207 col.formatter = item => {
208 this.populateRelated(item, item[f.name], f);
209 return item[f.name];
210 };
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800211 col.link = item => this.stateWithParamsForJs(f.relation.model, item[col.prop]);
Matteo Scandolo04964232017-01-07 12:53:46 -0800212 }
213
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700214 if (f.name === 'backend_status' || f.name === 'policy_status') {
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900215
216 const statusCol = f.name === 'backend_status' ? 'backend_code' : 'policy_code';
217
Matteo Scandolod58d5042016-12-16 16:59:21 -0800218 col.type = 'icon';
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800219 col.hover = (item) => {
220 return item[f.name];
221 };
Matteo Scandolod58d5042016-12-16 16:59:21 -0800222 col.formatter = (item) => {
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900223 if (parseInt(item[statusCol], 10) === 1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800224 return 'check';
225 }
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900226 if (parseInt(item[statusCol], 10) === 2) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800227 return 'exclamation-circle';
228 }
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900229 if (parseInt(item[statusCol], 10) === 0) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800230 return 'clock-o';
231 }
232 };
233 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800234
Matteo Scandolod58d5042016-12-16 16:59:21 -0800235 return col;
236 })
237 .filter(v => angular.isDefined(v));
238
Matteo Scandolo231de262017-01-04 16:33:14 -0800239 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800240 };
241
Matteo Scandoloa242c872017-01-12 15:13:00 -0800242 public stateFromCoreModel(name: string): string {
243 const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => {
244 if (s.data) {
245 return s.data.model === name;
246 }
247 return false;
248 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800249 return state ? state.name : null;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800250 }
251
252 public stateWithParams(name: string, model: any): string {
253 const state = this.stateFromCoreModel(name);
254 return `${state}({id: ${model['id']}})`;
255 }
256
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700257 public relatedStateWithParams(name: string, id: string): string {
258 const state = this.stateFromCoreModel(name);
259 return `${state}({id: ${id}})`;
260 }
261
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800262 public stateWithParamsForJs(name: string, id: number): any {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800263 const state = this.stateFromCoreModel(name);
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800264 return {name: state, params: {id: id}};
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800265 }
266
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800267 public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] {
268
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800269 return _.map(fields, (f: IXosModelDefsField) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800270 const input: IXosFormInput = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800271 name: f.name,
272 label: this.toLabel(f.name),
273 type: f.type,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800274 validators: this.formatValidators(f.validators),
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700275 hint: f.hint,
Matteo Scandolod67adee2018-03-08 16:27:05 -0800276 default: this.formatDefaultValues(f.default),
277 read_only: f.read_only
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800278 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700279
280 // NOTE populate drop-downs based on relation
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800281 if (f.relation) {
282 input.type = 'select';
283 this.populateSelectField(f, input);
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700284 }
285 // NOTE if static options are defined in modeldefs
286 // the f.options field is already populated,
287 // we just need to move it to the input
288 else if (f.options && f.options.length > 0) {
289 input.options = f.options;
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800290 }
291 return input;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800292 })
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700293 .filter(f => this.form_excluded_fields.indexOf(f.name) === -1);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800294 }
295
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800296 public modelToFormCfg(model: IXosModeldef): IXosFormCfg {
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700297
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800298 const formCfg: IXosFormCfg = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800299 formName: `${model.name}Form`,
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700300 exclude: this.form_excluded_fields,
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800301 actions: [{
302 label: 'Save',
303 class: 'success',
304 icon: 'ok',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800305 cb: null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800306 }],
307 inputs: this.modelFieldToInputCfg(model.fields)
308 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800309
310 formCfg.actions[0].cb = (item, form: angular.IFormController) => {
Matteo Scandolo63498472017-09-26 17:21:41 -0700311 const d = this.$q.defer();
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800312 if (!form.$valid) {
313 formCfg.feedback = {
314 show: true,
315 message: 'Form is invalid',
316 type: 'danger',
317 closeBtn: true
318 };
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800319
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800320 return;
321 }
Matteo Scandolo6f64a742018-06-22 14:25:59 -0700322 // remove fields that are not part of the model
Matteo Scandolo620cb492017-11-27 16:56:36 -0800323 item = this.removeExtraFields(item, model);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800324
Matteo Scandolo620cb492017-11-27 16:56:36 -0800325 const itemCopy = angular.copy(item);
Max Chubab0a582017-09-06 08:32:34 -0700326 const itemName = (angular.isUndefined(itemCopy.name)) ? model.name : itemCopy.name;
327
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800328 item.$save()
329 .then((res) => {
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800330 formCfg.feedback = {
331 show: true,
Max Chubab0a582017-09-06 08:32:34 -0700332 message: `${itemName} successfully saved`,
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800333 type: 'success',
334 closeBtn: true
335 };
Max Chubab0a582017-09-06 08:32:34 -0700336 this.toastr.success(`${itemName} successfully saved`);
Matteo Scandolo63498472017-09-26 17:21:41 -0700337 d.resolve(res);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800338 })
339 .catch(err => {
Matteo Scandolo39e04152017-11-29 14:24:45 -0800340 const errorMsg = err.specific_error || err.error || 'Internal Server Error';
Matteo Scandolo42c66922017-05-01 17:24:59 -0700341 formCfg.feedback = {
342 show: true,
Matteo Scandolo39e04152017-11-29 14:24:45 -0800343 message: `Error while saving ${itemName}: ${errorMsg}.`,
Matteo Scandolo42c66922017-05-01 17:24:59 -0700344 type: 'danger',
345 closeBtn: true
346 };
Matteo Scandolo39e04152017-11-29 14:24:45 -0800347 this.toastr.error(err.specific_error || '', `Error while saving ${itemName}: ${errorMsg}`);
Matteo Scandolo63498472017-09-26 17:21:41 -0700348 d.reject(err);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800349 });
Matteo Scandolo63498472017-09-26 17:21:41 -0700350
351 return d.promise;
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800352 };
353
354 return formCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800355 }
356
Matteo Scandolo620cb492017-11-27 16:56:36 -0800357 private removeExtraFields(item: any, modelDef: IXosModeldef) {
358 _.forEach(Object.keys(item), prop => {
Matteo Scandolo6f64a742018-06-22 14:25:59 -0700359 const field = _.find(modelDef.fields, {name: prop});
360 const hasField = angular.isDefined(field);
361 if (!hasField || field.read_only) {
Matteo Scandolo620cb492017-11-27 16:56:36 -0800362 delete item[prop];
363 }
364 });
365 return item;
366 }
367
Matteo Scandolof1e68cd2017-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}