swup 4.0.0-rc.31 → 4.0.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/dist/Swup.cjs +1 -1
- package/dist/Swup.cjs.map +1 -1
- package/dist/Swup.modern.js +1 -1
- package/dist/Swup.modern.js.map +1 -1
- package/dist/Swup.module.js +1 -1
- package/dist/Swup.module.js.map +1 -1
- package/dist/Swup.umd.js +1 -1
- package/dist/Swup.umd.js.map +1 -1
- package/dist/types/modules/Cache.d.ts +3 -3
- package/dist/types/modules/Classes.d.ts +6 -6
- package/dist/types/modules/Hooks.d.ts +11 -5
- package/package.json +1 -1
- package/src/modules/Cache.ts +10 -10
- package/src/modules/Classes.ts +9 -10
- package/src/modules/Hooks.ts +5 -5
- package/src/modules/__test__/replaceContent.test.ts +1 -1
package/src/modules/Cache.ts
CHANGED
|
@@ -9,10 +9,10 @@ export interface CacheData extends PageData {}
|
|
|
9
9
|
*/
|
|
10
10
|
export class Cache {
|
|
11
11
|
/** Swup instance this cache belongs to */
|
|
12
|
-
|
|
12
|
+
protected swup: Swup;
|
|
13
13
|
|
|
14
14
|
/** Cached pages, indexed by URL */
|
|
15
|
-
|
|
15
|
+
protected pages: Map<string, CacheData> = new Map();
|
|
16
16
|
|
|
17
17
|
constructor(swup: Swup) {
|
|
18
18
|
this.swup = swup;
|
|
@@ -29,17 +29,17 @@ export class Cache {
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
/** Check if the given URL has been cached. */
|
|
32
|
-
|
|
32
|
+
has(url: string): boolean {
|
|
33
33
|
return this.pages.has(this.resolve(url));
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
/** Return the cached page object if cached. */
|
|
37
|
-
|
|
37
|
+
get(url: string): CacheData | undefined {
|
|
38
38
|
return this.pages.get(this.resolve(url));
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/** Create a cache record for the specified URL. */
|
|
42
|
-
|
|
42
|
+
set(url: string, page: CacheData) {
|
|
43
43
|
url = this.resolve(url);
|
|
44
44
|
page = { ...page, url };
|
|
45
45
|
this.pages.set(url, page);
|
|
@@ -47,25 +47,25 @@ export class Cache {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/** Update a cache record, overwriting or adding custom data. */
|
|
50
|
-
|
|
50
|
+
update(url: string, page: CacheData) {
|
|
51
51
|
url = this.resolve(url);
|
|
52
52
|
page = { ...this.get(url), ...page, url };
|
|
53
53
|
this.pages.set(url, page);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/** Delete a cache record. */
|
|
57
|
-
|
|
57
|
+
delete(url: string): void {
|
|
58
58
|
this.pages.delete(this.resolve(url));
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/** Empty the cache. */
|
|
62
|
-
|
|
62
|
+
clear(): void {
|
|
63
63
|
this.pages.clear();
|
|
64
64
|
this.swup.hooks.callSync('cache:clear');
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/** Remove all cache entries that return true for a given predicate function. */
|
|
68
|
-
|
|
68
|
+
prune(predicate: (url: string, page: CacheData) => boolean): void {
|
|
69
69
|
this.pages.forEach((page, url) => {
|
|
70
70
|
if (predicate(url, page)) {
|
|
71
71
|
this.delete(url);
|
|
@@ -74,7 +74,7 @@ export class Cache {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
/** Resolve URLs by making them local and letting swup resolve them. */
|
|
77
|
-
|
|
77
|
+
protected resolve(urlToResolve: string): string {
|
|
78
78
|
const { url } = Location.fromUrl(urlToResolve);
|
|
79
79
|
return this.swup.resolveUrl(url);
|
|
80
80
|
}
|
package/src/modules/Classes.ts
CHANGED
|
@@ -2,15 +2,14 @@ import Swup from '../Swup.js';
|
|
|
2
2
|
import { queryAll } from '../utils.js';
|
|
3
3
|
|
|
4
4
|
export class Classes {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
swupClasses = ['to-', 'is-changing', 'is-rendering', 'is-popstate', 'is-animating'];
|
|
5
|
+
protected swup: Swup;
|
|
6
|
+
protected swupClasses = ['to-', 'is-changing', 'is-rendering', 'is-popstate', 'is-animating'];
|
|
8
7
|
|
|
9
8
|
constructor(swup: Swup) {
|
|
10
9
|
this.swup = swup;
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
get selectors(): string[] {
|
|
12
|
+
protected get selectors(): string[] {
|
|
14
13
|
const { scope } = this.swup.visit.animation;
|
|
15
14
|
if (scope === 'containers') return this.swup.visit.containers;
|
|
16
15
|
if (scope === 'html') return ['html'];
|
|
@@ -18,31 +17,31 @@ export class Classes {
|
|
|
18
17
|
return [];
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
get selector(): string {
|
|
20
|
+
protected get selector(): string {
|
|
22
21
|
return this.selectors.join(',');
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
get targets(): HTMLElement[] {
|
|
24
|
+
protected get targets(): HTMLElement[] {
|
|
26
25
|
if (!this.selector.trim()) return [];
|
|
27
26
|
return queryAll(this.selector) as HTMLElement[];
|
|
28
27
|
}
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
add(...classes: string[]): void {
|
|
31
30
|
this.targets.forEach((target) => target.classList.add(...classes));
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
|
|
33
|
+
remove(...classes: string[]): void {
|
|
35
34
|
this.targets.forEach((target) => target.classList.remove(...classes));
|
|
36
35
|
}
|
|
37
36
|
|
|
38
|
-
|
|
37
|
+
clear(): void {
|
|
39
38
|
this.targets.forEach((target) => {
|
|
40
39
|
const remove = target.className.split(' ').filter((c) => this.isSwupClass(c));
|
|
41
40
|
target.classList.remove(...remove);
|
|
42
41
|
});
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
|
|
44
|
+
protected isSwupClass(className: string): boolean {
|
|
46
45
|
return this.swupClasses.some((c) => className.startsWith(c));
|
|
47
46
|
}
|
|
48
47
|
}
|
package/src/modules/Hooks.ts
CHANGED
|
@@ -326,7 +326,7 @@ export class Hooks {
|
|
|
326
326
|
* @param registrations The registrations (handler + options) to execute
|
|
327
327
|
* @param args Arguments to pass to the handler
|
|
328
328
|
*/
|
|
329
|
-
|
|
329
|
+
protected async run<T extends HookName>(
|
|
330
330
|
registrations: HookRegistration<T>[],
|
|
331
331
|
args?: HookArguments<T>
|
|
332
332
|
): Promise<any> {
|
|
@@ -346,7 +346,7 @@ export class Hooks {
|
|
|
346
346
|
* @param registrations The registrations (handler + options) to execute
|
|
347
347
|
* @param args Arguments to pass to the handler
|
|
348
348
|
*/
|
|
349
|
-
|
|
349
|
+
protected runSync<T extends HookName>(
|
|
350
350
|
registrations: HookRegistration<T>[],
|
|
351
351
|
args?: HookArguments<T>
|
|
352
352
|
): any[] {
|
|
@@ -374,7 +374,7 @@ export class Hooks {
|
|
|
374
374
|
* @returns An object with the handlers sorted into `before` and `after` arrays,
|
|
375
375
|
* as well as a flag indicating if the original handler was replaced
|
|
376
376
|
*/
|
|
377
|
-
|
|
377
|
+
protected getHandlers<T extends HookName>(hook: T, defaultHandler?: Handler<T>) {
|
|
378
378
|
const ledger = this.get(hook);
|
|
379
379
|
if (!ledger) {
|
|
380
380
|
return { found: false, before: [], handler: [], after: [], replaced: false };
|
|
@@ -422,7 +422,7 @@ export class Hooks {
|
|
|
422
422
|
* @param b The other registration object to compare with
|
|
423
423
|
* @returns The sort direction
|
|
424
424
|
*/
|
|
425
|
-
|
|
425
|
+
protected sortRegistrations<T extends HookName>(
|
|
426
426
|
a: HookRegistration<T>,
|
|
427
427
|
b: HookRegistration<T>
|
|
428
428
|
): number {
|
|
@@ -435,7 +435,7 @@ export class Hooks {
|
|
|
435
435
|
* Dispatch a custom event on the `document` for a hook. Prefixed with `swup:`
|
|
436
436
|
* @param hook Name of the hook.
|
|
437
437
|
*/
|
|
438
|
-
|
|
438
|
+
protected dispatchDomEvent<T extends HookName>(hook: T, args?: HookArguments<T>): void {
|
|
439
439
|
const detail = { hook, args, visit: this.swup.visit };
|
|
440
440
|
document.dispatchEvent(new CustomEvent(`swup:${hook}`, { detail }));
|
|
441
441
|
}
|