typecomposer 0.1.2 → 0.1.4

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.
@@ -9,7 +9,7 @@ declare class __App__ {
9
9
  set theme(value: theme);
10
10
  set language(value: string);
11
11
  get language(): string;
12
- setPage(page: IComponent): void;
12
+ setPage(page: IComponent): IComponent;
13
13
  /**
14
14
  * Gets a reference to the root node of the document.
15
15
  * @returns {HTMLElement} The root node of the document.
@@ -43,6 +43,7 @@ class __App__ {
43
43
  setPage(page) {
44
44
  document.body.innerHTML = "";
45
45
  document.body.appendChild(page);
46
+ return page;
46
47
  }
47
48
  /**
48
49
  * Gets a reference to the root node of the document.
@@ -1 +1 @@
1
- {"version":3,"file":"App.js","sourceRoot":"","sources":["../../../../../src/core/element/base/App.ts"],"names":[],"mappings":";;;AAIA,MAAM,OAAO;IACH,aAAa,CAAK;IAClB,eAAe,GAAY,KAAK,CAAC;IAEjC,IAAI,GAAuC;QACjD,KAAK,EAAE,QAAQ,CAAC,eAAe,CAAC,YAAY,CAAC;QAC7C,QAAQ,EAAE,QAAQ,CAAC,eAAe,CAAC,IAAI;KACxC,CAAC;IAEF;QACE,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAC9C,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAC9B,IAAI,CAAC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;YACxC,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,QAAQ,CAAC,eAAe,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC;QACzF,CAAC;QACD,QAAQ,CAAC,gBAAgB,CAAC,kBAAkB,EAAE,GAAG,EAAE;YACjD,8BAA8B;YAC9B,IAAI,CAAC,eAAe,GAAG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Ae,CAAC;IAClC,CAAC;IAED,IAAW,IAAI;QACb,OAAO,QAAQ,CAAC,IAAI,CAAC;IACvB,CAAC;IAED,IAAW,IAAI;QACb,OAAO,QAAQ,CAAC,IAAI,CAAC;IACvB,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,MAAM,CAAC,QAAQ,CAAC;IACzB,CAAC;IAED,IAAW,KAAK;QACd,OAAO,QAAQ,CAAC,KAAK,CAAC;IACxB,CAAC;IAED,IAAW,KAAK,CAAC,KAAa;QAC5B,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC;IACzB,CAAC;IAED,IAAW,KAAK;QACd,aAAa;QACb,OAAO,QAAQ,CAAC,eAAe,CAAC,KAAK,CAAC;IACxC,CAAC;IAED,IAAW,SAAS,CAAC,KAAa;QAChC,IAAI,CAAC,aAAa,GAAG,KAAK,CAAC;IAC7B,CAAC;IAED,IAAW,SAAS;QAClB,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;CACF;AAIY,QAAA,GAAG,GAAG,IAAI,OAAO,EAAE,CAAC"}
@@ -8,6 +8,9 @@ class RouteView extends __1.Component {
8
8
  routePage = undefined;
9
9
  constructor(props) {
10
10
  super(props);
11
+ this.onEvent("router:watch", () => {
12
+ this.updateView();
13
+ });
11
14
  }
12
15
  get url() {
13
16
  return this._url;
@@ -16,13 +19,21 @@ class RouteView extends __1.Component {
16
19
  this.updateView();
17
20
  }
18
21
  updateView() {
19
- const routePage = __1.Router.controller.getRouteViewFree(this);
22
+ // @ts-ignore
23
+ const routePage = __1.Router.getRouteViewFree(this);
20
24
  if (routePage == undefined) {
25
+ if (this.routePage)
26
+ this.routePage.routeView = undefined;
21
27
  this.view = undefined;
28
+ this.routePage = undefined;
22
29
  return;
23
30
  }
24
- if (routePage == this.routePage)
31
+ if (routePage === this.routePage)
25
32
  return;
33
+ if (routePage.build && routePage.build === this.routePage?.build) {
34
+ routePage.routeView = this;
35
+ return;
36
+ }
26
37
  routePage.build = routePage.build || new routePage.component();
27
38
  this.routePage = routePage;
28
39
  this.view = routePage.build;
@@ -31,6 +42,7 @@ class RouteView extends __1.Component {
31
42
  if (this.routePage)
32
43
  this.routePage.routeView = undefined;
33
44
  this.routePage = undefined;
45
+ this.removeEvent("router:watch");
34
46
  }
35
47
  get view() {
36
48
  return this._view;
@@ -1 +1 @@
1
- {"version":3,"file":"RouteView.js","sourceRoot":"","sources":["../../../../src/core/router/RouteView.ts"],"names":[],"mappings":";;;AAAA,6BAA0D;AAU1D,MAAa,SAAU,SAAQ,aAAS;IAC9B,KAAK,GAA2B,SAAS,CAAC;IAC1C,IAAI,GAAW,EAAE,CAAC;IAClB,SAAS,GAA+B,SAAS,CAAC;IAE1D,YACE,KAEC;QAED,KAAK,CAAC,KAAK,CAAC,CAAC;IACf,CAAC;IAED,IAAI,GAAG;QACL,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAED,iBAAiB;QACf,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;IAEO,UAAU;QAChB,MAAM,SAAS,GAAG,UAAM,CAAC,UAAU,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAC3D,IAAI,SAAS,IAAI,SAAS,EAAE,CAAC;YAC3B,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;YACtB,OAAO;QACT,CAAC;QACD,IAAI,SAAS,IAAI,IAAI,CAAC,SAAS;YAAE,OAAO;QACxC,SAAS,CAAC,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,IAAI,SAAS,CAAC,SAAU,EAAE,CAAC;QAChE,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC;IAC9B,CAAC;IAED,oBAAoB;QAClB,IAAI,IAAI,CAAC,SAAS;YAAE,IAAI,CAAC,SAAS,CAAC,SAAS,GAAG,SAAS,CAAC;QACzD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED,I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AC,SAAS,GAAG,SAAS,CAAC;YAC3B,OAAO;QACT,CAAC;QACD,IAAI,SAAS,KAAK,IAAI,CAAC,SAAS;YAAE,OAAO;QACzC,IAAI,SAAS,CAAC,KAAK,IAAI,SAAS,CAAC,KAAK,KAAK,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,CAAC;YACjE,SAAS,CAAC,SAAS,GAAG,IAAI,CAAC;YAC3B,OAAO;QACT,CAAC;QACD,SAAS,CAAC,KAAK,GAAG,SAAS,CAAC,KAAK,IAAI,IAAI,SAAS,CAAC,SAAU,EAAE,CAAC;QAChE,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC,KAAK,CAAC;IAC9B,CAAC;IAED,oBAAoB;QAClB,IAAI,IAAI,CAAC,SAAS;YAAE,IAAI,CAAC,SAAS,CAAC,SAAS,GAAG,SAAS,CAAC;QACzD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,WAAW,CAAC,cAAc,CAAC,CAAC;IACnC,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,KAA6B;QACpC,IAAI,IAAI,CAAC,KAAK,IAAI,KAAK;YAAE,OAAO;QAChC,IAAI,IAAI,CAAC,KAAK;YAAE,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;QACpC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,KAAK;YAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC;CACF;AA5DD,8BA4DC;AACD,aAAa;AACb,cAAc,CAAC,MAAM,CAAC,YAAY,EAAE,SAAS,CAAC,CAAC"}
@@ -1,4 +1,3 @@
1
- import { RouteView } from "..";
2
1
  import { RouterGuard } from "./RouterGuard";
3
2
  export interface RoutePage {
4
3
  path: string;
@@ -7,59 +6,43 @@ export interface RoutePage {
7
6
  redirect?: string;
8
7
  guard?: RouterGuard;
9
8
  }
10
- interface RoutePageBuild extends RoutePage {
11
- parent: RoutePageBuild;
12
- build: IComponent;
13
- routeView: RouteView | undefined;
14
- id?: string;
15
- }
16
- declare class RouterController {
17
- private _route;
18
- routePages: RoutePageBuild[];
19
- private currentRoute;
20
- private previousRoute;
21
- private static _props;
22
- private urlLast;
23
- private view;
24
- constructor();
25
- doRoutesMatch(route1: string, route2: string): boolean;
26
- findCorrectRoute(pathname: string, routes?: RoutePage[]): string;
27
- private buildRoute;
28
- private buildPages;
29
- private updateRoute;
30
- getProps(routePage: RoutePageBuild): any;
31
- get route(): Router | undefined;
32
- set route(value: Router | undefined);
33
- get props(): any;
34
- set props(value: any);
35
- addHistory(pathname: string): void;
36
- getRouteViewFree(routeView: RouteView): RoutePageBuild | undefined;
37
- getView(routeView: RoutePageBuild): HTMLElement | undefined;
38
- private pageNotFound;
39
- private setView;
40
- private emitRouteChange;
41
- private extractParametersURL;
42
- }
43
9
  export declare class Router {
44
- routes: RoutePage[];
45
- history: "hash" | "history";
46
- pageNotFound: ComponentType;
47
- static controller: RouterController;
48
- private static _props;
49
- static get PATH(): string;
50
- static readonly PATH_FULL: string;
51
- constructor(routes?: RoutePage[], history?: "hash" | "history", pageNotFound?: ComponentType);
10
+ #private;
11
+ private routes;
12
+ private url;
13
+ private urlValid;
14
+ private oldUrl;
15
+ private match;
16
+ /**
17
+ * @param data - An array of route objects.
18
+ * @param history - The type of history to use. Can be either "hash" or "history".
19
+ */
20
+ static readonly SEGMENT_WILDCARD = "*";
21
+ /**
22
+ * @param data - An array of route objects.
23
+ * @param history - The type of history to use. Can be either "hash" or "history".
24
+ * @returns {void}
25
+ */
26
+ static readonly PATH_WILDCARD = "**";
52
27
  beforeEach(callback: (to: RoutePage) => void): void;
53
- static get props(): any;
28
+ private constructor();
29
+ private createAutoId;
30
+ private getQueryParams;
31
+ private handleRoute;
32
+ private buildRoutePage;
33
+ private checkGuard;
54
34
  static create(data: {
55
35
  routes: RoutePage[];
56
36
  history?: "hash" | "history";
57
- pageNotFound?: ComponentType;
58
37
  }): void;
59
38
  static reload(): void;
39
+ static get props(): {
40
+ [key: string]: any;
41
+ };
42
+ static get history(): "hash" | "history";
60
43
  private static buildURL;
61
- private static createAutoId;
62
- static go(pathname: string, props?: {}): Promise<void>;
44
+ static go(url: string, props?: {}): Promise<void>;
63
45
  static back(): void;
46
+ static forward(): void;
47
+ private static getRouteViewFree;
64
48
  }
65
- export {};
@@ -1,276 +1,238 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Router = void 0;
4
- class RouterController {
5
- _route = undefined;
6
- routePages = [];
7
- currentRoute = [];
8
- previousRoute = [];
9
- static _props = {};
10
- urlLast = "";
11
- view = undefined;
12
- constructor() {
13
- window.addEventListener("load", () => {
14
- if (this.route)
15
- this.updateRoute(this.route.history == "history" ? window.location.pathname : window.location.hash);
16
- });
17
- window.addEventListener("popstate", () => {
18
- if (this.route)
19
- this.updateRoute(this.route.history == "history" ? window.location.pathname : window.location.hash);
20
- });
21
- }
22
- doRoutesMatch(route1, route2) {
23
- if (route2 == Router.PATH_FULL)
24
- return true;
25
- // Replace '*' with a regex pattern that matches any segment
26
- const routePattern2 = route2.replace(/\*/g, "[a-zA-Z0-9-]+");
27
- // Create regex patterns for the routes
28
- const regex2 = new RegExp(`^${routePattern2}$`);
29
- // Test if both routes match each other
30
- return regex2.test(route1);
31
- }
32
- findCorrectRoute(pathname, routes = this.route?.routes || []) {
33
- if (routes) {
34
- for (const route of routes) {
35
- if (this.doRoutesMatch(pathname, route.path)) {
36
- this.currentRoute.push({
37
- ...route,
38
- build: undefined,
39
- routeView: undefined,
40
- parent: this.currentRoute.length > 0 ? this.currentRoute[this.currentRoute.length - 1] : undefined,
41
- });
42
- return route.path;
43
- }
44
- }
45
- for (const route of routes) {
46
- if (route.children) {
47
- this.currentRoute.push({
48
- ...route,
49
- build: undefined,
50
- routeView: undefined,
51
- parent: this.currentRoute.length > 0 ? this.currentRoute[this.currentRoute.length - 1] : undefined,
52
- });
53
- const r = this.findCorrectRoute(pathname, route.children);
54
- if (r != "/")
55
- return r;
56
- else
57
- this.currentRoute.pop();
58
- }
59
- }
4
+ const __1 = require("..");
5
+ function getRoutePairs(routes, basePath = '', parentRouters = []) {
6
+ let result = [];
7
+ for (const route of routes) {
8
+ const fullPath = `${basePath}/${route.path}`.replace(/\/+/g, '/');
9
+ const routers = [...parentRouters];
10
+ if (route.component) {
11
+ // @ts-ignore
12
+ routers.push({ component: route.component, path: route.path, id: route.id, guard: route.guard });
60
13
  }
61
- return "/";
62
- }
63
- async buildRoute(routes = this.route?.routes, parent = "") {
64
- if (routes) {
65
- for (let route of routes) {
66
- route.path = parent + route.path;
67
- if (route.children) {
68
- this.buildRoute(route.children, route.path == "/" ? route.path : route.path + "/");
69
- }
70
- }
14
+ result.push({ path: fullPath, routers });
15
+ if (route.children) {
16
+ result = result.concat(getRoutePairs(route.children, fullPath, routers));
71
17
  }
72
- return true;
73
18
  }
74
- async buildPages() {
75
- let routePageBuildLast = undefined;
76
- for (let i = 0; i < this.currentRoute.length; i++) {
77
- if (i + 1 < this.currentRoute.length && this.previousRoute[i] != undefined && this.currentRoute[i].id == this.previousRoute[i].id) {
78
- routePageBuildLast = this.previousRoute[i];
79
- this.currentRoute[i] = this.previousRoute[i];
80
- if (this.currentRoute[i + 1])
81
- this.currentRoute[i + 1].parent = this.currentRoute[i];
19
+ return result;
20
+ }
21
+ function matchRoute(url, routes) {
22
+ let bestMatch = null;
23
+ for (const route of routes) {
24
+ const routeRegex = new RegExp(`^${route.path
25
+ .replace(/\*\*/g, '.*')
26
+ .replace(/\*/g, '[^/]+')
27
+ .replace(/:[^\s/]+/g, '([^/]+)')}$`);
28
+ const match = url.match(routeRegex);
29
+ if (match) {
30
+ const params = {};
31
+ const paramKeys = route.path.match(/:[^\s/]+/g);
32
+ if (paramKeys) {
33
+ paramKeys.forEach((key, index) => {
34
+ params[key.replace(':', '')] = match[index + 1];
35
+ });
82
36
  }
83
- else if (this.currentRoute[i].guard) {
84
- const fallbackRoute = this.currentRoute[i].guard.fallbackRoute;
85
- const result = await this.currentRoute[i].guard.beforeEach();
86
- if (!result) {
87
- this.currentRoute = this.previousRoute;
88
- Router.go(fallbackRoute || this.urlLast);
89
- return;
90
- }
37
+ if (!bestMatch || route.path.length > bestMatch.path.length) {
38
+ bestMatch = {
39
+ path: route.path,
40
+ // @ts-ignore
41
+ routers: route.routers.map((route) => ({ ...route, build: undefined })),
42
+ params,
43
+ };
91
44
  }
92
45
  }
93
- const toBuild = this.currentRoute[0];
94
- if (toBuild.build == undefined) {
95
- toBuild.build = new toBuild.component();
96
- this.setView(toBuild.build);
97
- }
98
- if (routePageBuildLast?.routeView) {
99
- routePageBuildLast.routeView["updateView"]();
100
- }
101
- this.previousRoute = this.currentRoute;
102
- this.urlLast = window.location.pathname;
103
46
  }
104
- updateRoute(pathname) {
105
- pathname = pathname.replace(/\?$/, "");
106
- const { url, parameters } = this.extractParametersURL(pathname);
107
- Router["_props"] = parameters;
108
- pathname = url.replace(/^#/, "");
109
- if (pathname.charAt(0) != "/")
110
- pathname = "/" + pathname;
111
- this.previousRoute = this.currentRoute;
112
- this.currentRoute = [];
113
- this.findCorrectRoute(pathname);
114
- if (this.currentRoute.length == 0)
115
- this.pageNotFound();
116
- else
117
- this.buildPages();
118
- document.body.emitEvent("router:watch", { url: pathname });
119
- }
120
- getProps(routePage) {
121
- let props = {};
122
- if (routePage) {
123
- props = this.props[routePage.id.split("_")[0]] || {};
47
+ if (bestMatch)
48
+ bestMatch.url = url;
49
+ return bestMatch;
50
+ }
51
+ function pageNotFound() {
52
+ const body = document.createElement("body");
53
+ body.style.width = "100%";
54
+ body.style.height = "100%";
55
+ body.style.overflow = "hidden";
56
+ body.style.position = "fixed";
57
+ body.innerHTML =
58
+ "<div style='text-align: center;'>" +
59
+ "<h1 style='text-shadow: 0 3px 0px $color-base, 0 6px 0px #333; color: #f54f59; font-size: 6em; font-weight: 700; line-height: 0.6em;'>404</h1>" +
60
+ "<h1 style='text-shadow: 0 3px 0px $color-base, 0 6px 0px #333; color: #f54f59; font-size: 10; font-weight: 15; line-height: 0.6em;'>Page not found</h1>" +
61
+ "</div>";
62
+ document.body = body;
63
+ }
64
+ class Router {
65
+ static #router;
66
+ #history;
67
+ #props = {};
68
+ routes = [];
69
+ url = window.location.pathname;
70
+ urlValid = "";
71
+ oldUrl = "";
72
+ match = null;
73
+ /**
74
+ * @param data - An array of route objects.
75
+ * @param history - The type of history to use. Can be either "hash" or "history".
76
+ */
77
+ static SEGMENT_WILDCARD = "*";
78
+ /**
79
+ * @param data - An array of route objects.
80
+ * @param history - The type of history to use. Can be either "hash" or "history".
81
+ * @returns {void}
82
+ */
83
+ static PATH_WILDCARD = "**";
84
+ beforeEach(callback) { }
85
+ constructor(data, history) {
86
+ if (Router.#router)
87
+ throw new Error("Router already exists");
88
+ Router.#router = this;
89
+ this.#history = history;
90
+ this.createAutoId(data, 0);
91
+ this.routes = getRoutePairs(data);
92
+ window.addEventListener(history == "history" ? "popstate" : "hashchange", () => this.handleRoute());
93
+ this.handleRoute();
94
+ }
95
+ createAutoId(routes = [], id) {
96
+ for (const route of routes) {
97
+ route["id"] = `${route.path}_${++id}`;
98
+ if (route.children) {
99
+ id = this.createAutoId(route.children, id);
100
+ }
124
101
  }
125
- return props;
126
- }
127
- get route() {
128
- return this._route;
102
+ return id;
129
103
  }
130
- set route(value) {
131
- this._route = value;
132
- const isHistory = this.buildRoute();
133
- let pathname = window.location.pathname;
134
- pathname = pathname.replace(/\?$/, "");
135
- if (pathname.charAt(pathname.length - 1) == "/") {
136
- pathname = pathname.substring(0, pathname.length - 1) || "/";
104
+ getQueryParams(url) {
105
+ const queryParams = {};
106
+ const queryString = url.split('?')[1];
107
+ if (queryString) {
108
+ const pairs = queryString.split('&');
109
+ for (const pair of pairs) {
110
+ const [key, value] = pair.split('=');
111
+ queryParams[decodeURIComponent(key)] = decodeURIComponent(value || '');
112
+ }
137
113
  }
138
- if (this.route.history == "hash") {
139
- pathname = window.location.hash.replace(/^#/, "");
114
+ return queryParams;
115
+ }
116
+ handleRoute() {
117
+ const url = this.#history === "hash" ? window.location.hash : window.location.pathname || "/";
118
+ this.url = url;
119
+ if (this.#history === "hash")
120
+ this.url = this.url.replace(/^#\//, "/");
121
+ this.#props = this.getQueryParams(this.url);
122
+ this.buildRoutePage(matchRoute(this.url.split('?')[0], this.routes));
123
+ }
124
+ async buildRoutePage(match) {
125
+ if (!match) {
126
+ pageNotFound();
127
+ this.match = null;
140
128
  }
141
- if (isHistory)
142
- this.addHistory(pathname);
143
- }
144
- get props() {
145
- return RouterController._props;
146
- }
147
- set props(value) {
148
- RouterController._props = value;
149
- }
150
- addHistory(pathname) {
151
- pathname = pathname.replace(/^#/, "");
152
- pathname = pathname.replace(/\?$/, "");
153
- if (pathname.charAt(0) != "/")
154
- pathname = "/" + pathname;
155
- if (this.route.history == "hash")
156
- window.location.hash = "#" + pathname;
157
129
  else {
158
- window.history.pushState({}, "", pathname);
159
- this.updateRoute(this.route.history == "history" ? window.location.pathname : window.location.hash);
160
- }
161
- }
162
- getRouteViewFree(routeView) {
163
- // @ts-ignore
164
- let index = Array.from(document.querySelectorAll("route-view") || []).findIndex((r) => r == routeView);
165
- if (index == -1 || index >= this.currentRoute.length)
166
- return undefined;
167
- this.currentRoute[index++].routeView = routeView;
168
- return this.currentRoute.length > index ? this.currentRoute[index] : undefined;
169
- }
170
- getView(routeView) {
171
- if (routeView) {
172
- if (routeView.build == undefined) {
173
- routeView.build = new routeView.component();
130
+ if (!await this.checkGuard(match))
131
+ return;
132
+ if (this.match) {
133
+ for (let i = 1; i < match.routers.length; i++) {
134
+ if (!this.match?.routers[i]?.id)
135
+ break;
136
+ const route = match.routers[i];
137
+ if (route.id === this.match?.routers[i]?.id) {
138
+ route.build = this.match.routers[i].build;
139
+ }
140
+ }
174
141
  }
175
- return routeView.build;
142
+ for (let i = 1; i < match.routers.length; i++) {
143
+ const route = match.routers[i];
144
+ route.parent = match.routers[i - 1];
145
+ }
146
+ this.match = match;
147
+ const buildRoot = match.routers[0];
148
+ if (!buildRoot.build)
149
+ buildRoot.build = __1.App.setPage(new buildRoot.component);
176
150
  }
177
- return undefined;
178
- }
179
- pageNotFound() {
180
- if (this.route?.pageNotFound)
181
- this.setView(new this.route.pageNotFound());
182
- else {
183
- const body = document.createElement("body");
184
- body.style.width = "100%";
185
- body.style.height = "100%";
186
- body.style.overflow = "hidden";
187
- body.style.position = "fixed";
188
- body.innerHTML =
189
- "<div style='text-align: center;'>" +
190
- "<h1 style='text-shadow: 0 3px 0px $color-base, 0 6px 0px #333; color: #f54f59; font-size: 6em; font-weight: 700; line-height: 0.6em;'>404</h1>" +
191
- "<h1 style='text-shadow: 0 3px 0px $color-base, 0 6px 0px #333; color: #f54f59; font-size: 10; font-weight: 15; line-height: 0.6em;'>Page not found</h1>" +
192
- "</div>";
193
- document.body = body;
151
+ document.body.emitEvent("router:watch", { url: this.url, props: this.#props });
152
+ }
153
+ async checkGuard(match) {
154
+ const guards = match.routers.map((route) => route.guard).filter(Boolean);
155
+ let result = true;
156
+ for await (const guard of guards) {
157
+ const primose = new Promise((resolve) => {
158
+ const response = {
159
+ redirect: (path) => {
160
+ resolve(false);
161
+ Router.go(path);
162
+ },
163
+ resolve: () => {
164
+ resolve(true);
165
+ },
166
+ break: () => {
167
+ resolve(false);
168
+ },
169
+ reject: () => {
170
+ resolve(false);
171
+ }
172
+ };
173
+ guard.beforeEach(response);
174
+ });
175
+ result = await primose;
194
176
  }
195
- }
196
- setView(view) {
197
- if (this.view)
198
- document.body.removeChild(this.view);
199
- this.view = view;
200
- document.body.appendChild(view);
201
- }
202
- emitRouteChange(element, url) {
203
- if (element == undefined)
204
- return;
205
- element.onChangeRoute(url);
206
- for (const child of Array.from(element.children)) {
207
- this.emitRouteChange(child, url);
208
- }
209
- }
210
- extractParametersURL(url) {
211
- const parameters = {};
212
- const queryString = url.includes("?") ? url.split("?")[1] : "";
213
- const pares = queryString.split("&");
214
- pares.forEach((par) => {
215
- const [chave, valor] = par.split("=");
216
- parameters[decodeURIComponent(chave)] = decodeURIComponent(valor);
217
- });
218
- return {
219
- url: url.split("?")[0].replace("?", ""),
220
- parameters: parameters,
221
- };
222
- }
223
- }
224
- class Router {
225
- routes;
226
- history;
227
- pageNotFound;
228
- static controller = new RouterController();
229
- static _props = {};
230
- static get PATH() {
231
- return window.location.pathname == "/" ? (window.location.hash.replace(/^#/, "") || "/") : window.location.pathname;
232
- }
233
- static PATH_FULL = "*";
234
- constructor(routes = [], history = "history", pageNotFound = undefined) {
235
- this.routes = routes;
236
- this.history = history;
237
- this.pageNotFound = pageNotFound;
238
- Router.createAutoId(this.routes, 0);
239
- }
240
- beforeEach(callback) { }
241
- static get props() {
242
- return Router._props;
177
+ if (result)
178
+ this.urlValid = match.url;
179
+ else
180
+ Router.go(Router.#router?.urlValid);
181
+ return result;
243
182
  }
244
183
  static create(data) {
245
- if (Router.controller.route)
246
- throw new Error("Router already exists");
247
- Router.controller.route = new Router(data.routes, data.history || "history", data.pageNotFound);
184
+ Router.#router = new Router(data.routes, data.history || "history");
248
185
  }
249
186
  static reload() {
250
187
  window.location.reload();
251
188
  }
189
+ static get props() {
190
+ if (!Router.#router)
191
+ return {};
192
+ return Router.#router.#props;
193
+ }
194
+ static get history() {
195
+ return Router.#router.#history;
196
+ }
252
197
  static buildURL(baseUrl, parametros) {
253
198
  const queryString = Object.keys(parametros)
254
199
  .map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(parametros[key]))
255
200
  .join("&");
256
201
  return baseUrl + (queryString ? "?" + queryString : "");
257
202
  }
258
- static createAutoId(routes = [], id) {
259
- for (const route of routes) {
260
- route["id"] = `${route.path}_${++id}`;
261
- if (route.children) {
262
- id = this.createAutoId(route.children, id);
263
- }
264
- }
265
- return id;
266
- }
267
- static async go(pathname, props = {}) {
268
- pathname = Router.buildURL(pathname, props);
269
- this.controller.addHistory(pathname);
203
+ static async go(url, props) {
204
+ if (!url)
205
+ return;
206
+ if (!url.startsWith("/"))
207
+ url = "/" + url;
208
+ const newUrl = Router.buildURL(url, props || {});
209
+ if (Router.#router?.oldUrl === newUrl)
210
+ return;
211
+ else if (Router.#router)
212
+ Router.#router.oldUrl = newUrl;
213
+ Router.#router.url = newUrl;
214
+ if (Router.#router.#history === "history")
215
+ history.pushState(props || {}, "", newUrl);
216
+ else
217
+ window.location.hash = newUrl;
218
+ Router.#router?.handleRoute();
270
219
  }
271
220
  static back() {
272
221
  window.history.back();
273
222
  }
223
+ static forward() {
224
+ window.history.forward();
225
+ }
226
+ static getRouteViewFree(routeView) {
227
+ if (!Router.#router || !Router.#router.match)
228
+ return undefined;
229
+ const { routers } = Router.#router.match;
230
+ const route = routers.find((route) => (!route.routeView || route.routeView === routeView) && route.parent);
231
+ if (!route)
232
+ return undefined;
233
+ route.routeView = routeView;
234
+ return route;
235
+ }
274
236
  }
275
237
  exports.Router = Router;
276
238
  //# sourceMappingURL=Router.js.map