-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathState.ts
More file actions
177 lines (142 loc) · 4.69 KB
/
State.ts
File metadata and controls
177 lines (142 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Handles the state of a grid view.
*
* @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 Filter from "./Filter";
import Selection from "./Selection";
import Sorting from "./Sorting";
export const enum StateChangeCause {
Change,
History,
Pagination,
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class State extends EventTarget {
readonly #baseUrl: string;
readonly #filter: Filter;
readonly #pagination: WoltlabCorePaginationElement;
readonly #selection: Selection;
readonly #sorting: Sorting;
#pageNo: number;
constructor(
gridId: string,
table: HTMLTableElement,
pageNo: number,
baseUrl: string,
sortField: string,
sortOrder: string,
) {
super();
this.#baseUrl = baseUrl;
this.#pageNo = pageNo;
this.#pagination = document.getElementById(`${gridId}_pagination`) as WoltlabCorePaginationElement;
this.#pagination.addEventListener("switchPage", (event: CustomEvent) => {
void this.#switchPage(event.detail, StateChangeCause.Pagination);
});
this.#filter = new Filter(gridId);
this.#filter.addEventListener("grid-view:change", () => {
this.#switchPage(1, StateChangeCause.Change);
});
this.#sorting = new Sorting(table, sortField, sortOrder);
this.#sorting.addEventListener("grid-view:change", () => {
this.#switchPage(1, StateChangeCause.Change);
});
this.#selection = new Selection(gridId, table);
this.#selection.addEventListener("grid-view:get-bulk-interactions", (event) => {
this.dispatchEvent(
new CustomEvent("grid-view:get-bulk-interactions", { detail: { objectIds: event.detail.objectIds } }),
);
});
window.addEventListener("popstate", () => {
this.#handlePopState();
});
}
getPageNo(): number {
return this.#pageNo;
}
getSortField(): string {
return this.#sorting.getSortField();
}
getSortOrder(): string {
return this.#sorting.getSortOrder();
}
getActiveFilters(): Map<string, string> {
return this.#filter.getActiveFilters();
}
getSelectedIds(): number[] {
return this.#selection.getSelectedIds();
}
updateFromResponse(cause: StateChangeCause, count: number, filterLabels: ArrayLike<string>): void {
this.#filter.setFilterLabels(filterLabels);
this.#pagination.count = count;
this.#selection.refresh();
if (cause === StateChangeCause.Change || cause === StateChangeCause.Pagination) {
this.#updateQueryString();
}
}
#switchPage(pageNo: number, source: StateChangeCause): void {
this.#pagination.page = pageNo;
this.#pageNo = pageNo;
this.dispatchEvent(new CustomEvent("grid-view:change", { detail: { source } }));
}
#updateQueryString(): void {
if (!this.#baseUrl) {
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();
}
window.history.pushState({}, document.title, url.toString());
}
#handlePopState(): void {
let pageNo = 1;
const { searchParams } = new URL(window.location.href);
const value = 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.#switchPage(pageNo, StateChangeCause.History);
}
setBulkInteractionContextMenuOptions(options: string): void {
this.#selection.setBulkInteractionContextMenuOptions(options);
}
resetSelection(): void {
this.#selection.resetSelection();
}
}
interface StateEventMap {
"grid-view:change": CustomEvent<{ source: StateChangeCause }>;
"grid-view:get-bulk-interactions": CustomEvent<{ objectIds: number[] }>;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export interface State extends EventTarget {
addEventListener: {
<T extends keyof StateEventMap>(
type: T,
listener: (this: State, ev: StateEventMap[T]) => any,
options?: boolean | AddEventListenerOptions,
): void;
} & HTMLElement["addEventListener"];
}
export default State;