forked from bdougherty/BigScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigscreen.js
268 lines (268 loc) · 10.7 KB
/
bigscreen.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
/*! BigScreen
* v2.0.4 - 2014-02-06
* https://github.com/bdougherty/BigScreen
* Copyright 2014 Brad Dougherty; Apache 2.0 License
*/
(function(window, document, iframe) {
"use strict";
var keyboardAllowed = typeof Element !== "undefined" && "ALLOW_KEYBOARD_INPUT" in Element;
var iOS7 = /i(Pad|Phone|Pod)/.test(navigator.userAgent) && parseInt(navigator.userAgent.replace(/^.*OS (\d+)_(\d+).*$/, "$1.$2"), 10) >= 7;
var fn = function() {
var testElement = document.createElement("video");
var browserProperties = {
request: [ "requestFullscreen", "webkitRequestFullscreen", "webkitRequestFullScreen", "mozRequestFullScreen", "msRequestFullscreen" ],
exit: [ "exitFullscreen", "webkitCancelFullScreen", "webkitExitFullscreen", "mozCancelFullScreen", "msExitFullscreen" ],
enabled: [ "fullscreenEnabled", "webkitFullscreenEnabled", "mozFullScreenEnabled", "msFullscreenEnabled" ],
element: [ "fullscreenElement", "webkitFullscreenElement", "webkitCurrentFullScreenElement", "mozFullScreenElement", "msFullscreenElement" ],
change: [ "fullscreenchange", "webkitfullscreenchange", "mozfullscreenchange", "MSFullscreenChange" ],
error: [ "fullscreenerror", "webkitfullscreenerror", "mozfullscreenerror", "MSFullscreenError" ]
};
var properties = {};
for (var prop in browserProperties) {
for (var i = 0, length = browserProperties[prop].length; i < length; i++) {
if (browserProperties[prop][i] in testElement || browserProperties[prop][i] in document || "on" + browserProperties[prop][i].toLowerCase() in document) {
properties[prop] = browserProperties[prop][i];
break;
}
}
}
return properties;
}();
function _getVideo(element) {
var videoElement = null;
if (element.tagName === "VIDEO") {
videoElement = element;
} else {
var videos = element.getElementsByTagName("video");
if (videos[0]) {
videoElement = videos[0];
}
}
return videoElement;
}
var lastVideoElement = null;
var hasControls = null;
var emptyFunction = function() {};
var elements = [];
var chromeAndroid = false;
if (navigator.userAgent.indexOf("Android") > -1 && navigator.userAgent.indexOf("Chrome") > -1) {
chromeAndroid = parseInt(navigator.userAgent.replace(/^.*Chrome\/(\d+).*$/, "$1"), 10) || true;
}
function videoEnterFullscreen(element) {
var videoElement = _getVideo(element);
if (videoElement && videoElement.webkitEnterFullscreen) {
try {
if (videoElement.readyState < videoElement.HAVE_METADATA) {
videoElement.addEventListener("loadedmetadata", function onMetadataLoaded() {
videoElement.removeEventListener("loadedmetadata", onMetadataLoaded, false);
videoElement.webkitEnterFullscreen();
hasControls = !!videoElement.getAttribute("controls");
}, false);
videoElement.load();
} else {
videoElement.webkitEnterFullscreen();
hasControls = !!videoElement.getAttribute("controls");
}
lastVideoElement = videoElement;
} catch (err) {
return callOnError("not_supported", element);
}
return true;
}
return callOnError(fn.request === undefined ? "not_supported" : "not_enabled", element);
}
function resizeExitHack() {
if (!bigscreen.element) {
callOnExit();
removeWindowResizeHack();
}
}
function addWindowResizeHack() {
if (iframe && fn.change === "webkitfullscreenchange") {
window.addEventListener("resize", resizeExitHack, false);
}
}
function removeWindowResizeHack() {
if (iframe && fn.change === "webkitfullscreenchange") {
window.removeEventListener("resize", resizeExitHack, false);
}
}
var callOnEnter = function(actualElement) {
var lastElement = elements[elements.length - 1];
if ((actualElement === lastElement.element || actualElement === lastVideoElement) && lastElement.hasEntered) {
return;
}
if (actualElement.tagName === "VIDEO") {
lastVideoElement = actualElement;
}
if (elements.length === 1) {
bigscreen.onenter(bigscreen.element);
}
lastElement.enter.call(lastElement.element, actualElement || lastElement.element);
lastElement.hasEntered = true;
};
var callOnExit = function() {
if (lastVideoElement && !hasControls && !iOS7) {
lastVideoElement.setAttribute("controls", "controls");
lastVideoElement.removeAttribute("controls");
}
lastVideoElement = null;
hasControls = null;
var element = elements.pop();
if (element) {
element.exit.call(element.element);
if (!bigscreen.element) {
elements.forEach(function(element) {
element.exit.call(element.element);
});
elements = [];
bigscreen.onexit();
}
}
};
var callOnError = function(reason, element) {
if (elements.length > 0) {
var obj = elements.pop();
element = element || obj.element;
obj.error.call(element, reason);
bigscreen.onerror(element, reason);
}
};
var bigscreen = {
request: function(element, enterCallback, exitCallback, errorCallback) {
element = element || document.body;
elements.push({
element: element,
enter: enterCallback || emptyFunction,
exit: exitCallback || emptyFunction,
error: errorCallback || emptyFunction
});
if (fn.request === undefined) {
return videoEnterFullscreen(element);
}
if (iframe && document[fn.enabled] === false) {
return videoEnterFullscreen(element);
}
if (chromeAndroid !== false && chromeAndroid < 32) {
return videoEnterFullscreen(element);
}
if (iframe && fn.enabled === undefined) {
fn.enabled = "webkitFullscreenEnabled";
element[fn.request]();
setTimeout(function() {
if (!document[fn.element]) {
document[fn.enabled] = false;
videoEnterFullscreen(element);
} else {
document[fn.enabled] = true;
}
}, 250);
return;
}
try {
if (/5\.1[\.\d]* Safari/.test(navigator.userAgent)) {
element[fn.request]();
} else {
element[fn.request](keyboardAllowed && Element.ALLOW_KEYBOARD_INPUT);
}
setTimeout(function() {
if (!document[fn.element]) {
callOnError(iframe ? "not_enabled" : "not_allowed", element);
}
}, 100);
} catch (err) {
callOnError("not_enabled", element);
}
},
exit: function() {
removeWindowResizeHack();
document[fn.exit]();
},
toggle: function(element, enterCallback, exitCallback, errorCallback) {
if (bigscreen.element) {
bigscreen.exit();
} else {
bigscreen.request(element, enterCallback, exitCallback, errorCallback);
}
},
videoEnabled: function(element) {
if (bigscreen.enabled) {
return true;
}
element = element || document.body;
var video = _getVideo(element);
if (!video || video.webkitSupportsFullscreen === undefined) {
return false;
}
return video.readyState < video.HAVE_METADATA ? "maybe" : video.webkitSupportsFullscreen;
},
onenter: emptyFunction,
onexit: emptyFunction,
onchange: emptyFunction,
onerror: emptyFunction
};
try {
Object.defineProperties(bigscreen, {
element: {
enumerable: true,
get: function() {
if (lastVideoElement && lastVideoElement.webkitDisplayingFullscreen) {
return lastVideoElement;
}
return document[fn.element] || null;
}
},
enabled: {
enumerable: true,
get: function() {
if (fn.exit === "webkitCancelFullScreen" && !iframe) {
return true;
}
if (chromeAndroid !== false && chromeAndroid < 32) {
return false;
}
return document[fn.enabled] || false;
}
}
});
} catch (err) {
bigscreen.element = null;
bigscreen.enabled = false;
}
if (fn.change) {
document.addEventListener(fn.change, function onFullscreenChange(event) {
bigscreen.onchange(bigscreen.element);
if (bigscreen.element) {
var previousElement = elements[elements.length - 2];
if (previousElement && previousElement.element === bigscreen.element) {
callOnExit();
} else {
callOnEnter(bigscreen.element);
addWindowResizeHack();
}
} else {
callOnExit();
}
}, false);
}
document.addEventListener("webkitbeginfullscreen", function onBeginFullscreen(event) {
elements.push({
element: event.srcElement,
enter: emptyFunction,
exit: emptyFunction,
error: emptyFunction
});
bigscreen.onchange(event.srcElement);
callOnEnter(event.srcElement);
}, true);
document.addEventListener("webkitendfullscreen", function onEndFullscreen(event) {
bigscreen.onchange(event.srcElement);
callOnExit(event.srcElement);
}, true);
if (fn.error) {
document.addEventListener(fn.error, function onFullscreenError(event) {
callOnError("not_allowed");
}, false);
}
window["BigScreen"] = bigscreen;
})(window, document, self !== top);