blob: 599edaf367462c02747766f512c1e054356c0c5a [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',
Matteo Scandolod62ea792016-12-22 14:02:28 -080033 'validators',
34 'password'
Matteo Scandoloee655a12016-12-19 15:38:43 -080035 ];
36
Matteo Scandolod58d5042016-12-16 16:59:21 -080037 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 Scandoloe0d71ea2016-12-19 11:56:12 -080046 toLabels(strings: string[], pluralize?: boolean): string[] {
47 if (angular.isArray(strings)) {
48 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080049 return this.toLabel(s, pluralize);
50 });
51 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080052 }
53
54 toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080055
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 Scandoloee655a12016-12-19 15:38:43 -080067 modeldefToTableCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] {
68
Matteo Scandolod58d5042016-12-16 16:59:21 -080069 const cfg = _.map(fields, (f) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -080070 if (this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080071 return;
72 }
73 const col: IXosTableColumn = {
74 label: this.toLabel(f.name),
75 prop: f.name
76 };
77
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -080078 if (f.name === 'id' || f.name === 'name') {
Matteo Scandoloee655a12016-12-19 15:38:43 -080079 // NOTE can we find a better method to generalize?
80 col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`;
81 }
82
Matteo Scandolod58d5042016-12-16 16:59:21 -080083 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 Scandoloa8a6fbb2016-12-21 16:59:08 -080097
Matteo Scandolod58d5042016-12-16 16:59:21 -080098 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