Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 36 additions & 25 deletions ts/WoltLabSuite/Core/Component/GridView/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class State extends EventTarget {
readonly #selection: Selection;
readonly #sorting: Sorting;
readonly #gridViewFooter: HTMLElement;
#lastUrl: string;
#pageNo: number;

constructor(
Expand Down Expand Up @@ -75,6 +76,7 @@ export class State extends EventTarget {
});
}

this.#updateLastUrl();
this.#updatePaginationUrl();
this.#updateGridViewFooter();
}
Expand Down Expand Up @@ -104,6 +106,7 @@ export class State extends EventTarget {
this.#pagination.count = count;
this.#updatePaginationUrl();
this.#selection.refresh();
this.#updateLastUrl();

if (cause === StateChangeCause.Change || cause === StateChangeCause.Pagination) {
this.#updateQueryString();
Expand All @@ -124,26 +127,7 @@ export class State extends EventTarget {
return;
}

const url = new URL(this.#baseUrl);

const parameters: [string, string][] = [];
if (this.#pageNo > 1) {
parameters.push(["pageNo", this.#pageNo.toString()]);
}

for (const parameter of this.#sorting.getQueryParameters()) {
parameters.push(parameter);
}

for (const parameter of this.#filter.getQueryParameters()) {
parameters.push(parameter);
}

if (parameters.length > 0) {
url.search += url.search !== "" ? "&" : "?";
url.search += new URLSearchParams(parameters).toString();
}

const url = this.#getUrl();
window.history.pushState({}, document.title, url.toString());
}

Expand All @@ -152,9 +136,18 @@ export class State extends EventTarget {
return;
}

const url = this.#getUrl(false);
this.#pagination.url = url.toString();
}

#getUrl(withPageNo: boolean = true): URL {
const url = new URL(this.#baseUrl);

const parameters: [string, string][] = [];
if (withPageNo && this.#pageNo > 1) {
parameters.push(["pageNo", this.#pageNo.toString()]);
}

for (const parameter of this.#sorting.getQueryParameters()) {
parameters.push(parameter);
}
Expand All @@ -168,27 +161,45 @@ export class State extends EventTarget {
url.search += new URLSearchParams(parameters).toString();
}

this.#pagination.url = url.toString();
return url;
}

#handlePopState(): void {
const url = new URL(window.location.href);

if (this.#isOnlyHashChange(url)) {
return;
}

let pageNo = 1;

const { searchParams } = new URL(window.location.href);
const value = searchParams.get("pageNo");
const value = url.searchParams.get("pageNo");
if (value !== null) {
pageNo = parseInt(value);
if (Number.isNaN(pageNo) || pageNo < 1) {
pageNo = 1;
}
}

this.#filter.updateFromSearchParams(searchParams);
this.#sorting.updateFromSearchParams(searchParams);
this.#filter.updateFromSearchParams(url.searchParams);
this.#sorting.updateFromSearchParams(url.searchParams);

this.#switchPage(pageNo, StateChangeCause.History);
}

#updateLastUrl(): void {
if (!this.#baseUrl) {
return;
}

const url = this.#getUrl();
this.#lastUrl = url.pathname + url.search;
}

#isOnlyHashChange(url: URL): boolean {
return url.pathname + url.search === this.#lastUrl;
}

#updateGridViewFooter(): void {
const hasPagination = this.#pagination.count > 1;
this.#gridViewFooter.hidden = !hasPagination && !this.#selection.selectionBarVisible();
Expand Down
61 changes: 36 additions & 25 deletions ts/WoltLabSuite/Core/Component/ListView/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export class State extends EventTarget {
readonly #selection: Selection;
readonly #sorting: Sorting;
readonly #listViewFooter: HTMLElement;
#lastUrl: string;
#pageNo: number;

constructor(
Expand Down Expand Up @@ -96,6 +97,7 @@ export class State extends EventTarget {
);
});

this.#updateLastUrl();
this.#updatePaginationUrl();
this.#updateListViewFooter();
}
Expand Down Expand Up @@ -125,6 +127,7 @@ export class State extends EventTarget {
this.#pagination.count = count;
this.#updatePaginationUrl();
this.#selection.refresh();
this.#updateLastUrl();

if (cause === StateChangeCause.Change || cause === StateChangeCause.Pagination) {
this.#updateQueryString();
Expand All @@ -145,26 +148,7 @@ export class State extends EventTarget {
return;
}

const url = new URL(this.#baseUrl);

const parameters: [string, string][] = [];
if (this.#pageNo > 1) {
parameters.push(["pageNo", this.#pageNo.toString()]);
}

for (const parameter of this.#sorting.getQueryParameters()) {
parameters.push(parameter);
}

for (const parameter of this.#filter.getQueryParameters()) {
parameters.push(parameter);
}

if (parameters.length > 0) {
url.search += url.search !== "" ? "&" : "?";
url.search += new URLSearchParams(parameters).toString();
}

const url = this.#getUrl();
window.history.pushState({}, document.title, url.toString());
}

Expand All @@ -173,9 +157,18 @@ export class State extends EventTarget {
return;
}

const url = this.#getUrl(false);
this.#pagination.url = url.toString();
}

#getUrl(withPageNo: boolean = true): URL {
const url = new URL(this.#baseUrl);

const parameters: [string, string][] = [];
if (withPageNo && this.#pageNo > 1) {
parameters.push(["pageNo", this.#pageNo.toString()]);
}

for (const parameter of this.#sorting.getQueryParameters()) {
parameters.push(parameter);
}
Expand All @@ -189,27 +182,45 @@ export class State extends EventTarget {
url.search += new URLSearchParams(parameters).toString();
}

this.#pagination.url = url.toString();
return url;
}

#handlePopState(): void {
const url = new URL(window.location.href);

if (this.#isOnlyHashChange(url)) {
return;
}

let pageNo = 1;

const { searchParams } = new URL(window.location.href);
const value = searchParams.get("pageNo");
const value = url.searchParams.get("pageNo");
if (value !== null) {
pageNo = parseInt(value);
if (Number.isNaN(pageNo) || pageNo < 1) {
pageNo = 1;
}
}

this.#filter.updateFromSearchParams(searchParams);
this.#sorting.updateFromSearchParams(searchParams);
this.#filter.updateFromSearchParams(url.searchParams);
this.#sorting.updateFromSearchParams(url.searchParams);

this.#switchPage(pageNo, StateChangeCause.History);
}

#updateLastUrl(): void {
if (!this.#baseUrl) {
return;
}

const url = this.#getUrl();
this.#lastUrl = url.pathname + url.search;
}

#isOnlyHashChange(url: URL): boolean {
return url.pathname + url.search === this.#lastUrl;
}

#updateListViewFooter(): void {
const hasPagination = this.#pagination.count > 1;
this.#listViewFooter.hidden = !hasPagination && !this.#selection.selectionBarVisible();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading