1
0
mirror of https://github.com/ohwgiles/laminar.git synced 2026-03-02 03:40:21 +00:00

resolves #23: In-browser notification

This implements web notifications on job completion. Opt-in
by default, the preference is saved in js localStorage and
can be toggled directly on any page
This commit is contained in:
Oliver Giles
2017-12-29 17:18:43 +02:00
parent 93b428529e
commit 3b0efcac9d
2 changed files with 34 additions and 8 deletions

View File

@@ -32,8 +32,11 @@ const WebsocketHandler = function() {
// at this point, the component must be defined
if (!this.comp)
return console.error("Page component was undefined");
else if (typeof this.comp[msg.type] === 'function')
this.comp[msg.type](msg.data);
else {
this.comp.$root.showNotify(msg.type, msg.data);
if(typeof this.comp[msg.type] === 'function')
this.comp[msg.type](msg.data);
}
}
};
ws.onclose = function(ev) {
@@ -513,7 +516,28 @@ new Vue({
el: '#app',
data: {
title: '', // populated by status ws message
connected: false
connected: false,
notify: 'localStorage' in window && localStorage.getItem('showNotifications') == 1
},
computed: {
supportsNotifications() {
return 'Notification' in window && Notification.permission !== 'denied';
}
},
methods: {
toggleNotifications(en) {
if(Notification.permission !== 'granted')
Notification.requestPermission(p => this.notify = (p === 'granted'))
else
this.notify = en;
},
showNotify(msg, data) {
if(this.notify && msg === 'job_completed')
new Notification(data.name + ' ' + '#' + data.number +' completed');
}
},
watch: {
notify(e) { localStorage.setItem('showNotifications', e ? 1 : 0); }
},
router: new VueRouter({
mode: 'history',