Skip to content

Commit a1b48da

Browse files
authored
Merge pull request #33 from KiranMantha/dev-branch
Fixed issues in nested routes
2 parents b9b0966 + 052817f commit a1b48da

File tree

5 files changed

+114
-95
lines changed

5 files changed

+114
-95
lines changed

Diff for: dist/index.js

+100-93
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
var b = Object.defineProperty;
2-
var w = (e, t, a) => t in e ? b(e, t, { enumerable: !0, configurable: !0, writable: !0, value: a }) : e[t] = a;
3-
var u = (e, t, a) => w(e, typeof t != "symbol" ? t + "" : t, a);
4-
import { BehaviourSubjectObs as g, SubjectObs as P, fromEvent as O, wrapIntoObservable as v, Injectable as _, Component as E, signal as S, Subscriptions as T } from "@plumejs/core";
5-
const C = /:([^/]+)/g, x = (e) => {
1+
var R = Object.defineProperty;
2+
var w = (r, t, e) => t in r ? R(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
3+
var c = (r, t, e) => w(r, typeof t != "symbol" ? t + "" : t, e);
4+
import { BehaviourSubjectObs as g, SubjectObs as P, fromEvent as T, wrapIntoObservable as d, Injectable as _, Component as O, signal as E, Subscriptions as S } from "@plumejs/core";
5+
const A = /:([^/]+)/g, C = (r) => {
66
const t = [];
7-
let a;
8-
for (; (a = C.exec(e)) !== null; )
9-
t.push(a[0]);
7+
let e;
8+
for (; (e = A.exec(r)) !== null; )
9+
t.push(e[0]);
1010
return t;
11-
}, B = (e, t) => e && t ? new RegExp(`^${e.replace(/:[^\s/]+/g, "([^/]+)")}$`).test(t) : !1;
12-
function y(e, t) {
13-
const r = e.split("?")[0].split("/").filter(Boolean);
11+
}, B = (r, t) => r && t ? new RegExp(`^${r.replace(/:[^\s/]+/g, "([^/]+)")}$`).test(t) : !1;
12+
function x(r, t) {
13+
const a = r.split("?")[0].split("/").filter(Boolean);
1414
for (const n of t) {
1515
const s = n.fragments;
16-
if (s.length >= r.length || s.includes("*")) {
17-
const i = {};
16+
if (s.length >= a.length || s.includes("*")) {
17+
const o = {};
1818
let h = !0;
19-
for (let o = 0; o < s.length; o++) {
20-
const l = s[o], m = r[o];
19+
for (let i = 0; i < s.length; i++) {
20+
const l = s[i], m = a[i];
2121
if (l === "*") {
2222
h = !0;
2323
break;
@@ -27,83 +27,80 @@ function y(e, t) {
2727
break;
2828
}
2929
if (l.startsWith(":"))
30-
i[l.substring(1)] = m;
30+
o[l.substring(1)] = m;
3131
else if (l !== m) {
3232
h = !1;
3333
break;
3434
}
3535
}
3636
if (h) {
37-
const o = Object.fromEntries(
37+
const i = Object.fromEntries(
3838
new URLSearchParams(window.location.search).entries()
3939
);
40-
return { route: n, routeData: { path: e, routeParams: i, queryParams: o } };
40+
return { route: n, routeData: { path: r, routeParams: o, queryParams: i } };
4141
}
4242
}
4343
}
4444
return null;
4545
}
46-
const A = `
46+
const y = `
4747
<div style='text-align: center'>
4848
<h1>404</h1>
4949
<h3>not Found</h3>
5050
</div>
5151
`, p = class p {
5252
static formatRoute(t) {
53-
const a = {
53+
const e = {
5454
fragments: [],
55-
params: x(t.path),
55+
params: C(t.path),
5656
path: t.path,
5757
template: "",
58+
templatePath: null,
5859
paramCount: 0,
5960
isRegistered: !1,
6061
redirectTo: t.redirectTo,
6162
preload: t.preload,
6263
canActivate: () => !0
6364
};
64-
if (a.fragments = t.path.split("/").filter((r) => r.length > 0), t.template) {
65-
if (!t.templatePath) throw Error("templatePath is required in route if template is mentioned.");
66-
a.template = t.template, a.templatePath = t.templatePath;
67-
}
68-
t.canActivate && (a.canActivate = t.canActivate), a.paramCount = a.params.length, p.routeList.push(a);
65+
e.fragments = t.path.split("/").filter((a) => a.length > 0), t.template && (e.template = t.template, e.templatePath = t.templatePath), t.canActivate && (e.canActivate = t.canActivate), e.paramCount = e.params.length, p.routeList.push(e);
6966
}
7067
static preloadRoutes() {
7168
for (const t of p.routeList)
7269
t.templatePath && t.templatePath();
7370
}
7471
static preloadSelectedRoutes() {
75-
const t = p.routeList.filter((a) => a.preload === !0);
76-
for (const a of t)
77-
a.templatePath && a.templatePath();
72+
const t = p.routeList.filter((e) => e.preload === !0);
73+
for (const e of t)
74+
e.templatePath && e.templatePath();
7875
}
7976
};
80-
u(p, "routeList", []);
81-
let c = p;
82-
var j = Object.defineProperty, D = Object.getOwnPropertyDescriptor, L = (e, t, a, r) => {
83-
for (var n = r > 1 ? void 0 : r ? D(t, a) : t, s = e.length - 1, i; s >= 0; s--)
84-
(i = e[s]) && (n = (r ? i(t, a, n) : i(n)) || n);
85-
return r && n && j(t, a, n), n;
77+
c(p, "routeList", []);
78+
let u = p;
79+
var L = Object.defineProperty, $ = Object.getOwnPropertyDescriptor, j = (r, t, e, a) => {
80+
for (var n = a > 1 ? void 0 : a ? $(t, e) : t, s = r.length - 1, o; s >= 0; s--)
81+
(o = r[s]) && (n = (a ? o(t, e, n) : o(n)) || n);
82+
return a && n && L(t, e, n), n;
8683
};
8784
let f = class {
8885
constructor() {
89-
u(this, "_currentRoute", new g({
86+
c(this, "_currentRoute", new g({
9087
path: "",
9188
routeParams: {},
9289
queryParams: {},
9390
state: {}
9491
}));
95-
u(this, "_template", new g(""));
96-
u(this, "_navigationEndEvent", new P());
97-
u(this, "_routeStateMap", /* @__PURE__ */ new Map());
92+
c(this, "_template", new g(""));
93+
c(this, "_navigationEndEvent", new P());
94+
c(this, "_routeStateMap", /* @__PURE__ */ new Map());
9895
}
9996
listenRouteChanges() {
100-
const e = "popstate";
101-
return window.history.replaceState({}, null, ""), function(t, a) {
102-
const r = t.pushState;
97+
const r = "popstate";
98+
return window.history.replaceState({}, null, ""), function(t, e) {
99+
const a = t.pushState;
103100
t.pushState = function(...n) {
104-
r.apply(t, n), a();
101+
a.apply(t, n), e();
105102
};
106-
}(window.history, this._registerOnHashChange.bind(this)), O(window, e, () => {
103+
}(window.history, this._registerOnHashChange.bind(this)), T(window, r, () => {
107104
this._registerOnHashChange();
108105
});
109106
}
@@ -113,51 +110,51 @@ let f = class {
113110
getCurrentRoute() {
114111
return this._currentRoute.asObservable();
115112
}
116-
navigateTo(e = "/", t = null) {
117-
const a = window.location.pathname || "/";
118-
this._routeStateMap.clear(), this._routeStateMap.set(e, t), a === e ? this._navigateTo(e, t) : window.history.pushState(t, "", e);
113+
navigateTo(r = "/", t = null) {
114+
const e = window.location.pathname || "/";
115+
this._routeStateMap.clear(), this._routeStateMap.set(r, t), e === r ? this._navigateTo(r, t) : window.history.pushState(t, "", r);
119116
}
120117
onNavigationEnd() {
121118
return this._navigationEndEvent.asObservable();
122119
}
123120
_registerOnHashChange() {
124-
const e = window.location.pathname, t = this._routeStateMap.get(e);
125-
this._navigateTo(e, t);
126-
}
127-
_navigateTo(e, t) {
128-
const a = y(e, c.routeList);
129-
if (a) {
130-
const r = a.route, { routeParams: n, queryParams: s } = a.routeData, i = {
131-
path: e,
121+
const r = window.location.pathname, t = this._routeStateMap.get(r);
122+
this._navigateTo(r, t);
123+
}
124+
_navigateTo(r, t) {
125+
const e = x(r, u.routeList);
126+
if (e) {
127+
const a = e.route, { routeParams: n, queryParams: s } = e.routeData, o = {
128+
path: r,
132129
state: { ...t || {} },
133130
routeParams: n,
134131
queryParams: s
135132
};
136-
v(r.canActivate()).subscribe((h) => {
133+
d(a.canActivate()).subscribe((h) => {
137134
if (h)
138-
if (e) {
139-
const o = (l) => {
140-
l.isRegistered = !0, this._currentRoute.next(i), this._template.next(l.template), this._navigationEndEvent.next(null);
135+
if (r) {
136+
const i = (l) => {
137+
l.isRegistered = !0, this._currentRoute.next(o), this._template.next(l.template), this._navigationEndEvent.next(null);
141138
};
142-
r.isRegistered ? o(r) : r.templatePath ? v(r.templatePath()).subscribe(() => {
143-
o(r);
144-
}) : r.redirectTo && this.navigateTo(r.redirectTo, t);
139+
a.isRegistered ? i(a) : a.templatePath ? d(a.templatePath()).subscribe(() => {
140+
i(a);
141+
}) : a.redirectTo ? this.navigateTo(a.redirectTo, t) : i(a);
145142
} else
146-
this.navigateTo(r.redirectTo, t);
143+
this.navigateTo(a.redirectTo, t);
147144
});
148145
} else {
149-
const r = c.routeList.find((n) => n.path === "/404");
150-
r ? this._navigateTo(r.path, t) : (this._template.next(A), this._navigationEndEvent.next(null));
146+
const a = u.routeList.find((n) => n.path === "/404");
147+
a ? this._navigateTo(a.path, t) : (this._template.next(y), this._navigationEndEvent.next(null));
151148
}
152149
}
153150
};
154-
f = L([
151+
f = j([
155152
_()
156153
], f);
157-
class M {
154+
class D {
158155
constructor(t) {
159-
u(this, "_template", S(""));
160-
u(this, "_subscriptions", new T());
156+
c(this, "_template", E(""));
157+
c(this, "_subscriptions", new S());
161158
this.internalRouterSrvc = t;
162159
}
163160
beforeMount() {
@@ -178,49 +175,59 @@ class M {
178175
return this._template();
179176
}
180177
}
181-
const $ = () => {
182-
E({
178+
const M = () => {
179+
O({
183180
selector: "router-outlet",
184181
deps: [f]
185-
})(M);
182+
})(D);
186183
};
187-
var N = Object.defineProperty, q = Object.getOwnPropertyDescriptor, F = (e, t, a, r) => {
188-
for (var n = r > 1 ? void 0 : r ? q(t, a) : t, s = e.length - 1, i; s >= 0; s--)
189-
(i = e[s]) && (n = (r ? i(t, a, n) : i(n)) || n);
190-
return r && n && N(t, a, n), n;
184+
var N = Object.defineProperty, F = Object.getOwnPropertyDescriptor, I = (r, t, e, a) => {
185+
for (var n = a > 1 ? void 0 : a ? F(t, e) : t, s = r.length - 1, o; s >= 0; s--)
186+
(o = r[s]) && (n = (a ? o(t, e, n) : o(n)) || n);
187+
return a && n && N(t, e, n), n;
191188
};
192-
function R(e, t = "") {
193-
const a = `${t}/${e.path}`.replace(/\/+/g, "/");
194-
c.formatRoute({ ...e, path: a }), (e.children || []).forEach((r) => {
195-
R(r, a);
189+
function b(r, t = "") {
190+
const e = `${t}/${r.path}`.replace(/\/+/g, "/");
191+
u.formatRoute({ ...r, path: e }), (r.children || []).forEach((a) => {
192+
b({ ...a, canActivate: r.canActivate }, e);
196193
});
197194
}
198-
let d = class {
199-
constructor(e) {
200-
this.internalRouter = e, $();
195+
let v = class {
196+
constructor(r) {
197+
this.internalRouter = r, M();
201198
}
202199
getCurrentRoute() {
203200
return this.internalRouter.getCurrentRoute();
204201
}
205-
navigateTo(e, t) {
206-
this.internalRouter.navigateTo(e, t);
202+
navigateTo(r, t) {
203+
this.internalRouter.navigateTo(r, t);
207204
}
208205
onNavigationEnd() {
209206
return this.internalRouter.onNavigationEnd();
210207
}
211-
static registerRoutes({ routes: e, preloadAllRoutes: t = !1 }) {
212-
if (Array.isArray(e)) {
213-
for (const a of e)
214-
R(a);
215-
t ? c.preloadRoutes() : c.preloadSelectedRoutes();
208+
static registerRoutes({ routes: r, preloadAllRoutes: t = !1 }) {
209+
if (Array.isArray(r)) {
210+
for (const e of r) {
211+
if (!e.template && !e.redirectTo && !(e.children || []).length)
212+
throw Error("A route should have either a template or redirectTo path or child routes.");
213+
if (!e.template && !e.redirectTo && (e.children || []).length) {
214+
const a = e.children.find((n) => n.redirectTo);
215+
e.redirectTo = `${e.path}/${(a == null ? void 0 : a.redirectTo) ?? e.children[0].path}`.replace(
216+
"//",
217+
"/"
218+
);
219+
}
220+
b(e);
221+
}
222+
t ? u.preloadRoutes() : u.preloadSelectedRoutes(), console.log(u.routeList);
216223
} else
217224
throw Error("router.addRoutes: the parameter must be an array");
218225
}
219226
};
220-
d = F([
227+
v = I([
221228
_({ deps: [f] })
222-
], d);
229+
], v);
223230
export {
224-
d as Router,
231+
v as Router,
225232
B as matchPath
226233
};

Diff for: dist/index.js.br

93 Bytes
Binary file not shown.

Diff for: src/internalRouter.service.ts

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ export class InternalRouter {
8888
});
8989
} else if (routeItem.redirectTo) {
9090
this.navigateTo(routeItem.redirectTo, state);
91+
} else {
92+
triggerNavigation(routeItem);
9193
}
9294
} else {
9395
triggerNavigation(routeItem);

Diff for: src/router.service.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function normalizeRoute(route: Route, parentPath = '') {
1111
StaticRouter.formatRoute({ ...route, path: fullPath });
1212

1313
(route.children || []).forEach((childRoute) => {
14-
normalizeRoute(childRoute, fullPath);
14+
normalizeRoute({ ...childRoute, canActivate: route.canActivate }, fullPath);
1515
});
1616
}
1717

@@ -36,6 +36,16 @@ export class Router {
3636
static registerRoutes({ routes, preloadAllRoutes = false }: { routes: Array<Route>; preloadAllRoutes?: boolean }) {
3737
if (Array.isArray(routes)) {
3838
for (const route of routes) {
39+
if (!route.template && !route.redirectTo && !(route.children || []).length) {
40+
throw Error('A route should have either a template or redirectTo path or child routes.');
41+
}
42+
if (!route.template && !route.redirectTo && (route.children || []).length) {
43+
const redirectedChildRoute = route.children.find((childRoute) => childRoute.redirectTo);
44+
route.redirectTo = `${route.path}/${redirectedChildRoute?.redirectTo ?? route.children[0].path}`.replace(
45+
'//',
46+
'/'
47+
);
48+
}
3949
normalizeRoute(route);
4050
}
4151
if (preloadAllRoutes) {

Diff for: src/staticRouter.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class StaticRouter {
1010
params: getParams(route.path),
1111
path: route.path,
1212
template: '',
13+
templatePath: null,
1314
paramCount: 0,
1415
isRegistered: false,
1516
redirectTo: route.redirectTo,
@@ -20,7 +21,6 @@ export class StaticRouter {
2021
return str.length > 0;
2122
});
2223
if (route.template) {
23-
if (!route.templatePath) throw Error('templatePath is required in route if template is mentioned.');
2424
internalRouteItem.template = route.template;
2525
internalRouteItem.templatePath = route.templatePath;
2626
}

0 commit comments

Comments
 (0)