-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwinFocus.js
140 lines (125 loc) · 4.19 KB
/
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
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
/* winFocus() */
;(function() {
var callBacks = { blur: [], focus: [], blurFocus: [] },
hidden = "hidden"
function winFocus() {
var args = Array.prototype.slice.call(arguments, 0)
init = true, initMethods = [], methods = []
for (var x in args) {
switch (typeof args[x]) {
case 'boolean':
init: args[x];
break;
case 'function':
methods.push(args[x]);
break;
case 'object':
if (args[x].hasOwnProperty('init')) init = args[x]["init"];
if (args[x]["blur"]) {
callBacks.blur.push(args[x]["blur"]);
if (init) initMethods.push(args[x]["blur"]);
}
if (args[x]["focus"]) {
callBacks.focus.push(args[x]["focus"]);
if (init) initMethods.push(args[x]["focus"]);
}
if (args[x]["blurFocus"]) {
callBacks.blurFocus.push(args[x]["blurFocus"]);
if (init) initMethods.push(args[x]["blurFocus"]);
}
break;
}
}
if (methods && methods.length) {
if (init) initMethods.concat(methods);
switch (methods.length) {
case 1:
callBacks.blurFocus.push(methods[0]);
break;
case 2:
callBacks.blur.push(methods[0]);
callBacks.focus.push(methods[1]);
break;
default:
for (var x in methods) {
switch (x%3) {
case 0:
callBacks.blur.push(methods[x]);
break;
case 1:
callBacks.focus.push(methods[x]);
break;
case 2:
callBacks.blurFocus.push(methods[x]);
break;
}
}
}
}
if (init && initMethods.length) for (var x in initMethods) initMethods[x].apply(window, [{ hidden: document[hidden] }]);
}
function onChange(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[hidden];
window.visible = !e.hidden;
exeCB(e);
}
else {
try { onChange.call(document, new Event('visibilitychange')); }
catch(err) { }
}
}
function exeCB(e) {
if (e.hidden && callBacks.blur.length) for (var x in callBacks.blur) callBacks.blur[x].apply(window, [e]);
if (!e.hidden && callBacks.focus.length) for (var x in callBacks.focus) callBacks.focus[x].apply(window, [e]);
if (callBacks.blurFocus.length) for (var x in callBacks.blurFocus) callBacks.blurFocus[x].apply(window, [e, !e.hidden]);
}
function initWinFocus() {
if (console && console['log']) console.log('Initializing winFocus()');
// Standard initialization
if (hidden in document) // IE10 | FF20+
document.addEventListener("visibilitychange", onChange);
else if ((hidden = "mozHidden") in document) // Older FF Versions (?)
document.addEventListener("mozvisibilitychange", onChange);
else if ((hidden = "webkitHidden") in document) // Chrome
document.addEventListener("webkitvisibilitychange", onChange);
else if ((hidden = "msHidden") in document) // IE 4-6
document.addEventListener("msvisibilitychange", onChange);
else if ((hidden = "onfocusin") in document) // IE7-9
document.onfocusin = document.onfocusout = onChange;
else // All others:
window.onpageshow = window.onpagehide = window.onfocus = window.onblur = onChange;
}
winFocus.clear = function(what) {
if (what && callBacks[what]) callBacks[what] = [];
else if (void 0 == what || what == 'all') for (var x in callBacks) callBacks[x] = [];
return callBacks;
}
winFocus.getCallBacks = function(what) {
if (what && callBacks[what]) return callBacks[what];
return callBacks;
}
if (document.readyState == "complete") initWinFocus();
window.onload = initWinFocus;
// add as window variable
window.hasOwnProperty("winFocus")||(window.winFocus=winFocus);
// add as a jQuery extension
try {
if (window.hasOwnProperty('jQuery') && jQuery) {
jQuery.winFocus || (jQuery.extend({
winFocus: function() {
var args = Array.prototype.slice.call(arguments, 0);
if (args[0] && /^clear/i.test(args[0])) return winFocus.clear.apply(jQuery);
if (args[0] && /^callbacks$/i.test(args[0])) {
args = Array.prototype.slice.call(arguments, 1);
return winFocus.getCallBacks.apply(window, args);
}
return winFocus.apply(window, args);
}
}))
}
}
catch (err) {}
})();