]> jfr.im git - irc/quakenet/qwebirc.git/blobdiff - js/ui/notifications.js
iframes should not have borders -- breaks page layout
[irc/quakenet/qwebirc.git] / js / ui / notifications.js
index e57d6a2ed60d164f45355175e5a483484849e1e4..796da149ba573c16094633aee1e3e8971d7fc3a2 100644 (file)
@@ -56,15 +56,15 @@ qwebirc.ui.Flasher = new Class({
     this.canUpdateTitle = true;
     this.titleText = document.title;
 
-    var favIcons = $$("link[rel=icon]"), favIconParent = $$("head");
-    if(favIcons && favIcons.length > 0 && favIconParent && favIconParent.length > 0) {
-      this.favIcon = favIcons[0];
-      this.favIconParent = favIconParent[0];
+    var favIcon = this._getFavIcon();
+    if($defined(favIcon)) {
+      this.favIcon = favIcon;
+      this.favIconParent = favIcon.parentNode;
       this.favIconVisible = true;
       this.emptyFavIcon = new Element("link");
       this.emptyFavIcon.rel = "shortcut icon";
       this.emptyFavIcon.href = qwebirc.global.staticBaseURL + "images/empty_favicon.ico";
-      
+      this.emptyFavIcon.type = "image/x-icon";
       this.flashing = false;
     
       this.canFlash = true;
@@ -72,7 +72,13 @@ qwebirc.ui.Flasher = new Class({
       document.addEvent("keydown", this.cancelFlash.bind(this));
     } else {
       this.canFlash = false;
-    }    
+    }
+  },
+  _getFavIcon: function() {
+    var favIcons = $$("head link");
+    for(var i=0;i<favIcons.length;i++)
+      if(favIcons[i].getAttribute("rel") == "shortcut icon")
+        return favIcons[i];
   },
   flash: function() {
     if(!this.uiOptions.FLASH_ON_MENTION || this.windowFocused || !this.canFlash || this.flashing)
@@ -113,6 +119,7 @@ qwebirc.ui.Flasher = new Class({
   },
   hideFavIcon: function() {
     if(this.favIconVisible) {
+      /* only seems to work in firefox */
       this.favIconVisible = false;
       this.favIconParent.removeChild(this.favIcon);
       this.favIconParent.appendChild(this.emptyFavIcon);
@@ -136,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));
+    }
+  }
+});