zova-module-a-router 5.0.52 → 5.0.54

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zova-module-a-router",
3
- "version": "5.0.52",
3
+ "version": "5.0.54",
4
4
  "title": "a-router",
5
5
  "zovaModule": {
6
6
  "capabilities": {
@@ -40,5 +40,5 @@
40
40
  "dependencies": {
41
41
  "vue-router": "^4.3.2"
42
42
  },
43
- "gitHead": "20d5666c9835e208eb0ab4c4f09f658d314795f4"
43
+ "gitHead": "38b917802862663cf6c2e6829857de9a2a742dcd"
44
44
  }
@@ -1,5 +1,5 @@
1
- import { Bean, BeanBase, Cast, IModule, IPageNameRecord, IPagePathRecord, TypeEventOff } from 'zova';
2
- import { Router } from 'vue-router';
1
+ import { Bean, BeanBase, Cast, IModule, IPageNameRecord, IPagePathRecord } from 'zova';
2
+ import { createMemoryHistory, createRouter, createWebHashHistory, createWebHistory, Router } from 'vue-router';
3
3
  import * as ModuleInfo from '@cabloy/module-info';
4
4
  import { IModuleRoute, IModuleRouteComponent } from '../types.js';
5
5
  import { getRealRouteName } from '../utils.js';
@@ -11,7 +11,6 @@ export interface BeanRouter extends Router {}
11
11
  @Bean()
12
12
  export class BeanRouter extends BeanBase {
13
13
  [SymbolRouter]: Router;
14
- eventRouterGuards: TypeEventOff;
15
14
 
16
15
  get router(): Router {
17
16
  return this[SymbolRouter];
@@ -21,33 +20,30 @@ export class BeanRouter extends BeanBase {
21
20
  return this[SymbolRouter] && this[SymbolRouter][prop];
22
21
  }
23
22
 
24
- protected async __init__(router?: Router) {
25
- if (router) {
26
- this[SymbolRouter] = router;
27
- } else {
28
- // app router
29
- router = this.bean.inject('a-router:appRouter');
30
- if (!router) {
31
- throw new Error('Should provide router');
32
- }
33
- this[SymbolRouter] = router;
34
- // provide
35
- this.app.vue.provide('a-router:router', this);
36
- this.bean.provide('a-router:router', this);
23
+ public async initialize(mainRouter?: boolean) {
24
+ // create router
25
+ this[SymbolRouter] = this._createRouter();
26
+ if (mainRouter) {
37
27
  // config.routes
38
28
  this._loadConfigRoutes();
39
- // event
40
- this.eventRouterGuards = this.app.meta.event.on('a-router:routerGuards', async (context, next) => {
41
- this._routerGuards(context.data);
42
- await next();
43
- });
29
+ } else {
30
+ // emit event
31
+ await this.app.meta.event.emit('a-router:routerGuards', this);
44
32
  }
45
33
  }
46
34
 
47
- protected __dispose__() {
48
- if (this.eventRouterGuards) {
49
- this.eventRouterGuards();
50
- }
35
+ private _createRouter() {
36
+ const createHistory = this.app.config.env.appServer
37
+ ? createMemoryHistory
38
+ : this.app.config.env.appRouterMode === 'history'
39
+ ? createWebHistory
40
+ : createWebHashHistory;
41
+
42
+ return createRouter({
43
+ scrollBehavior: () => ({ left: 0, top: 0 }),
44
+ routes: [],
45
+ history: createHistory(this.app.config.env.appRouterBase),
46
+ });
51
47
  }
52
48
 
53
49
  public createAsyncComponent(component: string | IModuleRouteComponent) {
@@ -106,47 +102,6 @@ export class BeanRouter extends BeanBase {
106
102
  return `${fullPath}${join}${query2str}`;
107
103
  }
108
104
 
109
- private _routerGuards(router: BeanRouter) {
110
- router.beforeEach(async to => {
111
- // match path
112
- let match = to.matched.find(item => item.aliasOf);
113
- if (match) {
114
- match = match.aliasOf;
115
- } else {
116
- match = to.matched[to.matched.length - 1];
117
- // alias
118
- const configRoute = this._findConfigRoute(match?.name, match?.path);
119
- const alias = configRoute?.alias;
120
- if (alias) {
121
- // force load module
122
- const resLoadModule = await this._forceLoadModule(match?.name, match?.path);
123
- if (resLoadModule && resLoadModule !== true) return resLoadModule;
124
- if (resLoadModule === false) return to.fullPath;
125
- if (this.getRealRouteName(match?.name)) {
126
- // @ts-ignore ignore
127
- const routeAlias = this.resolveName(`$alias:${match?.name}`, {
128
- params: to.params,
129
- query: to.query,
130
- });
131
- return routeAlias.startsWith('/__alias__') ? routeAlias.substring('/__alias__'.length) : routeAlias;
132
- } else {
133
- return {
134
- path: Array.isArray(alias) ? alias[0] : alias,
135
- params: to.params,
136
- query: to.query,
137
- };
138
- }
139
- }
140
- }
141
- // force load module
142
- const resLoadModule = await this._forceLoadModule(match?.name, match?.path);
143
- if (resLoadModule === true) return;
144
- if (resLoadModule) return resLoadModule;
145
- // redirect again
146
- return to.fullPath;
147
- });
148
- }
149
-
150
105
  /** @internal */
151
106
  public _registerRoutes(module: IModule) {
152
107
  if (!module.resource.routes) return null;
@@ -240,7 +195,8 @@ export class BeanRouter extends BeanBase {
240
195
  this.router.addRoute(route);
241
196
  }
242
197
 
243
- private _findConfigRoute(
198
+ /** @internal */
199
+ public _findConfigRoute(
244
200
  name: string | symbol | null | undefined,
245
201
  path: string | undefined,
246
202
  ): IModuleRoute | undefined {
@@ -251,27 +207,4 @@ export class BeanRouter extends BeanBase {
251
207
  getRealRouteName(name?: string | symbol | null): string | undefined {
252
208
  return getRealRouteName(name);
253
209
  }
254
-
255
- private async _forceLoadModule(
256
- name: string | symbol | null | undefined,
257
- path: string | undefined,
258
- ): Promise<string | boolean | undefined> {
259
- const nameOrPath = this.getRealRouteName(name) || path;
260
- // module info
261
- const moduleInfo = ModuleInfo.parseInfo(ModuleInfo.parseName(nameOrPath));
262
- if (!moduleInfo) {
263
- // donothing
264
- return true;
265
- }
266
- const moduleName = moduleInfo.relativeName;
267
- // check if exists
268
- if (!this.app.meta.module.exists(moduleName)) return '/404';
269
- // check if loaded
270
- const module = this.app.meta.module.get(moduleName, false);
271
- if (module) return true;
272
- // use module
273
- await this.app.meta.module.use(moduleName);
274
- // means need load
275
- return false;
276
- }
277
210
  }
@@ -0,0 +1,75 @@
1
+ import { Local, Use } from 'zova';
2
+ import * as ModuleInfo from '@cabloy/module-info';
3
+ import { ScopeModule } from '../resource/this.js';
4
+ import { BeanRouterBase } from './virtual.router.js';
5
+ import { BeanRouter } from './bean.router.js';
6
+
7
+ @Local()
8
+ export class LocalRouter extends BeanRouterBase<ScopeModule> {
9
+ @Use()
10
+ $$router: BeanRouter;
11
+
12
+ protected onRouterGuards(router: BeanRouter) {
13
+ router.beforeEach(async to => {
14
+ // match path
15
+ let match = to.matched.find(item => item.aliasOf);
16
+ if (match) {
17
+ match = match.aliasOf;
18
+ } else {
19
+ match = to.matched[to.matched.length - 1];
20
+ // alias
21
+ const configRoute = this.$$router._findConfigRoute(match?.name, match?.path);
22
+ const alias = configRoute?.alias;
23
+ if (alias) {
24
+ // force load module
25
+ const resLoadModule = await this._forceLoadModule(match?.name, match?.path);
26
+ if (resLoadModule && resLoadModule !== true) return resLoadModule;
27
+ if (resLoadModule === false) return to.fullPath;
28
+ if (this.$$router.getRealRouteName(match?.name)) {
29
+ // @ts-ignore ignore
30
+ const routeAlias = this.resolveName(`$alias:${match?.name}`, {
31
+ params: to.params,
32
+ query: to.query,
33
+ });
34
+ return routeAlias.startsWith('/__alias__') ? routeAlias.substring('/__alias__'.length) : routeAlias;
35
+ } else {
36
+ return {
37
+ path: Array.isArray(alias) ? alias[0] : alias,
38
+ params: to.params,
39
+ query: to.query,
40
+ };
41
+ }
42
+ }
43
+ }
44
+ // force load module
45
+ const resLoadModule = await this._forceLoadModule(match?.name, match?.path);
46
+ if (resLoadModule === true) return;
47
+ if (resLoadModule) return resLoadModule;
48
+ // redirect again
49
+ return to.fullPath;
50
+ });
51
+ }
52
+
53
+ private async _forceLoadModule(
54
+ name: string | symbol | null | undefined,
55
+ path: string | undefined,
56
+ ): Promise<string | boolean | undefined> {
57
+ const nameOrPath = this.$$router.getRealRouteName(name) || path;
58
+ // module info
59
+ const moduleInfo = ModuleInfo.parseInfo(ModuleInfo.parseName(nameOrPath));
60
+ if (!moduleInfo) {
61
+ // donothing
62
+ return true;
63
+ }
64
+ const moduleName = moduleInfo.relativeName;
65
+ // check if exists
66
+ if (!this.app.meta.module.exists(moduleName)) return '/404';
67
+ // check if loaded
68
+ const module = this.app.meta.module.get(moduleName, false);
69
+ if (module) return true;
70
+ // use module
71
+ await this.app.meta.module.use(moduleName);
72
+ // means need load
73
+ return false;
74
+ }
75
+ }
package/src/monkey.ts CHANGED
@@ -17,11 +17,13 @@ import { BeanRouter } from './bean/bean.router.js';
17
17
  import { ScopeModule, __ThisModule__ } from './resource/this.js';
18
18
  import { markRaw } from 'vue';
19
19
  import { getRealRouteName } from './utils.js';
20
+ import { LocalRouter } from './bean/local.router.js';
20
21
 
21
22
  export class Monkey extends BeanSimple implements IMonkeySystem, IMonkeyModule, IMonkeyController {
22
23
  private _moduleSelf: IModule;
23
24
  private _beanRouter: BeanRouter;
24
25
  private _beanComponentDefault: any;
26
+ localRouter: LocalRouter;
25
27
 
26
28
  constructor(moduleSelf: IModule) {
27
29
  super();
@@ -31,18 +33,25 @@ export class Monkey extends BeanSimple implements IMonkeySystem, IMonkeyModule,
31
33
  async getBeanRouter() {
32
34
  if (!this._beanRouter) {
33
35
  this._beanRouter = (await this.bean._getBean('a-router.bean.router', false)) as BeanRouter;
36
+ await this._beanRouter.initialize(true);
34
37
  }
35
38
  return this._beanRouter;
36
39
  }
37
40
 
38
- async appInitialize(_bean: BeanContainer) {}
41
+ async appInitialize(bean: BeanContainer) {
42
+ // router
43
+ this.localRouter = await bean._newBean(LocalRouter, false);
44
+ }
39
45
  async appInitialized(bean: BeanContainer) {
40
46
  // component default
41
47
  const scope: ScopeModule = await bean.getScope(__ThisModule__);
42
48
  this._beanComponentDefault = await bean.getScope(scope.config.defaultComponent);
43
49
  // emit event
44
- const router = bean.inject('a-router:router');
45
- await this.app.meta.event.emit('a-router:routerGuards', router);
50
+ await this.app.meta.event.emit('a-router:routerGuards', this._beanRouter);
51
+ }
52
+ async appReady(_bean: BeanContainer) {
53
+ // use router
54
+ this.app.vue.use(this._beanRouter);
46
55
  }
47
56
  async beanInit(bean: BeanContainer, beanInstance: BeanBase) {
48
57
  const self = this;
@@ -50,7 +59,7 @@ export class Monkey extends BeanSimple implements IMonkeySystem, IMonkeyModule,
50
59
  enumerable: false,
51
60
  configurable: true,
52
61
  get() {
53
- return bean.inject('a-router:router');
62
+ return bean._getBeanFromHost('a-router.bean.router');
54
63
  },
55
64
  });
56
65
  bean.defineProperty(beanInstance, '$component', {
@@ -2,4 +2,3 @@ export * from './scope.js';
2
2
  export * from './beans.js';
3
3
  export * from './components.js';
4
4
  export * from './events.js';
5
- export * from './injects.js';
@@ -1,10 +0,0 @@
1
- import { Router } from 'vue-router';
2
- import { BeanRouter } from '../bean/bean.router.js';
3
-
4
- import 'zova';
5
- declare module 'zova' {
6
- export interface IInjectRecord {
7
- 'a-router:appRouter': Router;
8
- 'a-router:router': BeanRouter;
9
- }
10
- }