@base-framework/base 3.9.17 → 3.9.19

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.
@@ -20,7 +20,9 @@ export class Router {
20
20
  lastPath: any;
21
21
  path: any;
22
22
  /**
23
- * Cache for last matched route - provides 70-90% speedup on repeated navigation.
23
+ * The last route that matched a path. This is kept for
24
+ * backwards compatibility only: every route is checked on
25
+ * each navigation so stale routes are deactivated.
24
26
  * @type {object|null} lastMatchedRoute
25
27
  */
26
28
  lastMatchedRoute: object | null;
@@ -47,6 +49,30 @@ export class Router {
47
49
  * @type {number}
48
50
  */
49
51
  scrollWaitToken: number;
52
+ /**
53
+ * Removes the user interaction listeners of a pending
54
+ * history scroll restore.
55
+ * @type {function|null}
56
+ */
57
+ cancelScrollWait: Function | null;
58
+ /**
59
+ * The default keep-alive setting for routes that do not set
60
+ * `keepAlive` themselves. Keep-alive route pages are detached
61
+ * and cached instead of destroyed when the route deactivates.
62
+ * @type {boolean} keepAlive
63
+ */
64
+ keepAlive: boolean;
65
+ /**
66
+ * The max detached keep-alive pages kept across the router.
67
+ * The least recently used page is destroyed past it.
68
+ * @type {number} keepAliveMax
69
+ */
70
+ keepAliveMax: number;
71
+ /**
72
+ * This will track the detached keep-alive pages.
73
+ * @type {PageCache} pageCache
74
+ */
75
+ pageCache: PageCache;
50
76
  /**
51
77
  * This will be used to access our history object.
52
78
  */
@@ -82,6 +108,22 @@ export class Router {
82
108
  * @returns {object}
83
109
  */
84
110
  protected createRoute(settings: object): object;
111
+ /**
112
+ * This will set the default keep-alive setting for routes that
113
+ * do not set `keepAlive` themselves.
114
+ *
115
+ * @param {boolean} enabled
116
+ * @param {object} [options]
117
+ * @param {number} [options.max] The max detached pages kept across
118
+ * the router (default 5). The least recently used is destroyed.
119
+ * @returns {this} a reference to the router object.
120
+ *
121
+ * @example
122
+ * router.setKeepAlive(true, { max: 5 });
123
+ */
124
+ setKeepAlive(enabled: boolean, options?: {
125
+ max?: number;
126
+ }): this;
85
127
  /**
86
128
  * This will add a new route to the router.
87
129
  *
@@ -216,10 +258,26 @@ export class Router {
216
258
  /**
217
259
  * This will check if a link was routed.
218
260
  *
261
+ * Only plain primary clicks on same-origin links that open in the
262
+ * current browsing context are routed. Everything else is left to
263
+ * the browser (new tabs / windows, downloads, mailto:, tel:, other
264
+ * origins, or clicks another handler already cancelled).
265
+ *
219
266
  * @protected
220
267
  * @param {PointerEvent} evt
221
268
  */
222
269
  protected checkLink(evt: PointerEvent): boolean;
270
+ /**
271
+ * This will convert a link href to a router path. Absolute and
272
+ * scheme relative urls are only routed when they are on the
273
+ * current origin. The base uri is only removed as a prefix.
274
+ *
275
+ * @protected
276
+ * @param {string} href
277
+ * @returns {string|null} the path or null if the router should
278
+ * not handle the link.
279
+ */
280
+ protected getLinkPath(href: string): string | null;
223
281
  /**
224
282
  * This will reset the router.
225
283
  *
@@ -259,17 +317,22 @@ export class Router {
259
317
  *
260
318
  * @protected
261
319
  * @param {string} [path]
320
+ * @param {boolean} [restoreScroll] - when true (history back /
321
+ * forward) the caller restores the saved scroll position, so no
322
+ * route scroll intent is applied.
262
323
  */
263
- protected checkActiveRoutes(path?: string): void;
324
+ protected checkActiveRoutes(path?: string, restoreScroll?: boolean): void;
264
325
  /**
265
326
  * Checks all switch groups against the given path.
266
327
  * Activates the first matching route or falls back to the first route in the group if none match.
267
328
  *
268
329
  * @protected
269
330
  * @param {string} path
331
+ * @param {boolean} [skipDormant] - true to skip groups whose
332
+ * container is inside a detached keep-alive page.
270
333
  * @returns {void}
271
334
  */
272
- protected checkSwitches(path: string): void;
335
+ protected checkSwitches(path: string, skipDormant?: boolean): void;
273
336
  /**
274
337
  * Checks a single group of routes against the path.
275
338
  * Selects the first matching route if any; otherwise selects the first route.
@@ -325,6 +388,48 @@ export class Router {
325
388
  * @returns {void}
326
389
  */
327
390
  reassertScrollTop(): void;
391
+ /**
392
+ * This will restore a saved scroll position after the routes for
393
+ * a history navigation have been activated.
394
+ *
395
+ * When the page can already reach the position (e.g. a keep-alive
396
+ * page that was re-inserted at full height) it is restored
397
+ * synchronously so it lands before the next paint. Otherwise the
398
+ * page is still loading, so the document height is checked each
399
+ * frame and the position is restored once it can be reached (or
400
+ * after a short timeout). The wait is cancelled when the user
401
+ * starts interacting with the page or a newer navigation starts.
402
+ *
403
+ * @param {object} position
404
+ * @returns {void}
405
+ */
406
+ restoreScrollPosition(position: object): void;
407
+ /**
408
+ * This will remove the listeners of a pending scroll restore.
409
+ *
410
+ * @protected
411
+ * @returns {void}
412
+ */
413
+ protected clearScrollWait(): void;
414
+ /**
415
+ * This will check if the document is large enough to scroll the
416
+ * window to a position.
417
+ *
418
+ * @protected
419
+ * @param {number} x
420
+ * @param {number} y
421
+ * @returns {boolean}
422
+ */
423
+ protected canReachScroll(x: number, y: number): boolean;
424
+ /**
425
+ * This will scroll the element matching a url hash into view.
426
+ * Lazy routes can mount their DOM after activation, so a missing
427
+ * element is polled for on animation frames for a short window.
428
+ *
429
+ * @param {string} hash
430
+ * @returns {void}
431
+ */
432
+ scrollToHash(hash: string): void;
328
433
  /**
329
434
  * This will scroll to a target element by CSS selector.
330
435
  * For sticky elements, it scrolls to the position where the
@@ -379,3 +484,4 @@ export class Router {
379
484
  * @type {Router}
380
485
  */
381
486
  export const router: Router;
487
+ import { PageCache } from './routes/page-cache.js';
@@ -4,6 +4,17 @@
4
4
  * This will create a helper to create and destroy components
5
5
  * that are added to a route.
6
6
  *
7
+ * Keep-alive: when the route (or its component, or the router default)
8
+ * has `keepAlive` enabled, deactivating the route detaches the page
9
+ * root nodes from the document instead of destroying the component.
10
+ * Watchers, data and events stay alive, and the page is re-inserted
11
+ * as is when the route activates again. Keep-alive page components may
12
+ * define:
13
+ *
14
+ * - `onActivate(params, { restored })` called after the page is
15
+ * rendered (`restored: false`) or re-inserted (`restored: true`).
16
+ * - `onDeactivate()` called after the page is detached.
17
+ *
7
18
  * @class
8
19
  */
9
20
  export class ComponentHelper {
@@ -23,6 +34,68 @@ export class ComponentHelper {
23
34
  container: any;
24
35
  persist: any;
25
36
  parent: any;
37
+ /**
38
+ * The route keep-alive setting. Null uses the component
39
+ * `keepAlive` or the router default.
40
+ *
41
+ * @type {boolean|null} keepAlive
42
+ */
43
+ keepAlive: boolean | null;
44
+ /**
45
+ * The router page cache that tracks detached keep-alive
46
+ * pages and element scroll memory.
47
+ *
48
+ * @type {object|null} pageCache
49
+ */
50
+ pageCache: object | null;
51
+ /**
52
+ * The root nodes of a detached keep-alive page, or null when
53
+ * the page is not cached.
54
+ *
55
+ * @type {Array<object>|null} detachedNodes
56
+ */
57
+ detachedNodes: Array<object> | null;
58
+ /**
59
+ * The parent of each root node when the page was detached.
60
+ * Some pages mount outside the route container (an overlay
61
+ * mounts to the app root), so they go back to that parent.
62
+ *
63
+ * @type {WeakMap<object, object>|null} detachedParents
64
+ */
65
+ detachedParents: WeakMap<object, object> | null;
66
+ /**
67
+ * The element scroll offsets of the detached page.
68
+ *
69
+ * @type {object|null} savedScroll
70
+ */
71
+ savedScroll: object | null;
72
+ /**
73
+ * The last params the page was focused with.
74
+ *
75
+ * @type {object|null} params
76
+ */
77
+ params: object | null;
78
+ /**
79
+ * Token that cancels a pending element scroll restore.
80
+ *
81
+ * @type {number} scrollToken
82
+ */
83
+ scrollToken: number;
84
+ /**
85
+ * This will check if the route page is kept alive when the
86
+ * route deactivates. The route setting wins, then the component
87
+ * `keepAlive` property, then the router default.
88
+ *
89
+ * @param {object|null} [component]
90
+ * @returns {boolean}
91
+ */
92
+ isKeepAlive(component?: object | null): boolean;
93
+ /**
94
+ * This will check if the page is detached and cached.
95
+ *
96
+ * @returns {boolean}
97
+ */
98
+ isDetached(): boolean;
26
99
  /**
27
100
  * This will create the component.
28
101
  *
@@ -67,11 +140,107 @@ export class ComponentHelper {
67
140
  */
68
141
  protected create(): void;
69
142
  /**
70
- * This will remove the component.
143
+ * This will run the keep-alive and scroll memory work after the
144
+ * page has been rendered.
145
+ *
146
+ * @protected
147
+ * @param {object} params
148
+ * @returns {void}
149
+ */
150
+ protected afterCreate(params: object): void;
151
+ /**
152
+ * This will be called when an imported page module rendered.
153
+ *
154
+ * @protected
155
+ * @param {object} component
156
+ * @returns {void}
157
+ */
158
+ protected onModuleRender(component: object): void;
159
+ /**
160
+ * This will get the scroll memory key of the route page.
161
+ *
162
+ * @protected
163
+ * @returns {string}
164
+ */
165
+ protected getScrollKey(): string;
166
+ /**
167
+ * This will get the root nodes of the rendered page. Imported
168
+ * pages render their layout next to the import placeholder, so
169
+ * those roots are included.
170
+ *
171
+ * @protected
172
+ * @param {object} component
173
+ * @returns {Array<object>}
174
+ */
175
+ protected getRootNodes(component: object): Array<object>;
176
+ /**
177
+ * This will call a page lifecycle hook on the component and on
178
+ * an imported page component.
179
+ *
180
+ * @protected
181
+ * @param {string} name
182
+ * @param {Array<any>} args
183
+ * @returns {void}
184
+ */
185
+ protected callPageHook(name: string, args: Array<any>): void;
186
+ /**
187
+ * This will restore the saved element scroll of a re-rendered
188
+ * page on history navigation. The content may still be loading,
189
+ * so it is retried each frame for a short window.
190
+ *
191
+ * @protected
192
+ * @returns {void}
193
+ */
194
+ protected restoreHistoryScroll(): void;
195
+ /**
196
+ * This will restore the element scroll of a re-inserted page.
197
+ * The page keeps its content, so this is done synchronously
198
+ * before the next paint.
199
+ *
200
+ * @protected
201
+ * @returns {void}
202
+ */
203
+ protected restoreDetachedScroll(): void;
204
+ /**
205
+ * This will detach a keep-alive page from the document without
206
+ * destroying it. Watchers, data and events stay alive.
207
+ *
208
+ * @protected
209
+ * @param {object} component
210
+ * @returns {boolean} false when the page can not be detached.
211
+ */
212
+ protected detach(component: object): boolean;
213
+ /**
214
+ * This will re-insert a detached keep-alive page.
215
+ *
216
+ * @protected
217
+ * @returns {boolean} false when the page was destroyed while
218
+ * it was detached.
219
+ */
220
+ protected reattach(): boolean;
221
+ /**
222
+ * This will destroy a detached keep-alive page. This is called
223
+ * by the page cache when the page is evicted.
224
+ *
225
+ * @returns {void}
226
+ */
227
+ evict(): void;
228
+ /**
229
+ * This will destroy the component.
230
+ *
231
+ * @protected
232
+ * @returns {void}
233
+ */
234
+ protected releaseComponent(): void;
235
+ /**
236
+ * This will remove the component. A keep-alive page is detached
237
+ * and cached instead of destroyed unless `destroy` is true.
71
238
  *
239
+ * @param {boolean} [destroy] - true to always destroy the page,
240
+ * including a detached keep-alive page (route removal/teardown).
72
241
  * @returns {void}
73
242
  */
74
- remove(): void;
243
+ remove(destroy?: boolean): void;
75
244
  /**
76
245
  * This will call the component update method and pass the params.
77
246
  *
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Element scroll memory
3
+ *
4
+ * Elements inside a route page that carry a `data-scroll-key`
5
+ * attribute have their scrollTop / scrollLeft saved when the route
6
+ * deactivates and restored when it activates again. Detaching an
7
+ * element from the document resets its scroll offsets, so this is
8
+ * required for keep-alive pages and also lets re-rendered pages restore
9
+ * their inner scroll panes on history navigation.
10
+ *
11
+ * @module
12
+ */
13
+ /**
14
+ * @type {string} SCROLL_KEY_ATTR
15
+ */
16
+ export const SCROLL_KEY_ATTR: string;
17
+ export function saveElementScroll(nodes: Array<object>): object | null;
18
+ export function restoreElementScroll(nodes: Array<object>, saved: object | null): boolean;
@@ -0,0 +1,113 @@
1
+ /**
2
+ * The default number of detached keep-alive pages.
3
+ *
4
+ * @type {number} DEFAULT_MAX
5
+ */
6
+ export const DEFAULT_KEEP_ALIVE_MAX: number;
7
+ /**
8
+ * PageCache
9
+ *
10
+ * This will track the keep-alive route pages that are detached from
11
+ * the document for a router. The cache is ordered by recency so the
12
+ * least recently used page is evicted (really destroyed) once more than
13
+ * `max` pages are detached.
14
+ *
15
+ * It also keeps a bounded memory of element scroll offsets
16
+ * (`data-scroll-key`) per route and path.
17
+ *
18
+ * @class
19
+ */
20
+ export class PageCache {
21
+ /**
22
+ * This will create a page cache.
23
+ *
24
+ * @constructor
25
+ * @param {object} [owner] The router that owns the cache. The
26
+ * `keepAlive` and `keepAliveMax` settings are read from it so they
27
+ * can be changed at any time.
28
+ */
29
+ constructor(owner?: object);
30
+ /**
31
+ * @type {object|null} owner
32
+ */
33
+ owner: object | null;
34
+ /**
35
+ * Detached page helpers in least recently used order.
36
+ *
37
+ * @type {Set<object>} pages
38
+ */
39
+ pages: Set<object>;
40
+ /**
41
+ * @type {LRUCache} scrollMemory
42
+ */
43
+ scrollMemory: LRUCache;
44
+ /**
45
+ * This is set by the router while routes are checked for a
46
+ * history (back / forward) navigation.
47
+ *
48
+ * @type {boolean} historyNavigation
49
+ */
50
+ historyNavigation: boolean;
51
+ /**
52
+ * This will check if keep-alive is the router default.
53
+ *
54
+ * @returns {boolean}
55
+ */
56
+ isEnabled(): boolean;
57
+ /**
58
+ * This will get the max detached pages.
59
+ *
60
+ * @returns {number}
61
+ */
62
+ getMax(): number;
63
+ /**
64
+ * This will get the detached page count.
65
+ *
66
+ * @returns {number}
67
+ */
68
+ get size(): number;
69
+ /**
70
+ * This will add a detached page as the most recently used and
71
+ * evict the least recently used pages over the max.
72
+ *
73
+ * @param {object} helper
74
+ * @returns {void}
75
+ */
76
+ add(helper: object): void;
77
+ /**
78
+ * This will remove a page without destroying it (it has been
79
+ * re-activated or destroyed by its owner).
80
+ *
81
+ * @param {object} helper
82
+ * @returns {void}
83
+ */
84
+ delete(helper: object): void;
85
+ /**
86
+ * This will evict the least recently used pages over the max.
87
+ *
88
+ * @returns {void}
89
+ */
90
+ trim(): void;
91
+ /**
92
+ * This will destroy every detached page.
93
+ *
94
+ * @returns {void}
95
+ */
96
+ clear(): void;
97
+ /**
98
+ * This will save element scroll offsets.
99
+ *
100
+ * @param {string} key
101
+ * @param {object|null} offsets
102
+ * @returns {void}
103
+ */
104
+ saveScroll(key: string, offsets: object | null): void;
105
+ /**
106
+ * This will get saved element scroll offsets.
107
+ *
108
+ * @param {string} key
109
+ * @returns {object|null}
110
+ */
111
+ getScroll(key: string): object | null;
112
+ }
113
+ import { LRUCache } from '../../../shared/lru-cache.js';
@@ -13,12 +13,14 @@ export class Route extends BasicData {
13
13
  * @constructor
14
14
  * @param {object} settings
15
15
  * @param {function} titleCallBack
16
+ * @param {object|null} [pageCache] The router keep-alive page cache.
16
17
  */
17
- constructor(settings: object, titleCallBack: Function);
18
+ constructor(settings: object, titleCallBack: Function, pageCache?: object | null);
18
19
  uri: any;
19
20
  matchUri: string;
20
21
  paramKeys: string[];
21
22
  titleCallBack: Function;
23
+ pageCache: any;
22
24
  routeId: any;
23
25
  rawUri: any;
24
26
  uriQuery: RegExp;
@@ -52,11 +54,22 @@ export class Route extends BasicData {
52
54
  */
53
55
  setTitle(title: string | Function): void;
54
56
  /**
55
- * This will deactivate the route.
57
+ * This will deactivate the route. A keep-alive route page is
58
+ * detached and cached unless `destroy` is true.
56
59
  *
60
+ * @param {boolean} [destroy] - true to destroy the page even
61
+ * when it is kept alive (route removal and router teardown).
57
62
  * @returns {void}
58
63
  */
59
- deactivate(): void;
64
+ deactivate(destroy?: boolean): void;
65
+ /**
66
+ * This will check if the route page is detached from the
67
+ * document because it is kept alive, or because the route
68
+ * container is inside a detached keep-alive page.
69
+ *
70
+ * @returns {boolean}
71
+ */
72
+ isDormant(): boolean;
60
73
  /**
61
74
  * This will get the route layout.
62
75
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base-framework/base",
3
- "version": "3.9.17",
3
+ "version": "3.9.19",
4
4
  "description": "This is a javascript framework.",
5
5
  "main": "./dist/base.js",
6
6
  "module": "./dist/base.js",