typecomposer 0.1.2 → 0.1.3

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,241 @@
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
+ // Garante que os caminhos não fiquem com "//"
9
+ const fullPath = `${basePath}/${route.path}`.replace(/\/+/g, '/');
10
+ // Cria uma nova lista de routers herdando os pais
11
+ const routers = [...parentRouters];
12
+ if (route.component) {
13
+ // @ts-ignore
14
+ routers.push({ component: route.component, path: route.path, id: route.id, guard: route.guard });
60
15
  }
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
- }
16
+ result.push({ path: fullPath, routers });
17
+ if (route.children) {
18
+ result = result.concat(getRoutePairs(route.children, fullPath, routers));
71
19
  }
72
- return true;
73
20
  }
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];
21
+ return result;
22
+ }
23
+ function matchRoute(url, routes) {
24
+ let bestMatch = null;
25
+ for (const route of routes) {
26
+ const routeRegex = new RegExp(`^${route.path
27
+ .replace(/\*\*/g, '.*') // '**' aceita qualquer coisa após aquele ponto
28
+ .replace(/\*/g, '[^/]+') // '*' aceita apenas um único segmento
29
+ .replace(/:[^\s/]+/g, '([^/]+)')}$`);
30
+ const match = url.match(routeRegex);
31
+ if (match) {
32
+ const params = {};
33
+ const paramKeys = route.path.match(/:[^\s/]+/g);
34
+ if (paramKeys) {
35
+ paramKeys.forEach((key, index) => {
36
+ params[key.replace(':', '')] = match[index + 1];
37
+ });
82
38
  }
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
- }
39
+ // Verifica se a nova correspondência é mais específica que a anterior
40
+ if (!bestMatch || route.path.length > bestMatch.path.length) {
41
+ bestMatch = {
42
+ path: route.path,
43
+ // @ts-ignore
44
+ routers: route.routers,
45
+ params,
46
+ };
91
47
  }
92
48
  }
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
49
  }
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]] || {};
50
+ if (bestMatch)
51
+ bestMatch.url = url;
52
+ return bestMatch;
53
+ }
54
+ function pageNotFound() {
55
+ const body = document.createElement("body");
56
+ body.style.width = "100%";
57
+ body.style.height = "100%";
58
+ body.style.overflow = "hidden";
59
+ body.style.position = "fixed";
60
+ body.innerHTML =
61
+ "<div style='text-align: center;'>" +
62
+ "<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>" +
63
+ "<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>" +
64
+ "</div>";
65
+ document.body = body;
66
+ }
67
+ class Router {
68
+ static #router;
69
+ #history;
70
+ #props = {};
71
+ routes = [];
72
+ url = window.location.pathname;
73
+ urlValid = "";
74
+ oldUrl = "";
75
+ match = null;
76
+ /**
77
+ * @param data - An array of route objects.
78
+ * @param history - The type of history to use. Can be either "hash" or "history".
79
+ */
80
+ static SEGMENT_WILDCARD = "*";
81
+ /**
82
+ * @param data - An array of route objects.
83
+ * @param history - The type of history to use. Can be either "hash" or "history".
84
+ * @returns {void}
85
+ */
86
+ static PATH_WILDCARD = "**";
87
+ beforeEach(callback) { }
88
+ constructor(data, history) {
89
+ if (Router.#router)
90
+ throw new Error("Router already exists");
91
+ Router.#router = this;
92
+ this.#history = history;
93
+ this.createAutoId(data, 0);
94
+ this.routes = getRoutePairs(data);
95
+ window.addEventListener(history == "history" ? "popstate" : "hashchange", () => this.handleRoute());
96
+ this.handleRoute();
97
+ }
98
+ createAutoId(routes = [], id) {
99
+ for (const route of routes) {
100
+ route["id"] = `${route.path}_${++id}`;
101
+ if (route.children) {
102
+ id = this.createAutoId(route.children, id);
103
+ }
124
104
  }
125
- return props;
126
- }
127
- get route() {
128
- return this._route;
105
+ return id;
129
106
  }
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) || "/";
107
+ getQueryParams(url) {
108
+ const queryParams = {};
109
+ const queryString = url.split('?')[1]; // Separa a parte dos parâmetros
110
+ if (queryString) {
111
+ const pairs = queryString.split('&'); // Divide cada par chave=valor
112
+ for (const pair of pairs) {
113
+ const [key, value] = pair.split('=');
114
+ queryParams[decodeURIComponent(key)] = decodeURIComponent(value || '');
115
+ }
137
116
  }
138
- if (this.route.history == "hash") {
139
- pathname = window.location.hash.replace(/^#/, "");
117
+ return queryParams;
118
+ }
119
+ handleRoute() {
120
+ const url = this.#history === "hash" ? window.location.hash : window.location.pathname || "/";
121
+ this.url = url;
122
+ if (this.#history === "hash")
123
+ this.url = this.url.replace(/^#\//, "/");
124
+ this.#props = this.getQueryParams(this.url);
125
+ this.buildRoutePage(matchRoute(this.url.split('?')[0], this.routes), Router.buildURL(this.url, this.#props));
126
+ }
127
+ async buildRoutePage(match, url) {
128
+ if (!match) {
129
+ pageNotFound();
130
+ this.match = null;
140
131
  }
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
132
  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();
133
+ if (!await this.checkGuard(match))
134
+ return;
135
+ const buildRoot = match.routers[0];
136
+ if (this.match) {
137
+ for (let i = 1; i < match.routers.length; i++) {
138
+ if (!this.match?.routers[i]?.id)
139
+ break;
140
+ const route = match.routers[i];
141
+ if (route.id === this.match?.routers[i]?.id) {
142
+ route.build = this.match.routers[i].build;
143
+ }
144
+ }
174
145
  }
175
- return routeView.build;
146
+ for (let i = 1; i < match.routers.length; i++) {
147
+ const route = match.routers[i];
148
+ route.parent = match.routers[i - 1];
149
+ }
150
+ this.match = match;
151
+ if (!buildRoot.build)
152
+ buildRoot.build = __1.App.setPage(new buildRoot.component);
176
153
  }
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;
154
+ document.body.emitEvent("router:watch", { url: this.url, props: this.#props });
155
+ }
156
+ async checkGuard(match) {
157
+ const guards = match.routers.map((route) => route.guard).filter(Boolean);
158
+ let result = true;
159
+ for await (const guard of guards) {
160
+ const primose = new Promise((resolve) => {
161
+ const response = {
162
+ redirect: (path) => {
163
+ resolve(false);
164
+ Router.go(path);
165
+ },
166
+ resolve: () => {
167
+ resolve(true);
168
+ },
169
+ break: () => {
170
+ resolve(false);
171
+ },
172
+ reject: () => {
173
+ resolve(false);
174
+ }
175
+ };
176
+ guard.beforeEach(response);
177
+ });
178
+ result = await primose;
194
179
  }
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;
180
+ if (result)
181
+ this.urlValid = match.url;
182
+ else
183
+ Router.go(Router.#router?.urlValid);
184
+ return result;
243
185
  }
244
186
  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);
187
+ Router.#router = new Router(data.routes, data.history || "history");
248
188
  }
249
189
  static reload() {
250
190
  window.location.reload();
251
191
  }
192
+ static get props() {
193
+ if (!Router.#router)
194
+ return {};
195
+ return Router.#router.#props;
196
+ }
197
+ static get history() {
198
+ return Router.#router.#history;
199
+ }
252
200
  static buildURL(baseUrl, parametros) {
253
201
  const queryString = Object.keys(parametros)
254
202
  .map((key) => encodeURIComponent(key) + "=" + encodeURIComponent(parametros[key]))
255
203
  .join("&");
256
204
  return baseUrl + (queryString ? "?" + queryString : "");
257
205
  }
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);
206
+ static async go(url, props) {
207
+ if (!url)
208
+ return;
209
+ if (!url.startsWith("/"))
210
+ url = "/" + url;
211
+ const newUrl = Router.buildURL(url, props || {});
212
+ if (Router.#router?.oldUrl === newUrl)
213
+ return;
214
+ else if (Router.#router)
215
+ Router.#router.oldUrl = newUrl;
216
+ Router.#router.url = newUrl;
217
+ if (Router.#router.#history === "history")
218
+ history.pushState(props || {}, "", newUrl);
219
+ else
220
+ window.location.hash = newUrl;
221
+ Router.#router?.handleRoute();
270
222
  }
271
223
  static back() {
272
224
  window.history.back();
273
225
  }
226
+ static forward() {
227
+ window.history.forward();
228
+ }
229
+ static getRouteViewFree(routeView) {
230
+ if (!Router.#router || !Router.#router.match)
231
+ return undefined;
232
+ const { routers } = Router.#router.match;
233
+ const route = routers.find((route) => (!route.routeView || route.routeView === routeView) && route.parent);
234
+ if (!route)
235
+ return undefined;
236
+ route.routeView = routeView;
237
+ return route;
238
+ }
274
239
  }
275
240
  exports.Router = Router;
276
241
  //# sourceMappingURL=Router.js.map