-
Notifications
You must be signed in to change notification settings - Fork 1
/
page-visibility.js
208 lines (196 loc) · 7.18 KB
/
page-visibility.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
/*!
* page-visibility.js - Page Visibility API Wrapper and Polyfill
* http://github.com/Fiery-Fenix
* @version 1.1.0
* @license MIT
*/
/* Public methods:
* pageVisibility.isHidden
* pageVisibility.getVisibilityState()
* pageVisibility.subscribe(callback)
* pageVisibility.unsubscribe(callbackId)
* pageVisibility.isNativelySupported();
*
* Browser support:
* IE 8-9 - polyfilled by onfocusin/onfocusout events
* IE 10+ - native support
* Firefox 18+ - native support
* Chrome 14-32 - native support with webkit prefix
* Chrome 33+ - native support
* iOS Safari 7.x - native support not working, polyfilled with pageshow/pagehide events
* iOS Safari 8.0+ - native support
* Opera 15-19 - native support with webkit prefix
* Opera 20+ - native support
* iOS Safari 7.1 - native support
* Android Browser 4-4.3 - polyfilled by focus/blur events
* Android Browser 4.4+ - native support
* Opera Mobile 30+ - native support
* Chrome for Android - native support
* Firefox for Android - native support
*/
;(function (scope, win, doc) {
var idCounter = -1,
self;
self = {
/**
* @const State visible
*/
STATE_VISIBLE: 'visible',
/**
* @const State hidden
*/
STATE_HIDDEN: 'hidden',
/**
* @property {{current: string|undefined, prev: string|undefined}} - State of visibility
*/
_visibilityState: {
current: undefined,
prev: undefined
},
/**
* @property {boolean} - Is natively supported
*/
_isNative: false,
/**
* @property {Object} - Callbacks list
*/
_callbacks: {},
/**
* Checks if current state is hidden
* @return {boolean} - Page is hidden?
*/
isHidden: function () {
return self._visibilityState.current === self.STATE_HIDDEN;
},
/**
* Gets current state object
* @return {{current: string, prev: string}} - Current and previous state of visibility
*/
getVisibilityState: function () {
return self._visibilityState;
},
/**
* Subscribes on change visibility event
* @param {Function} callback - Callback function
* @return {string} - Uniq ID of callback, needed for unsubscribe
*/
subscribe: function (callback) {
if (typeof callback === 'function') {
var listenerId = self._uniqId();
self._callbacks[listenerId] = callback;
return listenerId;
}
},
/**
* Unsubscribes from change visibility event
* @param {string} listenerId - Uniq ID of callback to unsubscribe
*/
unsubscribe: function (listenerId) {
if (self._callbacks[listenerId] && !self._isDestructing) {
delete self._callbacks[listenerId];
}
},
/**
* Check if Page Visibility API has native supported in current browser
* @return {boolean} - Has native support?
*/
isNativelySupported: function () {
return self._isNative;
},
/**
* Generates uniq ID for callbacks
* @return {string} - Uniq ID
* @private
*/
_uniqId: function () {
return 'pageVisibility_' + String(++idCounter);
},
/**
* Internal change visibility event handler
* @param {Event} event
* @private
*/
_onChange: function (event) {
var eventMap = {
focus: self.STATE_VISIBLE,
focusin: self.STATE_VISIBLE,
pageshow: self.STATE_VISIBLE,
blur: self.STATE_HIDDEN,
focusout: self.STATE_HIDDEN,
pagehide: self.STATE_HIDDEN
};
if (self._isDestructing) {
return;
}
event = event || win.event;
self._visibilityState.prev = self._visibilityState.current;
if (self.isNativelySupported()) {
// For native supported events - we are using native defined property
self._visibilityState.current = doc.webkitVisibilityState || doc.visibilityState;
} else if (event.type in eventMap) {
// For shims - we are using event map
self._visibilityState.current = eventMap[event.type];
} else {
// For unsupported events - just use wisibile state
self._visibilityState.current = self.STATE_VISIBLE;
}
self._notifySubscribers(event);
},
/**
* Notifies subscribers on change visibility event
* @param {Event} event
* @private
*/
_notifySubscribers: function (event) {
for (var c in self._callbacks) {
if (self._callbacks.hasOwnProperty(c) && self._callbacks[c]) {
self._callbacks[c].call(doc, event, self._visibilityState);
}
}
},
/**
* Init function
* Checks native Page Visibility API supports and provides vendor prefix wrapper
* Subscribes to some needed events to provide replacement for native functionality
* @private
*/
_init: function () {
var isSafari7 = navigator.userAgent.match(/(iPad|iPhone|iPod touch);.*CPU.*OS 7_\d/i),
listener;
// Prevent multiple inits
if (self._inited) {
return;
}
self._isNative = ('hidden' in doc || 'webkitHidden' in doc) && !isSafari7;
listener = function () {
self._onChange.apply(self, arguments);
};
if (isSafari7) {
// Hack for iOS Safari 7.x
// Safari reports that support Page Visibility API, but event 'visibilitychange' never happen
win.addEventListener('pageshow', listener, true);
win.addEventListener('pagehide', listener, true);
} else if (self._isNative) {
// For normal browsers - subscribe to standart event visibilitychange
self._visibilityState.current = doc.webkitVisibilityState || doc.visibilityState;
doc.addEventListener(('webkitHidden' in doc ? 'webkit' : '') + 'visibilitychange', listener);
} else if ('onfocusin' in doc) {
// For IE < 10 use focusin/focusout
doc.attachEvent('onfocusin', listener);
doc.attachEvent('onfocusout', listener);
} else {
// For other unsupported browsers, like Android 4.0-4.3
// Used window instead of document because of Android Browser and Opera
win.addEventListener('focus', listener, true);
win.addEventListener('blur', listener, true);
}
self._inited = true;
}
};
self._init();
if (typeof module !== 'undefined' && module.exports) {
module.exports = self;
} else {
scope.pageVisibility = self;
}
})(this, window, document);