Skip to content

Commit b70c604

Browse files
authored
Merge pull request #6258 from WoltLab/6.2-pagination-behavior-attribute
Allow control of the behavior of the pagination
2 parents 59e3e61 + 2b87e16 commit b70c604

6 files changed

Lines changed: 92 additions & 8 deletions

File tree

com.woltlab.wcf/templates/shared_gridView.tpl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@
6868
{/if}
6969

7070
<div class="gridView__pagination">
71-
<woltlab-core-pagination id="{$view->getID()}_pagination" page="{$view->getPageNo()}" count="{$view->countPages()}"></woltlab-core-pagination>
71+
<woltlab-core-pagination
72+
id="{$view->getID()}_pagination"
73+
page="{$view->getPageNo()}"
74+
count="{$view->countPages()}"
75+
behavior="button"
76+
url="{$view->getBaseUrl()}"
77+
></woltlab-core-pagination>
7278
</div>
7379
</div>
7480

ts/WoltLabSuite/Core/Component/GridView/State.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export class State extends EventTarget {
6464
window.addEventListener("popstate", () => {
6565
this.#handlePopState();
6666
});
67+
68+
this.#updatePaginationUrl();
6769
}
6870

6971
getPageNo(): number {
@@ -89,6 +91,7 @@ export class State extends EventTarget {
8991
updateFromResponse(cause: StateChangeCause, count: number, filterLabels: ArrayLike<string>): void {
9092
this.#filter.setFilterLabels(filterLabels);
9193
this.#pagination.count = count;
94+
this.#updatePaginationUrl();
9295
this.#selection.refresh();
9396

9497
if (cause === StateChangeCause.Change || cause === StateChangeCause.Pagination) {
@@ -131,6 +134,30 @@ export class State extends EventTarget {
131134
window.history.pushState({}, document.title, url.toString());
132135
}
133136

137+
#updatePaginationUrl(): void {
138+
if (!this.#baseUrl) {
139+
return;
140+
}
141+
142+
const url = new URL(this.#baseUrl);
143+
144+
const parameters: [string, string][] = [];
145+
for (const parameter of this.#sorting.getQueryParameters()) {
146+
parameters.push(parameter);
147+
}
148+
149+
for (const parameter of this.#filter.getQueryParameters()) {
150+
parameters.push(parameter);
151+
}
152+
153+
if (parameters.length > 0) {
154+
url.search += url.search !== "" ? "&" : "?";
155+
url.search += new URLSearchParams(parameters).toString();
156+
}
157+
158+
this.#pagination.url = url.toString();
159+
}
160+
134161
#handlePopState(): void {
135162
let pageNo = 1;
136163

ts/WoltLabSuite/WebComponent/woltlab-core-pagination.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@
135135
if (url) {
136136
button = document.createElement("a");
137137
button.href = url;
138+
139+
if (this.behavior === "button") {
140+
button.addEventListener("click", (event) => {
141+
event.preventDefault();
142+
143+
if (this.page !== page) {
144+
this.#switchPage(page);
145+
}
146+
});
147+
}
138148
} else {
139149
button = document.createElement("button");
140150
button.type = "button";
@@ -241,6 +251,10 @@
241251

242252
getLinkUrl(page: number): string {
243253
if (!this.url) {
254+
if (this.behavior === "link") {
255+
throw new Error("The 'url' attribute is missing.");
256+
}
257+
244258
return "";
245259
}
246260

@@ -253,7 +267,7 @@
253267

254268
jumpToPage(page: number): void {
255269
const url = this.getLinkUrl(page);
256-
if (url) {
270+
if (url && this.behavior !== "button") {
257271
window.location.href = url;
258272
} else {
259273
this.#switchPage(page);
@@ -298,6 +312,21 @@
298312
this.setAttribute("url", url);
299313
this.#render();
300314
}
315+
316+
get behavior(): "auto" | "button" | "link" {
317+
const behavior = this.getAttribute("behavior");
318+
319+
if (behavior === "button" || behavior === "link") {
320+
return behavior;
321+
}
322+
323+
return "auto";
324+
}
325+
326+
set behavior(behavior: "auto" | "button" | "link") {
327+
this.setAttribute("behavior", behavior);
328+
this.#render();
329+
}
301330
}
302331

303332
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging

ts/global.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ declare global {
128128
set page(page: number);
129129
get url(): string;
130130
set url(url: string);
131+
get behavior(): "auto" | "button" | "link";
132+
set behavior(url: "auto" | "button" | "link");
131133
}
132134

133135
interface WoltlabCoreToggleButtonElement extends HTMLElement {

wcfsetup/install/files/js/WoltLabSuite/Core/Component/GridView/State.js

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

0 commit comments

Comments
 (0)