blob: 6b8985687063fce64ecacdb2f7816aaaf85cc5c7 [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 Scandolo80c3a652017-01-06 10:48:31 -08005import {IXosFormConfig, IXosFormInput} from '../../form/form';
Matteo Scandolod58d5042016-12-16 16:59:21 -08006
7export interface IXosModelDefsField {
8 name: string;
9 type: string;
Matteo Scandolocb466ed2017-01-04 17:16:24 -080010 validators?: any;
Matteo Scandolod58d5042016-12-16 16:59:21 -080011}
12
13export interface IXosConfigHelpersService {
Matteo Scandoloee655a12016-12-19 15:38:43 -080014 excluded_fields: string[];
Matteo Scandolocb466ed2017-01-04 17:16:24 -080015 modelFieldsToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[]; // TODO use a proper interface
Matteo Scandolo80c3a652017-01-06 10:48:31 -080016 modelToTableCfg(model: IModeldef, baseUrl: string): IXosTableCfg;
17 modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[];
18 modelToFormCfg(model: IModeldef): IXosFormConfig;
Matteo Scandolod58d5042016-12-16 16:59:21 -080019 pluralize(string: string, quantity?: number, count?: boolean): string;
20 toLabel(string: string, pluralize?: boolean): string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080021 toLabels(string: string[], pluralize?: boolean): string[];
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080022 urlFromCoreModel(name: string): string;
Matteo Scandolod58d5042016-12-16 16:59:21 -080023}
24
25export class ConfigHelpers {
26
Matteo Scandoloee655a12016-12-19 15:38:43 -080027 excluded_fields = [
28 'created',
29 'updated',
30 'enacted',
31 'policed',
32 'backend_register',
33 'deleted',
34 'write_protect',
35 'lazy_blocked',
36 'no_sync',
37 'no_policy',
38 'omf_friendly',
39 'enabled',
Matteo Scandolod62ea792016-12-22 14:02:28 -080040 'validators',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080041 'password',
42 'backend_need_delete',
43 'backend_need_reap'
Matteo Scandoloee655a12016-12-19 15:38:43 -080044 ];
45
Matteo Scandolocb466ed2017-01-04 17:16:24 -080046 constructor(
47 private toastr: ng.toastr.IToastrService
48 ) {
Matteo Scandolod58d5042016-12-16 16:59:21 -080049 pluralize.addIrregularRule('xos', 'xosses');
50 pluralize.addPluralRule(/slice$/i, 'slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -080051 pluralize.addSingularRule(/slice$/i, 'slice');
Matteo Scandolod58d5042016-12-16 16:59:21 -080052 }
53
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080054 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080055 return pluralize(string, quantity, count);
56 }
57
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080058 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080059 if (angular.isArray(strings)) {
60 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -080061 return this.toLabel(s, pluralize);
62 });
63 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080064 }
65
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080066 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -080067
68 if (pluralize) {
69 string = this.pluralize(string);
70 }
71
72 string = this.fromCamelCase(string);
73 string = this.fromSnakeCase(string);
74 string = this.fromKebabCase(string);
75
76 return this.capitalizeFirst(string);
77 }
78
Matteo Scandolo1c5905f2017-01-04 17:41:15 -080079 public modelToTableCfg(model: IModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -080080 const cfg = {
81 columns: this.modelFieldsToColumnsCfg(model.fields, baseUrl),
82 filter: 'fulltext',
83 order: {field: 'id', reverse: false},
84 actions: [
85 {
86 label: 'delete',
87 icon: 'remove',
88 color: 'red',
89 cb: (item) => {
90 let obj = angular.copy(item);
91
92 item.$delete()
93 .then((res) => {
94 if (res.status === 404) {
95 // TODO understand why it does not go directly in catch
96 throw new Error();
97 }
98 this.toastr.info(`${model.name} ${obj.name} succesfully deleted`);
99 })
100 .catch(() => {
101 this.toastr.error(`Error while deleting ${obj.name}`);
102 });
103 }
104 }
105 ]
106 };
107 return cfg;
108 }
109
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800110 public modelFieldsToColumnsCfg(fields: IXosModelDefsField[], baseUrl: string): IXosTableColumn[] {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800111
Matteo Scandolo231de262017-01-04 16:33:14 -0800112 const columns = _.map(fields, (f) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -0800113 if (this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800114 return;
115 }
116 const col: IXosTableColumn = {
117 label: this.toLabel(f.name),
118 prop: f.name
119 };
120
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800121 if (f.name === 'id' || f.name === 'name') {
Matteo Scandolo99ac9d92017-01-03 13:58:19 -0800122 // NOTE can we find a better method to generalize the route?
Matteo Scandoloee655a12016-12-19 15:38:43 -0800123 col.link = item => `#/core${baseUrl.replace(':id?', item.id)}`;
124 }
125
Matteo Scandolod58d5042016-12-16 16:59:21 -0800126 if (f.name === 'backend_status') {
127 col.type = 'icon';
128 col.formatter = (item) => {
129 if (item.backend_status.indexOf('1') > -1) {
130 return 'check';
131 }
132 if (item.backend_status.indexOf('2') > -1) {
133 return 'exclamation-circle';
134 }
135 if (item.backend_status.indexOf('0') > -1) {
136 return 'clock-o';
137 }
138 };
139 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800140
Matteo Scandolod58d5042016-12-16 16:59:21 -0800141 return col;
142 })
143 .filter(v => angular.isDefined(v));
144
Matteo Scandolo231de262017-01-04 16:33:14 -0800145 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800146 };
147
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800148 public urlFromCoreModel(name: string): string {
149 return `/core/${this.pluralize(name.toLowerCase())}`;
150 }
151
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800152 public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] {
153
154 return _.map(fields, f => {
155 return {
156 name: f.name,
157 label: this.toLabel(f.name),
158 type: f.type,
159 validators: f.validators
160 };
161 })
162 .filter(f => this.excluded_fields.indexOf(f.name) === -1);
163 }
164
165 public modelToFormCfg(model: IModeldef): IXosFormConfig {
166 return {
167 formName: `${model.name}Form`,
168 exclude: ['backend_status'],
169 actions: [{
170 label: 'Save',
171 class: 'success',
172 icon: 'ok',
173 cb: (item, form) => {
174 item.$save()
175 .then(res => {
176 this.toastr.success(`${item.name} succesfully saved`);
177 })
178 .catch(err => {
179 this.toastr.error(`Error while saving ${item.name}`);
180 });
181 }
182 }],
183 inputs: this.modelFieldToInputCfg(model.fields)
184 };
185 }
186
Matteo Scandolod58d5042016-12-16 16:59:21 -0800187 private fromCamelCase(string: string): string {
188 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
189 }
190
191 private fromSnakeCase(string: string): string {
192 return string.split('_').join(' ').trim();
193 }
194
195 private fromKebabCase(string: string): string {
196 return string.split('-').join(' ').trim();
197 }
198
199 private capitalizeFirst(string: string): string {
200 return string.slice(0, 1).toUpperCase() + string.slice(1);
201 }
202}