blob: 0ae1011a4c8cc959358e605e55dfc701ba5577bb [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 Scandolo5d962a32017-08-01 18:16:14 -0700114 pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss');
Scott Baker0d4c34e2018-10-05 10:45:09 -0700115 pluralize.addPluralRule(/controllerimages/i, 'controllerimageses');
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700116 pluralize.addPluralRule(/servicedependency/i, 'servicedependencys');
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 Chubab0a582017-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 }
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800174 this.toastr.info(`Requested removal for ${model.name} ${objName}`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800175 })
176 .catch(() => {
Max Chubab0a582017-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 Scandolo1888b2a2018-01-08 16:49:06 -0800199 col.link = item => this.stateWithParamsForJs(modelName, item.id);
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 Scandolo1888b2a2018-01-08 16:49:06 -0800209 col.link = item => this.stateWithParamsForJs(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 Scandolo8cd21b02017-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 Scandolo8cd21b02017-09-20 10:13:13 +0900221 if (parseInt(item[statusCol], 10) === 1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800222 return 'check';
223 }
Matteo Scandolo8cd21b02017-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 Scandolo8cd21b02017-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 Scandolo1888b2a2018-01-08 16:49:06 -0800260 public stateWithParamsForJs(name: string, id: number): any {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800261 const state = this.stateFromCoreModel(name);
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800262 return {name: state, params: {id: id}};
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800263 }
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 Scandolod67adee2018-03-08 16:27:05 -0800274 default: this.formatDefaultValues(f.default),
275 read_only: f.read_only
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800276 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700277
278 // NOTE populate drop-downs based on relation
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800279 if (f.relation) {
280 input.type = 'select';
281 this.populateSelectField(f, input);
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700282 }
283 // NOTE if static options are defined in modeldefs
284 // the f.options field is already populated,
285 // we just need to move it to the input
286 else if (f.options && f.options.length > 0) {
287 input.options = f.options;
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800288 }
289 return input;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800290 })
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700291 .filter(f => this.form_excluded_fields.indexOf(f.name) === -1);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800292 }
293
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800294 public modelToFormCfg(model: IXosModeldef): IXosFormCfg {
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700295
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800296 const formCfg: IXosFormCfg = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800297 formName: `${model.name}Form`,
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700298 exclude: this.form_excluded_fields,
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800299 actions: [{
300 label: 'Save',
301 class: 'success',
302 icon: 'ok',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800303 cb: null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800304 }],
305 inputs: this.modelFieldToInputCfg(model.fields)
306 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800307
308 formCfg.actions[0].cb = (item, form: angular.IFormController) => {
Matteo Scandolo63498472017-09-26 17:21:41 -0700309 const d = this.$q.defer();
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800310 if (!form.$valid) {
311 formCfg.feedback = {
312 show: true,
313 message: 'Form is invalid',
314 type: 'danger',
315 closeBtn: true
316 };
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800317
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800318 return;
319 }
Matteo Scandolo6f64a742018-06-22 14:25:59 -0700320 // remove fields that are not part of the model
Matteo Scandolo620cb492017-11-27 16:56:36 -0800321 item = this.removeExtraFields(item, model);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800322
Matteo Scandolo620cb492017-11-27 16:56:36 -0800323 const itemCopy = angular.copy(item);
Max Chubab0a582017-09-06 08:32:34 -0700324 const itemName = (angular.isUndefined(itemCopy.name)) ? model.name : itemCopy.name;
325
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800326 item.$save()
327 .then((res) => {
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800328 formCfg.feedback = {
329 show: true,
Max Chubab0a582017-09-06 08:32:34 -0700330 message: `${itemName} successfully saved`,
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800331 type: 'success',
332 closeBtn: true
333 };
Max Chubab0a582017-09-06 08:32:34 -0700334 this.toastr.success(`${itemName} successfully saved`);
Matteo Scandolo63498472017-09-26 17:21:41 -0700335 d.resolve(res);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800336 })
337 .catch(err => {
Matteo Scandolo39e04152017-11-29 14:24:45 -0800338 const errorMsg = err.specific_error || err.error || 'Internal Server Error';
Matteo Scandolo42c66922017-05-01 17:24:59 -0700339 formCfg.feedback = {
340 show: true,
Matteo Scandolo39e04152017-11-29 14:24:45 -0800341 message: `Error while saving ${itemName}: ${errorMsg}.`,
Matteo Scandolo42c66922017-05-01 17:24:59 -0700342 type: 'danger',
343 closeBtn: true
344 };
Matteo Scandolo39e04152017-11-29 14:24:45 -0800345 this.toastr.error(err.specific_error || '', `Error while saving ${itemName}: ${errorMsg}`);
Matteo Scandolo63498472017-09-26 17:21:41 -0700346 d.reject(err);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800347 });
Matteo Scandolo63498472017-09-26 17:21:41 -0700348
349 return d.promise;
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800350 };
351
352 return formCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800353 }
354
Matteo Scandolo620cb492017-11-27 16:56:36 -0800355 private removeExtraFields(item: any, modelDef: IXosModeldef) {
356 _.forEach(Object.keys(item), prop => {
Matteo Scandolo6f64a742018-06-22 14:25:59 -0700357 const field = _.find(modelDef.fields, {name: prop});
358 const hasField = angular.isDefined(field);
359 if (!hasField || field.read_only) {
Matteo Scandolo620cb492017-11-27 16:56:36 -0800360 delete item[prop];
361 }
362 });
363 return item;
364 }
365
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700366 private formatDefaultValues(val: any): any {
367
368 if (angular.isString(val)) {
369 const unquoted = val.split('"').join('').toLowerCase();
370 if (unquoted === 'true') {
371 return true;
372 }
373 else if (unquoted === 'false') {
374 return false;
375 }
376 }
377
378 return val || undefined;
379 }
380
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800381 private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator {
382 // convert validators as expressed from modelDefs,
383 // to the object required by xosForm
384 return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => {
385 formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value;
386 return formValidators;
387 }, {});
388 }
389
Matteo Scandolod58d5042016-12-16 16:59:21 -0800390 private fromCamelCase(string: string): string {
391 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
392 }
393
394 private fromSnakeCase(string: string): string {
395 return string.split('_').join(' ').trim();
396 }
397
398 private fromKebabCase(string: string): string {
399 return string.split('-').join(' ').trim();
400 }
401
402 private capitalizeFirst(string: string): string {
403 return string.slice(0, 1).toUpperCase() + string.slice(1);
404 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800405
406 private populateRelated(item: any, fk: string, field: IXosModelDefsField): any {
407 // if the relation is not defined return
408 if (!fk || angular.isUndefined(fk) || fk === null) {
409 return;
410 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800411 this.XosModelStore.query(field.relation.model)
Matteo Scandolo04964232017-01-07 12:53:46 -0800412 .subscribe(res => {
413 if (angular.isDefined(res) && angular.isDefined(fk)) {
414 let ri = _.find(res, {id: fk});
415 if (angular.isDefined(ri)) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800416 if (angular.isDefined(ri.name)) {
417 item[`${field.name}-formatted`] = ri.name;
418 }
419 else if (angular.isDefined(ri.humanReadableName)) {
420 item[`${field.name}-formatted`] = ri.humanReadableName;
421 }
422 else {
423 item[`${field.name}-formatted`] = ri.id;
424 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800425 }
426 }
427 });
428 }
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800429
430 // augment a select field with related model informations
431 private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800432 this.XosModelStore.query(field.relation.model)
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800433 .subscribe(res => {
434 input.options = _.map(res, item => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800435 let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name};
436 if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) {
437 opt.label = item.id;
438 }
439 return opt;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800440 });
441 });
442 }
Matteo Scandolod58d5042016-12-16 16:59:21 -0800443}