]> 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 0fe40b698f5feae3f39bc24edd2aa812320c40d9..796da149ba573c16094633aee1e3e8971d7fc3a2 100644 (file)
-qwebirc.ui.HilightController = new Class({\r
-  initialize: function(parent) {\r
-    this.parent = parent;\r
-    this.regex = null;\r
-    this.prevnick = null;\r
-  },\r
-  match: function(text) {\r
-    var nick = this.parent.nickname;\r
-    if(nick != this.prevnick) {\r
-      var classes = '[\\s\\.,;:]';\r
-      this.regex = new RegExp('(^|' + classes + ')' + RegExp.escape(nick) + '(' + classes + '|$)', "i");\r
-    }\r
-    if(text.match(this.regex))\r
-      return true;\r
-    return false;\r
-  }\r
-});\r
-\r
-qwebirc.ui.Beeper = new Class({\r
-  initialize: function(uiOptions) {\r
-    this.uiOptions = uiOptions;\r
-    \r
-    this.soundInited = false;\r
-    this.soundReady = false;\r
-\r
-    if(this.uiOptions.BEEP_ON_MENTION)\r
-      this.soundInit();\r
-  },\r
-  soundInit: function() {\r
-    if(this.soundInited)\r
-      return;\r
-    if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)\r
-      return;\r
-    this.soundInited = true;\r
-    \r
-    this.soundPlayer = new qwebirc.sound.SoundPlayer();\r
-    this.soundPlayer.addEvent("ready", function() {\r
-      this.soundReady = true;\r
-    }.bind(this));\r
-    \r
-    this.soundPlayer.go();\r
-  },\r
-  beep: function() {\r
-    if(!this.soundReady || !this.uiOptions.BEEP_ON_MENTION)\r
-      return;\r
-      \r
-    this.soundPlayer.beep();\r
-  }\r
-});\r
-\r
-qwebirc.ui.Flasher = new Class({\r
-  initialize: function(uiOptions) {\r
-    this.uiOptions = uiOptions;\r
-    \r
-    this.windowFocused = false;\r
-    this.canUpdateTitle = true;\r
-    this.titleText = document.title;\r
-\r
-    var favIcons = $$("link[rel=icon]"), favIconParent = $$("head");\r
-    if(favIcons && favIcons.length > 0 && favIconParent && favIconParent.length > 0) {\r
-      this.favIcon = favIcons[0];\r
-      this.favIconParent = favIconParent[0];\r
-      this.favIconVisible = true;\r
-      this.emptyFavIcon = new Element("link");\r
-      this.emptyFavIcon.rel = "shortcut icon";\r
-      this.emptyFavIcon.href = "/images/empty_favicon.ico";\r
-      \r
-      this.flashing = false;\r
-    \r
-      this.canFlash = true;\r
-      document.addEvent("mousedown", this.cancelFlash.bind(this));\r
-      document.addEvent("keydown", this.cancelFlash.bind(this));\r
-    } else {\r
-      this.canFlash = false;\r
-    }    \r
-  },\r
-  flash: function() {\r
-    if(!this.uiOptions.FLASH_ON_MENTION || this.windowFocused || !this.canFlash || this.flashing)\r
-      return;\r
-\r
-    this.titleText = document.title; /* just in case */      \r
-    var flashA = function() {\r
-      this.hideFavIcon();\r
-      this.canUpdateTitle = false;\r
-      document.title = "Activity!";\r
-      \r
-      this.flasher = flashB.delay(500);\r
-    }.bind(this);\r
-    \r
-    var flashB = function() {\r
-      this.showFavIcon();\r
-      this.canUpdateTitle = true;\r
-      document.title = this.titleText;\r
-      \r
-      this.flasher = flashA.delay(500);\r
-    }.bind(this);\r
-\r
-    this.flashing = true;\r
-    flashA();\r
-  },\r
-  cancelFlash: function() {\r
-    if(!this.canFlash || !$defined(this.flasher))\r
-      return;\r
-      \r
-    this.flashing = false;\r
-    \r
-    $clear(this.flasher);\r
-    this.flasher = null;\r
-    \r
-    this.showFavIcon();\r
-    document.title = this.titleText;\r
-    this.canUpdateTitle = true;\r
-  },\r
-  hideFavIcon: function() {\r
-    if(this.favIconVisible) {\r
-      this.favIconVisible = false;\r
-      this.favIconParent.removeChild(this.favIcon);\r
-      this.favIconParent.appendChild(this.emptyFavIcon);\r
-    }\r
-  },\r
-  showFavIcon: function() {\r
-    if(!this.favIconVisible) {\r
-      this.favIconVisible = true;\r
-      this.favIconParent.removeChild(this.emptyFavIcon);\r
-      this.favIconParent.appendChild(this.favIcon);\r
-    }\r
-  },\r
-  updateTitle: function(text) {\r
-    this.titleText = text;\r
-    return this.canUpdateTitle;\r
-  },\r
-  focusChange: function(value) {\r
-    this.windowFocused = value;\r
-\r
-    if(value)\r
-      this.cancelFlash();\r
-  }\r
-});\r
+qwebirc.ui.HilightController = new Class({
+  initialize: function(parent) {
+    this.parent = parent;
+    this.regex = null;
+    this.prevnick = null;
+  },
+  match: function(text) {
+    var nick = this.parent.nickname;
+    if(nick != this.prevnick) {
+      var classes = '[\\s\\.,;:]';
+      this.regex = new RegExp('(^|' + classes + ')' + RegExp.escape(nick) + '(' + classes + '|$)', "i");
+    }
+    if(text.match(this.regex))
+      return true;
+    return false;
+  }
+});
+
+qwebirc.ui.Beeper = new Class({
+  initialize: function(uiOptions) {
+    this.uiOptions = uiOptions;
+    
+    this.soundInited = false;
+    this.soundReady = false;
+
+    if(this.uiOptions.BEEP_ON_MENTION)
+      this.soundInit();
+  },
+  soundInit: function() {
+    if(this.soundInited)
+      return;
+    if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
+      return;
+    this.soundInited = true;
+    
+    this.soundPlayer = new qwebirc.sound.SoundPlayer();
+    this.soundPlayer.addEvent("ready", function() {
+      this.soundReady = true;
+    }.bind(this));
+    
+    this.soundPlayer.go();
+  },
+  beep: function() {
+    if(!this.soundReady || !this.uiOptions.BEEP_ON_MENTION)
+      return;
+      
+    this.soundPlayer.beep();
+  }
+});
+
+qwebirc.ui.Flasher = new Class({
+  initialize: function(uiOptions) {
+    this.uiOptions = uiOptions;
+    
+    this.windowFocused = false;
+    this.canUpdateTitle = true;
+    this.titleText = document.title;
+
+    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;
+      document.addEvent("mousedown", this.cancelFlash.bind(this));
+      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)
+      return;
+
+    this.titleText = document.title; /* just in case */      
+    var flashA = function() {
+      this.hideFavIcon();
+      this.canUpdateTitle = false;
+      document.title = "Activity!";
+      
+      this.flasher = flashB.delay(500);
+    }.bind(this);
+    
+    var flashB = function() {
+      this.showFavIcon();
+      this.canUpdateTitle = true;
+      document.title = this.titleText;
+      
+      this.flasher = flashA.delay(500);
+    }.bind(this);
+
+    this.flashing = true;
+    flashA();
+  },
+  cancelFlash: function() {
+    if(!this.canFlash || !$defined(this.flasher))
+      return;
+      
+    this.flashing = false;
+    
+    $clear(this.flasher);
+    this.flasher = null;
+    
+    this.showFavIcon();
+    document.title = this.titleText;
+    this.canUpdateTitle = true;
+  },
+  hideFavIcon: function() {
+    if(this.favIconVisible) {
+      /* only seems to work in firefox */
+      this.favIconVisible = false;
+      this.favIconParent.removeChild(this.favIcon);
+      this.favIconParent.appendChild(this.emptyFavIcon);
+    }
+  },
+  showFavIcon: function() {
+    if(!this.favIconVisible) {
+      this.favIconVisible = true;
+      this.favIconParent.removeChild(this.emptyFavIcon);
+      this.favIconParent.appendChild(this.favIcon);
+    }
+  },
+  updateTitle: function(text) {
+    this.titleText = text;
+    return this.canUpdateTitle;
+  },
+  focusChange: function(value) {
+    this.windowFocused = value;
+
+    if(value)
+      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));
+    }
+  }
+});