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 | cf5a993 | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 37 | relatedStateWithParams(name: string, id: string): 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 = [ |
Matteo Scandolo | 88be90f | 2017-09-11 12:21:39 -0700 | [diff] [blame] | 43 | '$log', |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 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 | 88be90f | 2017-09-11 12:21:39 -0700 | [diff] [blame] | 68 | private $log: ng.ILogService, |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 69 | private $state: ng.ui.IStateService, |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 70 | private toastr: ng.toastr.IToastrService, |
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'); |
Matteo Scandolo | d487853 | 2017-03-20 17:39:55 -0700 | [diff] [blame] | 79 | pluralize.addPluralRule(/servicedependency/i, 'servicedependencys'); |
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 | { |
Matteo Scandolo | 2c61b88 | 2017-08-07 13:11:47 -0700 | [diff] [blame] | 117 | label: 'details', |
| 118 | icon: 'search', |
| 119 | cb: (item) => { |
| 120 | this.$state.go(this.$state.current.name, {id: item.id}); |
| 121 | } |
| 122 | }, |
| 123 | { |
Matteo Scandolo | cb466ed | 2017-01-04 17:16:24 -0800 | [diff] [blame] | 124 | label: 'delete', |
| 125 | icon: 'remove', |
| 126 | color: 'red', |
| 127 | cb: (item) => { |
| 128 | let obj = angular.copy(item); |
| 129 | |
| 130 | item.$delete() |
| 131 | .then((res) => { |
| 132 | if (res.status === 404) { |
| 133 | // TODO understand why it does not go directly in catch |
| 134 | throw new Error(); |
| 135 | } |
| 136 | this.toastr.info(`${model.name} ${obj.name} succesfully deleted`); |
| 137 | }) |
| 138 | .catch(() => { |
| 139 | this.toastr.error(`Error while deleting ${obj.name}`); |
| 140 | }); |
| 141 | } |
| 142 | } |
| 143 | ] |
| 144 | }; |
| 145 | return cfg; |
| 146 | } |
| 147 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 148 | public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] { |
| 149 | const fields: IXosModelDefsField[] = model.fields; |
| 150 | const modelName: string = model.name; |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 151 | const columns = _.map(fields, (f) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 152 | if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | const col: IXosTableColumn = { |
| 156 | label: this.toLabel(f.name), |
| 157 | prop: f.name |
| 158 | }; |
| 159 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 160 | if (f.name === 'id' || f.name === 'name') { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 161 | col.link = item => this.stateWithParams(modelName, item); |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 164 | // if the field identify a relation, create a link |
| 165 | if (f.relation && f.relation.type === 'many_to_one') { |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 166 | col.type = 'custom'; |
| 167 | col.formatter = item => { |
| 168 | this.populateRelated(item, item[f.name], f); |
| 169 | return item[f.name]; |
| 170 | }; |
Matteo Scandolo | cf5a993 | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 171 | col.link = item => this.relatedStateWithParams(f.relation.model, item[col.prop]); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 174 | if (f.name === 'backend_status') { |
| 175 | col.type = 'icon'; |
Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 176 | col.hover = (item) => { |
| 177 | return item[f.name]; |
| 178 | }; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 179 | col.formatter = (item) => { |
| 180 | if (item.backend_status.indexOf('1') > -1) { |
| 181 | return 'check'; |
| 182 | } |
| 183 | if (item.backend_status.indexOf('2') > -1) { |
| 184 | return 'exclamation-circle'; |
| 185 | } |
| 186 | if (item.backend_status.indexOf('0') > -1) { |
| 187 | return 'clock-o'; |
| 188 | } |
| 189 | }; |
| 190 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 191 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 192 | return col; |
| 193 | }) |
| 194 | .filter(v => angular.isDefined(v)); |
| 195 | |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 196 | return columns; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 199 | public stateFromCoreModel(name: string): string { |
| 200 | const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => { |
| 201 | if (s.data) { |
| 202 | return s.data.model === name; |
| 203 | } |
| 204 | return false; |
| 205 | }); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 206 | return state ? state.name : null; |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | public stateWithParams(name: string, model: any): string { |
| 210 | const state = this.stateFromCoreModel(name); |
| 211 | return `${state}({id: ${model['id']}})`; |
| 212 | } |
| 213 | |
Matteo Scandolo | cf5a993 | 2017-08-09 13:46:04 -0700 | [diff] [blame] | 214 | public relatedStateWithParams(name: string, id: string): string { |
| 215 | const state = this.stateFromCoreModel(name); |
| 216 | return `${state}({id: ${id}})`; |
| 217 | } |
| 218 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 219 | public stateWithParamsForJs(name: string, model: any): any { |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 220 | const state = this.stateFromCoreModel(name); |
| 221 | return {name: state, params: {id: model.id}}; |
| 222 | } |
| 223 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 224 | public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] { |
| 225 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 226 | return _.map(fields, (f: IXosModelDefsField) => { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 227 | const input: IXosFormInput = { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 228 | name: f.name, |
| 229 | label: this.toLabel(f.name), |
| 230 | type: f.type, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 231 | validators: this.formatValidators(f.validators), |
| 232 | hint: f.hint |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 233 | }; |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 234 | if (f.relation) { |
| 235 | input.type = 'select'; |
| 236 | this.populateSelectField(f, input); |
| 237 | return input; |
| 238 | } |
| 239 | return input; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 240 | }) |
| 241 | .filter(f => this.excluded_fields.indexOf(f.name) === -1); |
| 242 | } |
| 243 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 244 | public modelToFormCfg(model: IXosModeldef): IXosFormCfg { |
| 245 | const formCfg: IXosFormCfg = { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 246 | formName: `${model.name}Form`, |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 247 | exclude: ['backend_status', 'creator', 'id'], |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 248 | actions: [{ |
| 249 | label: 'Save', |
| 250 | class: 'success', |
| 251 | icon: 'ok', |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 252 | cb: null |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 253 | }], |
| 254 | inputs: this.modelFieldToInputCfg(model.fields) |
| 255 | }; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 256 | |
| 257 | formCfg.actions[0].cb = (item, form: angular.IFormController) => { |
| 258 | |
| 259 | if (!form.$valid) { |
| 260 | formCfg.feedback = { |
| 261 | show: true, |
| 262 | message: 'Form is invalid', |
| 263 | type: 'danger', |
| 264 | closeBtn: true |
| 265 | }; |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 266 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | |
| 270 | const model = angular.copy(item); |
| 271 | |
| 272 | // TODO remove ManyToMany relations and save them separately (how??) |
| 273 | delete item.networks; |
| 274 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 275 | // remove field added by xosTable |
| 276 | _.forEach(Object.keys(item), prop => { |
| 277 | if (prop.indexOf('-formatted') > -1) { |
| 278 | delete item[prop]; |
| 279 | } |
| 280 | }); |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 281 | |
| 282 | item.$save() |
| 283 | .then((res) => { |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 284 | formCfg.feedback = { |
| 285 | show: true, |
| 286 | message: `${model.name} succesfully saved`, |
| 287 | type: 'success', |
| 288 | closeBtn: true |
| 289 | }; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 290 | this.toastr.success(`${model.name} succesfully saved`); |
| 291 | }) |
| 292 | .catch(err => { |
Matteo Scandolo | 7d5af1c | 2017-05-01 17:24:59 -0700 | [diff] [blame] | 293 | formCfg.feedback = { |
| 294 | show: true, |
| 295 | message: `Error while saving ${model.name}: ${err.error}. ${err.specific_error || ''}`, |
| 296 | type: 'danger', |
| 297 | closeBtn: true |
| 298 | }; |
| 299 | 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] | 300 | }); |
| 301 | }; |
| 302 | |
| 303 | return formCfg; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 306 | private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator { |
| 307 | // convert validators as expressed from modelDefs, |
| 308 | // to the object required by xosForm |
| 309 | return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => { |
| 310 | formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value; |
| 311 | return formValidators; |
| 312 | }, {}); |
| 313 | } |
| 314 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 315 | private fromCamelCase(string: string): string { |
| 316 | return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' '); |
| 317 | } |
| 318 | |
| 319 | private fromSnakeCase(string: string): string { |
| 320 | return string.split('_').join(' ').trim(); |
| 321 | } |
| 322 | |
| 323 | private fromKebabCase(string: string): string { |
| 324 | return string.split('-').join(' ').trim(); |
| 325 | } |
| 326 | |
| 327 | private capitalizeFirst(string: string): string { |
| 328 | return string.slice(0, 1).toUpperCase() + string.slice(1); |
| 329 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 330 | |
| 331 | private populateRelated(item: any, fk: string, field: IXosModelDefsField): any { |
| 332 | // if the relation is not defined return |
| 333 | if (!fk || angular.isUndefined(fk) || fk === null) { |
| 334 | return; |
| 335 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 336 | this.XosModelStore.query(field.relation.model) |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 337 | .subscribe(res => { |
| 338 | if (angular.isDefined(res) && angular.isDefined(fk)) { |
| 339 | let ri = _.find(res, {id: fk}); |
| 340 | if (angular.isDefined(ri)) { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 341 | if (angular.isDefined(ri.name)) { |
| 342 | item[`${field.name}-formatted`] = ri.name; |
| 343 | } |
| 344 | else if (angular.isDefined(ri.humanReadableName)) { |
| 345 | item[`${field.name}-formatted`] = ri.humanReadableName; |
| 346 | } |
| 347 | else { |
| 348 | item[`${field.name}-formatted`] = ri.id; |
| 349 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | }); |
| 353 | } |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 354 | |
| 355 | // augment a select field with related model informations |
| 356 | private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void { |
Matteo Scandolo | 88be90f | 2017-09-11 12:21:39 -0700 | [diff] [blame] | 357 | |
| 358 | if (field.relation.model.indexOf('_decl') > -1) { |
| 359 | field.relation.model = field.relation.model.replace('_decl', ''); |
| 360 | } |
| 361 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 362 | this.XosModelStore.query(field.relation.model) |
Matteo Scandolo | 88be90f | 2017-09-11 12:21:39 -0700 | [diff] [blame] | 363 | .subscribe( |
| 364 | res => { |
| 365 | input.options = _.map(res, item => { |
| 366 | let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name}; |
| 367 | if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) { |
| 368 | opt.label = item.id; |
| 369 | } |
| 370 | return opt; |
| 371 | }); |
| 372 | }, |
| 373 | err => { |
| 374 | this.$log.error(`[XOSConfigHelpers] Failed to build relations for ${field.relation.model}`); |
| 375 | } |
| 376 | ); |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 377 | } |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 378 | } |