]> jfr.im git - irc/quakenet/qwebirc.git/blob - js/ui/notifications.js
Merge pull request #402 from retropc/reqs
[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 var classes = '[\\s\\.,;:]';
11 this.regex = new RegExp('(^|' + classes + ')' + RegExp.escape(nick) + '(' + classes + '|$)', "i");
12 }
13 if(text.match(this.regex))
14 return true;
15 return false;
16 }
17 });
18
19 qwebirc.ui.Beeper = new Class({
20 initialize: function(uiOptions) {
21 this.uiOptions = uiOptions;
22
23 this.soundPlayer = new qwebirc.sound.SoundPlayer();
24 },
25 beep: function(notification) {
26 if(notification && !this.uiOptions.BEEP_ON_MENTION)
27 return;
28
29 this.soundPlayer.play("beep3.mp3");
30 }
31 });
32
33 qwebirc.ui.Flasher = new Class({
34 initialize: function(uiOptions) {
35 this.uiOptions = uiOptions;
36
37 this.windowFocused = false;
38 this.canUpdateTitle = true;
39 this.titleText = document.title;
40
41 var favIcon = this._getFavIcon();
42 if($defined(favIcon)) {
43 this.favIcon = favIcon;
44 this.favIconParent = favIcon.parentNode;
45 this.favIconVisible = true;
46 this.emptyFavIcon = new Element("link");
47 this.emptyFavIcon.rel = "shortcut icon";
48 this.emptyFavIcon.href = qwebirc.global.staticBaseURL + "images/empty_favicon.ico";
49 this.emptyFavIcon.type = "image/x-icon";
50 this.flashing = false;
51
52 this.canFlash = true;
53 document.addEvent("mousedown", this.cancelFlash.bind(this));
54 document.addEvent("keydown", this.cancelFlash.bind(this));
55 } else {
56 this.canFlash = false;
57 }
58 },
59 _getFavIcon: function() {
60 var favIcons = $$("head link");
61 for(var i=0;i<favIcons.length;i++)
62 if(favIcons[i].getAttribute("rel") == "shortcut icon")
63 return favIcons[i];
64 },
65 flash: function() {
66 if(!this.uiOptions.FLASH_ON_MENTION || this.windowFocused || !this.canFlash || this.flashing)
67 return;
68
69 this.titleText = document.title; /* just in case */
70 var flashA = function() {
71 this.hideFavIcon();
72 this.canUpdateTitle = false;
73 document.title = "Activity!";
74
75 this.flasher = flashB.delay(500);
76 }.bind(this);
77
78 var flashB = function() {
79 this.showFavIcon();
80 this.canUpdateTitle = true;
81 document.title = this.titleText;
82
83 this.flasher = flashA.delay(500);
84 }.bind(this);
85
86 this.flashing = true;
87 flashA();
88 },
89 cancelFlash: function() {
90 if(!this.canFlash || !$defined(this.flasher))
91 return;
92
93 this.flashing = false;
94
95 $clear(this.flasher);
96 this.flasher = null;
97
98 this.showFavIcon();
99 document.title = this.titleText;
100 this.canUpdateTitle = true;
101 },
102 hideFavIcon: function() {
103 if(this.favIconVisible) {
104 /* only seems to work in firefox */
105 this.favIconVisible = false;
106 this.favIconParent.removeChild(this.favIcon);
107 this.favIconParent.appendChild(this.emptyFavIcon);
108 }
109 },
110 showFavIcon: function() {
111 if(!this.favIconVisible) {
112 this.favIconVisible = true;
113 this.favIconParent.removeChild(this.emptyFavIcon);
114 this.favIconParent.appendChild(this.favIcon);
115 }
116 },
117 updateTitle: function(text) {
118 this.titleText = text;
119 return this.canUpdateTitle;
120 },
121 focusChange: function(value) {
122 this.windowFocused = value;
123
124 if(value)
125 this.cancelFlash();
126 }
127 });
128
129 qwebirc.ui.Notifier = new Class({
130 initialize: function(uiOptions) {
131 this.uiOptions = uiOptions;
132
133 this.windowFocused = false;
134 this.previous = null;
135 this.setEnabled(this.uiOptions.NOTIFICATIONS);
136 },
137 focusChange: function(value) {
138 this.windowFocused = value;
139 },
140 setEnabled: function(value) {
141 this.enabled = value;
142 if(!value)
143 return;
144
145 if(this.isGranted())
146 return;
147
148 Notification.requestPermission(function (permission) {
149 if (!("permission" in Notification))
150 Notification.permission = permission;
151 });
152 },
153 isGranted: function() {
154 if(!("Notification" in window))
155 return false;
156
157 return Notification.permission === "granted";
158 },
159 notify: function(title, message, callback) {
160 if(this.windowFocused && !this.enabled || !this.isGranted())
161 return;
162
163 if(this.previous)
164 this.previous.close();
165
166 var n = new Notification(title, {body: message, icon: qwebirc.global.staticBaseURL + "images/qwebircsmall.png"});
167 var delay = function() {
168 n.close();
169 this.previous = null;
170 }.bind(this).delay(5000);
171
172 this.previous = n;
173 if(callback) {
174 n.addEventListener("click", function() {
175 this.previous = null;
176 window.focus();
177 callback();
178 });
179 n.addEventListener("close", function() {
180 this.previous = null;
181 }.bind(this));
182 }
183 }
184 });