|
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", |
14 | 10 | },
|
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", |
24 | 20 | },
|
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", |
34 | 30 | },
|
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; |
54 | 50 | }
|
| 51 | +} |
55 | 52 |
|
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]; |
57 | 58 |
|
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); |
60 | 61 |
|
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 | +}; |
62 | 66 |
|
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 | +}; |
66 | 71 |
|
67 |
| - document.registerFullscreen = function(target) { |
| 72 | +// Register Fullscreen API to document or custom-element |
| 73 | +fullscreen.registerFullscreen = function(target) { |
68 | 74 |
|
69 |
| - target = target || document; |
| 75 | + target = target || document; |
70 | 76 |
|
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(); |
74 | 82 |
|
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]; |
77 | 86 |
|
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 | + }); |
80 | 90 |
|
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 | + }); |
84 | 95 |
|
85 |
| - return true; |
86 |
| - }; |
| 96 | + return true; |
87 | 97 |
|
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; |
90 | 101 |
|
91 | 102 | } else {
|
92 | 103 |
|
93 |
| - document.registerFullscreen = function() { |
94 |
| - return false; |
95 |
| - }; |
| 104 | + return false; |
96 | 105 |
|
97 | 106 | }
|
98 | 107 |
|
99 |
| -})(document); |
| 108 | +}; |
| 109 | + |
| 110 | +export default fullscreen; |
0 commit comments