-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnotify-osd.js
364 lines (309 loc) · 10.9 KB
/
notify-osd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*global window: false, document: false */
(function ($) {
"use strict";
var defaults = {
text : '',
icon : '',
timeout : 5,
sticky : false,
dismissable : false,
click_through : true,
buffer : 40,
opacity_max : 0.85,
opacity_min : 0.20,
spacing : 20,
visible_max : 3
};
// helper objects
// Change Region, Point and PriorityQueue to proper constructor function
// Memory required will be less, since methods will be passed by reference and not by value
var Point = (function(){
function Point(x, y){
this.x = x;
this.y = y;
};
Point.prototype.lies_inside = function(region){
return ((this.y > region.top) && (this.y < region.bottom) && (this.x > region.left) && (this.x < region.right));
};
Point.prototype.min_distance_in = function(region){
var rel_position = {
top : Math.abs(this.y - region.top),
right : Math.abs(this.x - region.right),
bottom : Math.abs(this.y - region.bottom),
left : Math.abs(this.x - region.left)
};
var min = rel_position.left;
if (rel_position.top < min) {
min = rel_position.top;
}
if (rel_position.right < min) {
min = rel_position.right;
}
if (rel_position.bottom < min) {
min = rel_position.bottom;
}
return min;
};
Point.prototype.to_string = function(){
return "x: "+this.x+" y: "+this.y;
};
return Point;
})();
var Region = (function(){
function Region(data){
this.update(data);
}
Region.prototype.update = function(data){
this.top = data.top;
this.left = data.left;
this.width = data.width;
this.height = data.height;
this.bottom = data.top + data.height;
this.right = data.left + data.width;
};
Region.prototype.to_string = function(){
return "t: "+this.top+" l: "+this.left+" h: "+this.height+" w: "+this.width;
};
return Region;
})();
var PriorityQueue = (function(){
function PriorityQueue(events){
var q = [];
this.getQ = function(){
return q;
};
this.events = events;
}
PriorityQueue.prototype.first = function(){
return this.getQ()[0];
};
PriorityQueue.prototype.last = function(){
var q = this.getQ();
return q[q.length - 1];
};
PriorityQueue.prototype.clear = function(){
var q = this.getQ();
q.length = 0;
};
PriorityQueue.prototype.indexOf = function(x){
var self = this;
return $.inArray(x, self.getQ());
};
PriorityQueue.prototype.enqueue = function(x){
var last = this.last(),
q = this.getQ();
q.push(x);
if(last){
last.below = x;
x.above = last;
}
// alert subscribers of the event 'events.enqueue'
this.events.enqueue && $(document).trigger(this.events.enqueue);
};
PriorityQueue.prototype.extract = function(i){
var q = this.getQ(),
x_above = q[i-1],
x_below = q[i+1],
x = q.splice(i,1)[0];
if (x_above) {
x_above.below = x_below;
}
if (x_below) {
x_below.above = x_above;
}
// alert subscribers of the event 'events.extract'
this.events.extract && $(document).trigger({ 'type': this.events.extract, 'extracted': x });
return x;
};
PriorityQueue.prototype.dequeue = function(){
return this.extract(0);
};
PriorityQueue.prototype.length = function(){
return this.getQ().length;
}
return PriorityQueue;
})();
// notification queues
var notifs = {
visible : new PriorityQueue({ enqueue: 'notif_display', extract: 'notif_removed' }), // queue of currently-visible notifications
queued : new PriorityQueue({ enqueue: 'notif_created' }) // queue of yet-to-be-displayed notifications
};
// bind subscriber event handlers for handling notifications
// new notification is created and enqueued
var display_next_notif = function () {
if (notifs.visible.length() < defaults.visible_max && notifs.queued.length() > 0) {
var notif_obj = notifs.queued.dequeue();
notifs.visible.enqueue(notif_obj);
}
};
$(document).bind(notifs.queued.events.enqueue, display_next_notif);
// oldest-queued notification is to be displayed on page
$(document).bind(notifs.visible.events.enqueue, function (evt) {
var notif_obj = notifs.visible.last();
var above = notif_obj.above;
var notif_obj_top = defaults.spacing;
if (above) {
notif_obj_top += above.offset().top + above.height();
}
$(notif_obj).css('top', notif_obj_top);
notif_obj.display();
});
// a notification is removed ("extracted") from the page
$(document).bind(notifs.visible.events.extract, function (evt) {
var extracted = evt.extracted;
// if there are any visible notifications below the extracted one ...
if (extracted.below !== undefined) {
// ... move them up ...
extracted.reposition_below(function () {
// ... and display the next queued notification *after* all notifications have finished repositioning ...
display_next_notif();
});
} else {
// ... but if there aren't any below, then just display the next one directly
display_next_notif();
}
// remove it from the DOM
extracted.remove();
});
// user-facing notification API
var reposition_count = 0; // to keep track of how many notifications are done repositioning after one of them is dismissed
$.notify_osd = {
defaults : defaults,
create : function (options) {
var opts = $.extend({}, defaults, options);
var mouse, notification, buffer;
notification = new Region({});
buffer = new Region({});
var notif_obj = $('<div class="notify-osd"><div><table><tr><td class="notify-osd-content">'+opts.text+'</td></tr></table></div></div>').css({
'opacity' : opts.opacity_max,
'z-index' : 9999
}).hide();
notif_obj.extend({
opts : opts,
set_text : function (text) {
$(this).find('.notify-osd-content').html(text);
return this;
},
set_icon : function (src) {
if (src !== '') {
$(this).find('tr').prepend('<td class="notify-osd-icon"><img src="'+src+'" /></td>');
} else {
$(this).find('.notify-osd-icon').remove();
}
return this;
},
set_dismissable : function (dismissable) {
if (opts.dismissable || opts.sticky) {
$(this).children('div').append('<span class="notify-osd-dismiss" title="Dismiss">×</span>');
$(this).find('.notify-osd-dismiss').unbind('click').click(function () {
notif_obj.dismiss();
});
} else {
$(this).find('.notify-osd-dismiss').unbind('click').remove();
}
return this;
},
set_click_through : function (click_through) {
var pointer_events = click_through ? 'none' : 'auto';
$('.notify-osd').css('pointer-events', pointer_events);
// ensure dismiss button is always clickable
$('.notify-osd .notify-osd-dismiss').css('pointer-events', 'auto');
},
update_regions : function () {
// update notification and buffer regions
notification.update({
top : notif_obj.offset().top,
left : notif_obj.offset().left,
width : notif_obj.width(),
height : notif_obj.height()
});
buffer.update({
top : notification.top - opts.buffer,
left : notification.left - opts.buffer,
width : notification.width + 2 * opts.buffer,
height : notification.height + 2 * opts.buffer
});
},
display : function () {
this.set_text(opts.text).set_icon(opts.icon).set_dismissable(opts.dismissable);
$(this).appendTo('body').fadeIn('fast', function () {
notif_obj.update_regions();
window.clearTimeout(notif_obj.timeout);
notif_obj.timeout = (!opts.sticky && opts.timeout) ? window.setTimeout(function () { notif_obj.dismiss(); },opts.timeout*1000) : null;
mouse = new Point(0, 0);
notif_obj.set_click_through(opts.click_through);
$(document).mousemove(notif_obj.mousemove);
});
return this;
},
translate : function (move, max_count, callback) {
var notif_obj_top = notif_obj.offset().top - move.up;
notif_obj.animate({
'top' : notif_obj_top
}, 'fast', function () {
notif_obj.update_regions();
reposition_count++;
// check if this was the last notification to be repositioned
if (reposition_count === max_count) {
callback();
}
});
},
reposition_below : function (callback) {
// callback will be called once ALL notifications have FINISHED repositioning
var notif = notif_obj.below;
var max_count = 0;
reposition_count = 0;
while (notif !== undefined) {
max_count++;
notif = notif.below;
}
var notif = notif_obj.below;
while (notif !== undefined) {
notif.translate({ up: notif_obj.height() + defaults.spacing }, max_count, callback);
notif = notif.below;
}
},
dismiss : function () {
$(document).unbind('mousemove', notif_obj.mousemove);
$(this).fadeOut('fast', function () {
var notif_obj_index = notifs.visible.indexOf(notif_obj);
if (notif_obj_index >= 0) {
notifs.visible.extract(notif_obj_index);
}
});
},
mousemove : function (e) {
mouse.x = e.pageX - $('body').scrollLeft();
mouse.y = e.pageY - $('body').scrollTop();
var opacity;
if (mouse.lies_inside(buffer)) {
// find the minimum distance of the mouse from the edges of the buffer region
var min_distance = mouse.min_distance_in(buffer);
if (mouse.lies_inside(notification)) {
opacity = opts.opacity_min;
} else {
opacity = opts.opacity_max - (opts.opacity_max-opts.opacity_min) * (min_distance/opts.buffer);
}
} else {
opacity = opts.opacity_max;
}
notif_obj.css('opacity', opacity);
}
});
notifs.queued.enqueue(notif_obj);
return notif_obj;
},
setup : function (options) {
defaults = $.extend({}, defaults, options);
},
dismiss : function () {
notifs.queued.clear();
var notif = notifs.visible.first();
while (notif !== undefined) {
notif.dismiss();
notif = notif.below;
}
}
};
}(jQuery));