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