2020-07-11 11:46:16 +02:00
|
|
|
import nunjucks, {Environment} from "nunjucks";
|
2020-04-23 11:18:23 +02:00
|
|
|
import config from "config";
|
2020-07-15 10:24:01 +02:00
|
|
|
import {Express, Request, Router} from "express";
|
2020-04-23 11:18:23 +02:00
|
|
|
import ApplicationComponent from "../ApplicationComponent";
|
|
|
|
import Controller from "../Controller";
|
|
|
|
import {ServerError} from "../HttpError";
|
2020-07-15 10:24:01 +02:00
|
|
|
import * as querystring from "querystring";
|
2020-04-23 11:18:23 +02:00
|
|
|
|
|
|
|
export default class NunjucksComponent extends ApplicationComponent<void> {
|
2020-07-15 10:24:01 +02:00
|
|
|
public static getPreviousURL(req: Request, defaultUrl?: string): string {
|
|
|
|
return req.query.redirect_uri?.toString() || req.headers.referer?.[0] || defaultUrl || '/';
|
|
|
|
}
|
|
|
|
|
2020-07-08 10:49:29 +02:00
|
|
|
private readonly viewsPath: string;
|
2020-07-11 11:46:16 +02:00
|
|
|
private env?: Environment;
|
2020-07-08 10:49:29 +02:00
|
|
|
|
|
|
|
constructor(viewsPath: string = 'views') {
|
|
|
|
super();
|
|
|
|
this.viewsPath = viewsPath;
|
|
|
|
}
|
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
public async start(app: Express): Promise<void> {
|
2020-07-08 11:09:27 +02:00
|
|
|
let coreVersion = 'unknown';
|
|
|
|
try {
|
|
|
|
coreVersion = require('../../package.json').version;
|
|
|
|
} catch (e) {
|
|
|
|
try {
|
|
|
|
coreVersion = require('../package.json').version;
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 11:46:21 +02:00
|
|
|
this.env = nunjucks.configure([
|
|
|
|
this.viewsPath,
|
|
|
|
'views',
|
|
|
|
'node_modules/wms-core/views'
|
|
|
|
], {
|
2020-04-23 11:18:23 +02:00
|
|
|
autoescape: true,
|
|
|
|
express: app,
|
|
|
|
noCache: !config.get('view.cache'),
|
|
|
|
throwOnUndefined: true,
|
|
|
|
})
|
|
|
|
.addGlobal('route', (route: string, params: { [p: string]: string } | [] = [], absolute: boolean = false) => {
|
|
|
|
const path = Controller.route(route, params, absolute);
|
|
|
|
if (path === null) throw new ServerError(`Route ${route} not found.`);
|
|
|
|
return path;
|
|
|
|
})
|
2020-04-25 09:34:02 +02:00
|
|
|
.addGlobal('app_version', this.app!.getVersion())
|
2020-07-08 11:09:27 +02:00
|
|
|
.addGlobal('core_version', coreVersion)
|
2020-07-15 10:24:01 +02:00
|
|
|
.addGlobal('querystring', querystring)
|
2020-04-23 11:18:23 +02:00
|
|
|
.addFilter('hex', (v: number) => {
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
|
|
|
app.set('view engine', 'njk');
|
2020-07-11 11:46:16 +02:00
|
|
|
}
|
2020-04-23 11:18:23 +02:00
|
|
|
|
2020-07-11 11:46:16 +02:00
|
|
|
public async init(router: Router): Promise<void> {
|
2020-04-23 11:18:23 +02:00
|
|
|
router.use((req, res, next) => {
|
2020-07-11 11:46:16 +02:00
|
|
|
req.env = this.env!;
|
2020-04-23 11:18:23 +02:00
|
|
|
res.locals.url = req.url;
|
|
|
|
res.locals.params = () => req.params;
|
2020-06-27 17:11:31 +02:00
|
|
|
|
|
|
|
res.locals.app = config.get('app');
|
|
|
|
|
2020-07-15 10:24:01 +02:00
|
|
|
// Redirect back
|
|
|
|
res.redirectBack = (defaultUrl?: string) => {
|
|
|
|
res.redirect(NunjucksComponent.getPreviousURL(req, defaultUrl));
|
|
|
|
};
|
|
|
|
res.locals.getPreviousURL = (defaultURL?: string) => {
|
|
|
|
return NunjucksComponent.getPreviousURL(req, defaultURL);
|
|
|
|
};
|
|
|
|
|
2020-04-23 11:18:23 +02:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|