From 467720f1c45f50ff70395309bb58a62d8368fc16 Mon Sep 17 00:00:00 2001 From: Felix Wiedenbach Date: Fri, 15 Oct 2021 06:23:50 +0200 Subject: [PATCH] migrate manual DOM creation for notifications to nunjuck template --- modules/default/alert/alert.js | 33 +++++++++++-------- .../default/alert/templates/notification.njk | 9 +++++ 2 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 modules/default/alert/templates/notification.njk diff --git a/modules/default/alert/alert.js b/modules/default/alert/alert.js index 703c8034..395857e0 100644 --- a/modules/default/alert/alert.js +++ b/modules/default/alert/alert.js @@ -39,6 +39,10 @@ Module.register("alert", { }; }, + getTemplate(type) { + return `templates/${type}.njk`; + }, + start() { Log.info(`Starting module: ${this.name}`); @@ -52,23 +56,14 @@ Module.register("alert", { } }, - showNotification(message) { - let msg = ""; - if (message.title) { - msg += "" + message.title + ""; - } - if (message.message) { - if (msg !== "") { - msg += "
"; - } - msg += "" + message.message + ""; - } + async showNotification(notification) { + const message = await this.renderMessage("notification", notification); new NotificationFx({ - message: msg, + message, layout: "growl", effect: this.config.effect, - ttl: message.timer !== undefined ? message.timer : this.config.display_time + ttl: notification.timer || this.config.display_time }).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) { if (notification === "SHOW_ALERT") { if (payload.type === "notification") { diff --git a/modules/default/alert/templates/notification.njk b/modules/default/alert/templates/notification.njk new file mode 100644 index 00000000..1d67bcda --- /dev/null +++ b/modules/default/alert/templates/notification.njk @@ -0,0 +1,9 @@ +{% if title %} + {{ title }} +{% endif %} +{% if message %} + {% if title %} +
+ {% endif %} + {{ message }} +{% endif %}