blob: 332c60c499e5f1e9e4e1068f09586324429f5d0f [file] [log] [blame]
Matteo Scandolod58d5042016-12-16 16:59:21 -08001import * as _ from 'lodash';
2import * as pluralize from 'pluralize';
Matteo Scandolocb466ed2017-01-04 17:16:24 -08003import {IXosTableColumn, IXosTableCfg} from '../../table/table';
4import {IModeldef} from '../../../datasources/rest/modeldefs.rest';
Matteo Scandolod58d5042016-12-16 16:59:21 -08005
6export interface IXosModelDefsField {
7 name: string;
8 type: string;
Matteo Scandolocb466ed2017-01-04 17:16:24 -08009 validators?: any;
Matteo Scandolod58d5042016-12-16 16:59:21 -080010}
11
12export interface IXosConfigHelpersService {
Matteo Scandoloee655a12016-12-19 15:38:43 -080013 excluded_fields: string[];
Matteo Scandolocb466ed2017-01-04 17:16:24 -080014 modelToTableCfg(model: IModeldef, baseUrl: string): IXosTableCfg;
15 modelFieldsToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[]; // TODO use a proper interface
Matteo Scandolod58d5042016-12-16 16:59:21 -080016 pluralize(string: string, quantity?: number, count?: boolean): string;
17 toLabel(string: string, pluralize?: boolean): string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080018 toLabels(string: string[], pluralize?: boolean): string[];
Matteo Scandolod58d5042016-12-16 16:59:21 -080019}
20
21export class ConfigHelpers {
22
Matteo Scandoloee655a12016-12-19 15:38:43 -080023 excluded_fields = [
24 'created',
25 'updated',
26 'enacted',
27 'policed',
28 'backend_register',
29 'deleted',
30 'write_protect',
31 'lazy_blocked',
32 'no_sync',
33 'no_policy',
34 'omf_friendly',
35 'enabled',
Matteo Scandolod62ea792016-12-22 14:02:28 -080036 'validators',
37 'password'
Matteo Scandoloee655a12016-12-19 15:38:43 -080038 ];
39
Matteo Scandolocb466ed2017-01-04 17:16:24 -080040 constructor(
41 private toastr: ng.toastr.IToastrService
42 ) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080043 pluralize.addIrregularRule('xos', 'xosses');
44 pluralize.addPluralRule(/slice$/i, 'slices');
45 }
46
47 pluralize(string: string, quantity?: number, count?: boolean): string {
48 return pluralize(string, quantity, count);
49 }
50
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080051 toLabels(strings: string[], pluralize?: boolean): string[] {
52 if (angular.isArray(strings)) {
53 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080054 return this.toLabel(s, pluralize);
55 });
56 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080057 }
58
59 toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080060
61 if (pluralize) {
62 string = this.pluralize(string);
63 }
64
65 string = this.fromCamelCase(string);
66 string = this.fromSnakeCase(string);
67 string = this.fromKebabCase(string);
68
69 return this.capitalizeFirst(string);
70 }
71
Matteo Scandolocb466ed2017-01-04 17:16:24 -080072 modelToTableCfg(model: IModeldef, baseUrl: string): IXosTableCfg {
73 const cfg = {
74 columns: this.modelFieldsToColumnsCfg(model.fields, baseUrl),
75 filter: 'fulltext',
76 order: {field: 'id', reverse: false},
77 actions: [
78 {
79 label: 'delete',
80 icon: 'remove',
81 color: 'red',
82 cb: (item) => {
83 let obj = angular.copy(item);
84
85 item.$delete()
86 .then((res) => {
87 if (res.status === 404) {
88 // TODO understand why it does not go directly in catch
89 throw new Error();
90 }
91 this.toastr.info(`${model.name} ${obj.name} succesfully deleted`);
92 })
93 .catch(() => {
94 this.toastr.error(`Error while deleting ${obj.name}`);
95 });
96 }
97 }
98 ]
99 };
100 return cfg;
101 }
102
103 modelFieldsToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800104
Matteo Scandolo231de262017-01-04 16:33:14 -0800105 const columns = _.map(fields, (f) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800106 if (this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800107 return;
108 }
109 const col: IXosTableColumn = {
110 label: this.toLabel(f.name),
111 prop: f.name
112 };
113
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800114 if (f.name === 'id' || f.name === 'name') {
Matteo Scandolo99ac9d92017-01-03 13:58:19 -0800115 // NOTE can we find a better method to generalize the route?
Matteo Scandoloee655a12016-12-19 15:38:43 -0800116 col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`;
117 }
118
Matteo Scandolod58d5042016-12-16 16:59:21 -0800119 if (f.name === 'backend_status') {
120 col.type = 'icon';
121 col.formatter = (item) => {
122 if (item.backend_status.indexOf('1') > -1) {
123 return 'check';
124 }
125 if (item.backend_status.indexOf('2') > -1) {
126 return 'exclamation-circle';
127 }
128 if (item.backend_status.indexOf('0') > -1) {
129 return 'clock-o';
130 }
131 };
132 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800133
Matteo Scandolod58d5042016-12-16 16:59:21 -0800134 return col;
135 })
136 .filter(v => angular.isDefined(v));
137
Matteo Scandolo231de262017-01-04 16:33:14 -0800138 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800139 };
140
141 private fromCamelCase(string: string): string {
142 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
143 }
144
145 private fromSnakeCase(string: string): string {
146 return string.split('_').join(' ').trim();
147 }
148
149 private fromKebabCase(string: string): string {
150 return string.split('-').join(' ').trim();
151 }
152
153 private capitalizeFirst(string: string): string {
154 return string.slice(0, 1).toUpperCase() + string.slice(1);
155 }
156}
157