-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.js
145 lines (121 loc) · 4.05 KB
/
list.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
// TODO use bind for dom interaction/manipulation
function get(s,c) {
try {
return (c||document).querySelector(s);
} catch (err) {
return null;
}
}
function buildItem (elem, content) {
var elem = elem.cloneNode();
elem.innerHTML = content;
return elem;
}
var getFieldLabel = require('./validate').getFieldLabel;
function createFilterItem (hash) {
var self = this;
var item = buildItem(self.domRefs.listItem, self.domRefs.listItemContent);
var checkbox = get(self.config.ui.item.onoff, item);
var field = get(self.config.ui.item.field, item);
var operator = get(self.config.ui.item.operator, item);
var value = get(self.config.ui.item.value, item);
var edit = get(self.config.ui.item.edit, item);
var rm = get(self.config.ui.item.remove, item);
// enable/disable filter
if (self.filters[hash].disabled) {
item.setAttribute('class', item.getAttribute('class') + ' disabled');
if (checkbox) {
checkbox.removeAttribute('checked');
}
}
// hide filter item
if (self.filters[hash].hidden) {
item.style.display = 'none';
}
// set content
if (field) {
field.innerHTML = getFieldLabel.call(self, self.filters[hash].field);
}
if (operator) {
var myOperator = self.filters[hash].operator;
operator.innerHTML = (self.config.i18n || {})[myOperator] || myOperator;
}
if (value) {
var myValue = self.filters[hash].value === undefined ? '' : (typeof self.filters[hash].originalValue === 'string' ? self.filters[hash].originalValue : self.filters[hash].value);
value.innerHTML = (self.config.i18n || {})[myValue] || myValue;
}
// hide edit if it's a core field
if (edit && self.filters[hash].field[0] === '_') {
edit.style.display = 'none';
}
if (!self.filters[hash].fixed) {
if (checkbox) {
checkbox.addEventListener('click', function (event) {
self.emit('filtersChanged');
if (checkbox.checked) {
self.emit('enableFilter', hash);
} else {
self.emit('disableFilter', hash);
}
}, false);
}
// edit filter
if (edit) {
edit.addEventListener(self.config.ui.events.itemEdit || 'click', function (event) {
self.emit('filtersChanged');
event.stopPropagation();
event.preventDefault();
self.emit('editFilter', hash);
}, false);
delete edit.style.display;
}
// remove filter
if (rm) {
rm.addEventListener(self.config.ui.events.itemRemove || 'click', function (event) {
self.emit('filtersChanged');
event.stopPropagation();
event.preventDefault();
self.emit('removeFilter', hash);
}, false);
}
} else {
item.setAttribute('class', item.getAttribute('class') + ' fixed' + (self.filters[hash].disabled ? ' disabled' : ''));
if (checkbox) {
checkbox.setAttribute('disabled', true);
}
if (edit) {
edit.style.display = 'none';
}
}
return item;
}
function save (hash) {
var self = this;
// create filter item
var item = createFilterItem.call(self, hash);
if (self.filters[hash].item) {
// replace filter
try {
self.domRefs.list.replaceChild(item, self.filters[hash].item);
} catch (e) {}
//self.domRefs.list.appendChild(item);
} else {
// add new filter
if (self.domRefs.list) {
self.domRefs.list.appendChild(item);
}
}
// update cache
self.filters[hash].item = item;
}
function remove (hash) {
var self = this;
if (self.filters[hash]) {
// remove dom element
self.domRefs.list.removeChild(self.filters[hash].item);
// remove from cache
delete self.filters[hash];
}
}
exports.save = save;
exports.remove = remove;