-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathvirtual-select.js
134 lines (123 loc) · 3.38 KB
/
virtual-select.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
import $ from "jquery";
import "shiny";
import { updateLabel } from "../modules/utils";
import "virtual-select-plugin/dist/virtual-select.min.css";
import "virtual-select-plugin/dist/virtual-select.min.js";
import "tooltip-plugin/dist/tooltip.min.css";
import "tooltip-plugin/dist/tooltip.min.js";
function transpose(data) {
var res = [];
var key, i;
for (key in data) {
for (i = 0; i < data[key].length; i++) {
res[i] = res[i] || {};
if (data[key][i] !== undefined) res[i][key] = data[key][i];
}
}
return res;
}
function makeOptions(options) {
var newOptions;
if (options.type == "vector") {
newOptions = options.choices.map(x => {
return { label: x, value: x };
});
} else if (options.type == "transpose") {
newOptions = transpose(options.choices);
} else if (options.type == "transpose_group") {
var choices = options.choices;
for (var i = 0; i < choices.length; i++) {
choices[i].options = transpose(choices[i].options);
}
newOptions = choices;
} else {
newOptions = options.choices;
}
return newOptions;
}
var virtualSelectBinding = new Shiny.InputBinding();
$.extend(virtualSelectBinding, {
find: scope => {
return $(scope).find(".virtual-select");
},
getValue: el => {
return el.value;
},
setValue: (el, value) => {
el.setValue(value);
},
subscribe: (el, callback) => {
var updateOn = $(el).attr("data-update");
if (updateOn == "change") {
$(el).on("change.virtualSelectBinding", function(e) {
callback();
});
} else {
$(el).on("afterClose.virtualSelectBinding reset.virtualSelectBinding", function(e) {
callback();
});
}
},
unsubscribe: el => {
$(el).off(".virtualSelectBinding");
},
receiveMessage: (el, data) => {
if (data.hasOwnProperty("label")) {
var label = $("#" + el.id + "-label");
updateLabel(data.label, label);
}
if (data.hasOwnProperty("options")) {
var newOptions = makeOptions(data.options);
el.setOptions(newOptions);
}
if (data.hasOwnProperty("value")) {
el.setValue(data.value);
}
if (data.hasOwnProperty("disable")) {
if (data.disable) {
el.disable();
} else {
el.enable();
}
}
if (data.hasOwnProperty("disabledChoices")) {
el.setDisabledOptions(data.disabledChoices, true);
}
if (data.hasOwnProperty("open")) {
if (data.open) {
el.open();
} else {
el.close();
}
}
},
initialize: el => {
var data = el.querySelector('script[data-for="' + el.id + '"]');
data = JSON.parse(data.text);
var config = data.config;
config.options = makeOptions(data.options);
if (config.onServerSearch) {
config.onServerSearch = window[config.onServerSearch];
}
if (config.labelRenderer) {
config.labelRenderer = window[config.labelRenderer];
}
if (config.selectedLabelRenderer) {
config.selectedLabelRenderer = window[config.selectedLabelRenderer];
}
config.ele = el;
VirtualSelect.init(config);
if (data.stateInput) {
$(el).on("afterOpen", function(e) {
Shiny.setInputValue(el.id + "_open", true);
});
$(el).on("afterClose", function(e) {
Shiny.setInputValue(el.id + "_open", false);
});
}
}
});
Shiny.inputBindings.register(
virtualSelectBinding,
"shinyWidgets.virtualSelectBinding"
);