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 | 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 | |
| 36 | export class ConfigHelpers { |
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, |
| 62 | private ModelStore: IModelStoreService |
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}, |
| 99 | actions: [ |
| 100 | { |
| 101 | label: 'delete', |
| 102 | icon: 'remove', |
| 103 | color: 'red', |
| 104 | cb: (item) => { |
| 105 | let obj = angular.copy(item); |
| 106 | |
| 107 | item.$delete() |
| 108 | .then((res) => { |
| 109 | if (res.status === 404) { |
| 110 | // TODO understand why it does not go directly in catch |
| 111 | throw new Error(); |
| 112 | } |
| 113 | this.toastr.info(`${model.name} ${obj.name} succesfully deleted`); |
| 114 | }) |
| 115 | .catch(() => { |
| 116 | this.toastr.error(`Error while deleting ${obj.name}`); |
| 117 | }); |
| 118 | } |
| 119 | } |
| 120 | ] |
| 121 | }; |
| 122 | return cfg; |
| 123 | } |
| 124 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 125 | public modelFieldsToColumnsCfg(fields: IXosModelDefsField[], modelName: string): IXosTableColumn[] { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 126 | |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 127 | const columns = _.map(fields, (f) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 128 | if (this.excluded_fields.indexOf(f.name) > -1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | const col: IXosTableColumn = { |
| 132 | label: this.toLabel(f.name), |
| 133 | prop: f.name |
| 134 | }; |
| 135 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 136 | if (f.name === 'id' || f.name === 'name') { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 137 | col.link = item => this.stateWithParams(modelName, item); |
Matteo Scandolo | 99ac9d9 | 2017-01-03 13:58:19 -0800 | [diff] [blame] | 138 | // NOTE can we find a better method to generalize the route? |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 139 | // col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 142 | // if the field identify a relation, create a link |
| 143 | if (f.relation && f.relation.type === 'many_to_one') { |
| 144 | // TODO read the related model name and replace the value, use the xosTable format method? |
| 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); |
| 151 | // col.link = item => `#${this.urlFromCoreModel(f.relation.model)}/${item[f.name]}`; |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 154 | if (f.name === 'backend_status') { |
| 155 | col.type = 'icon'; |
| 156 | col.formatter = (item) => { |
| 157 | if (item.backend_status.indexOf('1') > -1) { |
| 158 | return 'check'; |
| 159 | } |
| 160 | if (item.backend_status.indexOf('2') > -1) { |
| 161 | return 'exclamation-circle'; |
| 162 | } |
| 163 | if (item.backend_status.indexOf('0') > -1) { |
| 164 | return 'clock-o'; |
| 165 | } |
| 166 | }; |
| 167 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 168 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 169 | return col; |
| 170 | }) |
| 171 | .filter(v => angular.isDefined(v)); |
| 172 | |
Matteo Scandolo | 231de26 | 2017-01-04 16:33:14 -0800 | [diff] [blame] | 173 | return columns; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 176 | public urlFromCoreModel(name: string): string { |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 177 | |
Matteo Scandolo | 1c5905f | 2017-01-04 17:41:15 -0800 | [diff] [blame] | 178 | return `/core/${this.pluralize(name.toLowerCase())}`; |
| 179 | } |
| 180 | |
Matteo Scandolo | a242c87 | 2017-01-12 15:13:00 -0800 | [diff] [blame] | 181 | public stateFromCoreModel(name: string): string { |
| 182 | const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => { |
| 183 | if (s.data) { |
| 184 | return s.data.model === name; |
| 185 | } |
| 186 | return false; |
| 187 | }); |
| 188 | return state.name; |
| 189 | } |
| 190 | |
| 191 | public stateWithParams(name: string, model: any): string { |
| 192 | const state = this.stateFromCoreModel(name); |
| 193 | return `${state}({id: ${model['id']}})`; |
| 194 | } |
| 195 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 196 | public stateWithParamsForJs(name: string, model: any): any { |
| 197 | // TODO test and interface |
| 198 | const state = this.stateFromCoreModel(name); |
| 199 | return {name: state, params: {id: model.id}}; |
| 200 | } |
| 201 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 202 | public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] { |
| 203 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 204 | return _.map(fields, (f: IXosModelDefsField) => { |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 205 | if (f.relation) { |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 206 | const input: IXosFormInput = { |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 207 | name: f.name, |
| 208 | label: this.toLabel(f.name), |
| 209 | type: 'select', |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 210 | validators: f.validators, |
| 211 | hint: f.hint |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 212 | }; |
| 213 | this.populateSelectField(f, input); |
| 214 | return input; |
| 215 | } |
| 216 | |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 217 | return { |
| 218 | name: f.name, |
| 219 | label: this.toLabel(f.name), |
| 220 | type: f.type, |
| 221 | validators: f.validators |
| 222 | }; |
| 223 | }) |
| 224 | .filter(f => this.excluded_fields.indexOf(f.name) === -1); |
| 225 | } |
| 226 | |
| 227 | public modelToFormCfg(model: IModeldef): IXosFormConfig { |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 228 | const formCfg: IXosFormConfig = { |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 229 | formName: `${model.name}Form`, |
Matteo Scandolo | 0a8b02e | 2017-01-06 14:43:36 -0800 | [diff] [blame] | 230 | exclude: ['backend_status', 'creator'], |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 231 | actions: [{ |
| 232 | label: 'Save', |
| 233 | class: 'success', |
| 234 | icon: 'ok', |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 235 | cb: null |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 236 | }], |
| 237 | inputs: this.modelFieldToInputCfg(model.fields) |
| 238 | }; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 239 | |
| 240 | formCfg.actions[0].cb = (item, form: angular.IFormController) => { |
| 241 | |
| 242 | if (!form.$valid) { |
| 243 | formCfg.feedback = { |
| 244 | show: true, |
| 245 | message: 'Form is invalid', |
| 246 | type: 'danger', |
| 247 | closeBtn: true |
| 248 | }; |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 249 | |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
| 253 | const model = angular.copy(item); |
| 254 | |
| 255 | // TODO remove ManyToMany relations and save them separately (how??) |
| 256 | delete item.networks; |
| 257 | |
| 258 | // adding userId as creator |
| 259 | item.creator = this.AuthService.getUser().id; |
| 260 | |
| 261 | item.$save() |
| 262 | .then((res) => { |
| 263 | if (res.status === 403 || res.status === 405 || res.status === 500) { |
| 264 | // TODO understand why 405 does not go directly in catch (it may be realted to ng-rest-gw) |
| 265 | throw new Error(); |
| 266 | } |
Matteo Scandolo | ac8c8c2 | 2017-01-09 15:04:32 -0800 | [diff] [blame] | 267 | formCfg.feedback = { |
| 268 | show: true, |
| 269 | message: `${model.name} succesfully saved`, |
| 270 | type: 'success', |
| 271 | closeBtn: true |
| 272 | }; |
Matteo Scandolo | 6f45e26 | 2017-01-09 14:47:26 -0800 | [diff] [blame] | 273 | this.toastr.success(`${model.name} succesfully saved`); |
| 274 | }) |
| 275 | .catch(err => { |
| 276 | // TODO keep the edited model |
| 277 | this.toastr.error(`Error while saving ${model.name}`); |
| 278 | }); |
| 279 | }; |
| 280 | |
| 281 | return formCfg; |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 284 | private fromCamelCase(string: string): string { |
| 285 | return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' '); |
| 286 | } |
| 287 | |
| 288 | private fromSnakeCase(string: string): string { |
| 289 | return string.split('_').join(' ').trim(); |
| 290 | } |
| 291 | |
| 292 | private fromKebabCase(string: string): string { |
| 293 | return string.split('-').join(' ').trim(); |
| 294 | } |
| 295 | |
| 296 | private capitalizeFirst(string: string): string { |
| 297 | return string.slice(0, 1).toUpperCase() + string.slice(1); |
| 298 | } |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 299 | |
| 300 | private populateRelated(item: any, fk: string, field: IXosModelDefsField): any { |
| 301 | // if the relation is not defined return |
| 302 | if (!fk || angular.isUndefined(fk) || fk === null) { |
| 303 | return; |
| 304 | } |
| 305 | this.ModelStore.query(field.relation.model) |
| 306 | .subscribe(res => { |
| 307 | if (angular.isDefined(res) && angular.isDefined(fk)) { |
| 308 | let ri = _.find(res, {id: fk}); |
| 309 | if (angular.isDefined(ri)) { |
| 310 | item[`${field.name}-formatted`] = angular.isDefined(ri.name) ? ri.name : ri.humanReadableName; |
| 311 | } |
| 312 | } |
| 313 | }); |
| 314 | } |
Matteo Scandolo | 07e2f62 | 2017-01-09 10:54:13 -0800 | [diff] [blame] | 315 | |
| 316 | // augment a select field with related model informations |
| 317 | private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void { |
| 318 | this.ModelStore.query(field.relation.model) |
| 319 | .subscribe(res => { |
| 320 | input.options = _.map(res, item => { |
| 321 | return {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name}; |
| 322 | }); |
| 323 | }); |
| 324 | } |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 325 | } |