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