Skip to content

Commit 098f154

Browse files
committed
Change to use a module based approach
1 parent cf76f73 commit 098f154

File tree

4 files changed

+89
-80
lines changed

4 files changed

+89
-80
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
bower_components
21
node_modules
32
*.sublime-*

fullscreen-api.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

fullscreen-api.js

Lines changed: 88 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,110 @@
1-
(function(document) {
2-
'use strict';
3-
4-
const apis = {
5-
w3: {
6-
enabled: "fullscreenEnabled",
7-
element: "fullscreenElement",
8-
request: "requestFullscreen",
9-
exit: "exitFullscreen",
10-
events: {
11-
change: "fullscreenchange",
12-
error: "fullscreenerror"
13-
}
1+
const apis = {
2+
w3: {
3+
enabled: "fullscreenEnabled",
4+
element: "fullscreenElement",
5+
request: "requestFullscreen",
6+
exit: "exitFullscreen",
7+
events: {
8+
change: "fullscreenchange",
9+
error: "fullscreenerror",
1410
},
15-
webkit: {
16-
enabled: "webkitFullscreenEnabled",
17-
element: "webkitCurrentFullScreenElement",
18-
request: "webkitRequestFullscreen",
19-
exit: "webkitExitFullscreen",
20-
events: {
21-
change: "webkitfullscreenchange",
22-
error: "webkitfullscreenerror"
23-
}
11+
},
12+
webkit: {
13+
enabled: "webkitFullscreenEnabled",
14+
element: "webkitCurrentFullScreenElement",
15+
request: "webkitRequestFullscreen",
16+
exit: "webkitExitFullscreen",
17+
events: {
18+
change: "webkitfullscreenchange",
19+
error: "webkitfullscreenerror",
2420
},
25-
moz: {
26-
enabled: "mozFullScreenEnabled",
27-
element: "mozFullScreenElement",
28-
request: "mozRequestFullScreen",
29-
exit: "mozCancelFullScreen",
30-
events: {
31-
change: "mozfullscreenchange",
32-
error: "mozfullscreenerror"
33-
}
21+
},
22+
moz: {
23+
enabled: "mozFullScreenEnabled",
24+
element: "mozFullScreenElement",
25+
request: "mozRequestFullScreen",
26+
exit: "mozCancelFullScreen",
27+
events: {
28+
change: "mozfullscreenchange",
29+
error: "mozfullscreenerror",
3430
},
35-
ms: {
36-
enabled: "msFullscreenEnabled",
37-
element: "msFullscreenElement",
38-
request: "msRequestFullscreen",
39-
exit: "msExitFullscreen",
40-
events: {
41-
change: "MSFullscreenChange",
42-
error: "MSFullscreenError"
43-
}
44-
}
45-
};
46-
47-
const w3 = apis.w3;
48-
let api = null;
49-
for (const vendor in apis) {
50-
if (apis[vendor].enabled in document) {
51-
api = apis[vendor];
52-
break;
53-
}
31+
},
32+
ms: {
33+
enabled: "msFullscreenEnabled",
34+
element: "msFullscreenElement",
35+
request: "msRequestFullscreen",
36+
exit: "msExitFullscreen",
37+
events: {
38+
change: "MSFullscreenChange",
39+
error: "MSFullscreenError",
40+
},
41+
},
42+
};
43+
44+
const w3 = apis.w3;
45+
let api = null;
46+
for (const vendor in apis) {
47+
if (apis[vendor].enabled in document) {
48+
api = apis[vendor];
49+
break;
5450
}
51+
}
5552

56-
if (api && api !== w3) {
53+
const fullscreen = {};
54+
55+
// Set initial static properties
56+
fullscreen[w3.enabled] = document[api.enabled];
57+
fullscreen[w3.element] = document[api.element];
5758

58-
document[w3.enabled] = document[api.enabled];
59-
document[w3.element] = document[api.element];
59+
// Bind exit method
60+
fullscreen[w3.exit] = document[api.exit].bind(document);
6061

61-
document[w3.exit] = document[api.exit];
62+
// Call request for fullscreen on element
63+
fullscreen[w3.request] = function(target) {
64+
target[api.request].call(target);
65+
};
6266

63-
Element.prototype[w3.request] = function() {
64-
this[api.request].call(this);
65-
};
67+
// Check if target is active fullscreen element
68+
fullscreen.fullscreenActive = function(target) {
69+
return this[w3.enabled] && this[w3.element] === target;
70+
};
6671

67-
document.registerFullscreen = function(target) {
72+
// Register Fullscreen API to document or custom-element
73+
fullscreen.registerFullscreen = function(target) {
6874

69-
target = target || document;
75+
target = target || document;
7076

71-
target.addEventListener(api.events.change, (e) => {
72-
e.stopPropagation();
73-
e.stopImmediatePropagation();
77+
if (api && api !== w3) {
78+
79+
target.addEventListener(api.events.change, (e) => {
80+
e.stopPropagation();
81+
e.stopImmediatePropagation();
7482

75-
document[w3.enabled] = document[api.enabled];
76-
document[w3.element] = document[api.element];
83+
// Update static properties on change
84+
this[w3.enabled] = document[api.enabled];
85+
this[w3.element] = document[api.element];
7786

78-
document.dispatchEvent(new Event(w3.events.change));
79-
});
87+
// [TODO]: What should this be dispatched on? target || document || by-function
88+
target.dispatchEvent(new CustomEvent(w3.events.change, {detail:{originalEvent:e}}));
89+
});
8090

81-
target.addEventListener(api.events.error, (e) => {
82-
document.dispatchEvent(new Event(w3.events.error));
83-
});
91+
target.addEventListener(api.events.error, (e) => {
92+
// [TODO]: What should this be dispatched on? target || document || by-function
93+
target.dispatchEvent(new CustomEvent(w3.events.error, {detail:{originalEvent:e}}));
94+
});
8495

85-
return true;
86-
};
96+
return true;
8797

88-
// document.body.registerFullscreen.call(document);
89-
document.registerFullscreen();
98+
// [TODO]: Support official w3 api here
99+
// } else if (api && api === w3) {
100+
// return true;
90101

91102
} else {
92103

93-
document.registerFullscreen = function() {
94-
return false;
95-
};
104+
return false;
96105

97106
}
98107

99-
})(document);
108+
};
109+
110+
export default fullscreen;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@jsilvermist/fullscreen-api",
33
"version": "0.1.0",
4-
"description": "A Fullscreen API polyfill for use with custom elements",
4+
"description": "A modular Fullscreen API interface",
55
"main": "fullscreen-api.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)