Skip to content

Commit 81f2451

Browse files
committed
Use dboAction instead of apiOnce
Move callback functions into a separate methode
1 parent eac2b52 commit 81f2451

File tree

2 files changed

+124
-132
lines changed
  • ts/WoltLabSuite/Core/Ui/Sortable
  • wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Sortable

2 files changed

+124
-132
lines changed

ts/WoltLabSuite/Core/Ui/Sortable/List.ts

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import * as Core from "../../Core";
1111
import Sortable from "sortablejs";
12-
import { apiOnce } from "WoltLabSuite/Core/Ajax";
12+
import { dboAction } from "WoltLabSuite/Core/Ajax";
1313
import { show as showNotification } from "WoltLabSuite/Core/Ui/Notification";
1414
import { getPhrase } from "WoltLabSuite/Core/Language";
1515

@@ -66,64 +66,9 @@ class UiSortableList {
6666
chosenClass: "sortablePlaceholder",
6767
ghostClass: "",
6868
draggable: "li",
69-
filter: (event: Event | TouchEvent, target: HTMLElement) => {
70-
if (Sortable.utils.is(target, ".sortableNoSorting")) {
71-
return true;
72-
}
73-
74-
const eventTarget = event.target as HTMLElement;
75-
if (eventTarget === target) {
76-
return false;
77-
}
78-
if (eventTarget.parentElement === target) {
79-
return false;
80-
}
81-
if (!this._options.toleranceElement) {
82-
return true;
83-
}
84-
85-
return !Sortable.utils.is(eventTarget, this._options.toleranceElement);
86-
},
87-
onMove: (event: Sortable.MoveEvent) => {
88-
if (this._options.maxNestingLevel === undefined) {
89-
return true;
90-
}
91-
92-
const closest = Sortable.utils.closest(event.to, ".sortableNode");
93-
if (!closest) {
94-
// Top level
95-
return true;
96-
}
97-
98-
if (closest && Sortable.utils.is(closest, ".sortableNoNesting")) {
99-
return false;
100-
}
101-
102-
const levelOfDraggedNode = Math.max(
103-
...Array.from(event.dragged.querySelectorAll(".sortableList")).map((list: HTMLElement) => {
104-
return getNestingLevel(list, event.dragged);
105-
}),
106-
);
107-
108-
if (getNestingLevel(event.to) + levelOfDraggedNode > this._options.maxNestingLevel) {
109-
return false;
110-
}
111-
112-
return true;
113-
},
114-
onEnd: (event: Sortable.SortableEvent) => {
115-
if (this._options.maxNestingLevel === undefined) {
116-
return;
117-
}
118-
119-
event.to.querySelectorAll(".sortableNode").forEach((node: HTMLElement) => {
120-
if (getNestingLevel(node) > this._options.maxNestingLevel!) {
121-
node.classList.add("sortableNoNesting");
122-
} else {
123-
node.classList.remove("sortableNoNesting");
124-
}
125-
});
126-
},
69+
filter: this.#filter.bind(this),
70+
onMove: this.#onMove.bind(this),
71+
onEnd: this.#onEnd.bind(this),
12772
} as Sortable.Options,
12873
isSimpleSorting: false,
12974
additionalParameters: {},
@@ -173,12 +118,12 @@ class UiSortableList {
173118
}
174119

175120
formSubmit.querySelector('button[data-type="submit"]')?.addEventListener("click", () => {
176-
this.save();
121+
void this.save();
177122
});
178123
}
179124
}
180125

181-
public save(): void {
126+
public async save() {
182127
if (!this._options.className) {
183128
return;
184129
}
@@ -195,20 +140,73 @@ class UiSortableList {
195140
},
196141
},
197142
this._options.additionalParameters,
143+
) as Record<string, unknown>;
144+
145+
await dboAction("updatePosition", this._options.className).payload(parameters).dispatch();
146+
147+
showNotification(getPhrase("wcf.global.success.edit"));
148+
}
149+
150+
#onMove(event: Sortable.MoveEvent) {
151+
if (this._options.maxNestingLevel === undefined) {
152+
return true;
153+
}
154+
155+
const closest = Sortable.utils.closest(event.to, ".sortableNode");
156+
if (!closest) {
157+
// Top level
158+
return true;
159+
}
160+
161+
if (closest && Sortable.utils.is(closest, ".sortableNoNesting")) {
162+
return false;
163+
}
164+
165+
const levelOfDraggedNode = Math.max(
166+
...Array.from(event.dragged.querySelectorAll(".sortableList")).map((list: HTMLElement) => {
167+
return getNestingLevel(list, event.dragged);
168+
}),
198169
);
199170

200-
apiOnce({
201-
data: {
202-
actionName: "updatePosition",
203-
className: this._options.className,
204-
interfaceName: "wcf\\data\\ISortableAction",
205-
parameters: parameters,
206-
},
207-
success: () => {
208-
showNotification(getPhrase("wcf.global.success.edit"));
209-
},
171+
if (getNestingLevel(event.to) + levelOfDraggedNode > this._options.maxNestingLevel) {
172+
return false;
173+
}
174+
175+
return true;
176+
}
177+
178+
#onEnd(event: Sortable.SortableEvent) {
179+
if (this._options.maxNestingLevel === undefined) {
180+
return;
181+
}
182+
183+
event.to.querySelectorAll(".sortableNode").forEach((node: HTMLElement) => {
184+
if (getNestingLevel(node) > this._options.maxNestingLevel!) {
185+
node.classList.add("sortableNoNesting");
186+
} else {
187+
node.classList.remove("sortableNoNesting");
188+
}
210189
});
211190
}
191+
192+
#filter(event: Event | TouchEvent, target: HTMLElement) {
193+
if (Sortable.utils.is(target, ".sortableNoSorting")) {
194+
return true;
195+
}
196+
197+
const eventTarget = event.target as HTMLElement;
198+
if (eventTarget === target) {
199+
return false;
200+
}
201+
if (eventTarget.parentElement === target) {
202+
return false;
203+
}
204+
if (!this._options.toleranceElement) {
205+
return true;
206+
}
207+
208+
return !Sortable.utils.is(eventTarget, this._options.toleranceElement);
209+
}
212210
}
213211

214212
export = UiSortableList;

wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Sortable/List.js

Lines changed: 55 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)