-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
298 lines (269 loc) · 9.09 KB
/
index.html
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
<html>
<head>
<title>summaries filter</title>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.4/js/standalone/selectize.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.4/css/selectize.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
<style>
.first {
float: left;
margin-right: 20px;
}
#results {
padding-top: 20px;
}
.selectize-input {
width: 300px;
}
</style>
<script language="JavaScript">
/* Module used to store data and methods. */
var SummaCls = (function(clsCfg) {
var cfg;
var keys;
var data;
var originalData;
var filters;
var uniqueKeys;
var uniqueValues;
var init = function(initCfg) {
cfg = initCfg || {};
keys = cfg.keys || [];
data = cfg.data || [];
filters = [];
uniqueKeys = [];
uniqueValues = {};
// a copy of the data to be used when the filters are applied.
originalData = _.cloneDeep(data);
// keys is transformed in a object where the key is the label
// and the value is the configuration for that label.
keys = _.map(keys, function(item) {
return (item instanceof Object) ? item : {key: item, label: item};
});
keys = _.chain(keys).map('label').zip(keys).fromPairs().value();
uniqueKeys = _.chain(keys).map('key').uniq().value();
// uniqueValues list, for each key, all the unique values found in data.
_.each(uniqueKeys, function(key) {
uniqueValues[key] = _.chain(data).map(key).flatten().uniq().sort().value();
});
};
/* filter the data. */
var filter = function() {
var data = originalData;
_.each(filters, function(f) {
var cfg = keys[f.label];
var cmp;
if (cfg.cmp) {
// filter data using the configured function.
cmp = function(item, key) {
return cfg.cmp(f.value, item[cfg.key], key);
};
} else {
cmp = {};
cmp[cfg.key] = f.value;
}
data = _.filter(data, cmp);
});
return data;
};
/* set the rference value of a given label. */
var setFilter = function(label, value) {
_.remove(filters, {'label': label});
if (value !== '') {
filters.push({'label': label, 'value': value});
}
};
init(clsCfg);
return {
init: init,
keys: keys,
filter: filter,
setFilter: setFilter,
uniqueKeys: uniqueKeys,
uniqueValues: uniqueValues
};
});
// the global object.
var Summa;
/* configure a selectize dropdown. */
function enhanceSummaWidget(cfg, data) {
var selector = cfg.selector || '#' + cfg.label + '_select';
data = _.map(data, function(item) {
return {value: item, text: item};
});
data.unshift({value: '', text: '-- any --'});
var el = $(selector);
if (!el.length) {
return;
}
return $(selector).selectize({
options: data,
onChange: function(val) {
if (cfg.valueProcess) {
val = cfg.valueProcess(val);
}
Summa.setFilter(cfg.label, val);
updateShownData();
}
});
}
/* what to do when the data is initialized or a selected item is changed. */
function updateShownData() {
filteredData = Summa.filter();
$('#items_count').text(filteredData.length);
}
/* inizialize the global Summa object. */
function initSummaries(summaries, callBack) {
Summa = SummaCls({
// data is, quite obviously, the data.
data: summaries,
/* list of keys we want to filter upon, with a description
of *how* we're filtering them.
A key can be a simple string, in which case the data will
be filtered upon that key with the identify comparison of
the provided value.
A key can be an object with a label (to describe the kind
of filter we're creating), the reference key and a cmp
function that will be used to run the comparison.
valueProcess can be a function that will be used to preprocess
the value received from the select, if needed.
*/
keys: ['ident',
'network',
{label: 'lon_min', key: 'lon',
valueProcess: function(val) {
val = parseInt(val);
return isNaN(val) ? '' : val;
},
// refVal is the value of the selected item; val is the
// value from the data currently being compared.
cmp: function(refVal, val, key) {
return val >= refVal;
}
},
{label: 'lon_max', key: 'lon',
valueProcess: function(val) {
val = parseInt(val);
return isNaN(val) ? '' : val;
},
cmp: function(refVal, val, key) {
return val <= refVal;
}
},
{label: 'lat_min', key: 'lat',
valueProcess: function(val) {
val = parseInt(val);
return isNaN(val) ? '' : val;
},
cmp: function(refVal, val, key) {
return val >= refVal;
}
},
{label: 'lat_max', key: 'lat',
valueProcess: function(val) {
val = parseInt(val);
return isNaN(val) ? '' : val;
},
cmp: function(refVal, val, key) {
return val <= refVal;
}
},
{label: 'date_min', key: 'date',
cmp: function(refVal, val, key) {
return val[0] >= refVal;
}
},
{label: 'date_max', key: 'date',
cmp: function(refVal, val, key) {
return val[1] <= refVal;
}
}
]
});
// create the nice dropdowns.
_.each(Summa.keys, function(item) {
enhanceSummaWidget(item, Summa.uniqueValues[item.key]);
});
callBack && callBack();
}
/* load the summaries. */
function loadSummaries(selected_id) {
$.ajax({
// should be http://rmap.cc/borinud/api/v1/dbajson/*/*/*/*/*/*/summaries
url: '/summaries',
dataType: 'json',
method: 'GET'
}).done(function(summaries) {
initSummaries(summaries, updateShownData);
});
}
$(document).ready(function() {
loadSummaries();
});
</script>
</head>
<body>
<div if="filters">
<div>
<div class="first">
<label>Ident</label>
<select id="ident_select">
<option value="">-- any --</option>
</select>
</div>
<div>
<label>Network</label>
<select id="network_select">
<option value="">-- any --</option>
</select>
</div>
</div>
<div>
<div class="first">
<label>Lon min</label>
<select id="lon_min_select">
<option value="">-- any --</option>
</select>
</div>
<div>
<label>Lon max</label>
<select id="lon_max_select">
<option value="">-- any --</option>
</select>
</div>
</div>
<div>
<div class="first">
<label>Lat min</label>
<select id="lat_min_select">
<option value="">-- any --</option>
</select>
</div>
<div>
<label>Lat max</label>
<select id="lat_max_select">
<option value="">-- any --</option>
</select>
</div>
</div>
<div>
<div class="first">
<label>Date min</label>
<select id="date_min_select">
<option value="">-- any --</option>
</select>
</div>
<div>
<label>Date max</label>
<select id="date_max_select">
<option value="">-- any --</option>
</select>
</div>
</div>
</div>
<div id="results">
Filtered data (<span id="items_count">0</span>):
</div>
</body>
</html>