Fix internal reference
[GitHub/WoltLab/woltlab.github.io.git] / docs / javascript_new-api_events.md
CommitLineData
e3747bbc 1# Event Handling - JavaScript API
e9e547a0
AE
2
3## `EventKey`
4
5This class offers a set of static methods that can be used to determine if some
6common keys are being pressed. Internally it compares either the `.key` property
7if it is supported or the value of `.which`.
8
9```js
10require(["EventKey"], function(EventKey) {
11 elBySel(".some-input").addEventListener("keydown", function(event) {
12 if (EventKey.Enter(event)) {
13 // the `Enter` key was pressed
14 }
15 });
16});
17```
18
19### `ArrowDown(event: KeyboardEvent): boolean`
20
21Returns true if the user has pressed the `↓` key.
22
23### `ArrowLeft(event: KeyboardEvent): boolean`
24
25Returns true if the user has pressed the `←` key.
26
27### `ArrowRight(event: KeyboardEvent): boolean`
28
29Returns true if the user has pressed the `→` key.
30
31### `ArrowUp(event: KeyboardEvent): boolean`
32
33Returns true if the user has pressed the `↑` key.
34
35### `Comma(event: KeyboardEvent): boolean`
36
37Returns true if the user has pressed the `,` key.
38
39### `Enter(event: KeyboardEvent): boolean`
40
41Returns true if the user has pressed the `↲` key.
42
43### `Escape(event: KeyboardEvent): boolean`
44
45Returns true if the user has pressed the `Esc` key.
46
47### `Tab(event: KeyboardEvent): boolean`
48
49Returns true if the user has pressed the `↹` key.
50
51## `EventHandler`
52
53A synchronous event system based on string identifiers rather than DOM elements,
54similar to the PHP event system in WoltLab Suite. Any components can listen to
55events or trigger events itself at any time.
56
57### Identifiying Events with the Developer Tools
58
59The Developer Tools in WoltLab Suite 3.1 offer an easy option to identify existing
60events that are fired while code is being executed. You can enable this watch
61mode through your browser's console using `Devtools.toggleEventLogging()`:
62
63```
64> Devtools.toggleEventLogging();
65< Event logging enabled
66< [Devtools.EventLogging] Firing event: bar @ com.example.app.foo
67< [Devtools.EventLogging] Firing event: baz @ com.example.app.foo
68```
69
70### `add(identifier: string, action: string, callback: (data: Object) => void): string`
71
72Adding an event listeners returns a randomly generated UUIDv4 that is used to
73identify the listener. This UUID is required to remove a specific listener through
74the `remove()` method.
75
76### `fire(identifier: string, action: string, data?: Object)`
77
78Triggers an event using an optional `data` object that is passed to each listener
79by reference.
80
81### `remove(identifier: string, action: string, uuid: string)`
82
83Removes a previously registered event listener using the UUID returned by `add()`.
84
85### `removeAll(identifier: string, action: string)`
86
87Removes all event listeners registered for the provided `identifier` and `action`.
88
89### `removeAllBySuffix(identifier: string, suffix: string)`
90
91Removes all event listeners for an `identifier` whose action ends with the value
92of `suffix`.