|
| 1 | +/** |
| 2 | + * Handles the list of users. |
| 3 | + * |
| 4 | + * @author Olaf Braun |
| 5 | + * @copyright 2001-2024 WoltLab GmbH |
| 6 | + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 7 | + */ |
| 8 | + |
| 9 | +import * as EventHandler from "../../../Event/Handler"; |
| 10 | +import { ClipboardActionData } from "WoltLabSuite/Core/Controller/Clipboard/Data"; |
| 11 | +import { dboAction } from "WoltLabSuite/Core/Ajax"; |
| 12 | +import { show as showNotification } from "WoltLabSuite/Core/Ui/Notification"; |
| 13 | +import UiDropdownSimple from "WoltLabSuite/Core/Ui/Dropdown/Simple"; |
| 14 | +import BanHandler from "WoltLabSuite/Core/Acp/Ui/User/Action/Handler/Ban"; |
| 15 | +import SendNewPassword from "WoltLabSuite/Core/Acp/Ui/User/Action/Handler/SendNewPassword"; |
| 16 | +import { setup as setupClipboard, unmark as unmarkClipboard } from "WoltLabSuite/Core/Controller/Clipboard"; |
| 17 | +import AcpUiUserList from "WoltLabSuite/Core/Acp/Ui/User/Editor"; |
| 18 | +import { AcpUserContentRemoveClipboard } from "WoltLabSuite/Core/Acp/Ui/User/Content/Remove/Clipboard"; |
| 19 | + |
| 20 | +function getUserElements(userIDs: number[]): HTMLElement[] { |
| 21 | + return Array.from(document.querySelectorAll<HTMLElement>(".jsUserRow")).filter((userRow) => |
| 22 | + userIDs.includes(parseInt(userRow.dataset.objectId!)), |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +function getDropdownMenu(userRow: HTMLElement): HTMLElement { |
| 27 | + const userId = ~~userRow.dataset.objectId!; |
| 28 | + const dropdownId = `userListDropdown${userId}`; |
| 29 | + return UiDropdownSimple.getDropdownMenu(dropdownId)!; |
| 30 | +} |
| 31 | + |
| 32 | +function refresh(userIDs: number[]) { |
| 33 | + unmarkClipboard("com.woltlab.wcf.user", userIDs); |
| 34 | + |
| 35 | + showNotification(); |
| 36 | + |
| 37 | + EventHandler.fire("com.woltlab.wcf.acp.user", "refresh", { |
| 38 | + userIds: userIDs, |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +async function enableUsers(userIDs: number[]) { |
| 43 | + await dboAction("enable", "wcf\\data\\user\\UserAction").objectIds(userIDs).dispatch(); |
| 44 | + |
| 45 | + getUserElements(userIDs).forEach((userRow) => { |
| 46 | + userRow.dataset.enabled = "true"; |
| 47 | + const button = getDropdownMenu(userRow).querySelector<HTMLElement>(".jsEnable")!; |
| 48 | + button.textContent = button.dataset.disableMessage!; |
| 49 | + }); |
| 50 | + |
| 51 | + refresh(userIDs); |
| 52 | +} |
| 53 | + |
| 54 | +function banUsers(userIDs: number[]) { |
| 55 | + new BanHandler(userIDs).ban(() => { |
| 56 | + getUserElements(userIDs).forEach((userRow) => { |
| 57 | + userRow.dataset.banned = "true"; |
| 58 | + |
| 59 | + const button = getDropdownMenu(userRow).querySelector<HTMLElement>(".jsBan")!; |
| 60 | + button.textContent = button.dataset.unbanMessage!; |
| 61 | + |
| 62 | + refresh(userIDs); |
| 63 | + }); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +function sendNewPasswords(userIDs: number[]) { |
| 68 | + new SendNewPassword(userIDs, () => { |
| 69 | + refresh(userIDs); |
| 70 | + }).send(); |
| 71 | +} |
| 72 | + |
| 73 | +function setupUserClipboard(hasMarkedItems: boolean) { |
| 74 | + setupClipboard({ |
| 75 | + pageClassName: "wcf\\acp\\page\\UserListPage", |
| 76 | + hasMarkedItems: hasMarkedItems, |
| 77 | + }); |
| 78 | + |
| 79 | + EventHandler.add("com.woltlab.wcf.clipboard", "com.woltlab.wcf.user", (data: { data: ClipboardActionData }) => { |
| 80 | + switch (data.data.actionName) { |
| 81 | + case "com.woltlab.wcf.user.enable": |
| 82 | + void enableUsers(data.data.parameters.objectIDs); |
| 83 | + break; |
| 84 | + case "com.woltlab.wcf.user.ban": |
| 85 | + banUsers(data.data.parameters.objectIDs); |
| 86 | + break; |
| 87 | + case "com.woltlab.wcf.user.sendNewPassword": |
| 88 | + sendNewPasswords(data.data.parameters.objectIDs); |
| 89 | + break; |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + new AcpUserContentRemoveClipboard(); |
| 94 | +} |
| 95 | + |
| 96 | +export function setup(hasMarkedItems: boolean) { |
| 97 | + setupUserClipboard(hasMarkedItems); |
| 98 | + |
| 99 | + new AcpUiUserList(); |
| 100 | +} |
0 commit comments