zova-module-a-router 5.0.71 → 5.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.
package/dist/index.js ADDED
@@ -0,0 +1,775 @@
1
+ import { BeanBase, BeanControllerBase, BeanControllerPageBase, BeanInfo, BeanScopeBase, BeanSimple, Use, Virtual, cast, deepExtend, prepareComponentOptions, useComputed, useController } from "zova";
2
+ import { BeanModelBase, Model } from "zova-module-a-model";
3
+ import * as ModuleInfo from "@cabloy/module-info";
4
+ import { combineParamsAndQuery, combineQueries } from "@cabloy/utils";
5
+ import { RouterView, createMemoryHistory, createRouter, createWebHashHistory, createWebHistory, routerViewLocationKey } from "@cabloy/vue-router";
6
+ import { Bean, Controller, Scope, Service, Sys } from "zova-module-a-bean";
7
+ import { KeepAlive, Transition, createVNode, defineComponent, h, inject, shallowReactive } from "vue";
8
+ import "vue-router";
9
+ //#region src/model/pageData.ts
10
+ var _dec$6, _dec2$6, _class$6;
11
+ var ModelPageData = (_dec$6 = Model(), _dec2$6 = BeanInfo({ module: "a-router" }), _dec$6(_class$6 = _dec2$6(_class$6 = class ModelPageData extends BeanModelBase {
12
+ constructor(...args) {
13
+ super(...args);
14
+ this._pageDataInner = void 0;
15
+ this.current = void 0;
16
+ }
17
+ async __init__() {
18
+ if (process.env.SERVER) {
19
+ const pagePath = this.$ssr.state.pagePath;
20
+ if (pagePath) {
21
+ this._pageDataInner = this.getPageData(pagePath);
22
+ this._pageDataInner = this.$ssr.state.pageData;
23
+ this.current = this._pageDataInner;
24
+ }
25
+ } else if (this.$ssr.isRuntimeSsrPreHydration) this.current = this.$ssr.state.pageData;
26
+ else {
27
+ const route = this.$pageRoute;
28
+ this.current = route ? this.getPageData(route.path) : void 0;
29
+ }
30
+ }
31
+ getPageData(pagePath) {
32
+ return this.$useStateMem({ queryKey: ["pageData", pagePath] });
33
+ }
34
+ }) || _class$6) || _class$6);
35
+ //#endregion
36
+ //#region src/lib/const.ts
37
+ var pageRouteKey = "$$pageRoute";
38
+ var routerViewKey = "$$routerView";
39
+ //#endregion
40
+ //#region src/lib/utils.ts
41
+ function getRouteMatched(route) {
42
+ let match = route.matched.find((item) => item.aliasOf);
43
+ if (match) match = match.aliasOf;
44
+ else match = route.matched[route.matched.length - 1];
45
+ return match;
46
+ }
47
+ function getRealRouteName(name) {
48
+ if (!name) return void 0;
49
+ name = String(name);
50
+ if (name.startsWith("$:")) return void 0;
51
+ return name;
52
+ }
53
+ function isRouterName(name) {
54
+ return !!name && name.includes(":") && !name.includes("/");
55
+ }
56
+ function getPageRoute(ctx) {
57
+ return ctx.bean._getBeanFromHost({ name: pageRouteKey });
58
+ }
59
+ function getCurrentRoute(ctx) {
60
+ return ctx.util.instanceScope(() => {
61
+ return inject(routerViewLocationKey);
62
+ });
63
+ }
64
+ var scrollBehavior = (to, _from, savedPosition) => {
65
+ if (savedPosition) return new Promise((resolve) => {
66
+ setTimeout(() => {
67
+ resolve(savedPosition);
68
+ }, 100);
69
+ });
70
+ else if (to.hash) return new Promise((resolve) => {
71
+ setTimeout(() => {
72
+ resolve({ el: to.hash });
73
+ }, 200);
74
+ });
75
+ else return {
76
+ left: 0,
77
+ top: 0
78
+ };
79
+ };
80
+ //#endregion
81
+ //#region src/types/utils.ts
82
+ var SymbolRouterHistory = Symbol("SymbolRouterHistory");
83
+ //#endregion
84
+ //#region src/bean/sys.router.ts
85
+ var _dec$5, _dec2$5, _class$5;
86
+ var SysRouter = (_dec$5 = Sys(), _dec2$5 = BeanInfo({ module: "a-router" }), _dec$5(_class$5 = _dec2$5(_class$5 = class SysRouter extends BeanBase {
87
+ constructor(...args) {
88
+ super(...args);
89
+ this._vueRouterSys = void 0;
90
+ }
91
+ get router() {
92
+ return this._vueRouterSys;
93
+ }
94
+ __get__(prop) {
95
+ return this._vueRouterSys && this._vueRouterSys[prop];
96
+ }
97
+ async __init__() {
98
+ this._vueRouterSys = this.createRouter();
99
+ this._loadConfigRoutes();
100
+ this._loadLegacyRoutes();
101
+ }
102
+ createRouter(options) {
103
+ options = Object.assign({}, options);
104
+ if (!options.matcher) options.matcher = this._vueRouterSys?.matcher;
105
+ if (!options.routes) {
106
+ if (!this._vueRouterSys) options.routes = [];
107
+ }
108
+ if (!options.scrollBehavior) options.scrollBehavior = this.scope.config.scrollBehavior;
109
+ if (!options.history) {
110
+ const createHistory = process.env.SERVER ? createMemoryHistory : this.sys.env.ROUTER_MODE === "history" ? createWebHistory : createWebHashHistory;
111
+ const routeBase = process.env.SERVER || this.sys.env.ROUTER_MODE === "history" ? this.sys.env.APP_PUBLIC_PATH : void 0;
112
+ options.history = createHistory(routeBase);
113
+ }
114
+ const router = createRouter(options);
115
+ cast(router).__hasDevtools = true;
116
+ router[SymbolRouterHistory] = options.history;
117
+ return router;
118
+ }
119
+ createAsyncComponent(component) {
120
+ if (typeof component !== "string") return component;
121
+ return this.sys.meta.component.createAsyncComponent(component);
122
+ }
123
+ getPagePath(path, options, absolute) {
124
+ const pagePath = combineParamsAndQuery(path, {
125
+ params: options?.params,
126
+ query: options?.query
127
+ });
128
+ return absolute ? this.sys.util.getAbsoluteUrlFromPagePath(pagePath) : pagePath;
129
+ }
130
+ async resolveRoute(url, check404, checkAliasOf) {
131
+ const pagePath = this.sys.util.getPagePathFromAbsoluteUrl(url);
132
+ let route = await this.ensureRoute(pagePath);
133
+ if (check404 && route.name === "$:/:catchAll(.*)*") return;
134
+ if (checkAliasOf) {
135
+ const matchItem = route.matched.find((item) => item.aliasOf);
136
+ if (matchItem) route = matchItem.aliasOf;
137
+ if (check404 && route.name === "$:/:catchAll(.*)*") return;
138
+ }
139
+ return route;
140
+ }
141
+ checkPathValid(to) {
142
+ const _name = to && typeof to === "object" ? to.name : void 0;
143
+ const _path = to && typeof to === "object" ? to.name ?? to.path : to;
144
+ if (this._findLegacyRoute(_name, _path)) return true;
145
+ if (!_path) return true;
146
+ const moduleName = ModuleInfo.parseName(_path);
147
+ if (!moduleName) return true;
148
+ return this.sys.meta.module.exists(moduleName);
149
+ }
150
+ async ensureRoute(pagePath) {
151
+ let route = this._vueRouterSys.resolve(pagePath);
152
+ if (route && route.name !== "$:/:catchAll(.*)*") return route;
153
+ const moduleName = ModuleInfo.parseName(pagePath);
154
+ if (moduleName) {
155
+ if (this.sys.meta.module.exists(moduleName)) {
156
+ if (!this.sys.meta.module.get(moduleName)) {
157
+ await this.sys.meta.module.use(moduleName);
158
+ route = this._vueRouterSys.resolve(pagePath);
159
+ }
160
+ }
161
+ }
162
+ return route;
163
+ }
164
+ /** @internal */
165
+ _registerRoutes(module) {
166
+ if (!module.resource.routes) return;
167
+ for (const route of module.resource.routes) this._registerRoute(route, module);
168
+ }
169
+ /** @internal */
170
+ _findConfigRoute(name, path) {
171
+ name = this.getRealRouteName(name);
172
+ return name ? this.sys.config.routes.name[name] : this.sys.config.routes.path[path];
173
+ }
174
+ /** @internal */
175
+ _findLegacyRoute(name, path) {
176
+ const legacyRoutes = cast(this.sys.meta).legacyRoutes;
177
+ if (!legacyRoutes) return;
178
+ name = this.getRealRouteName(name);
179
+ return legacyRoutes.find((item) => {
180
+ return name ? item.name === name : item.path === path;
181
+ });
182
+ }
183
+ getRouteMatched(route) {
184
+ return getRouteMatched(route);
185
+ }
186
+ getRealRouteName(name) {
187
+ return getRealRouteName(name);
188
+ }
189
+ isRouterName(name) {
190
+ return isRouterName(name);
191
+ }
192
+ resolveName(name, options) {
193
+ const params = cast(options)?.params;
194
+ const query = cast(options)?.query;
195
+ return this._resolveNameOrPath(query, (query) => {
196
+ return this.router.resolve({
197
+ name,
198
+ params,
199
+ query
200
+ }).fullPath;
201
+ });
202
+ }
203
+ resolvePath(path, query) {
204
+ return this._resolveNameOrPath(query, (query) => {
205
+ return this.router.resolve({
206
+ path,
207
+ query
208
+ }).fullPath;
209
+ });
210
+ }
211
+ _resolveNameOrPath(query, fn) {
212
+ const query1 = {};
213
+ const query2 = [];
214
+ if (query) for (const key in query) {
215
+ const value = query[key];
216
+ if (value && typeof value === "object") query2.push([key, value]);
217
+ else query1[key] = value;
218
+ }
219
+ const fullPath = fn(query1);
220
+ const query2str = query2.map(([key, value]) => {
221
+ return `${encodeURIComponent(key)}=${encodeURIComponent(JSON.stringify(value))}`;
222
+ }).join("&");
223
+ if (!query2str) return fullPath;
224
+ return `${fullPath}${Object.keys(query1).length > 0 ? "&" : "?"}${query2str}`;
225
+ }
226
+ _loadConfigRoutes() {
227
+ const routesPath = this.sys.config.routes.path;
228
+ for (const key in routesPath) {
229
+ const route = routesPath[key];
230
+ if (!route) continue;
231
+ this._loadConfigRoute({
232
+ ...route,
233
+ path: key,
234
+ name: `$:${key}`
235
+ });
236
+ }
237
+ const routesName = this.sys.config.routes.name;
238
+ for (const key in routesName) {
239
+ const route = routesName[key];
240
+ if (!route) continue;
241
+ this._loadConfigRoute({
242
+ ...route,
243
+ path: route.path || route.alias,
244
+ name: key
245
+ });
246
+ }
247
+ }
248
+ _loadLegacyRoutes() {
249
+ const legacyRoutes = cast(this.sys.meta).legacyRoutes;
250
+ if (!legacyRoutes) return;
251
+ for (const route of legacyRoutes) this._registerRoute(route);
252
+ }
253
+ _loadConfigRoute(route) {
254
+ this.router.addRoute(route);
255
+ }
256
+ _registerRoute(route, module) {
257
+ let path;
258
+ if (route.path !== void 0) if (!module || route.meta?.absolute === true) path = route.path;
259
+ else path = route.path ? `/${module.info.pid}/${module.info.name}/${route.path}` : `/${module.info.pid}/${module.info.name}`;
260
+ let name;
261
+ if (route.name) if (!module || route.meta?.absolute === true) name = String(route.name);
262
+ else name = `${module.info.relativeName}:${String(route.name)}`;
263
+ const configRoute = name ? this.sys.config.routes.name[name] : this.sys.config.routes.path[path];
264
+ if (configRoute) route = deepExtend({}, route, configRoute);
265
+ if (name && configRoute?.alias) this.router.addRoute({
266
+ name: `$alias:${name}`,
267
+ path: `/__alias__${configRoute?.alias}`,
268
+ redirect: ""
269
+ });
270
+ if (!name) name = `$:${path}`;
271
+ const meta = route.meta;
272
+ const component = route.component;
273
+ let layout = meta?.layout;
274
+ let routeData;
275
+ let routeNameParent;
276
+ if (layout === false) routeData = {
277
+ ...route,
278
+ name,
279
+ path,
280
+ component,
281
+ meta
282
+ };
283
+ else {
284
+ if (layout === void 0 || layout === "default") layout = this.sys.config.layout.component.default;
285
+ else if (layout === "empty") layout = this.sys.config.layout.component.empty;
286
+ routeNameParent = `$:${name}`;
287
+ routeData = {
288
+ name: routeNameParent,
289
+ path,
290
+ component: this.createAsyncComponent(layout),
291
+ children: [{
292
+ ...route,
293
+ name,
294
+ path: "",
295
+ component,
296
+ meta
297
+ }]
298
+ };
299
+ }
300
+ if (this.router.hasRoute(routeNameParent)) this.router.removeRoute(routeNameParent);
301
+ if (this.router.hasRoute(name)) this.router.removeRoute(name);
302
+ this.router.addRoute(routeData);
303
+ }
304
+ }) || _class$5) || _class$5);
305
+ //#endregion
306
+ //#region src/bean/bean.router.ts
307
+ var _dec$4, _dec2$4, _dec3$1, _dec4, _dec5, _dec6, _class$4, _class2, _descriptor, _descriptor2;
308
+ function _initializerDefineProperty(e, i, r, l) {
309
+ r && Object.defineProperty(e, i, {
310
+ enumerable: r.enumerable,
311
+ configurable: r.configurable,
312
+ writable: r.writable,
313
+ value: r.initializer ? r.initializer.call(l) : void 0
314
+ });
315
+ }
316
+ function _applyDecoratedDescriptor(i, e, r, n, l) {
317
+ var a = {};
318
+ return Object.keys(n).forEach(function(i) {
319
+ a[i] = n[i];
320
+ }), a.enumerable = !!a.enumerable, a.configurable = !!a.configurable, ("value" in a || a.initializer) && (a.writable = !0), a = r.slice().reverse().reduce(function(r, n) {
321
+ return n(i, e, r) || r;
322
+ }, a), l && void 0 !== a.initializer && (a.value = a.initializer ? a.initializer.call(l) : void 0, a.initializer = void 0), void 0 === a.initializer ? (Object.defineProperty(i, e, a), null) : a;
323
+ }
324
+ var BeanRouter = (_dec$4 = Bean(), _dec2$4 = BeanInfo({ module: "a-router" }), _dec3$1 = Use(), _dec4 = Reflect.metadata("design:type", typeof SysRouter === "undefined" ? Object : SysRouter), _dec5 = Use(), _dec6 = Reflect.metadata("design:type", typeof ModelPageData === "undefined" ? Object : ModelPageData), _dec$4(_class$4 = _dec2$4(_class$4 = (_class2 = class BeanRouter extends BeanBase {
325
+ constructor(...args) {
326
+ super(...args);
327
+ this._vueRouterApp = void 0;
328
+ this._eventRouterGuards = [];
329
+ this._routerViews = [];
330
+ _initializerDefineProperty(this, "$$sysRouter", _descriptor, this);
331
+ _initializerDefineProperty(this, "$$modelPageData", _descriptor2, this);
332
+ }
333
+ get router() {
334
+ return this._vueRouterApp;
335
+ }
336
+ __dispose__() {
337
+ for (const fn of this._eventRouterGuards) fn();
338
+ }
339
+ __get__(prop) {
340
+ const value = this._vueRouterApp?.[prop];
341
+ if (value !== void 0) return value;
342
+ return this.$$sysRouter?.[prop];
343
+ }
344
+ async __init__(mainRouter) {
345
+ this._vueRouterApp = this.$$sysRouter.createRouter();
346
+ if (!mainRouter) await this.app.meta.event.emit("a-router:routerGuards", this);
347
+ }
348
+ addRouterView(routerView) {
349
+ this._routerViews.push(routerView);
350
+ }
351
+ removeRouterView(routerView) {
352
+ const index = this._routerViews.findIndex((item) => item === routerView);
353
+ if (index > -1) this._routerViews.splice(index, 1);
354
+ }
355
+ afterEachBackRoute(route) {
356
+ for (const routerView of this._routerViews) if (routerView.backRoute(route)) break;
357
+ }
358
+ afterEachForwardRoute(route) {
359
+ for (const routerView of this._routerViews) if (routerView.forwardRoute(route)) break;
360
+ }
361
+ beforeEach(guard) {
362
+ const fn = this._vueRouterApp.beforeEach(guard);
363
+ this._eventRouterGuards.push(fn);
364
+ return fn;
365
+ }
366
+ beforeResolve(guard) {
367
+ const fn = this._vueRouterApp.beforeResolve(guard);
368
+ this._eventRouterGuards.push(fn);
369
+ return fn;
370
+ }
371
+ afterEach(guard) {
372
+ const fn = this._vueRouterApp.afterEach(guard);
373
+ this._eventRouterGuards.push(fn);
374
+ return fn;
375
+ }
376
+ onError(handler) {
377
+ const fn = this._vueRouterApp.onError(handler);
378
+ this._eventRouterGuards.push(fn);
379
+ return fn;
380
+ }
381
+ setPageMeta(route, pageMeta) {
382
+ for (const routerView of this._routerViews) routerView.setPageMeta(route, pageMeta);
383
+ }
384
+ }, _descriptor = _applyDecoratedDescriptor(_class2.prototype, "$$sysRouter", [_dec3$1, _dec4], {
385
+ configurable: true,
386
+ enumerable: true,
387
+ writable: true,
388
+ initializer: null
389
+ }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "$$modelPageData", [_dec5, _dec6], {
390
+ configurable: true,
391
+ enumerable: true,
392
+ writable: true,
393
+ initializer: null
394
+ }), _class2)) || _class$4) || _class$4);
395
+ //#endregion
396
+ //#region src/bean/bean.routerGuardsBase.ts
397
+ var _dec$3, _dec2$3, _dec3, _class$3;
398
+ var BeanRouterGuardsBase = (_dec$3 = Bean(), _dec2$3 = Virtual(), _dec3 = BeanInfo({ module: "a-router" }), _dec$3(_class$3 = _dec2$3(_class$3 = _dec3(_class$3 = class BeanRouterGuardsBase extends BeanBase {
399
+ constructor(...args) {
400
+ super(...args);
401
+ this._eventRouterGuards = void 0;
402
+ }
403
+ async __init__() {
404
+ this._eventRouterGuards = this.app.meta.event.on("a-router:routerGuards", async (router, next) => {
405
+ this.onRouterGuards(router);
406
+ return await next();
407
+ });
408
+ }
409
+ __dispose__() {
410
+ this.dispose();
411
+ }
412
+ dispose() {
413
+ if (this._eventRouterGuards) this._eventRouterGuards();
414
+ }
415
+ onRouterGuards(_router) {}
416
+ }) || _class$3) || _class$3) || _class$3);
417
+ //#endregion
418
+ //#region src/types/router.ts
419
+ var NavigationType = /* @__PURE__ */ function(NavigationType) {
420
+ NavigationType["pop"] = "pop";
421
+ NavigationType["push"] = "push";
422
+ return NavigationType;
423
+ }({});
424
+ var NavigationDirection = /* @__PURE__ */ function(NavigationDirection) {
425
+ NavigationDirection["back"] = "back";
426
+ NavigationDirection["forward"] = "forward";
427
+ NavigationDirection["unknown"] = "";
428
+ return NavigationDirection;
429
+ }({});
430
+ //#endregion
431
+ //#region src/service/routerGuards.ts
432
+ var _dec$2, _dec2$2, _class$2;
433
+ var ServiceRouterGuards = (_dec$2 = Service(), _dec2$2 = BeanInfo({ module: "a-router" }), _dec$2(_class$2 = _dec2$2(_class$2 = class ServiceRouterGuards extends BeanRouterGuardsBase {
434
+ onRouterGuards(router) {
435
+ const self = this;
436
+ router.beforeEach(async (to) => {
437
+ let match = to.matched.find((item) => item.aliasOf);
438
+ if (match) match = match.aliasOf;
439
+ else {
440
+ match = to.matched[to.matched.length - 1];
441
+ if (!await this._prepareCheck(match?.path, to.path)) return to.fullPath;
442
+ if (router._findLegacyRoute(match?.name, match?.path)) return;
443
+ const alias = router._findConfigRoute(match?.name, match?.path)?.alias;
444
+ if (alias) {
445
+ const resLoadModule = await this._forceLoadModule(router, match?.name, match?.path);
446
+ if (resLoadModule && resLoadModule !== true) return resLoadModule;
447
+ if (resLoadModule === false) return to.fullPath;
448
+ if (router.getRealRouteName(match?.name)) {
449
+ const routeAlias = router.resolveName(`$alias:${match?.name}`, {
450
+ params: to.params,
451
+ query: to.query
452
+ });
453
+ return (routeAlias.startsWith("/__alias__") ? routeAlias.substring(10) : routeAlias) || "/";
454
+ } else return {
455
+ path: Array.isArray(alias) ? alias[0] : alias,
456
+ params: to.params,
457
+ query: to.query
458
+ };
459
+ }
460
+ }
461
+ const resLoadModule = await this._forceLoadModule(router, match?.name, match?.path);
462
+ if (resLoadModule === true) return;
463
+ if (resLoadModule) return resLoadModule;
464
+ return to.fullPath;
465
+ });
466
+ router.afterEach(function(to, from, error) {
467
+ if (error) return;
468
+ const info = arguments[3];
469
+ if (from.fullPath !== to.fullPath) self._afterEachFrom(router, from, info);
470
+ router.afterEachForwardRoute(to);
471
+ });
472
+ }
473
+ _afterEachFrom(router, from, info) {
474
+ if (!info) return;
475
+ if (!(info.type === NavigationType.pop && info.direction === NavigationDirection.back || info.type === NavigationType.push && info.replace)) return;
476
+ router.afterEachBackRoute(from);
477
+ }
478
+ async _prepareCheck(pathMatched, pathTo) {
479
+ if (pathMatched === "/:catchAll(.*)*") {
480
+ const moduleInfo = ModuleInfo.parseInfo(ModuleInfo.parseName(pathTo));
481
+ if (moduleInfo && this.app.meta.module.exists(moduleInfo.relativeName) && !this.app.meta.module.get(moduleInfo.relativeName, false)) {
482
+ await this.app.meta.module.use(moduleInfo.relativeName);
483
+ return false;
484
+ }
485
+ }
486
+ return true;
487
+ }
488
+ async _forceLoadModule(router, name, path) {
489
+ const nameOrPath = router.getRealRouteName(name) || path;
490
+ const moduleInfo = ModuleInfo.parseInfo(ModuleInfo.parseName(nameOrPath));
491
+ if (!moduleInfo) return true;
492
+ const moduleName = moduleInfo.relativeName;
493
+ if (!this.app.meta.module.exists(moduleName)) return "/404";
494
+ if (this.app.meta.module.get(moduleName, false)) return true;
495
+ await this.app.meta.module.use(moduleName);
496
+ return false;
497
+ }
498
+ }) || _class$2) || _class$2);
499
+ //#endregion
500
+ //#region src/lib/routerViewBase.tsx
501
+ var BeanRouterViewBase = class extends BeanControllerBase {
502
+ async __init__() {
503
+ this.bean._setBean(routerViewKey, this);
504
+ this.$router.addRouterView(this);
505
+ }
506
+ __dispose__() {
507
+ this.$router.removeRouterView(this);
508
+ }
509
+ backRoute(_route) {
510
+ return false;
511
+ }
512
+ forwardRoute(_route) {
513
+ return false;
514
+ }
515
+ setPageMeta(_route, _pageMeta) {}
516
+ prepareRouteMeta(_route) {
517
+ throw new Error("Not Implemented");
518
+ }
519
+ getKeepAliveInclude() {
520
+ throw new Error("Not Implemented");
521
+ }
522
+ render() {
523
+ return createVNode(RouterView, null, { default: (component) => {
524
+ const routeMeta = this.prepareRouteMeta(component.route);
525
+ return h(Transition, null, { default: () => {
526
+ const vnode = h(component.Component, { key: routeMeta.componentKey });
527
+ cast(vnode).zovaHostProviders = { [pageRouteKey]: component.route };
528
+ return [h(KeepAlive, { include: this.getKeepAliveInclude() }, [vnode])];
529
+ } });
530
+ } });
531
+ }
532
+ };
533
+ //#endregion
534
+ //#region src/component/routerViewEmpty/controller.tsx
535
+ var _dec$1, _dec2$1, _class$1;
536
+ var ControllerRouterViewEmpty = (_dec$1 = Controller(), _dec2$1 = BeanInfo({ module: "a-router" }), _dec$1(_class$1 = _dec2$1(_class$1 = class ControllerRouterViewEmpty extends BeanRouterViewBase {
537
+ async __init__() {}
538
+ render() {
539
+ return createVNode(RouterView, null, { default: (component) => {
540
+ const vnode = h(component.Component);
541
+ cast(vnode).zovaHostProviders = { ["$$pageRoute"]: component.route };
542
+ return vnode;
543
+ } });
544
+ }
545
+ }) || _class$1) || _class$1);
546
+ //#endregion
547
+ //#region src/.metadata/component/routerViewEmpty.ts
548
+ var ZRouterViewEmpty = defineComponent((_props) => {
549
+ useController(ControllerRouterViewEmpty, void 0, void 0);
550
+ return () => {};
551
+ }, prepareComponentOptions());
552
+ //#endregion
553
+ //#region src/config/config.ts
554
+ var config = (_sys) => {
555
+ return { scrollBehavior };
556
+ };
557
+ //#endregion
558
+ //#region src/monkey.ts
559
+ var Monkey = class extends BeanSimple {
560
+ constructor(...args) {
561
+ super(...args);
562
+ this._beanRouter = void 0;
563
+ this.serviceRouterGuards = void 0;
564
+ }
565
+ async getBeanRouter() {
566
+ if (!this._beanRouter) this._beanRouter = this.app.meta.$router = await this.bean._getBean("a-router.bean.router", true, true);
567
+ return this._beanRouter;
568
+ }
569
+ async appInitialize() {
570
+ this.serviceRouterGuards = await this.bean._newBean(ServiceRouterGuards, false);
571
+ if (process.env.CLIENT) this._ssrErrorHandler();
572
+ }
573
+ async appInitialized() {
574
+ const beanRouter = await this.getBeanRouter();
575
+ await this.app.meta.event.emit("a-router:routerGuards", beanRouter);
576
+ }
577
+ appClose() {
578
+ if (this.serviceRouterGuards) this.serviceRouterGuards.dispose();
579
+ }
580
+ async appReady() {
581
+ const beanRouter = await this.getBeanRouter();
582
+ if (process.env.CLIENT && this.ctx.meta.$ssr.isRuntimeSsrPreHydration) {
583
+ const pagePathFull = this.ctx.meta.$ssr.state.pagePathFull;
584
+ if (pagePathFull) beanRouter.router[SymbolRouterHistory].push(pagePathFull);
585
+ }
586
+ this.app.vue.use(beanRouter);
587
+ if (process.env.SERVER) {
588
+ const pagePath = this.app.$getCurrentPagePath();
589
+ beanRouter.push(pagePath);
590
+ await beanRouter.isReady();
591
+ } else if (process.env.CLIENT && this.ctx.meta.$ssr.isRuntimeSsrPreHydration) await beanRouter.isReady();
592
+ }
593
+ async beanInit(bean, beanInstance) {
594
+ bean.defineProperty(beanInstance, "$router", {
595
+ enumerable: false,
596
+ configurable: true,
597
+ get() {
598
+ return bean._getBeanFromHost("a-router.bean.router");
599
+ }
600
+ });
601
+ bean.defineProperty(beanInstance, "$routerView", {
602
+ enumerable: false,
603
+ configurable: true,
604
+ get() {
605
+ return bean._getBeanFromHost({ name: routerViewKey });
606
+ }
607
+ });
608
+ bean.defineProperty(beanInstance, "$pageRoute", {
609
+ enumerable: false,
610
+ configurable: true,
611
+ get() {
612
+ return useComputed(() => {
613
+ return getPageRoute(cast(bean).ctx);
614
+ });
615
+ }
616
+ });
617
+ bean.defineProperty(beanInstance, "$currentRoute", {
618
+ enumerable: false,
619
+ configurable: true,
620
+ get() {
621
+ return useComputed(() => {
622
+ return getCurrentRoute(cast(bean).ctx);
623
+ });
624
+ }
625
+ });
626
+ }
627
+ controllerDataPrepare(controllerData, ctx) {
628
+ controllerData.context.route = getPageRoute(ctx);
629
+ }
630
+ controllerDataInit(controllerData, controller) {
631
+ if (!(controller instanceof BeanControllerPageBase)) return;
632
+ const route = controllerData.context.route;
633
+ this._initControllerRoute(route, controller);
634
+ }
635
+ controllerDataUpdate(controller) {
636
+ if (!(controller instanceof BeanControllerPageBase)) return;
637
+ const route = getPageRoute(cast(cast(controller).ctx));
638
+ this._initControllerRoute(route, controller);
639
+ }
640
+ _initControllerRoute(route, controller) {
641
+ if (!route) return;
642
+ const routeMatched = getRouteMatched(route);
643
+ if (!routeMatched) return;
644
+ if (controller.$routeMatched && !this._checkIfRouteSame(routeMatched, controller.$routeMatched)) return;
645
+ if (!(!controller.$route || controller.$route.fullPath !== route.fullPath)) return;
646
+ controller.$route = route;
647
+ controller.$routeMatched = routeMatched;
648
+ const routeName = getRealRouteName(routeMatched.name);
649
+ const schemaKey = routeName || String(routeMatched.path);
650
+ let schemas;
651
+ const moduleInfo = ModuleInfo.parseInfo(ModuleInfo.parseName(schemaKey));
652
+ if (!moduleInfo) return;
653
+ if (!this.app.meta.module.exists(moduleInfo.relativeName)) return;
654
+ const module = this.app.meta.module.get(moduleInfo.relativeName);
655
+ if (routeName) schemas = module.resource.pageNameSchemas?.[schemaKey];
656
+ else schemas = module.resource.pagePathSchemas?.[schemaKey];
657
+ if (schemas?.params) {
658
+ const params = schemas.params.parse(route.params);
659
+ if (!controller.$params) controller.$params = process.env.SERVER ? params : shallowReactive(params);
660
+ else Object.assign(controller.$params, params);
661
+ }
662
+ if (schemas?.query) {
663
+ const query = schemas.query.parse(route.query);
664
+ if (!controller.$query) controller.$query = process.env.SERVER ? query : shallowReactive(query);
665
+ else Object.assign(controller.$query, query);
666
+ }
667
+ }
668
+ _checkIfRouteSame(route1, route2) {
669
+ return route1.name && route1.name === route2.name || route1.path === route2.path;
670
+ }
671
+ _ssrErrorHandler() {
672
+ if (!process.env.CLIENT) return;
673
+ this.app.meta.event.on("app:errorHandler", (data, next) => {
674
+ const err = next();
675
+ if (!err || !(err instanceof Error)) return err;
676
+ return this._errorHandlerDefaultClient(err, data);
677
+ });
678
+ }
679
+ _errorHandlerDefaultClient(err, _data) {
680
+ if (!process.env.CLIENT) return err;
681
+ if ([301, 302].includes(Number(err.code))) {
682
+ this.app.$gotoPage(err.pagePath);
683
+ return;
684
+ }
685
+ if (err.code === 600) return;
686
+ if (err.code === 401) {
687
+ this.app.$gotoLogin();
688
+ return;
689
+ }
690
+ return err;
691
+ }
692
+ };
693
+ //#endregion
694
+ //#region src/monkeySys.ts
695
+ var MonkeySys = class extends BeanSimple {
696
+ constructor(moduleSelf) {
697
+ super();
698
+ this._moduleSelf = void 0;
699
+ this._sysRouter = void 0;
700
+ this._moduleSelf = moduleSelf;
701
+ }
702
+ async getSysRouter() {
703
+ if (!this._sysRouter) this._sysRouter = await this.bean._getBean("a-router.sys.router", false);
704
+ return this._sysRouter;
705
+ }
706
+ async moduleLoading(module) {
707
+ if (this._moduleSelf === module) return;
708
+ if (!module.resource.routes) return;
709
+ (await this.getSysRouter())._registerRoutes(module);
710
+ }
711
+ async moduleLoaded(_module) {}
712
+ async configLoaded(_module, _config) {}
713
+ sysApplicationInitialize(app) {
714
+ app.$redirect = (pagePath, status) => {
715
+ const error = /* @__PURE__ */ new Error();
716
+ error.code = status ?? 302;
717
+ if (pagePath.startsWith("http://") || pagePath.startsWith("https://")) {
718
+ error.pagePath = pagePath;
719
+ error.url = pagePath;
720
+ } else {
721
+ error.pagePath = pagePath;
722
+ error.url = app.sys.util.getAbsoluteUrlFromPagePath(pagePath, true);
723
+ }
724
+ error.message = process.env.SERVER ? error.url : error.pagePath;
725
+ throw error;
726
+ };
727
+ app.$gotoPage = (pagePath, options) => {
728
+ const query = options?.query ?? {};
729
+ if (options?.returnTo) {
730
+ const returnTo = typeof options?.returnTo === "string" ? options?.returnTo : app.$getCurrentPagePath();
731
+ if (returnTo !== app.sys.env.ROUTER_PAGE_HOME) query[app.sys.env.ROUTER_KEY_RETURNTO] = returnTo;
732
+ }
733
+ pagePath = combineQueries(pagePath, query);
734
+ if (process.env.SERVER || options?.forceRedirect) return app.$redirect(pagePath);
735
+ if (pagePath.startsWith("http://") || pagePath.startsWith("https://")) window.location[options?.replace ? "replace" : "assign"](pagePath);
736
+ else return app.meta.$router[options?.replace ? "replace" : "push"](pagePath);
737
+ };
738
+ app.$gotoHome = () => {
739
+ return app.$gotoPage(app.sys.env.ROUTER_PAGE_HOME);
740
+ };
741
+ app.$gotoLogin = (returnTo, cause) => {
742
+ if (!returnTo && cast(app.meta.$router.currentRoute)?.path === app.sys.env.ROUTER_PAGE_LOGIN) return;
743
+ const query = {};
744
+ if (cause) query.cause = cause;
745
+ const returnTo2 = returnTo === app.sys.env.ROUTER_PAGE_LOGIN ? void 0 : returnTo ?? true;
746
+ return app.$gotoPage(app.sys.env.ROUTER_PAGE_LOGIN, {
747
+ query,
748
+ returnTo: returnTo2
749
+ });
750
+ };
751
+ app.$gotoReturnTo = (returnTo) => {
752
+ const pagePath = app.$getReturnTo(returnTo);
753
+ return app.$gotoPage(pagePath, { replace: true });
754
+ };
755
+ app.$getReturnTo = (returnTo) => {
756
+ return returnTo || cast(app.meta.$router.currentRoute)?.query?.[app.sys.env.ROUTER_KEY_RETURNTO] || app.sys.env.ROUTER_PAGE_HOME;
757
+ };
758
+ app.$getCurrentPagePath = () => {
759
+ if (process.env.SERVER) return app.ctx.meta.$ssr.state.pagePathFull ?? app.sys.util.getPagePathFromAbsoluteUrl(app.ctx.meta.$ssr.context.req.url);
760
+ return cast(app.meta.$router.currentRoute)?.fullPath;
761
+ };
762
+ }
763
+ };
764
+ //#endregion
765
+ //#region src/.metadata/index.ts
766
+ /** monkeySys: end */
767
+ /** scope: begin */
768
+ var _dec, _dec2, _class;
769
+ var components = { "routerViewEmpty": ZRouterViewEmpty };
770
+ var ScopeModuleARouter = (_dec = Scope(), _dec2 = BeanInfo({ module: "a-router" }), _dec(_class = _dec2(_class = class ScopeModuleARouter extends BeanScopeBase {}) || _class) || _class);
771
+ /** scope: end */
772
+ //#endregion
773
+ export { BeanRouter, BeanRouterGuardsBase, BeanRouterViewBase, ControllerRouterViewEmpty, ModelPageData, Monkey, MonkeySys, NavigationDirection, NavigationType, ScopeModuleARouter, ServiceRouterGuards, SymbolRouterHistory, SysRouter, ZRouterViewEmpty, components, config, getCurrentRoute, getPageRoute, getRealRouteName, getRouteMatched, isRouterName, pageRouteKey, routerViewKey, scrollBehavior };
774
+
775
+ //# sourceMappingURL=index.js.map