-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjquery.winFocus.js
73 lines (69 loc) · 3.18 KB
/
jquery.winFocus.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
;(function($) {
if (!$.winFocus) {
$.extend({
winFocus: function() {
var init = true, methods = [];
if (!$(document).data('winFocus')) $(document).data('winFocus', $.winFocus.init());
for (x in arguments) {
if (typeof arguments[x] == "object") {
if (arguments[x]["blur"]) $.winFocus.methods.blur.push(arguments[x].blur);
if (arguments[x]["focus"]) $.winFocus.methods.focus.push(arguments[x].focus);
if (arguments[x]["blurFocus"]) $.winFocus.methods.blurFocus.push(arguments[x].blurFocus);
if (arguments[x]["initRun"]) init = arguments[x].initRun;
}
else if (typeof arguments[x] == "function") methods.push(arguments[x]);
else if (typeof arguments[x] == "boolean") {
init = arguments[x];
}
}
if (methods) {
if (methods.length == 1) $.winFocus.methods.blurFocus.push(methods[0]);
else {
$.winFocus.methods.blur.push(methods[0]);
$.winFocus.methods.focus.push(methods[1]);
}
}
if (init) $.winFocus.methods.onChange();
}
});
$.winFocus.init = function() {
// var document.hidden || document.msHidden || document.webkitHidden || document.mozHidden;
// Standards:
if ($.winFocus.props.hidden in document) // IE10 | FF20+
document.addEventListener("visibilitychange", $.winFocus.methods.onChange);
else if (($.winFocus.props.hidden = "mozHidden") in document) // Older FF Versions (?)
document.addEventListener("mozvisibilitychange", $.winFocus.methods.onChange);
else if (($.winFocus.props.hidden = "webkitHidden") in document) // Chrome
document.addEventListener("webkitvisibilitychange", $.winFocus.methods.onChange);
else if (($.winFocus.props.hidden = "msHidden") in document) // IE 4-6
document.addEventListener("msvisibilitychange", $.winFocus.methods.onChange);
else if (($.winFocus.props.hidden = "onfocusin") in document) // IE7-9
document.onfocusin = document.onfocusout = $.winFocus.methods.onChange;
else // All others:
window.onpageshow = window.onpagehide = window.onfocus = window.onblur = $.winFocus.methods.onChange;
return $.winFocus;
}
$.winFocus.methods = {
blurFocus: [], blur: [], focus: [],
exeCB: function(e) {
if ($.winFocus.methods.blurFocus) $.each($.winFocus.methods.blurFocus, function(k, v) { if (typeof this == 'function') this.apply($.winFocus, [e, !e.hidden]) });
if (e.hidden && $.winFocus.methods.blur) $.each($.winFocus.methods.blur, function(k, v) { if (typeof this == 'function') this.apply($.winFocus, [e]) });
if (!e.hidden && $.winFocus.methods.focus) $.each($.winFocus.methods.focus, function(k, v) { if (typeof this == 'function') this.apply($.winFocus, [e]) });
},
onChange: function(e) {
var eMap = { focus: false, focusin: false, pageshow: false, blur: true, focusout: true, pagehide: true };
e = e || window.event;
if (e) {
e.hidden = e.type in eMap ? eMap[e.type] : document[$.winFocus.props.hidden];
$(window).data("visible", !e.hidden);
$.winFocus.methods.exeCB(e);
}
else {
try { $.winFocus.methods.onChange.call(document, new Event('visibilitychange')); }
catch(err) { }
}
}
}
$.winFocus.props = { hidden: "hidden" }
}
})(jQuery);