Document `Ui/Object/Action` as replacement for `WCF.Action.(Delete|Toggle)` (#137)
[GitHub/WoltLab/woltlab.github.io.git] / docs / migration / wsc53 / javascript.md
CommitLineData
e3747bbc 1# Migrating from WSC 5.3 - JavaScript
530a1037
MS
2
3## `WCF_CLICK_EVENT`
4
5For event listeners on click events, `WCF_CLICK_EVENT` is deprecated and should no longer be used.
6Instead, use `click` directly:
7
8```javascript
9// before
10element.addEventListener(WCF_CLICK_EVENT, this._click.bind(this));
11
12// after
13element.addEventListener('click', (ev) => this._click(ev));
14```
cae71ef1 15
64aae803
MS
16## `WCF.Action.Delete` and `WCF.Action.Toggle`
17
18`WCF.Action.Delete` and `WCF.Action.Toggle` were used for buttons to delete or enable/disable objects via JavaScript.
19In each template, `WCF.Action.Delete` or `WCF.Action.Toggle` instances had to be manually created for each object listing.
20
21With version 5.4 of WoltLab Suite, we have added a CSS selector-based global TypeScript module that only requires specific CSS classes to be added to the HTML structure for these buttons to work.
22Additionally, we have added a new `{objectAction}` template plugin, which generates these buttons reducing the amount of boilerplate template code.
23
24The required base HTML structure is as follows:
25
261. A `.jsObjectActionContainer` element with a `data-object-action-class-name` attribute that contains the name of PHP class that executes the actions.
272. `.jsObjectActionObject` elements within `.jsObjectActionContainer` that represent the objects for which actions can be executed.
28 Each `.jsObjectActionObject` element must have a `data-object-id` attribute with the id of the object.
293. `.jsObjectAction` elements within `.jsObjectActionObject` for each action with a `data-object-action` attribute with the name of the action.
30 These elements can be generated with the `{objectAction}` template plugin for the `delete` and `toggle` action.
31
32Example:
33
34```
35<table class="table jsObjectActionContainer" {*
36 *}data-object-action-class-name="wcf\data\foo\FooAction">
37 <thead>
38 <tr>
39 {* … *}
40 </tr>
41 </thead>
42
43 <tbody>
44 {foreach from=$objects item=foo}
45 <tr class="jsObjectActionObject" data-object-id="{@$foo->getObjectID()}">
46 <td class="columnIcon">
47 {objectAction action="toggle" isDisabled=$foo->isDisabled}
48 {objectAction action="delete" objectTitle=$foo->getTitle()}
49 {* … *}
50 </td>
51 {* … *}
52 </tr>
53 {/foreach}
54 </tbody>
55</table>
56```
57
58Please refer to the documentation in [`ObjectActionFunctionTemplatePlugin`](https://github.com/WoltLab/WCF/blob/master/wcfsetup/install/files/lib/system/template/plugin/ObjectActionFunctionTemplatePlugin.class.php) for details and examples on how to use this template plugin.
59
60The relevant TypeScript module registering the event listeners on the object action buttons is [`Ui/Object/Action`](https://github.com/WoltLab/WCF/blob/master/ts/WoltLabSuite/Core/Ui/Object/Action.ts).
61When an action button is clicked, an AJAX request is sent using the PHP class name and action name.
62After the successful execution of the action, the page is either reloaded if the action button has a `data-object-action-success="reload"` attribute or an event using the `EventHandler` module is fired using `WoltLabSuite/Core/Ui/Object/Action` as the identifier and the object action name.
63[`Ui/Object/Action/Delete`](https://github.com/WoltLab/WCF/blob/master/ts/WoltLabSuite/Core/Ui/Object/Action/Delete.ts) and [`Ui/Object/Action/Toggle`](https://github.com/WoltLab/WCF/blob/master/ts/WoltLabSuite/Core/Ui/Object/Action/Toggle.ts) listen to these events and update the user interface depending on the execute action by removing the object or updating the toggle button, respectively.
64
65Converting from `WCF.Action.*` to the new approach requires minimal changes per template, as shown in the relevant pull request [#4080](https://github.com/WoltLab/WCF/pull/4080).
66
67
cae71ef1
MS
68## `WCF.Table.EmptyTableHandler`
69
70When all objects in a table or list are deleted via their delete button or clipboard actions, an empty table or list can remain.
71Previously, `WCF.Table.EmptyTableHandler` had to be explicitly used in each template for these tables and lists to reload the page.
72As a TypeScript-based replacement for `WCF.Table.EmptyTableHandler` that is only initialized once globally, `WoltLabSuite/Core/Ui/Empty` was added.
73To use this new module, you only have to add the CSS class `jsReloadPageWhenEmpty` to the relevant HTML element.
74Once this HTML element no longer has child elements, the page is reloaded.
75To also cover scenarios in which there are fixed child elements that should not be considered when determining if there are no child elements, the `data-reload-page-when-empty="ignore"` can be set for these elements.
76
77Examples:
78
79```smarty
80<table class="table">
81 <thead>
82 <tr>
83 {* … *}
84 </tr>
85 </thead>
86
87 <tbody class="jsReloadPageWhenEmpty">
88 {foreach from=$objects item=object}
89 <tr>
90 {* … *}
91 </tr>
92 {/foreach}
93 </tbody>
94</table>
95```
96
97```smarty
98<div class="section tabularBox messageGroupList">
99 <ol class="tabularList jsReloadPageWhenEmpty">
100 <li class="tabularListRow tabularListRowHead" data-reload-page-when-empty="ignore">
101 {* … *}
102 </li>
103
104 {foreach from=$objects item=object}
105 <li>
106 {* … *}
107 </li>
108 {/foreach}
109 </ol>
110</div>
111```