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