1 | /** |
---|
2 | * @preserve jquery.fullscreen 1.1.0 |
---|
3 | * https://github.com/kayahr/jquery-fullscreen-plugin |
---|
4 | * Copyright (C) 2012 Klaus Reimer <k@ailis.de> |
---|
5 | * Licensed under the MIT license |
---|
6 | * (See http://www.opensource.org/licenses/mit-license) |
---|
7 | */ |
---|
8 | |
---|
9 | (function() { |
---|
10 | |
---|
11 | /** |
---|
12 | * Sets or gets the fullscreen state. |
---|
13 | * |
---|
14 | * @param {boolean=} state |
---|
15 | * True to enable fullscreen mode, false to disable it. If not |
---|
16 | * specified then the current fullscreen state is returned. |
---|
17 | * @return {boolean|Element|jQuery|null} |
---|
18 | * When querying the fullscreen state then the current fullscreen |
---|
19 | * element (or true if browser doesn't support it) is returned |
---|
20 | * when browser is currently in full screen mode. False is returned |
---|
21 | * if browser is not in full screen mode. Null is returned if |
---|
22 | * browser doesn't support fullscreen mode at all. When setting |
---|
23 | * the fullscreen state then the current jQuery selection is |
---|
24 | * returned for chaining. |
---|
25 | * @this {jQuery} |
---|
26 | */ |
---|
27 | function fullScreen(state) |
---|
28 | { |
---|
29 | var e, func, doc; |
---|
30 | |
---|
31 | // Do nothing when nothing was selected |
---|
32 | if (!this.length) return this; |
---|
33 | |
---|
34 | // We only use the first selected element because it doesn't make sense |
---|
35 | // to fullscreen multiple elements. |
---|
36 | e = (/** @type {Element} */ this[0]); |
---|
37 | |
---|
38 | // Find the real element and the document (Depends on wether the |
---|
39 | // document itself or a HTML element was selected) |
---|
40 | if (e instanceof Document) |
---|
41 | { |
---|
42 | doc = e; |
---|
43 | e = doc.documentElement; |
---|
44 | } |
---|
45 | else |
---|
46 | { |
---|
47 | doc = e.ownerDocument; |
---|
48 | } |
---|
49 | |
---|
50 | // When no state was specified then return the current state. |
---|
51 | if (state == null) |
---|
52 | { |
---|
53 | // When fullscreen mode is not supported then return null |
---|
54 | if (!((/** @type {?Function} */ doc["cancelFullScreen"]) |
---|
55 | || (/** @type {?Function} */ doc["webkitCancelFullScreen"]) |
---|
56 | || (/** @type {?Function} */ doc["mozCancelFullScreen"]))) |
---|
57 | { |
---|
58 | return null; |
---|
59 | } |
---|
60 | |
---|
61 | // Check fullscreen state |
---|
62 | state = !!doc["fullScreen"] |
---|
63 | || !!doc["webkitIsFullScreen"] |
---|
64 | || !!doc["mozFullScreen"]; |
---|
65 | if (!state) return state; |
---|
66 | |
---|
67 | // Return current fullscreen element or "true" if browser doesn't |
---|
68 | // support this |
---|
69 | return (/** @type {?Element} */ doc["fullScreenElement"]) |
---|
70 | || (/** @type {?Element} */ doc["webkitCurrentFullScreenElement"]) |
---|
71 | || (/** @type {?Element} */ doc["mozFullScreenElement"]) |
---|
72 | || state; |
---|
73 | } |
---|
74 | |
---|
75 | // When state was specified then enter or exit fullscreen mode. |
---|
76 | if (state) |
---|
77 | { |
---|
78 | // Enter fullscreen |
---|
79 | func = (/** @type {?Function} */ e["requestFullScreen"]) |
---|
80 | || (/** @type {?Function} */ e["webkitRequestFullScreen"]) |
---|
81 | || (/** @type {?Function} */ e["mozRequestFullScreen"]); |
---|
82 | if (func) func.call(e); |
---|
83 | return this; |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
87 | // Exit fullscreen |
---|
88 | func = (/** @type {?Function} */ doc["cancelFullScreen"]) |
---|
89 | || (/** @type {?Function} */ doc["webkitCancelFullScreen"]) |
---|
90 | || (/** @type {?Function} */ doc["mozCancelFullScreen"]); |
---|
91 | if (func) func.call(doc); |
---|
92 | return this; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Toggles the fullscreen mode. |
---|
98 | * |
---|
99 | * @return {!jQuery} |
---|
100 | * The jQuery selection for chaining. |
---|
101 | * @this {jQuery} |
---|
102 | */ |
---|
103 | function toggleFullScreen() |
---|
104 | { |
---|
105 | return (/** @type {!jQuery} */ fullScreen.call(this, |
---|
106 | !fullScreen.call(this))); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Handles the browser-specific fullscreenchange event and triggers |
---|
111 | * a jquery event for it. |
---|
112 | * |
---|
113 | * @param {?Event} event |
---|
114 | * The fullscreenchange event. |
---|
115 | */ |
---|
116 | function fullScreenChangeHandler(event) |
---|
117 | { |
---|
118 | jQuery(document).trigger(new jQuery.Event("fullscreenchange")); |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Handles the browser-specific fullscreenerror event and triggers |
---|
123 | * a jquery event for it. |
---|
124 | * |
---|
125 | * @param {?Event} event |
---|
126 | * The fullscreenerror event. |
---|
127 | */ |
---|
128 | function fullScreenErrorHandler(event) |
---|
129 | { |
---|
130 | jQuery(document).trigger(new jQuery.Event("fullscreenerror")); |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Installs the fullscreenchange event handler. |
---|
135 | */ |
---|
136 | function installFullScreenHandlers() |
---|
137 | { |
---|
138 | var e, change, error; |
---|
139 | |
---|
140 | // Determine event name |
---|
141 | e = document; |
---|
142 | if (e["webkitCancelFullScreen"]) |
---|
143 | { |
---|
144 | change = "webkitfullscreenchange"; |
---|
145 | error = "webkitfullscreenerror"; |
---|
146 | } |
---|
147 | else if (e["mozCancelFullScreen"]) |
---|
148 | { |
---|
149 | change = "mozfullscreenchange"; |
---|
150 | error = "mozfullscreenerror"; |
---|
151 | } |
---|
152 | else |
---|
153 | { |
---|
154 | change = "fullscreenchange"; |
---|
155 | error = "fullscreenerror"; |
---|
156 | } |
---|
157 | |
---|
158 | // Install the event handlers |
---|
159 | document.addEventListener(change, fullScreenChangeHandler, true); |
---|
160 | document.addEventListener(error, fullScreenErrorHandler, true); |
---|
161 | } |
---|
162 | |
---|
163 | jQuery.fn["fullScreen"] = fullScreen; |
---|
164 | jQuery.fn["toggleFullScreen"] = toggleFullScreen; |
---|
165 | installFullScreenHandlers(); |
---|
166 | |
---|
167 | })(); |
---|