blob: fc836e7e6d7049fd6de2b3c3a19f8f33fdf326b5 [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 Scandolo1c5905f2017-01-04 17:41:15 -080019 urlFromCoreModel(name: string): string;
Matteo Scandolod58d5042016-12-16 16:59:21 -080020}
21
22export class ConfigHelpers {
23
Matteo Scandoloee655a12016-12-19 15:38:43 -080024 excluded_fields = [
25 'created',
26 'updated',
27 'enacted',
28 'policed',
29 'backend_register',
30 'deleted',
31 'write_protect',
32 'lazy_blocked',
33 'no_sync',
34 'no_policy',
35 'omf_friendly',
36 'enabled',
Matteo Scandolod62ea792016-12-22 14:02:28 -080037 'validators',
38 'password'
Matteo Scandoloee655a12016-12-19 15:38:43 -080039 ];
40
Matteo Scandolocb466ed2017-01-04 17:16:24 -080041 constructor(
42 private toastr: ng.toastr.IToastrService
43 ) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080044 pluralize.addIrregularRule('xos', 'xosses');
45 pluralize.addPluralRule(/slice$/i, 'slices');
46 }
47
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080048 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080049 return pluralize(string, quantity, count);
50 }
51
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080052 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080053 if (angular.isArray(strings)) {
54 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080055 return this.toLabel(s, pluralize);
56 });
57 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080058 }
59
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080060 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080061
62 if (pluralize) {
63 string = this.pluralize(string);
64 }
65
66 string = this.fromCamelCase(string);
67 string = this.fromSnakeCase(string);
68 string = this.fromKebabCase(string);
69
70 return this.capitalizeFirst(string);
71 }
72
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080073 public modelToTableCfg(model: IModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -080074 const cfg = {
75 columns: this.modelFieldsToColumnsCfg(model.fields, baseUrl),
76 filter: 'fulltext',
77 order: {field: 'id', reverse: false},
78 actions: [
79 {
80 label: 'delete',
81 icon: 'remove',
82 color: 'red',
83 cb: (item) => {
84 let obj = angular.copy(item);
85
86 item.$delete()
87 .then((res) => {
88 if (res.status === 404) {
89 // TODO understand why it does not go directly in catch
90 throw new Error();
91 }
92 this.toastr.info(`${model.name} ${obj.name} succesfully deleted`);
93 })
94 .catch(() => {
95 this.toastr.error(`Error while deleting ${obj.name}`);
96 });
97 }
98 }
99 ]
100 };
101 return cfg;
102 }
103
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800104 public modelFieldsToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800105
Matteo Scandolo231de262017-01-04 16:33:14 -0800106 const columns = _.map(fields, (f) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800107 if (this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800108 return;
109 }
110 const col: IXosTableColumn = {
111 label: this.toLabel(f.name),
112 prop: f.name
113 };
114
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800115 if (f.name === 'id' || f.name === 'name') {
Matteo Scandolo99ac9d92017-01-03 13:58:19 -0800116 // NOTE can we find a better method to generalize the route?
Matteo Scandoloee655a12016-12-19 15:38:43 -0800117 col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`;
118 }
119
Matteo Scandolod58d5042016-12-16 16:59:21 -0800120 if (f.name === 'backend_status') {
121 col.type = 'icon';
122 col.formatter = (item) => {
123 if (item.backend_status.indexOf('1') > -1) {
124 return 'check';
125 }
126 if (item.backend_status.indexOf('2') > -1) {
127 return 'exclamation-circle';
128 }
129 if (item.backend_status.indexOf('0') > -1) {
130 return 'clock-o';
131 }
132 };
133 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800134
Matteo Scandolod58d5042016-12-16 16:59:21 -0800135 return col;
136 })
137 .filter(v => angular.isDefined(v));
138
Matteo Scandolo231de262017-01-04 16:33:14 -0800139 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800140 };
141
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800142 public urlFromCoreModel(name: string): string {
143 return `/core/${this.pluralize(name.toLowerCase())}`;
144 }
145
Matteo Scandolod58d5042016-12-16 16:59:21 -0800146 private fromCamelCase(string: string): string {
147 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
148 }
149
150 private fromSnakeCase(string: string): string {
151 return string.split('_').join(' ').trim();
152 }
153
154 private fromKebabCase(string: string): string {
155 return string.split('-').join(' ').trim();
156 }
157
158 private capitalizeFirst(string: string): string {
159 return string.slice(0, 1).toUpperCase() + string.slice(1);
160 }
161}
162