-
Notifications
You must be signed in to change notification settings - Fork 0
/
fullscreen-api.js
137 lines (124 loc) · 3.27 KB
/
fullscreen-api.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
const apis = {
w3: {
enabled: "fullscreenEnabled",
element: "fullscreenElement",
request: "requestFullscreen",
exit: "exitFullscreen",
events: {
change: "fullscreenchange",
error: "fullscreenerror",
},
},
webkit: {
enabled: "webkitFullscreenEnabled",
element: "webkitCurrentFullScreenElement",
request: "webkitRequestFullscreen",
exit: "webkitExitFullscreen",
events: {
change: "webkitfullscreenchange",
error: "webkitfullscreenerror",
},
},
moz: {
enabled: "mozFullScreenEnabled",
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",
},
},
};
let api = null;
for (const vendor in apis) {
if (apis[vendor].enabled in document) {
api = apis[vendor];
break;
}
}
const changeCallbacks = [];
const errorCallbacks = [];
// Mirror initial static properties
export let fullscreenEnabled = api !== null && document[api.enabled];
export let fullscreenElement = api !== null ? document[api.element] : null;
// Bind exit method
export const exitFullscreen = api !== null ? document[api.exit].bind(document) : undefined;
// Call request for fullscreen on element
export const requestFullscreen = function(target) {
target[api.request].call(target);
}
function resolveListenerType(type) {
switch (type) {
case 'change':
return changeCallbacks;
case 'error':
return errorCallbacks;
default:
console.error(`[fullscreen-api] '${type}' is not a valid type, please use 'change' or 'error'.`);
return null;
}
}
function resolveListenerCallback(cb) {
if (typeof cb === 'function') {
return cb;
} else {
console.error('[fullscreen-api] Listener callback is not a function:', cb);
return null;
}
}
export const addFullscreenListener = function(type, cb) {
type = resolveListenerType(type);
cb = resolveListenerCallback(cb);
if (!type || !cb) {
return false;
}
const index = type.indexOf(cb);
if (index !== -1) {
return false;
}
type.push(cb);
return true;
}
export const removeFullscreenListener = function(type, cb) {
type = resolveListenerType(type);
cb = resolveListenerCallback(cb);
if (!type || !cb) {
return false;
}
const index = type.indexOf(cb);
if (index === -1) {
return false;
}
type.splice(index, 1);
return true;
}
if (fullscreenEnabled) {
document.addEventListener(api.events.change, (e) => {
// Update static properties on change
fullscreenEnabled = document[api.enabled];
fullscreenElement = document[api.element];
changeCallbacks.forEach(cb => cb(e));
});
document.addEventListener(api.events.error, (e) => {
errorCallbacks.forEach(cb => cb(e));
});
}
export default {
get enabled() { return fullscreenEnabled },
get element() { return fullscreenElement },
request: requestFullscreen,
exit: exitFullscreen,
addListener: addFullscreenListener,
removeListener: removeFullscreenListener,
};