]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/notifications.js
Refactor beeping into seperate class, and rename SoundUI to NotificationUI.
[irc/quakenet/qwebirc.git] / js / ui / notifications.js
1 qwebirc.ui.HilightController = new Class({
2 initialize: function(parent) {
3 this.parent = parent;
4 this.regex = null;
5 this.prevnick = null;
6 },
7 match: function(text) {
8 var nick = this.parent.nickname;
9 if(nick != this.prevnick)
10 this.regex = new RegExp('\\b' + RegExp.escape(nick) + '\\b', "i");
11
12 if(text.match(this.regex))
13 return true;
14 return false;
15 }
16 });
17
18 qwebirc.ui.Beeper = new Class({
19 initialize: function(uiOptions) {
20 this.uiOptions = uiOptions;
21
22 this.soundInited = false;
23 this.soundReady = false;
24
25 if(this.uiOptions.BEEP_ON_MENTION)
26 this.soundInit();
27 },
28 soundInit: function() {
29 if(this.soundInited)
30 return;
31 if(!$defined(Browser.Plugins.Flash) || Browser.Plugins.Flash.version < 8)
32 return;
33 this.soundInited = true;
34
35 this.soundPlayer = new qwebirc.sound.SoundPlayer();
36 this.soundPlayer.addEvent("ready", function() {
37 this.soundReady = true;
38 }.bind(this));
39
40 this.soundPlayer.go();
41 },
42 beep: function() {
43 if(!this.soundReady || !this.uiOptions.BEEP_ON_MENTION)
44 return;
45
46 this.soundPlayer.beep();
47 }
48 });
49
50 qwebirc.ui.Flasher = new Class({
51 initialize: function(uiOptions) {
52 this.uiOptions = uiOptions;
53
54 this.windowFocused = false;
55 this.canUpdateTitle = true;
56 this.titleText = document.title;
57
58 var favIcons = $$("link[rel=icon]"), favIconParent = $$("head");
59 if(favIcons && favIcons.length > 0 && favIconParent && favIconParent.length > 0) {
60 this.favIcon = favIcons[0];
61 this.favIconParent = favIconParent[0];
62 this.favIconVisible = true;
63 this.emptyFavIcon = new Element("link");
64 this.emptyFavIcon.rel = "shortcut icon";
65 this.emptyFavIcon.href = "/images/empty_favicon.ico";
66
67 this.flashing = false;
68
69 window.addEvent("focus", function() { this.windowFocused = true; this.cancelFlash(); console.log("focus") }.bind(this));
70 window.addEvent("blur", function() { this.windowFocused = false; console.log("blue") }.bind(this));
71
72 this.canFlash = true;
73 } else {
74 this.canFlash = false;
75 }
76 },
77 flash: function() {
78 if(!this.uiOptions.FLASH_ON_MENTION || this.windowFocused || !this.canFlash || this.flashing)
79 return;
80
81 this.titleText = document.title; /* just in case */
82 var flashA = function() {
83 this.hideFavIcon();
84 this.canUpdateTitle = false;
85 document.title = "Activity!";
86
87 this.flasher = flashB.delay(500);
88 }.bind(this);
89
90 var flashB = function() {
91 this.showFavIcon();
92 this.canUpdateTitle = true;
93 document.title = this.titleText;
94
95 this.flasher = flashA.delay(500);
96 }.bind(this);
97
98 this.flashing = true;
99 flashA();
100 },
101 cancelFlash: function() {
102 if(!this.canFlash || !$defined(this.flasher))
103 return;
104
105 this.flashing = false;
106
107 $clear(this.flasher);
108 this.flasher = null;
109
110 this.showFavIcon();
111 document.title = this.titleText;
112 this.canUpdateTitle = true;
113 },
114 hideFavIcon: function() {
115 if(this.favIconVisible) {
116 this.favIconVisible = false;
117 this.favIconParent.removeChild(this.favIcon);
118 this.favIconParent.appendChild(this.emptyFavIcon);
119 }
120 },
121 showFavIcon: function() {
122 if(!this.favIconVisible) {
123 this.favIconVisible = true;
124 this.favIconParent.removeChild(this.emptyFavIcon);
125 this.favIconParent.appendChild(this.favIcon);
126 }
127 },
128 updateTitle: function(text) {
129 this.titleText = text;
130 return this.canUpdateTitle;
131 }
132 });