-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathinput_checkboxgroup.js
52 lines (46 loc) · 1.25 KB
/
input_checkboxgroup.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
import * as input from "./input";
import { FilterHandle } from "./filter";
let $ = global.jQuery;
input.register({
className: "crosstalk-input-checkboxgroup",
factory: function(el, data) {
/*
* map: {"groupA": ["keyA", "keyB", ...], ...}
* group: "ct-groupname"
*/
let ctHandle = new FilterHandle(data.group);
let lastKnownKeys;
let $el = $(el);
function updateFilter() {
let checked = $el.find("input[type='checkbox']:checked");
if (checked.length === 0) {
lastKnownKeys = null;
ctHandle.clear();
} else {
let keys = {};
checked.each(function() {
data.map[this.value].forEach(function(key) {
keys[key] = true;
});
});
let keyArray = Object.keys(keys);
keyArray.sort();
lastKnownKeys = keyArray;
ctHandle.set(keyArray);
}
}
$el.on("change", "input[type='checkbox']", updateFilter);
// Update filter now in case this code happens to execute
// after widget(s) are done rendering
updateFilter();
return {
suspend: function() {
ctHandle.clear();
},
resume: function() {
if (lastKnownKeys)
ctHandle.set(lastKnownKeys);
}
};
}
});