You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
66 lines
1.8 KiB
// site-common.js
|
|
// Copyright (C) 2022,2024 DTP Technologies, LLC
|
|
// All Rights Reserved
|
|
|
|
'use strict';
|
|
|
|
import path from 'path';
|
|
import pug from 'pug';
|
|
|
|
import EventEmitter from 'events';
|
|
|
|
export class SiteCommon extends EventEmitter {
|
|
|
|
constructor (dtp) {
|
|
super();
|
|
this.dtp = dtp;
|
|
this.appTemplateRoot = path.join(this.dtp.config.root, 'app', 'templates');
|
|
}
|
|
|
|
saveSession (req) {
|
|
return new Promise((resolve, reject) => {
|
|
req.session.save((err) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
isValidString (text) {
|
|
return text && (typeof text === 'string') && (text.length > 0);
|
|
}
|
|
|
|
loadAppTemplate (type, name) {
|
|
return pug.compileFile(path.join(this.appTemplateRoot, type, name));
|
|
}
|
|
|
|
loadViewTemplate (filename) {
|
|
const scriptFile = path.join(this.dtp.config.root, 'app', 'views', filename);
|
|
this.log.debug('loading view template', { filename });
|
|
return pug.compileFile(scriptFile);
|
|
}
|
|
|
|
async renderTemplate (templateFn, templateModel) {
|
|
const { cache: cacheService } = this.dtp.services;
|
|
const { config, pkg } = this.dtp;
|
|
|
|
const settingsKey = `settings:${config.site.domainKey}:site`;
|
|
const adminSettings = await cacheService.getObject(settingsKey);
|
|
|
|
if (this.dtp.app && this.dtp.app.locals) {
|
|
templateModel = Object.assign(templateModel, this.dtp.app.locals); // global app objects
|
|
}
|
|
templateModel.pkg = pkg;
|
|
templateModel.site = Object.assign(templateModel.site || { }, config.site); // defaults and .env
|
|
templateModel.site = Object.assign(templateModel.site, adminSettings); // admin overrides
|
|
|
|
return templateFn(templateModel);
|
|
}
|
|
|
|
createDisplayList (name) {
|
|
const { displayEngine: displayEngineService } = this.dtp.services;
|
|
return displayEngineService.createDisplayList(name);
|
|
}
|
|
}
|