-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtoggle-buttons.js
47 lines (39 loc) · 1.18 KB
/
toggle-buttons.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
var toggleButtonsInputBinding = new Shiny.InputBinding();
$.extend(toggleButtonsInputBinding, {
find: function(scope) {
return $(scope).find(".btn-group.bslib-toggle-buttons");
},
getValue: function(el) {
var inputs = $(el).find("input.btn-check");
var vals = [];
inputs.each(function(i) {
if (this.checked) {
vals.push($(this).attr("data-value"));
}
});
return vals.length > 0 ? vals : null;
},
subscribe: function(el, callback) {
$(el).on(
'change.toggleButtonsInputBinding',
function(event) { callback(true); }
);
},
unsubscribe: function(el) {
$(el).off(".toggleButtonsInputBinding");
},
receiveMessage: function(el, data) {
if (data.hasOwnProperty("choices")) {
Shiny.renderContent(el, data.choices);
} else if (data.hasOwnProperty("selected")) {
const inputs = $(el).find("input");
inputs.each(function(i) {
const val = $(this).attr("data-value");
const checked = data.selected.indexOf(val) > -1;
this.checked = checked;
});
}
$(el).trigger("change.toggleButtonsInputBinding");
}
});
Shiny.inputBindings.register(toggleButtonsInputBinding);