migrate manual DOM creation for notifications to nunjuck template

This commit is contained in:
Felix Wiedenbach 2021-10-15 06:23:50 +02:00
parent 026e624e23
commit 467720f1c4
2 changed files with 29 additions and 13 deletions

View File

@ -39,6 +39,10 @@ Module.register("alert", {
}; };
}, },
getTemplate(type) {
return `templates/${type}.njk`;
},
start() { start() {
Log.info(`Starting module: ${this.name}`); Log.info(`Starting module: ${this.name}`);
@ -52,23 +56,14 @@ Module.register("alert", {
} }
}, },
showNotification(message) { async showNotification(notification) {
let msg = ""; const message = await this.renderMessage("notification", notification);
if (message.title) {
msg += "<span class='thin dimmed medium'>" + message.title + "</span>";
}
if (message.message) {
if (msg !== "") {
msg += "<br />";
}
msg += "<span class='light bright small'>" + message.message + "</span>";
}
new NotificationFx({ new NotificationFx({
message: msg, message,
layout: "growl", layout: "growl",
effect: this.config.effect, effect: this.config.effect,
ttl: message.timer !== undefined ? message.timer : this.config.display_time ttl: notification.timer || this.config.display_time
}).show(); }).show();
}, },
@ -143,6 +138,18 @@ Module.register("alert", {
} }
}, },
renderMessage(type, data) {
return new Promise((resolve) => {
this.nunjucksEnvironment().render(this.getTemplate(type), data, function (err, res) {
if (err) {
Log.error("Failed to render alert", err);
}
resolve(res);
});
});
},
notificationReceived(notification, payload, sender) { notificationReceived(notification, payload, sender) {
if (notification === "SHOW_ALERT") { if (notification === "SHOW_ALERT") {
if (payload.type === "notification") { if (payload.type === "notification") {

View File

@ -0,0 +1,9 @@
{% if title %}
<span class="thin dimmed medium">{{ title }}</span>
{% endif %}
{% if message %}
{% if title %}
<br/>
{% endif %}
<span class="light bright small">{{ message }}</span>
{% endif %}