-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacet.js
More file actions
316 lines (252 loc) · 9.14 KB
/
Facet.js
File metadata and controls
316 lines (252 loc) · 9.14 KB
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
if(!javaxt) var javaxt={};
if(!javaxt.express) javaxt.express={};
//******************************************************************************
//** Facet
//******************************************************************************
/**
* Used to render an individual facet in a facet panel.
* Requires javaxt.dhtml.Checkbox
*
******************************************************************************/
javaxt.express.Facet = function(parent, config) {
this.className = "javaxt.express.Facet";
var me = this;
var body;
var numOptions = 0;
var defaultConfig = {
title: "Title",
style: {
panel: {
backgroundColor: "#e1f2ff",
marginBottom: "8px"
},
header: {
backgroundColor: "#b0d4f3",
color: "#003a92",
padding: "4px 8px"
},
body: {
padding: "4px 8px"
},
label: {
//fontFamily: "helvetica,arial,verdana,sans-serif",
fontSize: "12px",
color: "#3e3e3e",
whiteSpace: "nowrap",
cursor: "pointer",
padding: "1px 0 0 5px"
},
checkbox: {
width: "13px",
height: "13px",
border: "1px solid #cccccc",
borderRadius: "3px",
background: "#F6F6F6",
cursor: "pointer",
margin: "0px",
color: "#2b2b2b"
},
checkmark: {
content: "",
display: "block",
width: "4px",
height: "7px",
border: "solid #ffffff",
borderWidth: "0 2px 2px 0",
transform: "rotate(45deg)",
margin: "1px 0 0 4px"
}
},
sort: {
field: "name", //or count
direction: "desc"
}
};
//**************************************************************************
//** Constructor
//**************************************************************************
var init = function(){
if (typeof parent === "string"){
parent = document.getElementById(parent);
}
if (!parent) return;
//Merge config with default config
config = merge(config, defaultConfig);
if (!config.filter) config.filter={};
if (!config.key) config.key=config.title;
var facet = document.createElement('div');
setStyle(facet, config.style["panel"]);
var header = document.createElement('div');
setStyle(header, config.style["header"]);
if (typeof config.title === "string"){
header.innerHTML = config.title;
}
else{
header.appendChild(config.title);
}
facet.appendChild(header);
body = document.createElement('div');
setStyle(body, config.style["body"]);
facet.appendChild(body);
parent.appendChild(facet);
};
this.onChange = function(){};
this.getTitle = function(){
return config.title;
};
this.getKey = function(){
return config.key;
};
this.clear = function(){
body.innerHTML = "";
};
this.getBody = function(){
return body;
};
this.getNumOptions = function(){
return numOptions;
};
//**************************************************************************
//** update
//**************************************************************************
/** Used to update the facet with new data.
*/
this.update = function(data){
me.clear();
var getLabel = config.getLabel;
var filterName = config.key;
//Convert data (json) into an array
var arr = [];
for (var key in data){
if (data.hasOwnProperty(key)){
arr.push({
label: key,
count: data[key]
});
}
}
numOptions = arr.length;
//Sort the array as needed
if (config.sort){
var field = config.sort.field;
var direction = config.sort.direction;
if (field=="name"){
if (direction==null) direction = "asc";
arr.sort(function(a, b){
var x = a.label.toLowerCase();
var y = b.label.toLowerCase();
if (direction=="desc"){
if (x < y) {return 1;}
if (x > y) {return -1;}
}
else{
if (x < y) {return -1;}
if (x > y) {return 1;}
}
return 0;
});
}
else if (field=="count"){
if (direction==null) direction = "desc";
arr.sort(function(a, b){
var x = a.count;
var y = b.count;
if (direction=="desc"){
if (x < y) {return 1;}
if (x > y) {return -1;}
}
else{
if (x < y) {return -1;}
if (x > y) {return 1;}
}
return 0;
});
}
}
//Iterate through the array and create facet labels and checkboxes
var data = arr;
for (var idx=0; idx<data.length; idx++){
var entry = data[idx];
var count = entry.count;
var label = entry.label;
if (getLabel) label = getLabel(label);
//Check whether the checkbox should be checked
var checked = false;
if (config.filter[filterName]){
var arr = config.filter[filterName].split(",");
for (var i=0; i<arr.length; i++){
if (arr[i]==entry.label+""){
checked = true;
break;
}
}
}
//Create checkbox
var div = document.createElement("div");
var checkbox = new javaxt.dhtml.Checkbox(div,{
label: label + " (" + count + ")",
value: entry.label,
checked: checked,
display: 'table',
style: {
label: config.style.label,
box: config.style.checkbox,
check: config.style.checkmark
}
});
//Add custom property to the checkbox
checkbox.filterName = filterName;
//Add logic to update the filter whenever the user clicks the checkbox
checkbox.onClick = function(checked){
var filterName = this.filterName;
var val = this.getValue()+"";
var currFilter = config.filter[filterName];
if (checked){
//Add val to filter
if (currFilter){
var updateFilter = true;
var arr = currFilter.split(",");
for (var i=0; i<arr.length; i++){
if (arr[i]==val){
updateFilter = false;
break;
}
}
if (updateFilter) config.filter[filterName] += "," + val;
}
else{
config.filter[filterName] = val;
}
}
else{
//Remove val from filter
if (currFilter){
var str = "";
var arr = currFilter.split(",");
for (var i=0; i<arr.length; i++){
if (arr[i]!=val){
if (str.length>0) str+= ",";
str += arr[i];
}
}
if (str.length>0){
config.filter[filterName] = str;
}
else{
delete config.filter[filterName];
}
}
}
me.onChange(config.filter[filterName]);
};
body.appendChild(div);
}
};
//**************************************************************************
//** Utilites
//**************************************************************************
/** Common functions found in Utils.js */
var merge = javaxt.dhtml.utils.merge;
var setStyle = javaxt.dhtml.utils.setStyle;
init();
};