-
Notifications
You must be signed in to change notification settings - Fork 2
/
FilterNode.js
160 lines (158 loc) · 5.01 KB
/
FilterNode.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
class FilterNode {
//FilterTree object - for category filters
static findFilterInList(filterList, filterName) {
for (let filter of filterList) {
if (filter.title == filterName) {
return filter;
}
}
return null;
}
constructor({ title, heTitle, docCount, aggKey, aggType, children, parent, selected } = {}) {
this.title = title;
this.heTitle = heTitle;
this.docCount = docCount;
this.aggKey = aggKey;
this.aggType = aggType;
this.children = typeof children === 'undefined' ? [] :
children.map(c => {
let ret = c instanceof FilterNode ? c.clone() : new FilterNode(c);
ret.parent = this;
return ret;
}
);
this.parent = typeof parent === 'undefined' ? null : parent;
this.selected = (typeof selected === 'undefined') ? 0 : selected; //0 - not selected, 1 - selected, 2 - partially selected
}
append(child) {
this.children.push(child);
child.parent = this;
}
hasChildren() {
return (this.children.length > 0);
}
getLeafNodes() {
//Return ordered array of leaf (book) level filters
if (!this.hasChildren()) {
return [this];
}
var results = [];
for (var i = 0; i < this.children.length; i++) {
results = results.concat(this.children[i].getLeafNodes());
}
return results;
}
getId() {
return this.aggKey.replace(new RegExp("[/',()]", 'g'),"-").replace(new RegExp(" ", 'g'),"_");
}
isSelected() {
return (this.selected == 1);
}
isPartial() {
return (this.selected == 2);
}
isUnselected() {
return (this.selected == 0);
}
setSelected(propogateParent, noPropogateChild) {
//default is to propogate children and not parents.
//Calls from front end should use (true, false), or just (true)
this.selected = 1;
if (!(noPropogateChild)) {
for (var i = 0; i < this.children.length; i++) {
this.children[i].setSelected(false);
}
}
if(propogateParent) {
if(this.parent) this.parent._deriveState();
}
}
setUnselected(propogateParent, noPropogateChild) {
//default is to propogate children and not parents.
//Calls from front end should use (true, false), or just (true)
this.selected = 0;
if (!(noPropogateChild)) {
for (var i = 0; i < this.children.length; i++) {
this.children[i].setUnselected(false);
}
}
if(propogateParent) {
if(this.parent) this.parent._deriveState();
}
}
setPartial() {
//Never propogate to children. Always propogate to parents
this.selected = 2;
if(this.parent) this.parent._deriveState();
}
_deriveState() {
//Always called from children, so we can assume at least one
var potentialState = this.children[0].selected;
if (potentialState == 2) {
this.setPartial();
return
}
for (var i = 1; i < this.children.length; i++) {
if (this.children[i].selected != potentialState) {
this.setPartial();
return
}
}
//Don't use setters, so as to avoid looping back through children.
if(potentialState == 1) {
this.setSelected(true, true);
} else {
this.setUnselected(true, true);
}
}
hasAppliedFilters() {
return (this.getAppliedFilters().length > 0)
}
getAppliedFilters() {
if (this.isUnselected()) {
return [];
}
if (this.isSelected()) {
return [this.aggKey];
}
var results = [];
for (var i = 0; i < this.children.length; i++) {
results = results.concat(this.children[i].getAppliedFilters());
}
return results;
}
getSelectedTitles(lang) {
if (this.isUnselected()) {
return [];
}
if (this.isSelected()) {
let enTitle = !!this.title ? this.title : this.heTitle;
let heTitle = !!this.heTitle ? this.heTitle : this.title;
if (!enTitle) {
if (this.aggType === 'group') { enTitle = '(No Group)'; }
if (this.aggType === 'tags') { enTitle = '(No Tag)'; }
}
if (!heTitle) {
if (this.aggType === 'group') { heTitle = '(ללא קבוצה)'; }
if (this.aggType === 'tags') { heTitle = '(ללא תוית)'; }
}
return[(lang == "en")?enTitle:heTitle];
}
var results = [];
for (var i = 0; i < this.children.length; i++) {
results = results.concat(this.children[i].getSelectedTitles(lang));
}
return results;
}
clone() {
const cloned = new FilterNode({ title: this.title, heTitle: this.heTitle, docCount: this.docCount, aggKey: this.aggKey, aggType: this.aggType, selected: this.selected });
const children = this.children.map( c => {
const cloned_child = c.clone();
cloned_child.parent = cloned;
return cloned_child;
});
cloned.children = children;
return cloned;
}
}
module.exports = FilterNode;