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