Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 1 | import * as _ from 'lodash'; |
| 2 | import * as pluralize from 'pluralize'; |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 3 | import {IXosTableColumn, IXosTableCfg} from '../../table/table'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 4 | import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest'; |
| 5 | import {IXosFormCfg, IXosFormInput, IXosFormInputValidator} from '../../form/form'; |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 6 | import {IXosAuthService} from '../../../datasources/rest/auth.rest'; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 7 | import {IXosModelStoreService} from '../../../datasources/stores/model.store'; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 8 | import {IXosState} from '../runtime-states'; |
| 9 | |
| 10 | export interface IXosModelDefsFieldValidators { |
| 11 | name: string; |
| 12 | bool_value?: boolean; |
| 13 | int_value?: number; |
| 14 | } |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 15 | |
| 16 | export interface IXosModelDefsField { |
| 17 | name: string; |
| 18 | type: string; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 19 | validators?: IXosModelDefsFieldValidators[]; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 20 | hint?: string; |
| 21 | relation?: { |
| 22 | model: string; |
| 23 | type: string; |
| 24 | }; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | export interface IXosConfigHelpersService { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 28 | excluded_fields: string[]; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 29 | modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[]; |
| 30 | modelToTableCfg(model: IXosModeldef, modelName: string): IXosTableCfg; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 31 | modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[]; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 32 | modelToFormCfg(model: IXosModeldef): IXosFormCfg; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 33 | pluralize(string: string, quantity?: number, count?: boolean): string; |
| 34 | toLabel(string: string, pluralize?: boolean): string; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 35 | toLabels(string: string[], pluralize?: boolean): string[]; |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 36 | stateFromCoreModel(name: string): string; |
| 37 | stateWithParams(name: string, model: any): string; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 38 | stateWithParamsForJs(name: string, model: any): any; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 41 | export class ConfigHelpers implements IXosConfigHelpersService { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 42 | static $inject = [ |
| 43 | '$state', |
| 44 | 'toastr', |
| 45 | 'AuthService', |
| 46 | 'XosModelStore']; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 47 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 48 | public excluded_fields = [ |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 49 | '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 Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 61 | 'validators', |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 62 | 'password', |
| 63 | 'backend_need_delete', |
| 64 | 'backend_need_reap' |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 65 | ]; |
| 66 | |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 67 | constructor( |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 68 | private $state: ng.ui.IStateService, |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 69 | private toastr: ng.toastr.IToastrService, |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 70 | private AuthService: IXosAuthService, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 71 | private XosModelStore: IXosModelStoreService |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 72 | ) { |
Matteo Scandolo | 08464e5 | 2017-01-17 13:35:27 -0800 | [diff] [blame] | 73 | pluralize.addIrregularRule('xos', 'xoses'); |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 74 | pluralize.addPluralRule(/slice$/i, 'slices'); |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 75 | pluralize.addSingularRule(/slice$/i, 'slice'); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 76 | pluralize.addPluralRule(/library$/i, 'librarys'); |
| 77 | pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentses'); |
| 78 | pluralize.addPluralRule(/controllerimages/i, 'controllerimageses'); |
| 79 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 82 | public pluralize(string: string, quantity?: number, count?: boolean): string { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 83 | return pluralize(string, quantity, count); |
| 84 | } |
| 85 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 86 | public toLabels(strings: string[], pluralize?: boolean): string[] { |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 87 | if (angular.isArray(strings)) { |
| 88 | return _.map(strings, s => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 89 | return this.toLabel(s, pluralize); |
| 90 | }); |
| 91 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 94 | public toLabel(string: string, pluralize?: boolean): string { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 95 | |
| 96 | if (pluralize) { |
| 97 | string = this.pluralize(string); |
| 98 | } |
| 99 | |
| 100 | string = this.fromCamelCase(string); |
| 101 | string = this.fromSnakeCase(string); |
| 102 | string = this.fromKebabCase(string); |
| 103 | |
| 104 | return this.capitalizeFirst(string); |
| 105 | } |
| 106 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 107 | public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg { |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 108 | const cfg = { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 109 | columns: this.modelFieldsToColumnsCfg(model), |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 110 | filter: 'fulltext', |
| 111 | order: {field: 'id', reverse: false}, |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 112 | pagination: { |
| 113 | pageSize: 10 |
| 114 | }, |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 115 | actions: [ |
| 116 | { |
| 117 | label: 'delete', |
| 118 | icon: 'remove', |
| 119 | color: 'red', |
| 120 | cb: (item) => { |
| 121 | let obj = angular.copy(item); |
| 122 | |
| 123 | item.$delete() |
| 124 | .then((res) => { |
| 125 | if (res.status === 404) { |
| 126 | // TODO understand why it does not go directly in catch |
| 127 | throw new Error(); |
| 128 | } |
| 129 | this.toastr.info(`${model.name} ${obj.name} succesfully deleted`); |
| 130 | }) |
| 131 | .catch(() => { |
| 132 | this.toastr.error(`Error while deleting ${obj.name}`); |
| 133 | }); |
| 134 | } |
| 135 | } |
| 136 | ] |
| 137 | }; |
| 138 | return cfg; |
| 139 | } |
| 140 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 141 | public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] { |
| 142 | const fields: IXosModelDefsField[] = model.fields; |
| 143 | const modelName: string = model.name; |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 144 | const columns = _.map(fields, (f) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 145 | if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | const col: IXosTableColumn = { |
| 149 | label: this.toLabel(f.name), |
| 150 | prop: f.name |
| 151 | }; |
| 152 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 153 | if (f.name === 'id' || f.name === 'name') { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 154 | col.link = item => this.stateWithParams(modelName, item); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 157 | // if the field identify a relation, create a link |
| 158 | if (f.relation && f.relation.type === 'many_to_one') { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 159 | col.type = 'custom'; |
| 160 | col.formatter = item => { |
| 161 | this.populateRelated(item, item[f.name], f); |
| 162 | return item[f.name]; |
| 163 | }; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 164 | col.link = item => { |
| 165 | return this.stateWithParams(f.relation.model, item); |
| 166 | }; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 169 | if (f.name === 'backend_status') { |
| 170 | col.type = 'icon'; |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 171 | col.hover = (item) => { |
| 172 | return item[f.name]; |
| 173 | }; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 174 | col.formatter = (item) => { |
| 175 | if (item.backend_status.indexOf('1') > -1) { |
| 176 | return 'check'; |
| 177 | } |
| 178 | if (item.backend_status.indexOf('2') > -1) { |
| 179 | return 'exclamation-circle'; |
| 180 | } |
| 181 | if (item.backend_status.indexOf('0') > -1) { |
| 182 | return 'clock-o'; |
| 183 | } |
| 184 | }; |
| 185 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 186 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 187 | return col; |
| 188 | }) |
| 189 | .filter(v => angular.isDefined(v)); |
| 190 | |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 191 | return columns; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 194 | public stateFromCoreModel(name: string): string { |
| 195 | const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => { |
| 196 | if (s.data) { |
| 197 | return s.data.model === name; |
| 198 | } |
| 199 | return false; |
| 200 | }); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 201 | return state ? state.name : null; |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | public stateWithParams(name: string, model: any): string { |
| 205 | const state = this.stateFromCoreModel(name); |
| 206 | return `${state}({id: ${model['id']}})`; |
| 207 | } |
| 208 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 209 | public stateWithParamsForJs(name: string, model: any): any { |
| 210 | // TODO test and interface |
| 211 | const state = this.stateFromCoreModel(name); |
| 212 | return {name: state, params: {id: model.id}}; |
| 213 | } |
| 214 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 215 | public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] { |
| 216 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 217 | return _.map(fields, (f: IXosModelDefsField) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 218 | const input: IXosFormInput = { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 219 | name: f.name, |
| 220 | label: this.toLabel(f.name), |
| 221 | type: f.type, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 222 | validators: this.formatValidators(f.validators), |
| 223 | hint: f.hint |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 224 | }; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 225 | if (f.relation) { |
| 226 | input.type = 'select'; |
| 227 | this.populateSelectField(f, input); |
| 228 | return input; |
| 229 | } |
| 230 | return input; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 231 | }) |
| 232 | .filter(f => this.excluded_fields.indexOf(f.name) === -1); |
| 233 | } |
| 234 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 235 | public modelToFormCfg(model: IXosModeldef): IXosFormCfg { |
| 236 | const formCfg: IXosFormCfg = { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 237 | formName: `${model.name}Form`, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 238 | exclude: ['backend_status', 'creator', 'id'], |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 239 | actions: [{ |
| 240 | label: 'Save', |
| 241 | class: 'success', |
| 242 | icon: 'ok', |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 243 | cb: null |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 244 | }], |
| 245 | inputs: this.modelFieldToInputCfg(model.fields) |
| 246 | }; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 247 | |
| 248 | formCfg.actions[0].cb = (item, form: angular.IFormController) => { |
| 249 | |
| 250 | if (!form.$valid) { |
| 251 | formCfg.feedback = { |
| 252 | show: true, |
| 253 | message: 'Form is invalid', |
| 254 | type: 'danger', |
| 255 | closeBtn: true |
| 256 | }; |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 257 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | |
| 261 | const model = angular.copy(item); |
| 262 | |
| 263 | // TODO remove ManyToMany relations and save them separately (how??) |
| 264 | delete item.networks; |
| 265 | |
| 266 | // adding userId as creator |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 267 | // item.creator = this.AuthService.getUser().id; |
| 268 | |
| 269 | // remove field added by xosTable |
| 270 | _.forEach(Object.keys(item), prop => { |
| 271 | if (prop.indexOf('-formatted') > -1) { |
| 272 | delete item[prop]; |
| 273 | } |
| 274 | }); |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 275 | |
| 276 | item.$save() |
| 277 | .then((res) => { |
| 278 | if (res.status === 403 || res.status === 405 || res.status === 500) { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 279 | // TODO understand why 405 does not go directly in catch (it may be related to ng-rest-gw) |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 280 | throw new Error(); |
| 281 | } |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 282 | formCfg.feedback = { |
| 283 | show: true, |
| 284 | message: `${model.name} succesfully saved`, |
| 285 | type: 'success', |
| 286 | closeBtn: true |
| 287 | }; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 288 | this.toastr.success(`${model.name} succesfully saved`); |
| 289 | }) |
| 290 | .catch(err => { |
| 291 | // TODO keep the edited model |
| 292 | this.toastr.error(`Error while saving ${model.name}`); |
| 293 | }); |
| 294 | }; |
| 295 | |
| 296 | return formCfg; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 299 | private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator { |
| 300 | // convert validators as expressed from modelDefs, |
| 301 | // to the object required by xosForm |
| 302 | return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => { |
| 303 | formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value; |
| 304 | return formValidators; |
| 305 | }, {}); |
| 306 | } |
| 307 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 308 | private fromCamelCase(string: string): string { |
| 309 | return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' '); |
| 310 | } |
| 311 | |
| 312 | private fromSnakeCase(string: string): string { |
| 313 | return string.split('_').join(' ').trim(); |
| 314 | } |
| 315 | |
| 316 | private fromKebabCase(string: string): string { |
| 317 | return string.split('-').join(' ').trim(); |
| 318 | } |
| 319 | |
| 320 | private capitalizeFirst(string: string): string { |
| 321 | return string.slice(0, 1).toUpperCase() + string.slice(1); |
| 322 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 323 | |
| 324 | private populateRelated(item: any, fk: string, field: IXosModelDefsField): any { |
| 325 | // if the relation is not defined return |
| 326 | if (!fk || angular.isUndefined(fk) || fk === null) { |
| 327 | return; |
| 328 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 329 | this.XosModelStore.query(field.relation.model) |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 330 | .subscribe(res => { |
| 331 | if (angular.isDefined(res) && angular.isDefined(fk)) { |
| 332 | let ri = _.find(res, {id: fk}); |
| 333 | if (angular.isDefined(ri)) { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 334 | if (angular.isDefined(ri.name)) { |
| 335 | item[`${field.name}-formatted`] = ri.name; |
| 336 | } |
| 337 | else if (angular.isDefined(ri.humanReadableName)) { |
| 338 | item[`${field.name}-formatted`] = ri.humanReadableName; |
| 339 | } |
| 340 | else { |
| 341 | item[`${field.name}-formatted`] = ri.id; |
| 342 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | }); |
| 346 | } |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 347 | |
| 348 | // augment a select field with related model informations |
| 349 | private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 350 | this.XosModelStore.query(field.relation.model) |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 351 | .subscribe(res => { |
| 352 | input.options = _.map(res, item => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 353 | let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name}; |
| 354 | if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) { |
| 355 | opt.label = item.id; |
| 356 | } |
| 357 | return opt; |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 358 | }); |
| 359 | }); |
| 360 | } |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 361 | } |