-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathshinyEvents.ts
255 lines (216 loc) · 5.98 KB
/
shinyEvents.ts
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
import type { InputBinding } from "../bindings/input/inputBinding";
import type { OutputBindingAdapter } from "../bindings/outputAdapter";
import type { EventPriority } from "../inputPolicies/inputPolicy";
import type { ErrorsMessageValue } from "../shiny/shinyapp";
import type { EvtFn } from "./jQueryEvents";
import $ from "jquery";
// This class implements a common interface for all Shiny events, and provides a
// layer of abstraction between the Shiny event and the underlying jQuery event
// object. We use a new class, rather than extending JQuery.Event, because
// jQuery.Event is an old function-style class. Each Event class has a
// corresponding ShinyEvent interface that describes the event object that is
// emitted. At the end of this file, we extend JQuery's `on()` method to
// associate the ShinyEvent interfaces with their corresponding event string.
class EventBase {
event: JQuery.Event;
constructor(type: string) {
this.event = $.Event(type);
}
triggerOn(
el: HTMLElement | JQuery<HTMLElement> | typeof document | null
): void {
$(el || window.document).trigger(this.event);
}
isDefaultPrevented(): boolean {
return this.event.isDefaultPrevented();
}
}
interface ShinyEventCommon extends JQuery.Event {
name: string;
value: any;
}
class EventCommon extends EventBase {
declare event: ShinyEventCommon;
constructor(
type: ShinyEventCommon["type"],
name: ShinyEventCommon["name"],
value: ShinyEventCommon["value"]
) {
super(type);
this.event.name = name;
this.event.value = value;
}
get name(): ShinyEventCommon["name"] {
return this.event.name;
}
get value(): ShinyEventCommon["value"] {
return this.event.value;
}
}
interface ShinyEventInputChanged extends ShinyEventCommon {
el: HTMLElement | null;
binding: InputBinding | null;
inputType: string;
priority?: EventPriority;
}
class EventInputChanged extends EventCommon {
declare event: ShinyEventInputChanged;
constructor({
name,
value,
el,
binding,
inputType,
priority,
}: {
name: ShinyEventInputChanged["name"];
value: ShinyEventInputChanged["value"];
el: ShinyEventInputChanged["el"];
binding: ShinyEventInputChanged["binding"];
inputType: ShinyEventInputChanged["inputType"];
priority?: ShinyEventInputChanged["priority"];
}) {
super("shiny:inputchanged", name, value);
this.event.el = el;
this.event.binding = binding;
this.event.inputType = inputType;
if (priority) {
this.event.priority = priority;
}
}
get el(): ShinyEventInputChanged["el"] {
return this.event.el;
}
get binding(): ShinyEventInputChanged["binding"] {
return this.event.binding;
}
get inputType(): ShinyEventInputChanged["inputType"] {
return this.event.inputType;
}
get priority(): ShinyEventInputChanged["priority"] {
return this.event.priority;
}
}
interface ShinyEventUpdateInput extends ShinyEventCommon {
message?: any;
binding: InputBinding;
}
class EventUpdateInput extends EventBase {
declare event: ShinyEventUpdateInput;
constructor({
message,
binding,
}: {
message?: ShinyEventUpdateInput["message"];
binding: ShinyEventUpdateInput["binding"];
}) {
super("shiny:updateinput");
if (message) {
this.event.message = message;
}
this.event.binding = binding;
}
get message(): ShinyEventUpdateInput["message"] {
return this.event.message;
}
get binding(): ShinyEventUpdateInput["binding"] {
return this.event.binding;
}
}
interface ShinyEventValue extends ShinyEventCommon {
binding: OutputBindingAdapter;
}
class EventValue extends EventCommon {
declare event: ShinyEventValue;
constructor({
name,
value,
binding,
}: {
name: ShinyEventValue["name"];
value: ShinyEventValue["value"];
binding: ShinyEventValue["binding"];
}) {
super("shiny:value", name, value);
this.event.binding = binding;
}
get binding(): ShinyEventValue["binding"] {
return this.event.binding;
}
}
interface ShinyEventError extends ShinyEventCommon {
binding: OutputBindingAdapter;
error: ErrorsMessageValue;
}
class EventError extends EventCommon {
declare event: ShinyEventError;
constructor({
name,
binding,
error,
}: {
name: ShinyEventError["name"];
binding: ShinyEventError["binding"];
error: ShinyEventError["error"];
}) {
super("shiny:error", name, null);
this.event.binding = binding;
this.event.error = error;
}
get binding(): ShinyEventError["binding"] {
return this.event.binding;
}
get error(): ShinyEventError["error"] {
return this.event.error;
}
}
interface ShinyEventMessage extends JQuery.Event {
message: { [key: string]: unknown };
}
class EventMessage extends EventBase {
declare event: ShinyEventMessage;
constructor(message: ShinyEventMessage["message"]) {
super("shiny:message");
this.event.message = message;
}
get message(): ShinyEventMessage["message"] {
return this.event.message;
}
}
// Augment the JQuery interface ----------------------------------------------
// This allows extensions to use .on() in Typescript with Shiny's custom events.
// E.g. in {bslib}, we can use the following with complete type information:
//
// ```
// $(document).on("shiny:value", function(event: ShinyEventValue) { })
// ```
declare global {
interface JQuery {
on(
events: "shiny:inputchanged",
handler: EvtFn<ShinyEventInputChanged>
): this;
on(
events: "shiny:updateinput",
handler: EvtFn<ShinyEventUpdateInput>
): this;
on(events: "shiny:value", handler: EvtFn<ShinyEventValue>): this;
on(events: "shiny:error", handler: EvtFn<ShinyEventError>): this;
on(events: "shiny:message", handler: EvtFn<ShinyEventMessage>): this;
}
}
export {
EventCommon,
EventInputChanged,
EventUpdateInput,
EventValue,
EventError,
EventMessage,
};
export type {
ShinyEventInputChanged,
ShinyEventUpdateInput,
ShinyEventValue,
ShinyEventError,
ShinyEventMessage,
};