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