aberdeen 1.0.12 → 1.1.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.
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Symbol to return when a custom {@link Dispatcher.addRoute} matcher cannot match a segment.
3
+ */
4
+ export declare const matchFailed: unique symbol;
5
+ /**
6
+ * Special {@link Dispatcher.addRoute} matcher that matches the rest of the segments as an array of strings.
7
+ */
8
+ export declare const matchRest: unique symbol;
9
+ type Matcher = string | ((segment: string) => any) | typeof matchRest;
10
+ type ExtractParamType<M> = M extends string ? never : (M extends ((segment: string) => infer R) ? Exclude<R, typeof matchFailed> : (M extends typeof matchRest ? string[] : never));
11
+ type ParamsFromMatchers<T extends Matcher[]> = T extends [infer M1, ...infer Rest] ? (M1 extends Matcher ? (ExtractParamType<M1> extends never ? ParamsFromMatchers<Rest extends Matcher[] ? Rest : []> : [ExtractParamType<M1>, ...ParamsFromMatchers<Rest extends Matcher[] ? Rest : []>]) : never) : [];
12
+ /**
13
+ * Simple route matcher and dispatcher.
14
+ *
15
+ * Example usage:
16
+ *
17
+ * ```ts
18
+ * const dispatcher = new Dispatcher();
19
+ *
20
+ * dispatcher.addRoute("user", Number, "stream", String, (id, stream) => {
21
+ * console.log(`User ${id}, stream ${stream}`);
22
+ * });
23
+ *
24
+ * dispatcher.dispatch(["user", "42", "stream", "music"]);
25
+ * // Logs: User 42, stream music
26
+ *
27
+ * dispatcher.addRoute("search", matchRest, (terms: string[]) => {
28
+ * console.log("Search terms:", terms);
29
+ * });
30
+ *
31
+ * dispatcher.dispatch(["search", "classical", "piano"]);
32
+ * // Logs: Search terms: [ 'classical', 'piano' ]
33
+ * ```
34
+ */
35
+ export declare class Dispatcher {
36
+ private routes;
37
+ /**
38
+ * Add a route with matchers and a handler function.
39
+ * @param args An array of matchers followed by a handler function. Each matcher can be:
40
+ * - A string: matches exactly that string.
41
+ * - A function: takes a string segment and returns a value (of any type) if it matches, or {@link matchFailed} if it doesn't match. The return value (if not `matchFailed` and not `NaN`) is passed as a parameter to the handler function. The built-in functions `Number` and `String` can be used to match numeric and string segments respectively.
42
+ * - The special {@link matchRest} symbol: matches the rest of the segments as an array of strings. Only one `matchRest` is allowed, and it must be the last matcher.
43
+ * @template T - Array of matcher types.
44
+ * @template H - Handler function type, inferred from the matchers.
45
+ */
46
+ addRoute<T extends Matcher[], H extends (...args: ParamsFromMatchers<T>) => void>(...args: [...T, H]): void;
47
+ /**
48
+ * Dispatches the given segments to the first route handler that matches.
49
+ * @param segments Array of string segments to match against the added routes. When using this class with the Aberdeen `route` module, one would typically pass `route.current.p`.
50
+ * @returns True if a matching route was found and handled, false otherwise.
51
+ */
52
+ dispatch(segments: string[]): boolean;
53
+ }
54
+ export {};
@@ -0,0 +1,65 @@
1
+ // src/dispatcher.ts
2
+ var matchFailed = Symbol("matchFailed");
3
+ var matchRest = Symbol("matchRest");
4
+
5
+ class Dispatcher {
6
+ routes = [];
7
+ addRoute(...args) {
8
+ const matchers = args.slice(0, -1);
9
+ const handler = args[args.length - 1];
10
+ if (typeof handler !== "function") {
11
+ throw new Error("Last argument should be a handler function");
12
+ }
13
+ const restCount = matchers.filter((m) => m === matchRest).length;
14
+ if (restCount > 1) {
15
+ throw new Error("Only one matchRest is allowed");
16
+ }
17
+ this.routes.push({ matchers, handler });
18
+ }
19
+ dispatch(segments) {
20
+ for (const route of this.routes) {
21
+ const args = matchRoute(route, segments);
22
+ if (args) {
23
+ route.handler(...args);
24
+ return true;
25
+ }
26
+ }
27
+ return false;
28
+ }
29
+ }
30
+ function matchRoute(route, segments) {
31
+ const args = [];
32
+ let segmentIndex = 0;
33
+ for (const matcher of route.matchers) {
34
+ if (matcher === matchRest) {
35
+ const len = segments.length - (route.matchers.length - 1);
36
+ if (len < 0)
37
+ return;
38
+ args.push(segments.slice(segmentIndex, segmentIndex + len));
39
+ segmentIndex += len;
40
+ continue;
41
+ }
42
+ if (segmentIndex >= segments.length)
43
+ return;
44
+ const segment = segments[segmentIndex];
45
+ if (typeof matcher === "string") {
46
+ if (segment !== matcher)
47
+ return;
48
+ } else if (typeof matcher === "function") {
49
+ const result = matcher(segment);
50
+ if (result === matchFailed || typeof result === "number" && isNaN(result))
51
+ return;
52
+ args.push(result);
53
+ }
54
+ segmentIndex++;
55
+ }
56
+ return args;
57
+ }
58
+ export {
59
+ matchRest,
60
+ matchFailed,
61
+ Dispatcher
62
+ };
63
+
64
+ //# debugId=30EC556B1C5E03CD64756E2164756E21
65
+ //# sourceMappingURL=dispatcher.js.map
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/dispatcher.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * Symbol to return when a custom {@link Dispatcher.addRoute} matcher cannot match a segment.\n */\nexport const matchFailed: unique symbol = Symbol(\"matchFailed\");\n\n/**\n * Special {@link Dispatcher.addRoute} matcher that matches the rest of the segments as an array of strings.\n */\nexport const matchRest: unique symbol = Symbol(\"matchRest\");\n\ntype Matcher = string | ((segment: string) => any) | typeof matchRest;\n\ntype ExtractParamType<M> = M extends string\n? never : (\n M extends ((segment: string) => infer R)\n ? Exclude<R, typeof matchFailed>\n : (M extends typeof matchRest ? string[] : never)\n);\n\ntype ParamsFromMatchers<T extends Matcher[]> = T extends [infer M1, ...infer Rest]\n? (\n M1 extends Matcher\n ? (\n ExtractParamType<M1> extends never\n ? ParamsFromMatchers<Rest extends Matcher[] ? Rest : []>\n : [ExtractParamType<M1>, ...ParamsFromMatchers<Rest extends Matcher[] ? Rest : []>]\n ) : never\n) : [];\n\ninterface DispatcherRoute {\n matchers: Matcher[];\n handler: (...params: any[]) => void;\n}\n\n/**\n * Simple route matcher and dispatcher.\n * \n * Example usage:\n * \n * ```ts\n * const dispatcher = new Dispatcher();\n * \n * dispatcher.addRoute(\"user\", Number, \"stream\", String, (id, stream) => {\n * console.log(`User ${id}, stream ${stream}`);\n * });\n *\n * dispatcher.dispatch([\"user\", \"42\", \"stream\", \"music\"]);\n * // Logs: User 42, stream music\n * \n * dispatcher.addRoute(\"search\", matchRest, (terms: string[]) => {\n * console.log(\"Search terms:\", terms);\n * });\n * \n * dispatcher.dispatch([\"search\", \"classical\", \"piano\"]);\n * // Logs: Search terms: [ 'classical', 'piano' ]\n * ```\n */\nexport class Dispatcher {\n private routes: Array<DispatcherRoute> = [];\n \n /**\n * Add a route with matchers and a handler function.\n * @param args An array of matchers followed by a handler function. Each matcher can be:\n * - A string: matches exactly that string.\n * - A function: takes a string segment and returns a value (of any type) if it matches, or {@link matchFailed} if it doesn't match. The return value (if not `matchFailed` and not `NaN`) is passed as a parameter to the handler function. The built-in functions `Number` and `String` can be used to match numeric and string segments respectively.\n * - The special {@link matchRest} symbol: matches the rest of the segments as an array of strings. Only one `matchRest` is allowed, and it must be the last matcher.\n * @template T - Array of matcher types.\n * @template H - Handler function type, inferred from the matchers.\n */\n addRoute<T extends Matcher[], H extends (...args: ParamsFromMatchers<T>) => void>(...args: [...T, H]): void {\n const matchers = args.slice(0, -1) as Matcher[];\n const handler = args[args.length - 1] as (...args: any) => any;\n\n if (typeof handler !== \"function\") {\n throw new Error(\"Last argument should be a handler function\");\n }\n \n const restCount = matchers.filter(m => m === matchRest).length;\n if (restCount > 1) {\n throw new Error(\"Only one matchRest is allowed\");\n }\n\n this.routes.push({ matchers, handler });\n }\n \n /**\n * Dispatches the given segments to the first route handler that matches.\n * @param segments Array of string segments to match against the added routes. When using this class with the Aberdeen `route` module, one would typically pass `route.current.p`.\n * @returns True if a matching route was found and handled, false otherwise.\n */\n dispatch(segments: string[]): boolean {\n for (const route of this.routes) {\n const args = matchRoute(route, segments);\n if (args) {\n route.handler(...args);\n return true;\n }\n }\n return false;\n }\n}\n\nfunction matchRoute(route: DispatcherRoute, segments: string[]): any[] | undefined {\n const args: any[] = [];\n let segmentIndex = 0;\n\n for (const matcher of route.matchers) {\n if (matcher === matchRest) {\n const len = segments.length - (route.matchers.length - 1);\n if (len < 0) return;\n args.push(segments.slice(segmentIndex, segmentIndex + len));\n segmentIndex += len;\n continue;\n }\n\n if (segmentIndex >= segments.length) return;\n const segment = segments[segmentIndex];\n \n if (typeof matcher === \"string\") {\n if (segment !== matcher) return;\n } else if (typeof matcher === \"function\") {\n const result = matcher(segment);\n if (result === matchFailed || (typeof result === 'number' && isNaN(result))) return;\n args.push(result);\n }\n \n segmentIndex++;\n }\n return args; // success!\n}\n"
6
+ ],
7
+ "mappings": ";AAGO,IAAM,cAA6B,OAAO,aAAa;AAKvD,IAAM,YAA2B,OAAO,WAAW;AAAA;AAiDnD,MAAM,WAAW;AAAA,EACZ,SAAiC,CAAC;AAAA,EAW1C,QAAiF,IAAI,MAAuB;AAAA,IACxG,MAAM,WAAW,KAAK,MAAM,GAAG,EAAE;AAAA,IACjC,MAAM,UAAU,KAAK,KAAK,SAAS;AAAA,IAEnC,IAAI,OAAO,YAAY,YAAY;AAAA,MAC/B,MAAM,IAAI,MAAM,4CAA4C;AAAA,IAChE;AAAA,IAEA,MAAM,YAAY,SAAS,OAAO,OAAK,MAAM,SAAS,EAAE;AAAA,IACxD,IAAI,YAAY,GAAG;AAAA,MACf,MAAM,IAAI,MAAM,+BAA+B;AAAA,IACnD;AAAA,IAEA,KAAK,OAAO,KAAK,EAAE,UAAU,QAAQ,CAAC;AAAA;AAAA,EAQ1C,QAAQ,CAAC,UAA6B;AAAA,IAClC,WAAW,SAAS,KAAK,QAAQ;AAAA,MAC7B,MAAM,OAAO,WAAW,OAAO,QAAQ;AAAA,MACvC,IAAI,MAAM;AAAA,QACN,MAAM,QAAQ,GAAG,IAAI;AAAA,QACrB,OAAO;AAAA,MACX;AAAA,IACJ;AAAA,IACA,OAAO;AAAA;AAEf;AAEA,SAAS,UAAU,CAAC,OAAwB,UAAuC;AAAA,EAC/E,MAAM,OAAc,CAAC;AAAA,EACrB,IAAI,eAAe;AAAA,EAEnB,WAAW,WAAW,MAAM,UAAU;AAAA,IAClC,IAAI,YAAY,WAAW;AAAA,MACvB,MAAM,MAAM,SAAS,UAAU,MAAM,SAAS,SAAS;AAAA,MACvD,IAAI,MAAM;AAAA,QAAG;AAAA,MACb,KAAK,KAAK,SAAS,MAAM,cAAc,eAAe,GAAG,CAAC;AAAA,MAC1D,gBAAgB;AAAA,MAChB;AAAA,IACJ;AAAA,IAEA,IAAI,gBAAgB,SAAS;AAAA,MAAQ;AAAA,IACrC,MAAM,UAAU,SAAS;AAAA,IAEzB,IAAI,OAAO,YAAY,UAAU;AAAA,MAC7B,IAAI,YAAY;AAAA,QAAS;AAAA,IAC7B,EAAO,SAAI,OAAO,YAAY,YAAY;AAAA,MACtC,MAAM,SAAS,QAAQ,OAAO;AAAA,MAC9B,IAAI,WAAW,eAAgB,OAAO,WAAW,YAAY,MAAM,MAAM;AAAA,QAAI;AAAA,MAC7E,KAAK,KAAK,MAAM;AAAA,IACpB;AAAA,IAEA;AAAA,EACJ;AAAA,EACA,OAAO;AAAA;",
8
+ "debugId": "30EC556B1C5E03CD64756E2164756E21",
9
+ "names": []
10
+ }
package/dist/route.d.ts CHANGED
@@ -1,47 +1,96 @@
1
+ type NavType = "load" | "back" | "forward" | "go";
1
2
  /**
2
- * The class for the singleton `route` object.
3
- *
4
- */
5
- export declare class Route {
6
- /** The current path of the URL split into components. For instance `/` or `/users/123/feed`. Updates will be reflected in the URL and will *push* a new entry to the browser history. */
3
+ * The class for the global `route` object.
4
+ */
5
+ export interface Route {
6
+ /** The current path of the URL as a string. For instance `"/"` or `"/users/123/feed"`. Paths are normalized to always start with a `/` and never end with a `/` (unless it's the root path). */
7
7
  path: string;
8
- /** Array containing the path segments. For instance `[]` or `['users', 123, 'feed']`. Updates will be reflected in the URL and will *push* a new entry to the browser history. Also, the values of `p` and `path` will be synced. */
8
+ /** An convenience array containing path segments, mapping to `path`. For instance `[]` (for `"/"`) or `['users', '123', 'feed']` (for `"/users/123/feed"`). */
9
9
  p: string[];
10
- /** An observable object containing search parameters (a split up query string). For instance `{order: "date", title: "something"}` or just `{}`. By default, updates will be reflected in the URL, replacing the current history state. */
10
+ /** The hash fragment including the leading `#`, or an empty string. For instance `"#my_section"` or `""`. */
11
11
  hash: string;
12
- /** A part of the browser history *state* that is considered part of the page *identify*, meaning changes will (by default) cause a history push, and when going *back*, it must match. */
12
+ /** The query string interpreted as search parameters. So `"a=x&b=y"` becomes `{a: "x", b: "y"}`. */
13
13
  search: Record<string, string>;
14
- /** The `hash` interpreted as search parameters. So `"a=x&b=y"` becomes `{a: "x", b: "y"}`. */
15
- id: Record<string, any>;
16
- /** The auxiliary part of the browser history *state*, not considered part of the page *identity*. Changes will be reflected in the browser history using a replace. */
17
- aux: Record<string, any>;
14
+ /** An object to be used for any additional data you want to associate with the current page. Data should be JSON-compatible. */
15
+ state: Record<string, any>;
18
16
  /** The navigation depth of the current session. Starts at 1. Writing to this property has no effect. */
19
17
  depth: number;
20
18
  /** The navigation action that got us to this page. Writing to this property has no effect.
21
- - `"load"`: An initial page load.
22
- - `"back"` or `"forward"`: When we navigated backwards or forwards in the stack.
23
- - `"push"`: When we added a new page on top of the stack.
24
- */
25
- nav: "load" | "back" | "forward" | "push";
26
- /** As described above, this library takes a best guess about whether pushing an item to the browser history makes sense or not. When `mode` is...
27
- - `"push"`: Force creation of a new browser history entry.
28
- - `"replace"`: Update the current history entry, even when updates to other keys would normally cause a *push*.
29
- - `"back"`: Unwind the history (like repeatedly pressing the *back* button) until we find a page that matches the given `path` and `id` (or that is the first page in our stack), and then *replace* that state by the full given state.
30
- The `mode` key can be written to `route` but will be immediately and silently removed.
19
+ - `"load"`: An initial page load.
20
+ - `"back"` or `"forward"`: When we navigated backwards or forwards in the stack.
21
+ - `"go"`: When we added a new page on top of the stack.
22
+ Mostly useful for page transition animations. Writing to this property has no effect.
31
23
  */
32
- mode: "push" | "replace" | "back" | undefined;
24
+ nav: NavType;
33
25
  }
34
26
  /**
35
- * The singleton {@link Route} object reflecting the current URL and browser history state. You can make changes to it to affect the URL and browser history. See {@link Route} for details.
27
+ * Configure logging on route changes.
28
+ * @param value `true` to enable logging to console, `false` to disable logging, or a custom logging function. Defaults to `false`.
36
29
  */
37
- export declare const route: Route;
30
+ export declare function setLog(value: boolean | ((...args: any[]) => void)): void;
31
+ type RouteTarget = string | (string | number)[] | Partial<Omit<Omit<Route, "p">, "search"> & {
32
+ /** An convenience array containing path segments, mapping to `path`. For instance `[]` (for `"/"`) or `['users', 123, 'feed']` (for `"/users/123/feed"`). Values may be integers but will be converted to strings.*/
33
+ p: (string | number)[];
34
+ /** The query string interpreted as search parameters. So `"a=x&b=y"` becomes `{a: "x", b: "y", c: 42}`. Values may be integers but will be converted to strings. */
35
+ search: Record<string, string | number>;
36
+ }>;
38
37
  /**
39
- * Restore and store the vertical and horizontal scroll position for
40
- * the parent element to the page state.
38
+ * Navigate to a new URL by pushing a new history entry.
39
+ *
40
+ * Note that this happens synchronously, immediately updating `route` and processing any reactive updates based on that.
41
+ *
42
+ * @param target A subset of the {@link Route} properties to navigate to. If neither `p` nor `path` is given, the current path is used. For other properties, an empty/default value is assumed if not given. For convenience:
43
+ * - You may pass a string instead of an object, which is interpreted as the `path`.
44
+ * - You may pass an array instead of an object, which is interpreted as the `p` array.
45
+ * - If you pass `p`, it may contain numbers, which will be converted to strings.
46
+ * - If you pass `search`, its values may be numbers, which will be converted to strings.
47
+ *
48
+ * Examples:
49
+ * ```js
50
+ * // Navigate to /users/123
51
+ * route.go("/users/123");
52
+ *
53
+ * // Navigate to /users/123?tab=feed#top
54
+ * route.go({p: ["users", 123], search: {tab: "feed"}, hash: "top"});
55
+ * ```
56
+ */
57
+ export declare function go(target: RouteTarget): void;
58
+ /**
59
+ * Modify the current route by merging `target` into it (using {@link merge}), pushing a new history entry.
41
60
  *
42
- * @param {string} name - A unique (within this page) name for this
43
- * scrollable element. Defaults to 'main'.
61
+ * This is useful for things like opening modals or side panels, where you want a browser back action to return to the previous state.
44
62
  *
45
- * The scroll position will be persisted in `route.aux.scroll.<name>`.
63
+ * @param target Same as for {@link go}, but merged into the current route instead deleting all state.
46
64
  */
65
+ export declare function push(target: RouteTarget): void;
66
+ /**
67
+ * Try to go back in history to the first entry that matches the given target. If none is found, the given state will replace the current page. This is useful for "cancel" or "close" actions that should return to the previous page if possible, but create a new page if not (for instance when arriving at the current page through a direct link).
68
+ *
69
+ * Consider using {@link up} to go up in the path hierarchy.
70
+ *
71
+ * @param target The target route to go back to. May be a subset of {@link Route}, or a string (for `path`), or an array of strings (for `p`).
72
+ */
73
+ export declare function back(target?: RouteTarget): void;
74
+ /**
75
+ * Navigate up in the path hierarchy, by going back to the first history entry
76
+ * that has a shorter path than the current one. If there's none, we just shorten
77
+ * the current path.
78
+ *
79
+ * Note that going back in browser history happens asynchronously, so `route` will not be updated immediately.
80
+ */
81
+ export declare function up(stripCount?: number): void;
82
+ /**
83
+ * Restore and store the vertical and horizontal scroll position for
84
+ * the parent element to the page state.
85
+ *
86
+ * @param {string} name - A unique (within this page) name for this
87
+ * scrollable element. Defaults to 'main'.
88
+ *
89
+ * The scroll position will be persisted in `route.aux.scroll.<name>`.
90
+ */
47
91
  export declare function persistScroll(name?: string): void;
92
+ /**
93
+ * The global {@link Route} object reflecting the current URL and browser history state. Changes you make to this affect the current browser history item (modifying the URL if needed).
94
+ */
95
+ export declare const current: Route;
96
+ export {};
package/dist/route.js CHANGED
@@ -1,169 +1,196 @@
1
1
  // src/route.ts
2
- import {
3
- clean,
4
- clone,
5
- getParentElement,
6
- immediateObserve,
7
- observe,
8
- proxy,
9
- runQueue,
10
- unproxy
11
- } from "./aberdeen.js";
12
-
13
- class Route {
14
- path;
15
- p;
16
- hash;
17
- search;
18
- id;
19
- aux;
20
- depth = 1;
21
- nav = "load";
22
- mode;
23
- }
24
- var route = proxy(new Route);
25
- var stateRoute = {
26
- nonce: -1,
27
- depth: 0
28
- };
29
- function handleLocationUpdate(event) {
30
- const state = event?.state || {};
31
- let nav = "load";
32
- if (state.route?.nonce == null) {
33
- state.route = {
34
- nonce: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER),
35
- depth: 1
36
- };
37
- history.replaceState(state, "");
38
- } else if (stateRoute.nonce === state.route.nonce) {
39
- nav = state.route.depth > stateRoute.depth ? "forward" : "back";
40
- }
41
- stateRoute = state.route;
42
- if (unproxy(route).mode === "back") {
43
- route.depth = stateRoute.depth;
44
- updateHistory();
45
- return;
46
- }
47
- const search = {};
48
- for (const [k, v] of new URLSearchParams(location.search)) {
49
- search[k] = v;
2
+ import { clean, getParentElement, $, proxy, runQueue, unproxy, copy, merge, clone, leakScope } from "./aberdeen.js";
3
+ var log = () => {};
4
+ function setLog(value) {
5
+ if (value === true) {
6
+ log = console.log.bind(console, "aberdeen router");
7
+ } else if (value === false) {
8
+ log = () => {};
9
+ } else {
10
+ log = value;
50
11
  }
51
- route.path = location.pathname;
52
- route.p = location.pathname.slice(1).split("/");
53
- route.search = search;
54
- route.hash = location.hash;
55
- route.id = state.id;
56
- route.aux = state.aux;
57
- route.depth = stateRoute.depth;
58
- route.nav = nav;
59
- if (event)
60
- runQueue();
61
12
  }
62
- handleLocationUpdate();
63
- window.addEventListener("popstate", handleLocationUpdate);
64
- function updatePath() {
65
- let path = route.path;
66
- if (path == null && unproxy(route).p) {
67
- updateP();
68
- return;
13
+ function getRouteFromBrowser() {
14
+ return toCanonRoute({
15
+ path: location.pathname,
16
+ hash: location.hash,
17
+ search: Object.fromEntries(new URLSearchParams(location.search)),
18
+ state: history.state?.state || {}
19
+ }, "load", (history.state?.stack?.length || 0) + 1);
20
+ }
21
+ function equal(a, b, partial) {
22
+ if (a === b)
23
+ return true;
24
+ if (typeof a !== "object" || !a || typeof b !== "object" || !b)
25
+ return false;
26
+ if (a.constructor !== b.constructor)
27
+ return false;
28
+ if (b instanceof Array) {
29
+ if (a.length !== b.length)
30
+ return false;
31
+ for (let i = 0;i < b.length; i++) {
32
+ if (!equal(a[i], b[i], partial))
33
+ return false;
34
+ }
35
+ } else {
36
+ for (const k of Object.keys(b)) {
37
+ if (!equal(a[k], b[k], partial))
38
+ return false;
39
+ }
40
+ if (!partial) {
41
+ for (const k of Object.keys(a)) {
42
+ if (!b.hasOwnProperty(k))
43
+ return false;
44
+ }
45
+ }
69
46
  }
70
- path = `${path || "/"}`;
47
+ return true;
48
+ }
49
+ function getUrl(target) {
50
+ const search = new URLSearchParams(target.search).toString();
51
+ return (search ? `${target.path}?${search}` : target.path) + target.hash;
52
+ }
53
+ function toCanonRoute(target, nav, depth) {
54
+ let path = target.path || (target.p || []).join("/") || "/";
55
+ path = ("" + path).replace(/\/+$/, "");
71
56
  if (!path.startsWith("/"))
72
57
  path = `/${path}`;
73
- route.path = path;
74
- route.p = path.slice(1).split("/");
58
+ return {
59
+ path,
60
+ hash: target.hash && target.hash !== "#" ? target.hash.startsWith("#") ? target.hash : "#" + target.hash : "",
61
+ p: path.length > 1 ? path.slice(1).replace(/\/+$/, "").split("/") : [],
62
+ nav,
63
+ search: typeof target.search === "object" && target.search ? clone(target.search) : {},
64
+ state: typeof target.state === "object" && target.state ? clone(target.state) : {},
65
+ depth
66
+ };
75
67
  }
76
- immediateObserve(updatePath);
77
- function updateP() {
78
- const p = route.p;
79
- if (p == null && unproxy(route).path) {
80
- updatePath();
81
- return;
68
+ function targetToPartial(target) {
69
+ if (typeof target === "string") {
70
+ target = { path: target };
71
+ } else if (target instanceof Array) {
72
+ target = { p: target };
82
73
  }
83
- if (!(p instanceof Array)) {
84
- console.error(`aberdeen route: 'p' must be a non-empty array, not ${JSON.stringify(p)}`);
85
- route.p = [""];
86
- } else if (p.length === 0) {
87
- route.p = [""];
88
- } else {
89
- route.path = `/${p.join("/")}`;
74
+ if (target.p) {
75
+ target.p = target.p.map(String);
76
+ }
77
+ if (target.search) {
78
+ for (const key of Object.keys(target.search)) {
79
+ target.search[key] = String(target.search[key]);
80
+ }
90
81
  }
82
+ return target;
91
83
  }
92
- immediateObserve(updateP);
93
- immediateObserve(() => {
94
- if (!route.search || typeof route.search !== "object")
95
- route.search = {};
96
- });
97
- immediateObserve(() => {
98
- if (!route.id || typeof route.id !== "object")
99
- route.id = {};
100
- });
101
- immediateObserve(() => {
102
- if (!route.aux || typeof route.aux !== "object")
103
- route.aux = {};
104
- });
105
- immediateObserve(() => {
106
- let hash = `${route.hash || ""}`;
107
- if (hash && !hash.startsWith("#"))
108
- hash = `#${hash}`;
109
- route.hash = hash;
110
- });
111
- function isSamePage(path, state) {
112
- return location.pathname === path && JSON.stringify(history.state.id || {}) === JSON.stringify(state.id || {});
84
+ function go(target) {
85
+ const stack = history.state?.stack || [];
86
+ prevStack = stack.concat(JSON.stringify(unproxy(current)));
87
+ const newRoute = toCanonRoute(targetToPartial(target), "go", prevStack.length + 1);
88
+ copy(current, newRoute);
89
+ log("go", newRoute);
90
+ history.pushState({ state: newRoute.state, stack: prevStack }, "", getUrl(newRoute));
91
+ runQueue();
113
92
  }
114
- function updateHistory() {
115
- let mode = route.mode;
116
- const state = {
117
- id: clone(route.id),
118
- aux: clone(route.aux),
119
- route: stateRoute
120
- };
121
- const path = route.path;
122
- if (mode === "back") {
123
- route.nav = "back";
124
- if (!isSamePage(path, state) && (history.state.route?.depth || 0) > 1) {
125
- history.back();
93
+ function push(target) {
94
+ let copy2 = clone(unproxy(current));
95
+ merge(copy2, targetToPartial(target));
96
+ go(copy2);
97
+ }
98
+ function back(target = {}) {
99
+ const partial = targetToPartial(target);
100
+ const stack = history.state?.stack || [];
101
+ for (let i = stack.length - 1;i >= 0; i--) {
102
+ const histRoute = JSON.parse(stack[i]);
103
+ if (equal(histRoute, partial, true)) {
104
+ const pages = i - stack.length;
105
+ log(`back`, pages, histRoute);
106
+ history.go(pages);
126
107
  return;
127
108
  }
128
- mode = "replace";
129
109
  }
130
- if (mode)
131
- route.mode = undefined;
132
- const search = new URLSearchParams(route.search).toString();
133
- const url = (search ? `${path}?${search}` : path) + route.hash;
134
- if (mode === "push" || !mode && !isSamePage(path, state)) {
135
- stateRoute.depth++;
136
- history.pushState(state, "", url);
137
- route.nav = "push";
138
- route.depth = stateRoute.depth;
139
- } else {
140
- history.replaceState(state, "", url);
110
+ const newRoute = toCanonRoute(partial, "back", stack.length + 1);
111
+ log(`back not found, replacing`, partial);
112
+ copy(current, newRoute);
113
+ }
114
+ function up(stripCount = 1) {
115
+ const currentP = unproxy(current).p;
116
+ const stack = history.state?.stack || [];
117
+ for (let i = stack.length - 1;i >= 0; i--) {
118
+ const histRoute = JSON.parse(stack[i]);
119
+ if (histRoute.p.length < currentP.length && equal(histRoute.p, currentP.slice(0, histRoute.p.length), false)) {
120
+ log(`up to ${i + 1} / ${stack.length}`, histRoute);
121
+ history.go(i - stack.length);
122
+ return;
123
+ }
141
124
  }
125
+ const newRoute = toCanonRoute({ p: currentP.slice(0, currentP.length - stripCount) }, "back", stack.length + 1);
126
+ log(`up not found, replacing`, newRoute);
127
+ copy(current, newRoute);
142
128
  }
143
- observe(updateHistory);
144
129
  function persistScroll(name = "main") {
145
130
  const el = getParentElement();
146
131
  el.addEventListener("scroll", onScroll);
147
132
  clean(() => el.removeEventListener("scroll", onScroll));
148
- const restore = unproxy(route).aux.scroll?.name;
133
+ const restore = unproxy(current).state.scroll?.[name];
149
134
  if (restore) {
135
+ log("restoring scroll", name, restore);
150
136
  Object.assign(el, restore);
151
137
  }
152
138
  function onScroll() {
153
- route.mode = "replace";
154
- if (!route.aux.scroll)
155
- route.aux.scroll = {};
156
- route.aux.scroll[name] = {
139
+ (current.state.scroll ||= {})[name] = {
157
140
  scrollTop: el.scrollTop,
158
141
  scrollLeft: el.scrollLeft
159
142
  };
160
143
  }
161
144
  }
145
+ var prevStack;
146
+ var current = proxy({});
147
+ function reset() {
148
+ prevStack = history.state?.stack || [];
149
+ const initRoute = getRouteFromBrowser();
150
+ log("initial", initRoute);
151
+ copy(unproxy(current), initRoute);
152
+ }
153
+ reset();
154
+ window.addEventListener("popstate", function(event) {
155
+ const newRoute = getRouteFromBrowser();
156
+ const stack = history.state?.stack || [];
157
+ if (stack.length !== prevStack.length) {
158
+ const maxIndex = Math.min(prevStack.length, stack.length) - 1;
159
+ if (maxIndex < 0 || stack[maxIndex] === prevStack[maxIndex]) {
160
+ newRoute.nav = stack.length < prevStack.length ? "back" : "forward";
161
+ }
162
+ }
163
+ prevStack = stack;
164
+ log("popstate", newRoute);
165
+ copy(current, newRoute);
166
+ runQueue();
167
+ });
168
+ leakScope(() => {
169
+ $(() => {
170
+ current.path = "/" + Array.from(current.p).join("/");
171
+ });
172
+ $(() => {
173
+ const stack = history.state?.stack || [];
174
+ const newRoute = toCanonRoute(current, unproxy(current).nav, stack.length + 1);
175
+ copy(current, newRoute);
176
+ const state = { state: newRoute.state, stack };
177
+ const url = getUrl(newRoute);
178
+ if (url !== location.pathname + location.search + location.hash || !equal(history.state, state, false)) {
179
+ log("replaceState", newRoute, state, url);
180
+ history.replaceState(state, "", url);
181
+ }
182
+ });
183
+ });
162
184
  export {
163
- route,
185
+ up,
186
+ setLog,
187
+ reset,
188
+ push,
164
189
  persistScroll,
165
- Route
190
+ go,
191
+ current,
192
+ back
166
193
  };
167
194
 
168
- //# debugId=D9D83FC76589C40E64756E2164756E21
195
+ //# debugId=A8B2D764F285F09564756E2164756E21
169
196
  //# sourceMappingURL=route.js.map