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