-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.js
342 lines (269 loc) · 9.15 KB
/
ui.js
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
var find = require('./find');
var list = require('./list');
var inputs = require('./inputs');
var message = require('./message');
var loader = require('./loader');
// TODO use bind for dom interaction/manipulation
function elm(d,a) {
try {
var b = document.createElement(d);
if ('object' === typeof a) {
for (var c in a) {
if (!a.hasOwnProperty(c)) continue;
if (typeof a[c] === 'object' && a[c] instanceof Array) {
b.setAttribute(c, a[c].join(', '));
} else {
b.setAttribute(c, a[c]);
}
}
return b;
}
} catch (e) {
return null;
}
}
function get(s,c) {
try {
return (c||document).querySelector(s);
} catch (err) {
return null;
}
}
function save () {
var self = this;
var filter = {
field: self.domRefs.inputs.field.value,
operator: self.domRefs.inputs.operator.value || '=',
value: self.domRefs.inputs.value.value,
hash: self.current
};
self.emit('showLoader');
self.emit('setFilters', [filter], false,
(self.filters[filter.hash] || {}).disabled, function (err, data) {
var createKey = 'create';
if (self.domRefs.controls[createKey]) {
self.domRefs.controls[createKey].focus();
}
});
}
function edit (hash) {
var self = this;
var values = hash ? self.filters[hash] : {};
self.current = hash || null;
// handle remove button
if (hash && self.filters[hash]) {
self.domRefs.controls.remove.style.display = 'inline';
} else {
self.domRefs.controls.remove.style.display = 'none';
}
// change value field and operator selection dependent of selected field
changeField.call(self, values.field, values.operator, typeof values.originalValue === 'string' ? values.originalValue : values.value, true);
self.domRefs.filter.style.display = 'block';
if (hash && self.filters[hash]) {
self.domRefs.inputs.value.focus();
} else {
self.domRefs.inputs.field.focus();
}
}
function remove (hash) {
var self = this;
self.emit('showLoader');
self.emit('resetSkip');
self.domRefs.filter.style.display = 'none';
list.remove.call(self, hash || self.current);
find.call(self);
}
function cancel () {
var self = this;
self.current = null;
self.domRefs.filter.style.display = 'none';
}
function enable (hash) {
var self = this;
self.emit('showLoader');
self.emit('resetSkip');
// TODO remove class with bind
$(self.filters[hash].item).removeClass('disabled');
self.filters[hash].disabled = false;
find.call(self);
}
function disable (hash) {
var self = this;
self.emit('showLoader');
self.emit('resetSkip');
// TODO add class with bind
$(self.filters[hash].item).addClass('disabled');
self.filters[hash].disabled = true;
find.call(self);
}
function changeField (field, operator, value, editMode) {
var self = this;
var selectedTemplateSchema = self.templates[self.template].schema;
if (!field) {
// field is set in the for loop
for (field in selectedTemplateSchema) {
if (!selectedTemplateSchema.hasOwnProperty(field)) continue;
if (field.indexOf('_') !== 0 && !selectedTemplateSchema[field].noSearch) {
break;
}
}
}
// select field if it exists in the schema
if (self.domRefs.inputs.field) {
self.domRefs.inputs.field.value = field;
}
// set operators which are compatible with the field template
// and create value field depending on schema and operator
inputs.value.call(self, field, operator, value, editMode);
self.emit('filtersChanged');
}
function createTemplateSelectOption (template) {
var option = elm('option', {value: template.id});
option.innerHTML = template.name;
return option;
}
function setFilters (filters, reset) {
var self = this;
// reset filters if reset is true
if (reset && self.domRefs.list) {
self.domRefs.list.innerHTML = '';
}
// filters to list
for (var hash in filters) {
if (!filters.hasOwnProperty(hash)) continue;
list.save.call(self, hash);
}
// hide filter form
self.domRefs.filter.style.display = 'none';
}
function setTemplateOptions () {
var self = this;
if (self.domRefs.templateSelector) {
var df = document.createDocumentFragment();
for (var template in self.templates) {
if (!self.templates.hasOwnProperty(template)) continue;
df.appendChild(createTemplateSelectOption(self.templates[template]));
}
self.domRefs.templateSelector.innerHTML = '';
self.domRefs.templateSelector.appendChild(df);
}
}
function setTemplateSelection (template) {
var self = this;
// set fields
inputs.fields.call(self);
// select a field
//changeField.call(self);
// add template to selection, it it not exists
if (!self.templates[template.id] && self.domRefs.templateSelector) {
self.domRefs.templateSelector.appendChild(createTemplateSelectOption(template));
}
// select template
if (self.domRefs.templateSelector) {
self.domRefs.templateSelector.value = template.id;
}
}
function handleFindResult (err, data) {
var self = this;
self.emit('hideLoader');
}
function showLoader () {
var self = this;
self.emit('showLoader');
}
function ui () {
var self = this;
if (!self.config.ui.controls) {
return self.emit('message', 'error', 'No controls configured.');
}
//create the operator order if it was not gven in the configuration
if (!self.config.ui.operatorOrder) {
self.config.ui.operatorOrder = [];
for (var operator in self.config.operators) {
if (!self.config.operators.hasOwnProperty(operator)) continue;
self.config.ui.operatorOrder.push(operator);
}
}
// TODO check dom elements
// get dom refs
self.domRefs = {};
self.domRefs.filter = get(self.config.ui.filter, self.dom);
self.domRefs.valueLabel = get(self.config.ui.valueLabel, self.dom);
self.domRefs.valueField = get(self.config.ui.valueField, self.dom);
if (self.config.ui.templateSelector) {
self.domRefs.templateSelector= get(self.config.ui.templateSelector, self.dom);
}
// list item
var listItem = get(self.config.ui.listItem, self.dom);
if (listItem) {
self.domRefs.listItem = listItem.cloneNode();
self.domRefs.listItemContent = listItem.innerHTML;
self.domRefs.list = listItem.parentNode;
self.domRefs.list.innerHTML = '';
}
self.domRefs.inputs = {};
for (var name in self.config.ui.inputs) {
if (!self.config.ui.inputs.hasOwnProperty(name)) continue;
self.domRefs.inputs[name] = get(self.config.ui.inputs[name], self.dom);
}
self.domRefs.controls = {};
for (var name in self.config.ui.controls) {
if (!self.config.ui.controls.hasOwnProperty(name)) continue;
self.domRefs.controls[name] = get(self.config.ui.controls[name], self.dom);
}
// init message
if (self.config.message) {
message.call(self);
}
// init loader
loader.call(self);
// listen to ui events
self.on('saveFilter', save);
self.on('createFilter', edit);
self.on('editFilter', edit);
self.on('enableFilter', enable);
self.on('disableFilter', disable);
self.on('removeFilter', remove);
self.on('cancelFilter', cancel);
self.on('fieldChange', changeField);
// listen to alter inputs events
self.on('templates', setTemplateOptions);
self.on('template', setTemplateSelection);
self.on('filtersCached', setFilters);
// listen crud events
self.on('getTemplates', showLoader);
self.on('find', showLoader);
self.on('result', handleFindResult);
self.on('templateResult', handleFindResult);
// add events to controls
for (var handler in self.domRefs.controls) {
if (!self.domRefs.controls.hasOwnProperty(handler)) continue;
var control = self.domRefs.controls[handler];
if (control) {
control.addEventListener(self.config.ui.events[handler] || 'click', (function (handler) {
return function () {
self.emit(handler + 'Filter');
}
})(handler));
}
}
// template change
if (self.domRefs.templateSelector) {
self.domRefs.templateSelector.addEventListener('change', function () {
self.emit('setTemplate', self.domRefs.templateSelector.value);
});
}
// field change
if (self.domRefs.inputs.field) {
self.domRefs.inputs.field.addEventListener('change', function () {
self.emit('fieldChange', self.domRefs.inputs.field.value);
});
}
// operator change
if (self.domRefs.inputs.operator) {
self.domRefs.inputs.operator.addEventListener('change', function () {
self.emit('fieldChange', self.domRefs.inputs.field.value, self.domRefs.inputs.operator.value);
});
}
}
module.exports = ui;