DTP Base provides a scalable and secure Node.js application development harness ready for production service.
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.
 
 
 
 

183 lines
3.7 KiB

// display-engine.js
// Copyright (C) 2022,2023 DTP Technologies, LLC
// All Rights Reserved
'use strict';
import path from 'node:path';
import pug from 'pug';
import { v4 as uuidv4 } from 'uuid';
import { SiteService, SiteError } from '../../lib/site-lib.js';
class DisplayList {
constructor (service, name) {
this.name = name;
this.id = uuidv4();
this.commands = [ ];
}
showNotification (message, status, pos, timeout) {
this.commands.push({
action: 'showNotification',
params: { message, status, pos, timeout },
});
}
showModal (html) {
this.commands.push({
action: 'showModal',
params: { html },
});
}
playNotificationSound (action) {
this.commands.push({
action: 'playNotificationSound',
params: { action },
});
}
playSound (soundId) {
this.commands.push({
action: 'playSound',
params: { soundId },
});
}
addElement (selector, where, html) {
this.commands.push({
selector, action: 'addElement',
params: { where, html },
});
}
removeChildren (selector) {
this.commands.push({
selector, action: 'removeChildren',
params: { },
});
}
setTextContent (selector, text) {
this.commands.push({
selector, action: 'setTextContent',
params: { text },
});
}
replaceElement (selector, html) {
this.commands.push({
selector, action: 'replaceElement',
params: { html },
});
}
removeElement (selector) {
this.commands.push({
selector, action: 'removeElement',
params: { },
});
}
setAttribute (selector, name, value) {
this.commands.push({
selector, action: 'setAttribute',
params: { name, value },
});
}
removeAttribute (selector, name) {
this.commands.push({
selector, action: 'removeAttribute',
params: { name },
});
}
toggleAttribute (selector, name, force) {
this.commands.push({
selector, action: 'toggleAttribute',
params: { name, force },
});
}
addClass (selector, add) {
this.commands.push({
selector, action: 'addClass',
params: { add },
});
}
removeClass (selector, remove) {
this.commands.push({
selector, action: 'removeClass',
params: { remove },
});
}
replaceClass (selector, remove, add) {
this.commands.push({
selector, action: 'replaceClass',
params: { remove, add },
});
}
setValue (selector, value) {
this.commands.push({
selector, action: 'setValue',
params: { value },
});
}
navigateTo (href) {
this.commands.push({
action: 'navigateTo',
params: { href },
});
}
navigateBack ( ) {
this.commands.push({
action: 'navigateBack',
params: { },
});
}
reloadView ( ) {
this.commands.push({
action: 'reloadView',
params: { },
});
}
}
export default class DisplayEngineService extends SiteService {
static get name ( ) { return 'DisplayEngineService'; }
static get slug ( ) { return 'displayEngine'; }
constructor (dtp) {
super(dtp, DisplayEngineService);
this.templates = { };
}
loadTemplate (name, pugScript) {
const scriptFile = path.join(this.dtp.config.root, 'app', 'views', pugScript);
this.templates[name] = pug.compileFile(scriptFile);
}
executeTemplate (name, data) {
if (!this.templates[name]) {
this.log.error('view engine template undefined', { name });
throw new SiteError(500, 'Unknown display engine template');
}
data = Object.assign(this.dtp.app.locals, data);
return this.templates[name](data);
}
createDisplayList (name = 'default') {
return new DisplayList(this, name);
}
}