From: Chris Porter Date: Sun, 17 Aug 2014 22:08:26 +0000 (+0100) Subject: add html5 notifications X-Git-Url: https://jfr.im/git/irc/quakenet/qwebirc.git/commitdiff_plain/f63006ab1aac07357148022344625257f0b7fd3f?hp=83d2124308a28050aca90b21c993c05c10ad4613 add html5 notifications --- diff --git a/js/ui/baseui.js b/js/ui/baseui.js index c20129e..34623fb 100644 --- a/js/ui/baseui.js +++ b/js/ui/baseui.js @@ -412,16 +412,25 @@ qwebirc.ui.NotificationUI = new Class({ this.__beeper = new qwebirc.ui.Beeper(this.uiOptions); this.__flasher = new qwebirc.ui.Flasher(this.uiOptions); - - this.beep = this.__beeper.beep.bind(this.__beeper); - - this.flash = this.__flasher.flash.bind(this.__flasher); + this.__notifier = new qwebirc.ui.Notifier(this.uiOptions); + this.cancelFlash = this.__flasher.cancelFlash.bind(this.__flasher); }, + beep: function() { + this.__beeper.beep(); + }, + notify: function(title, message, callback) { + this.__beeper.beep(); + this.__flasher.flash(); + this.__notifier.notify(title, message, callback); + }, setBeepOnMention: function(value) { if(value) this.__beeper.soundInit(); }, + setNotifications: function(value) { + this.__notifier.setEnabled(value); + }, updateTitle: function(text) { if(this.__flasher.updateTitle(text)) this.parent(text); @@ -429,6 +438,7 @@ qwebirc.ui.NotificationUI = new Class({ focusChange: function(value) { this.parent(value); this.__flasher.focusChange(value); + this.__notifier.focusChange(value); } }); diff --git a/js/ui/baseuiwindow.js b/js/ui/baseuiwindow.js index 22200ad..1466907 100644 --- a/js/ui/baseuiwindow.js +++ b/js/ui/baseuiwindow.js @@ -98,8 +98,10 @@ qwebirc.ui.Window = new Class({ addLine: function(type, line, colour, element) { var hilight = qwebirc.ui.HILIGHT_NONE; var lhilight = false; - + if(type) { + var selectMe = function() { this.parentObject.selectWindow(this); }.bind(this); + var message = $defined(line) ? line["m"] : null; hilight = qwebirc.ui.HILIGHT_ACTIVITY; if(type.match(/(NOTICE|ACTION|MSG)$/)) { @@ -108,15 +110,13 @@ qwebirc.ui.Window = new Class({ hilight = qwebirc.ui.HILIGHT_ACTIVITY; } else { hilight = qwebirc.ui.HILIGHT_US; - this.parentObject.beep(); - this.parentObject.flash(); + this.parentObject.notify("Private message from " + this.name, message, selectMe); } } - if(!type.match(/^OUR/) && this.client.hilightController.match(line["m"])) { + if(!type.match(/^OUR/) && this.client.hilightController.match(message)) { lhilight = true; hilight = qwebirc.ui.HILIGHT_US; - this.parentObject.beep(); - this.parentObject.flash(); + this.parentObject.notify("Hilighted in " + this.name, message, selectMe); } else if(hilight != qwebirc.ui.HILIGHT_US) { hilight = qwebirc.ui.HILIGHT_SPEECH; } diff --git a/js/ui/notifications.js b/js/ui/notifications.js index efee7ed..796da14 100644 --- a/js/ui/notifications.js +++ b/js/ui/notifications.js @@ -143,3 +143,60 @@ qwebirc.ui.Flasher = new Class({ this.cancelFlash(); } }); + +qwebirc.ui.Notifier = new Class({ + initialize: function(uiOptions) { + this.uiOptions = uiOptions; + + this.windowFocused = false; + this.previous = null; + this.setEnabled(this.uiOptions.NOTIFICATIONS); + }, + focusChange: function(value) { + this.windowFocused = value; + }, + setEnabled: function(value) { + this.enabled = value; + if(!value) + return; + + if(this.isGranted()) + return; + + Notification.requestPermission(function (permission) { + if (!("permission" in Notification)) + Notification.permission = permission; + }); + }, + isGranted: function() { + if(!("Notification" in window)) + return false; + + return Notification.permission === "granted"; + }, + notify: function(title, message, callback) { + if(this.windowFocused && !this.enabled || !this.isGranted()) + return; + + if(this.previous) + this.previous.close(); + + var n = new Notification(title, {body: message, icon: qwebirc.global.staticBaseURL + "images/qwebircsmall.png"}); + var delay = function() { + n.close(); + this.previous = null; + }.bind(this).delay(5000); + + this.previous = n; + if(callback) { + n.addEventListener("click", function() { + this.previous = null; + window.focus(); + callback(); + }); + n.addEventListener("close", function() { + this.previous = null; + }.bind(this)); + } + } +}); diff --git a/js/ui/panes/options.js b/js/ui/panes/options.js index 1ed73e4..7ee9310 100644 --- a/js/ui/panes/options.js +++ b/js/ui/panes/options.js @@ -14,12 +14,23 @@ qwebirc.ui.supportsFocus = function() { * settableByURL... */ qwebirc.config.DEFAULT_OPTIONS = [ - [1, "BEEP_ON_MENTION", "Beep when nick mentioned or on query activity", true, { + [1, "BEEP_ON_MENTION", "Beep on activity", true, { applyChanges: function(value, ui) { if(ui.setBeepOnMention) ui.setBeepOnMention(value); } }], + [16, "NOTIFICATIONS", "Emit HTML5 notifications on activity", false, { + enabled: function() { + if(!("Notification" in window)) + return [false, false]; /* [disabled, default_value] */ + return [true]; + }, + applyChanges: function(value, ui) { + if(ui.setNotifications) + ui.setNotifications(value); + } + }], [7, "FLASH_ON_MENTION", "Flash titlebar when nick mentioned or on query activity", true, { enabled: qwebirc.ui.supportsFocus }],