-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathinput.js
More file actions
166 lines (159 loc) · 6.02 KB
/
input.js
File metadata and controls
166 lines (159 loc) · 6.02 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
/*
* This default receiveMessage implementation expects data to contain whole
* configuration and value properties. If either is present, it will be set and
* the component will be re-rendered. Because receiveMessage is typically used
* by input authors to perform incremental updates, this default implementation
* can be overriden by the user with the receiveMessage arguments to
* reactShinyInput.
*/
function defaultReceiveMessage(el, { configuration, value }) {
let dirty = false;
if (configuration !== undefined) {
this.setInputConfiguration(el, configuration);
dirty = true;
}
if (value !== undefined) {
this.setInputValue(el, value);
dirty = true;
}
if (dirty) {
this.getCallback(el)();
this.render(el);
}
}
const defaultOptions = {
receiveMessage: defaultReceiveMessage,
type: false,
ratePolicy: null
};
/**
* Installs a new Shiny input binding based on a React component.
*
* @param {string} selector - jQuery selector that should identify the set of
* container elements within the scope argument of Shiny.InputBinding.find.
* @param {string} name - A name such as 'acme.FooInput' that should uniquely
* identify the component.
* @param {Object} component - React Component, either class or function.
* @param {Object} options - Additional configuration options. Supported
* options are:
* - receiveMessage: Implementation of Shiny.InputBinding to use in place of
* the default. Typically overridden as an optimization to perform
* incremental value updates.
* - type: `false`, a string, or a function.
* - `false` (the default): denotes that the value produced by this input
* should not be intercepted by any handlers registered in R on the
* server using shiny::registerInputHandler().
* - string: denotes the input's *type* and should correspond to the
* type parameter of shiny::registerInputHandler().
* - function: A function called with `this` bound to the InputBinding
* instance and passed a single argument, the input's containing DOM
* element. The function should return either `false` or a string
* corresponding to the type parameter of shiny::registerInputHandler().
* - ratePolicy: A rate policy object as defined in the documentation for
* getRatePolicy(): https://shiny.rstudio.com/articles/building-inputs.html
* A rate policy object has two members:
* - `policy`: Valid values are the strings "direct", "debounce", and
* "throttle". "direct" means that all events are sent immediately.
* - `delay`: Number indicating the number of milliseconds that should be
* used when debouncing or throttling. Has no effect if the policy is
* direct.
* The specified rate policy is only applied when `true` is passed as the
* second argument to the `setValue` function passed as a prop to the
* input component.
*
*/
export function reactShinyInput(selector,
name,
component,
options) {
options = Object.assign({}, defaultOptions, options);
Shiny.inputBindings.register(new class extends Shiny.InputBinding {
/*
* Methods override those in Shiny.InputBinding
*/
find(scope) {
return $(scope).find(selector);
}
getValue(el) {
return this.getInputValue(el);
}
setValue(el, value, rateLimited = false) {
/*
* We have to check whether $(el).data('callback') is undefined here
* in case shiny::renderUI() is involved. If an input is contained in a
* shiny::uiOutput(), the following strange thing happens occasionally:
*
* 1. setValue() is bound to an el in this.render(), below
* 2. An event that will call setValue() is enqueued
* 3. While the event is still enqueued, el is unbound and removed
* from the DOM by the JS code associated with shiny::uiOutput()
* - That code uses jQuery .html() in output_binding_html.js
* - .html() removes el from the DOM and clears ist data and events
* 4. By the time the setValue() bound to the original el is invoked,
* el has been unbound and its data cleared.
*
* Since the original input is gone along with its callback, it
* seems to make the most sense to do nothing.
*/
if ($(el).data('callback') !== undefined) {
this.setInputValue(el, value);
this.getCallback(el)(rateLimited);
this.render(el);
}
}
initialize(el) {
$(el).data('value', JSON.parse($(el).next().text()));
$(el).data('configuration', JSON.parse($(el).next().next().text()));
}
subscribe(el, callback) {
$(el).data('callback', callback);
this.render(el);
}
unsubscribe(el) {
ReactDOM.render(null, el);
}
receiveMessage(el, data) {
options.receiveMessage.call(this, el, data);
}
getType(el) {
if (typeof options.type === 'function') {
return options.type.call(this, el);
} else if (options.type === false || typeof options.type === 'string') {
return options.type;
} else {
throw new Error('options.type must be false, a string, or a function');
}
}
getRatePolicy() {
return options.ratePolicy;
}
/*
* Methods not present in Shiny.InputBinding but accessible to users
* through `this` in receiveMessage
*/
getInputValue(el) {
return $(el).data('value');
}
setInputValue(el, value) {
$(el).data('value', value);
}
getInputConfiguration(el) {
return $(el).data('configuration');
}
setInputConfiguration(el, configuration) {
$(el).data('configuration', configuration);
}
getCallback(el) {
return $(el).data('callback');
}
render(el) {
const element = React.createElement(component, {
configuration: this.getInputConfiguration(el),
value: this.getValue(el),
setValue: this.setValue.bind(this, el),
el: el
});
ReactDOM.render(element, el);
}
}, name);
}