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