Skip to content

Commit

Permalink
Delegate the sort field handling to Sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
dtdesign committed Jan 22, 2025
1 parent 5edba12 commit a77fee5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
20 changes: 5 additions & 15 deletions ts/WoltLabSuite/Core/Component/GridView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export class GridView {
this.#gridViewParameters = gridViewParameters;

this.#initPagination();
this.#initSorting();
this.#initInteractions();
this.#filter = this.#setupFilter(gridId);
this.#sorting = this.#setupSorting(sortField, sortOrder);
Expand All @@ -55,8 +54,6 @@ export class GridView {
});
}

#initSorting(): void {}

#switchPage(pageNo: number, updateQueryString: boolean = true): void {
this.#pagination.page = pageNo;
this.#pageNo = pageNo;
Expand Down Expand Up @@ -107,9 +104,9 @@ export class GridView {
if (this.#pageNo > 1) {
parameters.push(["pageNo", this.#pageNo.toString()]);
}
if (this.#sorting.getSortField()) {
parameters.push(["sortField", this.#sorting.getSortField()]);
parameters.push(["sortOrder", this.#sorting.getSortOrder()]);

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

this.#filter.getActiveFilters().forEach((value, key) => {
Expand Down Expand Up @@ -148,7 +145,6 @@ export class GridView {

#handlePopState(): void {
let pageNo = 1;
this.#sorting.resetSorting();
this.#filter.resetFilters();

const url = new URL(window.location.href);
Expand All @@ -158,20 +154,14 @@ export class GridView {
return;
}

if (key === "sortField") {
this.#sorting.setSortField(value);
}

if (key === "sortOrder") {
this.#sorting.setSortOrder(value);
}

const matches = key.match(/^filters\[([a-z0-9_]+)\]$/i);
if (matches) {
this.#filter.setFilter(matches[1], value);
}
});

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

this.#switchPage(pageNo, false);
}

Expand Down
27 changes: 19 additions & 8 deletions ts/WoltLabSuite/Core/Component/GridView/Sorting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,28 @@ export class Sorting extends EventTarget {
return this.#sortOrder;
}

resetSorting(): void {
this.#sortField = this.#defaultSortField;
this.#sortOrder = this.#defaultSortOrder;
}
getQueryParameters(): [string, string][] {
if (this.#sortField === "") {
return [];
}

setSortField(sortField: string): void {
this.#sortField = sortField;
return [
["sortField", this.#sortField],
["sortOrder", this.#sortOrder],
];
}

setSortOrder(sortOrder: string): void {
this.#sortOrder = sortOrder;
updateFromSearchParams(params: URLSearchParams): void {
this.#sortField = this.#defaultSortField;
this.#sortOrder = this.#defaultSortOrder;

params.forEach((value, key) => {
if (key === "sortField") {
this.#sortField = value;
} else if (key === "sortOrder") {
this.#sortOrder = value;
}
});
}

#sort(sortField: string): void {
Expand Down
15 changes: 3 additions & 12 deletions wcfsetup/install/files/js/WoltLabSuite/Core/Component/GridView.js

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

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

0 comments on commit a77fee5

Please sign in to comment.