@salesforcedevs/docs-components 1.3.170 → 1.3.171-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
export interface SearchSyncerConstructorArgs {
|
|
2
|
-
callbacks: {
|
|
3
|
-
onSearchChange?: (nextSearchString: string) => unknown;
|
|
4
|
-
onUrlChange?: (nextSearchString: string) => unknown;
|
|
5
|
-
};
|
|
6
|
-
eventName: string;
|
|
7
|
-
historyMethod:
|
|
8
|
-
| typeof window.history.pushState
|
|
9
|
-
| typeof window.history.replaceState;
|
|
10
|
-
searchParam: string;
|
|
11
|
-
shouldStopPropagation?: boolean;
|
|
12
|
-
target: EventTarget | undefined;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class SearchSyncer {
|
|
16
|
-
private callbacks: SearchSyncerConstructorArgs["callbacks"];
|
|
17
|
-
private eventName: SearchSyncerConstructorArgs["eventName"];
|
|
18
|
-
private historyMethod: SearchSyncerConstructorArgs["historyMethod"];
|
|
19
|
-
private searchParam: SearchSyncerConstructorArgs["searchParam"];
|
|
20
|
-
private shouldStopPropagation: SearchSyncerConstructorArgs["shouldStopPropagation"];
|
|
21
|
-
private target: SearchSyncerConstructorArgs["target"];
|
|
22
|
-
|
|
23
|
-
constructor({
|
|
24
|
-
callbacks = {},
|
|
25
|
-
eventName,
|
|
26
|
-
historyMethod = window.history.pushState,
|
|
27
|
-
searchParam,
|
|
28
|
-
shouldStopPropagation = true,
|
|
29
|
-
target
|
|
30
|
-
}: SearchSyncerConstructorArgs) {
|
|
31
|
-
this.callbacks = callbacks;
|
|
32
|
-
this.eventName = eventName;
|
|
33
|
-
this.historyMethod = historyMethod.bind(window.history);
|
|
34
|
-
this.searchParam = searchParam;
|
|
35
|
-
this.shouldStopPropagation = shouldStopPropagation;
|
|
36
|
-
this.target = target;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public init = (): void => {
|
|
40
|
-
this.target!.addEventListener(this.eventName, this.handleSearchChange);
|
|
41
|
-
this.target!.addEventListener("popstate", this.handlePopState);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
public dispose = (): void => {
|
|
45
|
-
this.target!.removeEventListener(
|
|
46
|
-
this.eventName,
|
|
47
|
-
this.handleSearchChange
|
|
48
|
-
);
|
|
49
|
-
this.target!.removeEventListener("popstate", this.handlePopState);
|
|
50
|
-
this.target = undefined;
|
|
51
|
-
this.callbacks.onSearchChange = undefined;
|
|
52
|
-
this.callbacks.onUrlChange = undefined;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
private handleSearchChange = (event: Event): void => {
|
|
56
|
-
if (this.shouldStopPropagation) {
|
|
57
|
-
event.stopPropagation();
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const { detail: searchTerm } = event as CustomEvent<string>;
|
|
61
|
-
const fullUrl = new URL(window.location.href);
|
|
62
|
-
const { searchParams } = fullUrl;
|
|
63
|
-
|
|
64
|
-
if (searchTerm) {
|
|
65
|
-
searchParams.set(this.searchParam, searchTerm);
|
|
66
|
-
} else {
|
|
67
|
-
searchParams.delete(this.searchParam);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const nextSearchString = searchParams.toString();
|
|
71
|
-
|
|
72
|
-
if (this.callbacks.onSearchChange) {
|
|
73
|
-
this.callbacks.onSearchChange(nextSearchString);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
fullUrl.search = nextSearchString;
|
|
77
|
-
this.historyMethod({}, "", fullUrl.toString());
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
private handlePopState = (): void => {
|
|
81
|
-
if (this.callbacks.onUrlChange) {
|
|
82
|
-
this.callbacks.onUrlChange(window.location.search);
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
}
|