mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: pageLoader: support moduleName
This commit is contained in:
1 parent
0569a98739
commit
ab3cad57b8
6 files changed
+56
-33
No files matched your search
@@ -1,15 +1,29 @@
|
||||
type callback = (pagename: string, loadPage: (name: string) => Promise<any>) => any;
|
||||
|
||||
export class Page {
|
||||
constructor(public name: string | string[], public autoload: boolean, public afterLoading, public beforeLoading) {
|
||||
moduleName?: string;
|
||||
autoload = false;
|
||||
afterLoading?: callback;
|
||||
beforeLoading?: callback;
|
||||
constructor(pagename: string | string[], afterLoading: callback, beforeLoading: callback);
|
||||
constructor(pagename: string | string[], moduleName: string, afterLoading: callback, beforeLoading: callback);
|
||||
constructor(pagename: string | string[], ...args: any[]);
|
||||
constructor(public name: string | string[], ...args: any[]) {
|
||||
if (typeof args[0] === 'string') {
|
||||
[this.moduleName, this.afterLoading, this.beforeLoading] = args;
|
||||
} else {
|
||||
[this.afterLoading, this.beforeLoading] = args;
|
||||
}
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
if (typeof name !== 'string' && !(name instanceof Array)) {
|
||||
// eslint-disable-next-line quotes
|
||||
throw new Error(`'name' should be a string or string[]`);
|
||||
}
|
||||
if (typeof afterLoading !== 'function' && afterLoading != null) {
|
||||
if (typeof this.afterLoading !== 'function' && this.afterLoading != null) {
|
||||
// eslint-disable-next-line quotes
|
||||
throw new Error(`'afterLoading' should be a function`);
|
||||
}
|
||||
if (typeof beforeLoading !== 'function' && beforeLoading != null) {
|
||||
if (typeof this.beforeLoading !== 'function' && this.beforeLoading != null) {
|
||||
// eslint-disable-next-line quotes
|
||||
throw new Error(`'beforeLoading' should be a function`);
|
||||
}
|
||||
@@ -23,15 +37,14 @@ export class Page {
|
||||
}
|
||||
}
|
||||
|
||||
export class NamedPage extends Page {
|
||||
constructor(name: string | string[], afterLoading: (pagename: string) => any = null, beforeLoading = null) {
|
||||
super(name, false, afterLoading, beforeLoading);
|
||||
}
|
||||
}
|
||||
export class NamedPage extends Page { }
|
||||
|
||||
export class AutoloadPage extends Page {
|
||||
constructor(name: string | string[], afterLoading: (pagename: string) => any = null, beforeLoading = null) {
|
||||
super(name, true, afterLoading, beforeLoading);
|
||||
constructor(pagename: string | string[], afterLoading: callback, beforeLoading: callback);
|
||||
constructor(pagename: string | string[], moduleName: string, afterLoading: callback, beforeLoading: callback);
|
||||
constructor(pagename: string | string[], ...args: any[]) {
|
||||
super(pagename, ...args);
|
||||
this.autoload = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,18 +5,14 @@ export default class PageLoader {
|
||||
const pages = require.context('../pages/', true, /\.page\.[jt]sx?$/i);
|
||||
const components = require.context('../components/', true, /\.page\.[jt]sx?$/i);
|
||||
this.pageInstances = [
|
||||
...pages.keys().map((key) => {
|
||||
const page = pages(key).default;
|
||||
if (!page || !(page instanceof Page)) return null;
|
||||
return page;
|
||||
}),
|
||||
...components.keys().map((key) => {
|
||||
const page = components(key).default;
|
||||
if (!page || !(page instanceof Page)) return null;
|
||||
return page;
|
||||
}),
|
||||
];
|
||||
if (window.Hydro.extraPages) this.pageInstances.push(...window.Hydro.extraPages.filter((i) => i instanceof Page));
|
||||
...pages.keys().map((key) => pages(key)),
|
||||
...components.keys().map((key) => components(key)),
|
||||
...window.Hydro.extraPages,
|
||||
].map((page) => {
|
||||
page = page?.default || page;
|
||||
if (!page || !(page instanceof Page)) return null;
|
||||
return page;
|
||||
}).filter((i) => i);
|
||||
window.Hydro.pageInstances = this.pageInstances;
|
||||
window.Hydro.extraPages.filter((i) => !(i instanceof Page)).forEach((i) => i());
|
||||
}
|
||||
@@ -28,4 +24,8 @@ export default class PageLoader {
|
||||
getNamedPage(pageName) {
|
||||
return this.pageInstances.filter((page) => page && page.isNameMatch(pageName));
|
||||
}
|
||||
|
||||
getPage(moduleName) {
|
||||
return this.pageInstances.filter((page) => page && page.moduleName === moduleName);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user