-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathdialogEditorService.ts
187 lines (169 loc) · 4.92 KB
/
dialogEditorService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import * as _ from 'lodash';
export default class DialogEditorService {
public data: any = {};
public activeTab: number = 0;
/**
* Store data passed in parameter.
* @memberof DialogEditorService
* @function setData
* @param {any} nested object containing data of the dialog
*/
public setData(data: any) {
this.data = data;
this.undefinedAttrsToBoolean();
// FIXME: Compensation of default values until it is been resolved in the API
this.forEachDialogField((field: any) => {
if (field.hasOwnProperty('values') && _.isArray(field.values)) {
field.values = field.values.filter(value => value[0] && value[1]);
}
});
}
/**
* Return dialog id loaded at service.
* @memberof DialogEditorService
* @function getDialogId
*/
public getDialogId() {
return String(this.data.content[0].id || 'new');
}
/**
* Return dialog label loaded at service.
* @memberof DialogEditorService
* @function getDialogLabel
*/
public getDialogLabel() {
return this.data.content[0].label;
}
/**
* Return dialog description loaded at service.
* @memberof DialogEditorService
* @function getDialogDescription
*/
public getDialogDescription() {
return this.data.content[0].description;
}
/**
* Return dialog tabs loaded at service.
* @memberof DialogEditorService
* @function getDialogTabs
*/
public getDialogTabs() {
return this.data.content[0].dialog_tabs;
}
public getDynamicFields(nameToExclude) {
let dynamicFields = [];
this.forEachDialogField((field) => {
if (nameToExclude && (field.name === nameToExclude)) {
return;
}
if (field.dynamic === true) {
dynamicFields.push(field);
}
});
return dynamicFields;
}
/**
* Update positions for elements in array.
* @memberof DialogEditorService
* @function updatePositions
* @param {any[]} array of elements to sort
*/
public updatePositions(elements: any[]) {
elements.forEach((value, key) => value.position = key);
this.backupSessionStorage(this.getDialogId(), this.data);
}
/**
* Iterates through the list of dialog field names and creates a new
* unique name for the added element
* @memberof DialogEditorService
* @function newFieldName
*/
public newFieldName(fieldType: string) {
let dialogFieldNames = [];
let newOrdinalNumber = 1;
this.forEachDialogField((field) => {
dialogFieldNames.push(field.name);
});
while (dialogFieldNames.includes(fieldType + '_' + newOrdinalNumber)) {
newOrdinalNumber++;
}
return fieldType + '_' + newOrdinalNumber;
}
public clearSessionStorage(id: string) {
sessionStorage.removeItem(this.sessionStorageKey(id));
}
public backupSessionStorage(id: string, dialog: any) {
sessionStorage.setItem(this.sessionStorageKey(id), JSON.stringify(dialog));
}
public restoreSessionStorage(id: string) {
return JSON.parse(sessionStorage.getItem(this.sessionStorageKey(id)));
}
/**
* Iterates through all the dialog fields and calls callback method
* sent through parameter
* @memberof DialogEditorService
* @function forEachDialogField
*/
private forEachDialogField(callback) {
_.forEach(this.data.content[0].dialog_tabs, (tab: any) => {
_.forEach(tab.dialog_groups, (group: any) => {
_.forEach(group.dialog_fields, (field: any) => {
callback(field);
});
});
});
}
/**
* Function iterates through all the groups in the dialog editor
* and returns true if any dialog fields are present
* @memberof DialogEditorService
* @function anyDialogFields
*/
private anyDialogFields() {
let ret = false;
_.forEach(this.data.content[0].dialog_tabs, (tab: any) => {
_.forEach(tab.dialog_groups, (group: any) => {
if (!_.isEmpty(group.dialog_fields)) {
ret = true;
}
});
});
return ret;
}
/**
* Function is used to replace undefined values in dialogs
* with boolean, so the bootstrap switch is not initialized with
* undefined state
* @memberof DialogEditorService
* @function undefinedAttrsToBoolean
*/
private undefinedAttrsToBoolean() {
if (!this.anyDialogFields()) {
return;
}
let attributes = [
'required', 'visible', 'read_only', 'show_refresh_button',
'load_values_on_init', 'reconfigurable',
];
let optionalAttributes = [
'show_past_days', 'protected', 'force_multi_value'
];
this.forEachDialogField((field) => {
attributes.forEach(function(attr) {
if (field[attr] == null) {
field[attr] = false;
}
});
if (field['options']) {
optionalAttributes.forEach(function(attr) {
if (field['options'][attr] == null) {
field['options'][attr] = false;
}
});
}
});
}
private sessionStorageKey(id: string) {
return 'service_dialog-' + id;
}
}