blob: f9b76f0709873be4537b5136bb964c274c113ac6 [file] [log] [blame]
Matteo Scandolod58d5042016-12-16 16:59:21 -08001import * as _ from 'lodash';
2import * as pluralize from 'pluralize';
3import {IXosTableColumn} from '../../table/table';
4
5export interface IXosModelDefsField {
6 name: string;
7 type: string;
8}
9
10export interface IXosConfigHelpersService {
Matteo Scandoloee655a12016-12-19 15:38:43 -080011 excluded_fields: string[];
12 modeldefToTableCfg(fields: IXosModelDefsField[], baseUrl: string): any[]; // TODO use a proper interface
Matteo Scandolod58d5042016-12-16 16:59:21 -080013 pluralize(string: string, quantity?: number, count?: boolean): string;
14 toLabel(string: string, pluralize?: boolean): string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080015 toLabels(string: string[], pluralize?: boolean): string[];
Matteo Scandolod58d5042016-12-16 16:59:21 -080016}
17
18export class ConfigHelpers {
19
Matteo Scandoloee655a12016-12-19 15:38:43 -080020 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 Scandolod58d5042016-12-16 16:59:21 -080036 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 Scandoloe0d71ea2016-12-19 11:56:12 -080045 toLabels(strings: string[], pluralize?: boolean): string[] {
46 if (angular.isArray(strings)) {
47 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080048 return this.toLabel(s, pluralize);
49 });
50 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080051 }
52
53 toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080054
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 Scandoloee655a12016-12-19 15:38:43 -080066 modeldefToTableCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] {
67
Matteo Scandolod58d5042016-12-16 16:59:21 -080068 const cfg = _.map(fields, (f) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -080069 if (this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080070 return;
71 }
72 const col: IXosTableColumn = {
73 label: this.toLabel(f.name),
74 prop: f.name
75 };
76
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080077 if (f.name === 'id' || f.name === 'name') {
Matteo Scandoloee655a12016-12-19 15:38:43 -080078 // NOTE can we find a better method to generalize?
79 col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`;
80 }
81
Matteo Scandolod58d5042016-12-16 16:59:21 -080082 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 Scandoloa8a6fbb2016-12-21 16:59:08 -080096
Matteo Scandolod58d5042016-12-16 16:59:21 -080097 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