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', |
| 33 | 'validators' |
| 34 | ]; |
| 35 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 36 | constructor() { |
| 37 | pluralize.addIrregularRule('xos', 'xosses'); |
| 38 | pluralize.addPluralRule(/slice$/i, 'slices'); |
| 39 | } |
| 40 | |
| 41 | pluralize(string: string, quantity?: number, count?: boolean): string { |
| 42 | return pluralize(string, quantity, count); |
| 43 | } |
| 44 | |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 45 | toLabels(strings: string[], pluralize?: boolean): string[] { |
| 46 | if (angular.isArray(strings)) { |
| 47 | return _.map(strings, s => { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 48 | return this.toLabel(s, pluralize); |
| 49 | }); |
| 50 | } |
Matteo Scandolo | e0d71ea | 2016-12-19 11:56:12 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | toLabel(string: string, pluralize?: boolean): string { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 54 | |
| 55 | if (pluralize) { |
| 56 | string = this.pluralize(string); |
| 57 | } |
| 58 | |
| 59 | string = this.fromCamelCase(string); |
| 60 | string = this.fromSnakeCase(string); |
| 61 | string = this.fromKebabCase(string); |
| 62 | |
| 63 | return this.capitalizeFirst(string); |
| 64 | } |
| 65 | |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 66 | modeldefToTableCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] { |
| 67 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 68 | const cfg = _.map(fields, (f) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 69 | if (this.excluded_fields.indexOf(f.name) > -1) { |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | const col: IXosTableColumn = { |
| 73 | label: this.toLabel(f.name), |
| 74 | prop: f.name |
| 75 | }; |
| 76 | |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame^] | 77 | if (f.name === 'id' || f.name === 'name') { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 78 | // NOTE can we find a better method to generalize? |
| 79 | col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`; |
| 80 | } |
| 81 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 82 | if (f.name === 'backend_status') { |
| 83 | col.type = 'icon'; |
| 84 | col.formatter = (item) => { |
| 85 | if (item.backend_status.indexOf('1') > -1) { |
| 86 | return 'check'; |
| 87 | } |
| 88 | if (item.backend_status.indexOf('2') > -1) { |
| 89 | return 'exclamation-circle'; |
| 90 | } |
| 91 | if (item.backend_status.indexOf('0') > -1) { |
| 92 | return 'clock-o'; |
| 93 | } |
| 94 | }; |
| 95 | } |
Matteo Scandolo | a8a6fbb | 2016-12-21 16:59:08 -0800 | [diff] [blame^] | 96 | |
Matteo Scandolo | d58d504 | 2016-12-16 16:59:21 -0800 | [diff] [blame] | 97 | return col; |
| 98 | }) |
| 99 | .filter(v => angular.isDefined(v)); |
| 100 | |
| 101 | return cfg; |
| 102 | }; |
| 103 | |
| 104 | private fromCamelCase(string: string): string { |
| 105 | return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' '); |
| 106 | } |
| 107 | |
| 108 | private fromSnakeCase(string: string): string { |
| 109 | return string.split('_').join(' ').trim(); |
| 110 | } |
| 111 | |
| 112 | private fromKebabCase(string: string): string { |
| 113 | return string.split('-').join(' ').trim(); |
| 114 | } |
| 115 | |
| 116 | private capitalizeFirst(string: string): string { |
| 117 | return string.slice(0, 1).toUpperCase() + string.slice(1); |
| 118 | } |
| 119 | } |
| 120 | |