Files
Hydro/ui/misc/PageLoader.js
T
masnn f9468536de upgrade dep:
css-loader 0.28->3.5
del 4.1->5.1
eslint 5.18->6.8
eslint-config-airbnb 17.1->18.1
eslint-loader 2.1->4.0
file-loader 2.0->6.0
fs.extra 8.0->9.0
gulp 2.0->3.0
gulp-nunjucks 4.0->5.0
json-loader 0.5->Removed
style-loader 0.23->1.1
uglifyjs-webpack-plugin 1.3->2.2
yargs 13.2->15.3
katex 0.10->0.11
react-monaco-editor 0.35->0.36
timeage-react 1.2->3.0
timeago.js 3.0->4.0
uuid 3.3->7.0
2020-04-15 12:47:55 +08:00

75 lines
2.1 KiB
JavaScript

// load all page stylesheets
const pageStyleReq = require.context('../', true, /\.page\.styl$/i);
pageStyleReq.keys().map((key) => pageStyleReq(key).default);
export class Page {
constructor(name, autoload, afterLoading, beforeLoading) {
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) {
// eslint-disable-next-line quotes
throw new Error(`'afterLoading' should be a function`);
}
if (typeof beforeLoading !== 'function' && beforeLoading != null) {
// eslint-disable-next-line quotes
throw new Error(`'beforeLoading' should be a function`);
}
}
this.name = name;
this.autoload = autoload;
this.afterLoading = afterLoading;
this.beforeLoading = beforeLoading;
}
isNameMatch(name) {
if (typeof this.name === 'string') {
return this.name === name;
}
if (this.name instanceof Array) {
return this.name.indexOf(name) !== -1;
}
return false;
}
}
export class NamedPage extends Page {
constructor(name, afterLoading = null, beforeLoading = null) {
super(name, false, afterLoading, beforeLoading);
}
}
export class AutoloadPage extends Page {
constructor(name, afterLoading = null, beforeLoading = null) {
super(name, true, afterLoading, beforeLoading);
}
}
export class PageLoader {
constructor() {
const pageReq = require.context('../', true, /\.page\.js$/i);
this.pageInstances = pageReq.keys().map((key) => {
const page = pageReq(key).default;
if (!page || !(page instanceof Page)) {
return null;
}
return page;
});
}
getAutoloadPages() {
const pages = this.pageInstances.filter((page) => page && page.autoload);
return pages;
}
getNamedPage(pageName) {
const pages = this.pageInstances.filter((page) => page && page.isNameMatch(pageName));
if (pages.length > 0) {
return pages[0];
}
return null;
}
}