Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 1 | import * as _ from 'lodash'; |
| 2 | import * as pluralize from 'pluralize'; |
| 3 | import {IXosTableColumn} from '../../table/table'; |
| 4 | |
| 5 | export interface IXosModelDefsField { |
| 6 | name: string; |
| 7 | type: string; |
| 8 | } |
| 9 | |
| 10 | export interface IXosConfigHelpersService { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 11 | excluded_fields: string[]; |
| 12 | modeldefToTableCfg(fields: IXosModelDefsField[], baseUrl: string): any[]; // TODO use a proper interface |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 13 | pluralize(string: string, quantity?: number, count?: boolean): string; |
| 14 | toLabel(string: string, pluralize?: boolean): string; |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 15 | toLabels(string: string[], pluralize?: boolean): string[]; |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | export class ConfigHelpers { |
| 19 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 20 | excluded_fields = [ |
| 21 | 'created', |
| 22 | 'updated', |
| 23 | 'enacted', |
| 24 | 'policed', |
| 25 | 'backend_register', |
| 26 | 'deleted', |
| 27 | 'write_protect', |
| 28 | 'lazy_blocked', |
| 29 | 'no_sync', |
| 30 | 'no_policy', |
| 31 | 'omf_friendly', |
| 32 | 'enabled', |
Matteo Scandolo | d62ea79 | 2016-12-22 14:02:28 -0800 | [diff] [blame] | 33 | 'validators', |
| 34 | 'password' |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 35 | ]; |
| 36 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 37 | constructor() { |
| 38 | pluralize.addIrregularRule('xos', 'xosses'); |
| 39 | pluralize.addPluralRule(/slice$/i, 'slices'); |
| 40 | } |
| 41 | |
| 42 | pluralize(string: string, quantity?: number, count?: boolean): string { |
| 43 | return pluralize(string, quantity, count); |
| 44 | } |
| 45 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 46 | toLabels(strings: string[], pluralize?: boolean): string[] { |
| 47 | if (angular.isArray(strings)) { |
| 48 | return _.map(strings, s => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 49 | return this.toLabel(s, pluralize); |
| 50 | }); |
| 51 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | toLabel(string: string, pluralize?: boolean): string { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 55 | |
| 56 | if (pluralize) { |
| 57 | string = this.pluralize(string); |
| 58 | } |
| 59 | |
| 60 | string = this.fromCamelCase(string); |
| 61 | string = this.fromSnakeCase(string); |
| 62 | string = this.fromKebabCase(string); |
| 63 | |
| 64 | return this.capitalizeFirst(string); |
| 65 | } |
| 66 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 67 | modeldefToTableCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] { |
| 68 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 69 | const cfg = _.map(fields, (f) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 70 | if (this.excluded_fields.indexOf(f.name) > -1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 71 | return; |
| 72 | } |
| 73 | const col: IXosTableColumn = { |
| 74 | label: this.toLabel(f.name), |
| 75 | prop: f.name |
| 76 | }; |
| 77 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 78 | if (f.name === 'id' || f.name === 'name') { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 79 | // NOTE can we find a better method to generalize? |
| 80 | col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`; |
| 81 | } |
| 82 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 83 | if (f.name === 'backend_status') { |
| 84 | col.type = 'icon'; |
| 85 | col.formatter = (item) => { |
| 86 | if (item.backend_status.indexOf('1') > -1) { |
| 87 | return 'check'; |
| 88 | } |
| 89 | if (item.backend_status.indexOf('2') > -1) { |
| 90 | return 'exclamation-circle'; |
| 91 | } |
| 92 | if (item.backend_status.indexOf('0') > -1) { |
| 93 | return 'clock-o'; |
| 94 | } |
| 95 | }; |
| 96 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame] | 97 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 98 | return col; |
| 99 | }) |
| 100 | .filter(v => angular.isDefined(v)); |
| 101 | |
| 102 | return cfg; |
| 103 | }; |
| 104 | |
| 105 | private fromCamelCase(string: string): string { |
| 106 | return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' '); |
| 107 | } |
| 108 | |
| 109 | private fromSnakeCase(string: string): string { |
| 110 | return string.split('_').join(' ').trim(); |
| 111 | } |
| 112 | |
| 113 | private fromKebabCase(string: string): string { |
| 114 | return string.split('-').join(' ').trim(); |
| 115 | } |
| 116 | |
| 117 | private capitalizeFirst(string: string): string { |
| 118 | return string.slice(0, 1).toUpperCase() + string.slice(1); |
| 119 | } |
| 120 | } |
| 121 | |