-
Notifications
You must be signed in to change notification settings - Fork 148
Generic implementation for the editing of show orders via drag and drop #6243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * Sends a get request to the given endpoint. | ||
| * | ||
| * @author Marcel Werk | ||
| * @copyright 2001-2025 WoltLab GmbH | ||
| * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | ||
| * @since 6.2 | ||
| * @woltlabExcludeBundle tiny | ||
| */ | ||
|
|
||
| import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend"; | ||
| import { ApiResult, apiResultFromError, apiResultFromValue } from "./Result"; | ||
|
|
||
| export async function getObject<T = unknown>(endpoint: string): Promise<ApiResult<T>> { | ||
| let response: T; | ||
|
|
||
| try { | ||
| response = (await prepareRequest(endpoint).get().fetchAsJson()) as T; | ||
| } catch (e) { | ||
| return apiResultFromError(e); | ||
| } | ||
|
|
||
| return apiResultFromValue(response); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * Handles the change of the show order of elements. | ||
| * | ||
| * @author Marcel Werk | ||
| * @copyright 2001-2025 WoltLab GmbH | ||
| * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | ||
| * @since 6.2 | ||
| */ | ||
|
|
||
| import { getObject } from "../Api/GetObject"; | ||
| import { postObject } from "../Api/PostObject"; | ||
| import { promiseMutex } from "../Helper/PromiseMutex"; | ||
| import { getPhrase } from "../Language"; | ||
| import { dialogFactory } from "./Dialog"; | ||
| import Sortable from "sortablejs"; | ||
| import { showDefaultSuccessSnackbar } from "./Snackbar"; | ||
|
|
||
| type Item = { | ||
| id: number; | ||
| label: string; | ||
| }; | ||
|
|
||
| async function showDialog(endpoint: string): Promise<void> { | ||
| const items = await getItems(endpoint); | ||
|
|
||
| const dialog = dialogFactory().fromHtml(getHtml(items)).asPrompt(); | ||
| dialog.show(getPhrase("wcf.global.changeShowOrder")); | ||
|
|
||
| const sortable = new Sortable(dialog.content.querySelector(".sortableList")!, { | ||
| direction: "vertical", | ||
| animation: 150, | ||
| fallbackOnBody: true, | ||
| dataIdAttr: "data-object-id", | ||
| draggable: "li", | ||
| handle: ".sortableList__handle", | ||
| }); | ||
|
|
||
| dialog.addEventListener("primary", () => { | ||
| void saveItems(endpoint, sortable.toArray().map(Number)).then(() => { | ||
| showDefaultSuccessSnackbar().addEventListener("snackbar:close", () => { | ||
| window.location.reload(); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function getItems(endpoint: string): Promise<Item[]> { | ||
| return (await getObject<Item[]>(`${window.WSC_RPC_API_URL}${endpoint}`)).unwrap(); | ||
| } | ||
|
|
||
| async function saveItems(endpoint: string, values: number[]): Promise<void> { | ||
| await postObject(`${window.WSC_RPC_API_URL}${endpoint}`, { values }); | ||
| } | ||
|
|
||
| function getHtml(items: Item[]): string { | ||
| const list = document.createElement("ol"); | ||
| list.classList.add("sortableList"); | ||
|
|
||
| items.forEach((item) => { | ||
| const listItem = document.createElement("li"); | ||
| listItem.dataset.objectId = item.id.toString(); | ||
| listItem.textContent = item.label; | ||
|
|
||
| const icon = document.createElement("fa-icon"); | ||
| icon.setIcon("up-down"); | ||
| const handle = document.createElement("span"); | ||
| handle.append(icon); | ||
| handle.classList.add("sortableList__handle"); | ||
| listItem.prepend(handle); | ||
|
|
||
| list.append(listItem); | ||
| }); | ||
|
|
||
| return list.outerHTML; | ||
| } | ||
|
|
||
| export function setup(button: HTMLElement, endpoint: string): void { | ||
| button.addEventListener( | ||
| "click", | ||
| promiseMutex(() => showDialog(endpoint)), | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
wcfsetup/install/files/js/WoltLabSuite/Core/Api/GetObject.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
wcfsetup/install/files/js/WoltLabSuite/Core/Component/ChangeShowOrder.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...ll/files/lib/system/endpoint/controller/core/labels/groups/ChangeLabelShowOrder.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| namespace wcf\system\endpoint\controller\core\labels\groups; | ||
|
|
||
| use Laminas\Diactoros\Response\JsonResponse; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use wcf\data\label\group\LabelGroup; | ||
| use wcf\data\label\Label; | ||
| use wcf\data\label\LabelEditor; | ||
| use wcf\http\Helper; | ||
| use wcf\system\endpoint\IController; | ||
| use wcf\system\endpoint\PostRequest; | ||
| use wcf\system\label\LabelHandler; | ||
| use wcf\system\showOrder\ShowOrderHandler; | ||
| use wcf\system\showOrder\ShowOrderItem; | ||
| use wcf\system\WCF; | ||
|
|
||
| /** | ||
| * API endpoint for changing the show order of the labels in a label group. | ||
| * | ||
| * @author Marcel Werk | ||
| * @copyright 2001-2025 WoltLab GmbH | ||
| * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | ||
| * @since 6.2 | ||
| */ | ||
| #[PostRequest('/core/labels/groups/{id:\d+}/labels/show-order')] | ||
| final class ChangeLabelShowOrder implements IController | ||
| { | ||
| public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface | ||
| { | ||
| WCF::getSession()->checkPermissions(['admin.content.label.canManageLabel']); | ||
|
|
||
| $labelGroup = Helper::fetchObjectFromRequestParameter($variables['id'], LabelGroup::class); | ||
| $items = \array_map( | ||
| static fn(Label $label) => new ShowOrderItem($label->labelID, $label->getTitle()), | ||
| LabelHandler::getInstance()->getLabelGroup($labelGroup->groupID)->getLabels() | ||
| ); | ||
|
|
||
| $sortedItems = (new ShowOrderHandler($items))->getSortedItemsFromRequest($request); | ||
| $this->saveShowOrder($sortedItems); | ||
|
|
||
| return new JsonResponse([]); | ||
| } | ||
|
|
||
| /** | ||
| * @param list<ShowOrderItem> $items | ||
| */ | ||
| private function saveShowOrder(array $items): void | ||
| { | ||
| $sql = "UPDATE wcf1_label | ||
| SET showOrder = ? | ||
| WHERE labelID = ?"; | ||
| $statement = WCF::getDB()->prepare($sql); | ||
|
|
||
| WCF::getDB()->beginTransaction(); | ||
| for ($i = 0, $length = \count($items); $i < $length; $i++) { | ||
| $statement->execute([ | ||
| $i + 1, | ||
| $items[$i]->id, | ||
| ]); | ||
| } | ||
| WCF::getDB()->commitTransaction(); | ||
|
|
||
| LabelEditor::resetCache(); | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
...install/files/lib/system/endpoint/controller/core/labels/groups/ChangeShowOrder.class.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| namespace wcf\system\endpoint\controller\core\labels\groups; | ||
|
|
||
| use Laminas\Diactoros\Response\JsonResponse; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use wcf\data\label\group\ViewableLabelGroup; | ||
| use wcf\system\endpoint\IController; | ||
| use wcf\system\endpoint\PostRequest; | ||
| use wcf\system\label\LabelHandler; | ||
| use wcf\system\showOrder\ShowOrderHandler; | ||
| use wcf\system\showOrder\ShowOrderItem; | ||
| use wcf\system\WCF; | ||
|
|
||
| /** | ||
| * API endpoint for changing the show order of label groups. | ||
| * | ||
| * @author Marcel Werk | ||
| * @copyright 2001-2025 WoltLab GmbH | ||
| * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | ||
| * @since 6.2 | ||
| */ | ||
| #[PostRequest('/core/labels/groups/show-order')] | ||
| final class ChangeShowOrder implements IController | ||
| { | ||
| public function __invoke(ServerRequestInterface $request, array $variables): ResponseInterface | ||
| { | ||
| WCF::getSession()->checkPermissions(['admin.content.label.canManageLabel']); | ||
|
|
||
| $items = \array_map( | ||
| static fn(ViewableLabelGroup $labelGroup) => new ShowOrderItem($labelGroup->groupID, $labelGroup->getTitle()), | ||
| LabelHandler::getInstance()->getLabelGroups() | ||
| ); | ||
|
|
||
| $sortedItems = (new ShowOrderHandler($items))->getSortedItemsFromRequest($request); | ||
| $this->saveShowOrder($sortedItems); | ||
|
|
||
| return new JsonResponse([]); | ||
| } | ||
|
|
||
| /** | ||
| * @param list<ShowOrderItem> $items | ||
| */ | ||
| private function saveShowOrder(array $items): void | ||
|
BurntimeX marked this conversation as resolved.
|
||
| { | ||
| WCF::getDB()->beginTransaction(); | ||
| $sql = "UPDATE wcf1_label_group | ||
| SET showOrder = ? | ||
| WHERE groupID = ?"; | ||
| $statement = WCF::getDB()->prepare($sql); | ||
| for ($i = 0, $length = \count($items); $i < $length; $i++) { | ||
| $statement->execute([ | ||
| $i + 1, | ||
| $items[$i]->id, | ||
| ]); | ||
| } | ||
| WCF::getDB()->commitTransaction(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.