-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfullscreen-api-polyfill.js
106 lines (92 loc) · 2.63 KB
/
fullscreen-api-polyfill.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
(function ( doc ) {
// Use JavaScript script mode
"use strict";
/*global Element */
var pollute = true,
api,
vendor,
apis = {
// http://dvcs.w3.org/hg/fullscreen/raw-file/tip/Overview.html
w3: {
enabled: "fullscreenEnabled",
element: "fullscreenElement",
request: "requestFullscreen",
exit: "exitFullscreen",
events: {
change: "fullscreenchange",
error: "fullscreenerror"
}
},
webkit: {
enabled: "webkitIsFullScreen",
element: "webkitCurrentFullScreenElement",
request: "webkitRequestFullScreen",
exit: "webkitCancelFullScreen",
events: {
change: "webkitfullscreenchange",
error: "webkitfullscreenerror"
}
},
moz: {
enabled: "mozFullScreen",
element: "mozFullScreenElement",
request: "mozRequestFullScreen",
exit: "mozCancelFullScreen",
events: {
change: "mozfullscreenchange",
error: "mozfullscreenerror"
}
},
ms: {
enabled: "msFullscreenEnabled",
element: "msFullscreenElement",
request: "msRequestFullscreen",
exit: "msExitFullscreen",
events: {
change: "MSFullscreenChange",
error: "MSFullscreenError"
}
}
},
w3 = apis.w3;
// Loop through each vendor's specific API
for (vendor in apis) {
// Check if document has the "enabled" property
if (apis[vendor].enabled in doc) {
// It seems this browser support the fullscreen API
api = apis[vendor];
break;
}
}
function dispatch( type, target ) {
var event = doc.createEvent( "Event" );
event.initEvent( type, true, false );
target.dispatchEvent( event );
} // end of dispatch()
function handleChange( e ) {
// Recopy the enabled and element values
doc[w3.enabled] = doc[api.enabled];
doc[w3.element] = doc[api.element];
dispatch( w3.events.change, e.target );
} // end of handleChange()
function handleError( e ) {
dispatch( w3.events.error, e.target );
} // end of handleError()
// Pollute only if the API doesn't already exists
if (pollute && !(w3.enabled in doc) && api) {
// Add listeners for fullscreen events
doc.addEventListener( api.events.change, handleChange, false );
doc.addEventListener( api.events.error, handleError, false );
// Copy the default value
doc[w3.enabled] = doc[api.enabled];
doc[w3.element] = doc[api.element];
// Match the reference for exitFullscreen
doc[w3.exit] = doc[api.exit];
// Add the request method to the Element's prototype
Element.prototype[w3.request] = function () {
return this[api.request].apply( this, arguments );
};
}
// Return the API found (or undefined if the Fullscreen API is unavailable)
return api;
}( document ));