From 60baaaabd2f98a60ba3c1893c827de597d06305e Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 1 Mar 2021 23:33:17 +0100 Subject: [PATCH] Browser notifications ! --- static/js/app.js | 38 ++++++++++++++++++++++++++++++++++++++ templates/index.html | 6 ++++++ templates/job.html | 5 +++++ 3 files changed, 49 insertions(+) diff --git a/static/js/app.js b/static/js/app.js index bc2366b..c22d5ed 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -343,3 +343,41 @@ var AnsiUp = (function () { Object.defineProperty(exports, "__esModule", { value: true }); exports.default = AnsiUp; })); + +function notify(message) { + // Let's check if the browser supports notifications + if (!("Notification" in window)) { + return; + } + + // Let's check whether notification permissions have already been granted + else if (Notification.permission === "granted") { + // If it's okay let's create a notification + var notification = new Notification( + "Yunohost Apps CI", + { + icon: "https://yunohost.org/_images/yunohost_package.png", + body: message + } + ); + } + + // Otherwise, we need to ask the user for permission + else if (Notification.permission !== "denied") { + Notification.requestPermission().then(function (permission) { + // If the user accepts, let's create a notification + if (permission === "granted") { + var notification = new Notification( + "Yunohost Apps CI", + { + icon: "https://yunohost.org/_images/yunohost_package.png", + body: message + } + ); + } + }); + } + + // At last, if the user has denied notifications, and you + // want to be respectful there is no need to bother them any more. +} diff --git a/templates/index.html b/templates/index.html index a2084a6..c342c12 100644 --- a/templates/index.html +++ b/templates/index.html @@ -72,6 +72,12 @@ for (var i = 0; i < app.jobs.length; ++i) { if (app.jobs[i].id == data.id) { Vue.set(app.jobs, i, data); + + if ((app.jobs[i].state !== undefined) && (app.jobs[i].state != data.state)) + { + notify("Job for " + data.name + " is " + data.state); + } + break; } } diff --git a/templates/job.html b/templates/job.html index c9c0d40..d19f296 100644 --- a/templates/job.html +++ b/templates/job.html @@ -75,6 +75,11 @@ var action = message.action; if (action == "init_job" || action == "update_job") { + if ((app.job.state !== undefined) && (app.job.state != data.state)) + { + notify("Job for " + data.name + " is " + data.state); + } + data.deleted = false; app.job = data; } else if (action == "delete_job") {