blob: 0e17d7329a802d9d59de02826af73244500a049a [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 Scandolo6f64a742018-06-22 14:25:59 -0700100 'policy_code',
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700101 'backend_status',
Matteo Scandolo6f64a742018-06-22 14:25:59 -0700102 'backend_code',
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700103 ]);
104
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800105 constructor(
Matteo Scandolo63498472017-09-26 17:21:41 -0700106 private $q: ng.IQService,
Matteo Scandoloa242c872017-01-12 15:13:00 -0800107 private $state: ng.ui.IStateService,
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -0800108 private toastr: ng.toastr.IToastrService,
Matteo Scandolo63498472017-09-26 17:21:41 -0700109 private XosModelStore: IXosModelStoreService,
110 private XosFormHelpers: IXosFormHelpersService
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800111 ) {
Matteo Scandolo08464e52017-01-17 13:35:27 -0800112 pluralize.addIrregularRule('xos', 'xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800113 pluralize.addPluralRule(/slice$/i, 'slices');
Matteo Scandolo8bd01ad2018-05-14 13:26:35 -0700114 pluralize.addPluralRule(/data/i, 'datas');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800115 pluralize.addSingularRule(/slice$/i, 'slice');
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800116 pluralize.addPluralRule(/library$/i, 'librarys');
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700117 pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss');
118 pluralize.addPluralRule(/controllerimages/i, 'controllerimagess');
119 pluralize.addPluralRule(/servicedependency/i, 'servicedependencys');
120 pluralize.addPluralRule(/servicemonitoringagentinfo/i, 'servicemonitoringagentinfoes');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800121 }
122
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800123 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800124 return pluralize(string, quantity, count);
125 }
126
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800127 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800128 if (angular.isArray(strings)) {
129 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800130 return this.toLabel(s, pluralize);
131 });
132 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800133 }
134
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800135 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800136
137 if (pluralize) {
138 string = this.pluralize(string);
139 }
140
141 string = this.fromCamelCase(string);
142 string = this.fromSnakeCase(string);
143 string = this.fromKebabCase(string);
144
145 return this.capitalizeFirst(string);
146 }
147
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800148 public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800149 const cfg = {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800150 columns: this.modelFieldsToColumnsCfg(model),
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800151 filter: 'fulltext',
152 order: {field: 'id', reverse: false},
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800153 pagination: {
154 pageSize: 10
155 },
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800156 actions: [
157 {
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700158 label: 'details',
159 icon: 'search',
160 cb: (item) => {
161 this.$state.go(this.$state.current.name, {id: item.id});
162 }
163 },
164 {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800165 label: 'delete',
166 icon: 'remove',
167 color: 'red',
168 cb: (item) => {
169 let obj = angular.copy(item);
Max Chubab0a582017-09-06 08:32:34 -0700170 const objName = (angular.isUndefined(obj.name)) ? 'instance' : obj.name;
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800171
172 item.$delete()
173 .then((res) => {
174 if (res.status === 404) {
175 // TODO understand why it does not go directly in catch
176 throw new Error();
177 }
Matteo Scandoloe9cdf9a2017-11-21 10:41:28 -0800178 this.toastr.info(`Requested removal for ${model.name} ${objName}`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800179 })
180 .catch(() => {
Max Chubab0a582017-09-06 08:32:34 -0700181 this.toastr.error(`Error while deleting ${objName}`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800182 });
183 }
184 }
185 ]
186 };
187 return cfg;
188 }
189
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800190 public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] {
191 const fields: IXosModelDefsField[] = model.fields;
192 const modelName: string = model.name;
Matteo Scandolo231de262017-01-04 16:33:14 -0800193 const columns = _.map(fields, (f) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800194 if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800195 return;
196 }
197 const col: IXosTableColumn = {
198 label: this.toLabel(f.name),
199 prop: f.name
200 };
201
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800202 if (f.name === 'id' || f.name === 'name') {
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800203 col.link = item => this.stateWithParamsForJs(modelName, item.id);
Matteo Scandoloee655a12016-12-19 15:38:43 -0800204 }
205
Matteo Scandolo04964232017-01-07 12:53:46 -0800206 // if the field identify a relation, create a link
Matteo Scandolo18975142017-08-01 14:48:04 -0700207 if (f.relation && f.relation.type === 'manytoone') {
Matteo Scandolo04964232017-01-07 12:53:46 -0800208 col.type = 'custom';
209 col.formatter = item => {
210 this.populateRelated(item, item[f.name], f);
211 return item[f.name];
212 };
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800213 col.link = item => this.stateWithParamsForJs(f.relation.model, item[col.prop]);
Matteo Scandolo04964232017-01-07 12:53:46 -0800214 }
215
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700216 if (f.name === 'backend_status' || f.name === 'policy_status') {
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900217
218 const statusCol = f.name === 'backend_status' ? 'backend_code' : 'policy_code';
219
Matteo Scandolod58d5042016-12-16 16:59:21 -0800220 col.type = 'icon';
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800221 col.hover = (item) => {
222 return item[f.name];
223 };
Matteo Scandolod58d5042016-12-16 16:59:21 -0800224 col.formatter = (item) => {
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900225 if (parseInt(item[statusCol], 10) === 1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800226 return 'check';
227 }
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900228 if (parseInt(item[statusCol], 10) === 2) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800229 return 'exclamation-circle';
230 }
Matteo Scandolo8cd21b02017-09-20 10:13:13 +0900231 if (parseInt(item[statusCol], 10) === 0) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800232 return 'clock-o';
233 }
234 };
235 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800236
Matteo Scandolod58d5042016-12-16 16:59:21 -0800237 return col;
238 })
239 .filter(v => angular.isDefined(v));
240
Matteo Scandolo231de262017-01-04 16:33:14 -0800241 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800242 };
243
Matteo Scandoloa242c872017-01-12 15:13:00 -0800244 public stateFromCoreModel(name: string): string {
245 const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => {
246 if (s.data) {
247 return s.data.model === name;
248 }
249 return false;
250 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800251 return state ? state.name : null;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800252 }
253
254 public stateWithParams(name: string, model: any): string {
255 const state = this.stateFromCoreModel(name);
256 return `${state}({id: ${model['id']}})`;
257 }
258
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700259 public relatedStateWithParams(name: string, id: string): string {
260 const state = this.stateFromCoreModel(name);
261 return `${state}({id: ${id}})`;
262 }
263
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800264 public stateWithParamsForJs(name: string, id: number): any {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800265 const state = this.stateFromCoreModel(name);
Matteo Scandolo1888b2a2018-01-08 16:49:06 -0800266 return {name: state, params: {id: id}};
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800267 }
268
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800269 public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] {
270
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800271 return _.map(fields, (f: IXosModelDefsField) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800272 const input: IXosFormInput = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800273 name: f.name,
274 label: this.toLabel(f.name),
275 type: f.type,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800276 validators: this.formatValidators(f.validators),
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700277 hint: f.hint,
Matteo Scandolod67adee2018-03-08 16:27:05 -0800278 default: this.formatDefaultValues(f.default),
279 read_only: f.read_only
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800280 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700281
282 // NOTE populate drop-downs based on relation
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800283 if (f.relation) {
284 input.type = 'select';
285 this.populateSelectField(f, input);
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700286 }
287 // NOTE if static options are defined in modeldefs
288 // the f.options field is already populated,
289 // we just need to move it to the input
290 else if (f.options && f.options.length > 0) {
291 input.options = f.options;
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800292 }
293 return input;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800294 })
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700295 .filter(f => this.form_excluded_fields.indexOf(f.name) === -1);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800296 }
297
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800298 public modelToFormCfg(model: IXosModeldef): IXosFormCfg {
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700299
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800300 const formCfg: IXosFormCfg = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800301 formName: `${model.name}Form`,
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700302 exclude: this.form_excluded_fields,
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800303 actions: [{
304 label: 'Save',
305 class: 'success',
306 icon: 'ok',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800307 cb: null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800308 }],
309 inputs: this.modelFieldToInputCfg(model.fields)
310 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800311
312 formCfg.actions[0].cb = (item, form: angular.IFormController) => {
Matteo Scandolo63498472017-09-26 17:21:41 -0700313 const d = this.$q.defer();
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800314 if (!form.$valid) {
315 formCfg.feedback = {
316 show: true,
317 message: 'Form is invalid',
318 type: 'danger',
319 closeBtn: true
320 };
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800321
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800322 return;
323 }
Matteo Scandolo6f64a742018-06-22 14:25:59 -0700324 // remove fields that are not part of the model
Matteo Scandolo620cb492017-11-27 16:56:36 -0800325 item = this.removeExtraFields(item, model);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800326
Matteo Scandolo620cb492017-11-27 16:56:36 -0800327 const itemCopy = angular.copy(item);
Max Chubab0a582017-09-06 08:32:34 -0700328 const itemName = (angular.isUndefined(itemCopy.name)) ? model.name : itemCopy.name;
329
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800330 item.$save()
331 .then((res) => {
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800332 formCfg.feedback = {
333 show: true,
Max Chubab0a582017-09-06 08:32:34 -0700334 message: `${itemName} successfully saved`,
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800335 type: 'success',
336 closeBtn: true
337 };
Max Chubab0a582017-09-06 08:32:34 -0700338 this.toastr.success(`${itemName} successfully saved`);
Matteo Scandolo63498472017-09-26 17:21:41 -0700339 d.resolve(res);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800340 })
341 .catch(err => {
Matteo Scandolo39e04152017-11-29 14:24:45 -0800342 const errorMsg = err.specific_error || err.error || 'Internal Server Error';
Matteo Scandolo42c66922017-05-01 17:24:59 -0700343 formCfg.feedback = {
344 show: true,
Matteo Scandolo39e04152017-11-29 14:24:45 -0800345 message: `Error while saving ${itemName}: ${errorMsg}.`,
Matteo Scandolo42c66922017-05-01 17:24:59 -0700346 type: 'danger',
347 closeBtn: true
348 };
Matteo Scandolo39e04152017-11-29 14:24:45 -0800349 this.toastr.error(err.specific_error || '', `Error while saving ${itemName}: ${errorMsg}`);
Matteo Scandolo63498472017-09-26 17:21:41 -0700350 d.reject(err);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800351 });
Matteo Scandolo63498472017-09-26 17:21:41 -0700352
353 return d.promise;
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800354 };
355
356 return formCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800357 }
358
Matteo Scandolo620cb492017-11-27 16:56:36 -0800359 private removeExtraFields(item: any, modelDef: IXosModeldef) {
360 _.forEach(Object.keys(item), prop => {
Matteo Scandolo6f64a742018-06-22 14:25:59 -0700361 const field = _.find(modelDef.fields, {name: prop});
362 const hasField = angular.isDefined(field);
363 if (!hasField || field.read_only) {
Matteo Scandolo620cb492017-11-27 16:56:36 -0800364 delete item[prop];
365 }
366 });
367 return item;
368 }
369
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700370 private formatDefaultValues(val: any): any {
371
372 if (angular.isString(val)) {
373 const unquoted = val.split('"').join('').toLowerCase();
374 if (unquoted === 'true') {
375 return true;
376 }
377 else if (unquoted === 'false') {
378 return false;
379 }
380 }
381
382 return val || undefined;
383 }
384
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800385 private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator {
386 // convert validators as expressed from modelDefs,
387 // to the object required by xosForm
388 return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => {
389 formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value;
390 return formValidators;
391 }, {});
392 }
393
Matteo Scandolod58d5042016-12-16 16:59:21 -0800394 private fromCamelCase(string: string): string {
395 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
396 }
397
398 private fromSnakeCase(string: string): string {
399 return string.split('_').join(' ').trim();
400 }
401
402 private fromKebabCase(string: string): string {
403 return string.split('-').join(' ').trim();
404 }
405
406 private capitalizeFirst(string: string): string {
407 return string.slice(0, 1).toUpperCase() + string.slice(1);
408 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800409
410 private populateRelated(item: any, fk: string, field: IXosModelDefsField): any {
411 // if the relation is not defined return
412 if (!fk || angular.isUndefined(fk) || fk === null) {
413 return;
414 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800415 this.XosModelStore.query(field.relation.model)
Matteo Scandolo04964232017-01-07 12:53:46 -0800416 .subscribe(res => {
417 if (angular.isDefined(res) && angular.isDefined(fk)) {
418 let ri = _.find(res, {id: fk});
419 if (angular.isDefined(ri)) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800420 if (angular.isDefined(ri.name)) {
421 item[`${field.name}-formatted`] = ri.name;
422 }
423 else if (angular.isDefined(ri.humanReadableName)) {
424 item[`${field.name}-formatted`] = ri.humanReadableName;
425 }
426 else {
427 item[`${field.name}-formatted`] = ri.id;
428 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800429 }
430 }
431 });
432 }
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800433
434 // augment a select field with related model informations
435 private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800436 this.XosModelStore.query(field.relation.model)
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800437 .subscribe(res => {
438 input.options = _.map(res, item => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800439 let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name};
440 if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) {
441 opt.label = item.id;
442 }
443 return opt;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800444 });
445 });
446 }
Matteo Scandolod58d5042016-12-16 16:59:21 -0800447}