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