UNPKG

95.6 kBJavaScriptView Raw
1/**
2 * @file Screen management. Contains the {@link CB_Screen} static class.
3 * @author Joan Alba Maldonado <workindalian@gmail.com>
4 * @license Creative Commons Attribution 4.0 International. See more at {@link https://crossbrowdy.com/about#what_is_the_crossbrowdy_copyright_and_license}.
5 */
6
7
8/**
9 * Static class to manage the screen. It will return itself if it is tried to be instantiated. It can use [detect-zoom]{@link https://github.com/tombigel/detect-zoom} and [NoSleep.js]{@link https://github.com/richtr/NoSleep.js?utm_source=recordnotfound.com}.
10 * @namespace
11 */
12var CB_Screen = function() { return CB_Screen; };
13{
14 CB_Screen._screenWidth = 0; //Screen width resolution.
15 CB_Screen._screenHeight = 0; //Screen height resolution.
16 CB_Screen._previousScreenWidth = 0; //Previous screen width resolution.
17 CB_Screen._previousScreenHeight = 0; //Previous screen height resolution.
18 CB_Screen._screenAvailableWidth = 0; //Screen available width resolution.
19 CB_Screen._screenAvailableHeight = 0; //Screen available height resolution.
20 CB_Screen._previousScreenAvailableWidth = 0; //Previous screen available width resolution.
21 CB_Screen._previousScreenAvailableHeight = 0; //Previous screen available height resolution.
22 CB_Screen._windowWidth = 0; //Window width resolution.
23 CB_Screen._windowHeight = 0; //Window height resolution.
24 CB_Screen._previousWindowWidth = 0; //Previous window width resolution.
25 CB_Screen._previousWindowHeight = 0; //Previous window height resolution.
26 CB_Screen._screenColorDepth = 0; //Screen color depth.
27 CB_Screen._scrollLeft = null; //Left scroll value.
28 CB_Screen._scrollTop = null; //Top scroll value.
29 CB_Screen._previousScrollLeft = null; //Previous left scroll value.
30 CB_Screen._previousScrollTop = null; //Previous top scroll value.
31 CB_Screen._zoom = 1; //Zoom applied to the web.
32 CB_Screen._pixelRatio = 1; //Pixel ratio multiplied by the zoom.
33 CB_Screen._previousZoom = 0; //Zoom applied to the web.
34 CB_Screen._previousPixelRatio = 0; //Pixel ratio multiplied by the zoom.
35 CB_Screen._isLandscape = null; //Tells whether web is displayed in landscape or portrait position.
36 CB_Screen._isVisible = null; //Tells whether web is visible or not.
37 CB_Screen._isFocused = null; //Tells whether web is focused or not (has lost the focus).
38 CB_Screen._isFullScreen = null; //Tells whether full screen mode is enabled or not.
39 CB_Screen._screenLock = null; //Keeps the MozWakeLock object to be able to release the lock related with the screen (so far, only works in Firefox/Firefox OS).
40 CB_Screen._noSleep = null; //Keeps the last NoSleep object for the NoSleep.js library.
41 CB_Screen._noSleepEnabled = false; //Boolean to keeps the status of the noSleep.js object (enabled or disabled).
42
43 CB_Screen._eventsHolder = {}; //Keeps the functions to fire for every special event (if any).
44 /*
45 CB_Screen.onResizeFunction; //Function that is executed when window is resized (onResize event).
46 CB_Screen.onScrollLeftFunction; //Function that is executen when scroll left is changed.
47 CB_Screen.onScrollTopFunction; //Function that is executen when scroll top is changed.
48 CB_Screen.onVisibilityChangeFunction; //Function that is executed when window gets or losts the visibility.
49 CB_Screen.onFocusChangeFunction; //Function that is executed when window gets or losts the focus.
50 CB_Screen.onResizeOrZoomFunction; //Function that is executed when window is resized or zoom is applied.
51 CB_Screen.onOrientationChangeFunction; //Function that is executed when orientation changes (landscape or portrait).
52 CB_Screen.onFullScreenChangeFunction; //Function that is executed when full screen mode changes.
53 */
54
55 CB_Screen._storedScreenWidth = 0; //Old screen available width resolution (internal use only).
56 CB_Screen._storedScreenHeight = 0; //Old screen available height resolution (internal use only).
57 CB_Screen._storedScreenAvailableWidth = 0; //Old screen available width resolution (internal use only).
58 CB_Screen._storedScreenAvailableHeight = 0; //Old screen available height resolution (internal use only).
59 CB_Screen._storedWindowWidth = 0; //Old window width resolution (internal use only).
60 CB_Screen._storedWindowHeight = 0; //Old window height resolution (internal use only).
61 CB_Screen._storedZoom = 0; //Zoom applied to the web.
62 CB_Screen._storedPixelRatio = 0; //Pixel ratio multiplied by the zoom.
63
64 CB_Screen._refreshTimeout = null; //It will store the timeout that refresh screen properties all the time.
65 CB_Screen.initialized = false; //It will tells whether the object has been initialized or not.
66
67
68 //Initializes all values:
69 CB_Screen.init = function()
70 {
71 if (CB_Screen.initialized) { return CB_Screen; }
72
73 //Sets that the object has already been initialized:
74 CB_Screen.initialized = true;
75
76 //Stores first values for both old window width and height:
77 CB_Screen._storedScreenWidth = CB_Screen._screenWidth;
78 CB_Screen._storedScreenHeight = CB_Screen._screenHeight;
79 CB_Screen._storedScreenAvailableWidth = CB_Screen._screenAvailableWidth;
80 CB_Screen._storedScreenAvailableHeight = CB_Screen._screenAvailableHeight;
81 CB_Screen._storedWindowWidth = CB_Screen._windowWidth;
82 CB_Screen._storedWindowHeight = CB_Screen._windowHeight;
83
84 //It also stores for both zoom level and pixel ratio:
85 CB_Screen._storedZoom = CB_Screen._zoom;
86 CB_Screen._storedPixelRatio = CB_Screen._pixelRatio;
87
88 //Sets the handler for the scroll event:
89 CB_Events.add(window, "scroll", function() { CB_Screen.getScrollTop(); CB_Screen.getScrollLeft(); }, true, true, false);
90
91 //It will check all the time if visibility changes:
92 CB_Screen._isVisible = true; //By default, window is visible. NOTE: this can produce a false positive if the script is loading being not visible.
93 if ("hidden" in document) { CB_Events.add(document, "visibilitychange", CB_Screen._visibilityChanged, true, true, false); } //document.addEventListener("visibilitychange", CB_Screen._visibilityChanged); }
94 else if ("mozHidden" in document) { CB_Events.add(document, "mozvisibilitychange", CB_Screen._visibilityChanged, true, true, false); } //document.addEventListener("mozvisibilitychange", CB_Screen._visibilityChanged); }
95 else if ("webkitHidden" in document) { CB_Events.add(document, "webkitvisibilitychange", CB_Screen._visibilityChanged, true, true, false); } //document.addEventListener("webkitvisibilitychange", CB_Screen._visibilityChanged); }
96 else if ("msHidden" in document) { CB_Events.add(document, "msvisibilitychange", CB_Screen._visibilityChanged, true, true, false); } // document.addEventListener("msvisibilitychange", CB_Screen._visibilityChanged); }
97 //else if ("onfocusin" in document) { document.onfocusin = document.onfocusout = CB_Screen._visibilityChanged; }
98 //else if ("onfocusin" in document) { document.onfocusin = document.onfocusout = function() { CB_Screen._visibilityChanged(); CB_Screen._focusChanged(); } }
99 //else { window.onfocus = window.onblur = CB_Screen._visibilityChanged; }
100 //else { window.onfocus = window.onblur = function() { CB_Screen._visibilityChanged(); CB_Screen._focusChanged(); } }
101
102 //It will check all the time if focus changes:
103 CB_Screen._isFocused = true; //By default, window is focused. NOTE: this can produce a false positive if the script is loading being not visible.
104
105
106 var focusOrBlurEventWorks = false;
107 try //Using catch due some web clients doesn't allow to manipulate the window object of parent iframes:
108 {
109 CB_Events.add(CB_Client.getWindow(), "focus", function() { focusOrBlurEventWorks = true; CB_Screen._focusChanged(true); }, true, true, false);
110 CB_Events.add(CB_Client.getWindow(), "blur", function() { focusOrBlurEventWorks = true; CB_Screen._focusChanged(false); }, true, true, false);
111 }
112 catch(E)
113 {
114 CB_Events.add(window, "focus", function() { focusOrBlurEventWorks = true; CB_Screen._focusChanged(true); }, true, true, false);
115 CB_Events.add(window, "blur", function() { focusOrBlurEventWorks = true; CB_Screen._focusChanged(false); }, true, true, false);
116 }
117
118 CB_Events.add(document, "mousedown", function() { if (focusOrBlurEventWorks) { return; } CB_Screen._focusChanged(true); }, true, true, false); //Mouse click will set focus too (IE8 fix).
119 CB_Events.add(document, "click", function() { if (focusOrBlurEventWorks) { return; } CB_Screen._focusChanged(true); }, true, true, false); //Click will set focus too (IE8 fix).
120 //CB_Events.add(document, "keydown", function() { if (focusOrBlurEventWorks) { return; } CB_Screen._focusChanged(true); }, true, true, false); //Key down event will set focus too (IE8 fix).
121 //CB_Events.add(document, "mousemove", function() { if (focusOrBlurEventWorks) { return; } CB_Screen._focusChanged(true); }, true, true, false); //Mouse movemenet will set focus too (IE8 fix).
122
123 //Starts running the loop:
124 CB_Screen._mainLoop();
125
126 return CB_Screen;
127 }
128
129
130 //Loop to watch the screen changes:
131 CB_Screen._mainLoop = function()
132 {
133 //Cancels the timeout (if any):
134 clearTimeout(CB_Screen._refreshTimeout);
135
136 CB_Screen.getWidth(); //Defines screen width resolution.
137 CB_Screen.getHeight(); //Defines screen height resolution.
138 CB_Screen.getWidthPrevious(); //Defines previous window width resolution.
139 CB_Screen.getHeightPrevious(); //Defines previous window height resolution.
140 CB_Screen.getAvailableWidth(); //Defines screen available width resolution.
141 CB_Screen.getAvailableHeight(); //Defines screen available height resolution.
142 CB_Screen.getAvailableWidthPrevious(); //Defines previous window width resolution.
143 CB_Screen.getAvailableHeightPrevious(); //Defines previous window height resolution.
144 CB_Screen.getWindowWidth(); //Defines window width resolution.
145 CB_Screen.getWindowHeight(); //Defines window height resolution.
146 CB_Screen.getWindowWidthPrevious(); //Defines previous window width resolution.
147 CB_Screen.getWindowHeightPrevious(); //Defines previous window height resolution.
148 CB_Screen.getColorDepth(); //Defines screen color depth.
149 //CB_Screen.getScrollLeft(); //Defines scroll left (and executes defined function if it changes).
150 //CB_Screen.getScrollTop(); //Defines scroll top (and executes defined function if it changes).
151 CB_Screen.getZoom(); //Defines zoom level.
152 CB_Screen.getPixelRatio(); //Defines pixel ratio multiplied by the zoom level.
153 CB_Screen.isLandscape(); //Defines whether device is in landscape or portrait position (and executes defined function if it changes).
154 CB_Screen.isFullScreen(); //Defines whether it's in full screen mode or not (and executes defined function if it changes).
155
156 //Executes the function defined for Resize and Zoom events (if any):
157 CB_Screen._processOnResizeOrZoomFunction();
158
159 //Executes the function defined for Full screen change event (if any):
160 //CB_Screen.onFullScreenChangeFunction(CB_Screen.onFullScreenChangeFunction, false);
161
162 //Executes the function again:
163 CB_Screen._refreshTimeout = setTimeout(CB_Screen._mainLoop, 1); //Calls itself again to update values all the time.
164 }
165
166
167 /**
168 * Gets the current screen width (horizontal resolution). Uses the [window.screen.width]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/width} property internally, when possible.
169 * @function
170 * @returns {number} Returns the current screen width (horizontal resolution) in pixels.
171 */
172 CB_Screen.getWidth = function()
173 {
174 if (screen && screen.width && !isNaN(screen.width))
175 {
176 CB_Screen._screenWidth = screen.width;
177 }
178
179 return CB_Screen._screenWidth;
180 }
181
182
183 /**
184 * Gets the current screen height (vertical resolution). Uses the [window.screen.height]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/height} property internally, when possible.
185 * @function
186 * @returns {number} Returns the current screen height (vertical resolution) in pixels.
187 */
188 CB_Screen.getHeight = function()
189 {
190 if (screen && screen.height && !isNaN(screen.height))
191 {
192 CB_Screen._screenHeight = screen.height;
193 }
194
195 return CB_Screen._screenHeight;
196 }
197
198
199 /**
200 * Gets the previous screen width (horizontal resolution). Calculated through the [window.screen.width]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/width} property internally, when possible. Useful when the resolution (screen size and/or orientation) changed.
201 * @function
202 * @returns {number} Returns the previous screen width (horizontal resolution) in pixels.
203 */
204 CB_Screen.getWidthPrevious = function()
205 {
206 if (CB_Screen._previousScreenWidth === 0) { CB_Screen._previousScreenWidth = CB_Screen.getWidth(); }
207 return CB_Screen._previousScreenWidth;
208 }
209
210
211 /**
212 * Gets the previous screen height (vertical resolution). Calculated through the [window.screen.height]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/height} property internally, when possible. Useful when the resolution (screen size and/or orientation) changed.
213 * @function
214 * @returns {number} Returns the previous screen height (vertical resolution) in pixels.
215 */
216 CB_Screen.getHeightPrevious = function()
217 {
218 if (CB_Screen._previousScreenHeight === 0) { CB_Screen._previousScreenHeight = CB_Screen.getHeight(); }
219 return CB_Screen._previousScreenHeight;
220 }
221
222
223 /**
224 * Gets the current available screen width (horizontal resolution). Uses the [window.screen.availWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/availWidth} property internally, when possible.
225 * @function
226 * @returns {number} Returns the current available screen width (horizontal resolution) in pixels.
227 */
228 CB_Screen.getAvailableWidth = function()
229 {
230 if (screen && screen.availWidth && !isNaN(screen.availWidth))
231 {
232 CB_Screen._screenAvailableWidth = screen.availWidth;
233 }
234
235 return CB_Screen._screenAvailableWidth;
236 }
237
238
239 /**
240 * Gets the current available screen height (vertical resolution). Uses the [window.screen.availHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/availHeight} property internally, when possible.
241 * @function
242 * @returns {number} Returns the current available screen height (vertical resolution) in pixels.
243 */
244 CB_Screen.getAvailableHeight = function()
245 {
246 if (screen && screen.availHeight && !isNaN(screen.availHeight))
247 {
248 CB_Screen._screenAvailableHeight = screen.availHeight;
249 }
250
251 return CB_Screen._screenAvailableHeight;
252 }
253
254
255 /**
256 * Gets the previous available screen width (horizontal resolution). Useful when the resolution (screen size and/or orientation) changed. Uses the [window.screen.availWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/availWidth} property internally, when possible.
257 * @function
258 * @returns {number} Returns the previous available screen width (horizontal resolution) in pixels.
259 */
260 CB_Screen.getAvailableWidthPrevious = function()
261 {
262 if (CB_Screen._previousScreenAvailableWidth === 0) { CB_Screen._previousScreenAvailableWidth = CB_Screen.getAvailableWidth(); }
263 return CB_Screen._previousScreenAvailableWidth;
264 }
265
266
267 /**
268 * Gets the previous available screen height (vertical resolution). Useful when the resolution (screen size and/or orientation) changed. Uses the [window.screen.availHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/availHeight} property internally, when possible.
269 * @function
270 * @returns {number} Returns the previous available screen height (vertical resolution) in pixels.
271 */
272 CB_Screen.getAvailableHeightPrevious = function()
273 {
274 if (CB_Screen._previousScreenAvailableHeight === 0) { CB_Screen._previousScreenAvailableHeight = CB_Screen.getAvailableHeight(); }
275 return CB_Screen._previousScreenAvailableHeight;
276 }
277
278
279 /**
280 * Gets the current window width (horizontal resolution). Internally, uses the [window.innerWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth} if possible and fallbacks to [document.documentElement.clientWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth} or [document.body.clientWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth} property otherwise, when possible.
281 * @function
282 * @returns {number} Returns the current window width (horizontal resolution) in pixels.
283 */
284 CB_Screen.getWindowWidth = function()
285 {
286 if (window && window.innerWidth && !isNaN(window.innerWidth))
287 {
288 CB_Screen._windowWidth = window.innerWidth;
289 }
290 else if (document && document.documentElement && document.documentElement.clientWidth && !isNaN(document.documentElement.clientWidth) && document.documentElement.clientWidth > 0)
291 {
292 CB_Screen._windowWidth = document.documentElement.clientWidth;
293 }
294 else if (document && document.body && document.body.clientWidth && !isNaN(document.body.clientWidth) && document.body.clientWidth > 0)
295 {
296 CB_Screen._windowWidth = document.body.clientWidth;
297 }
298 return CB_Screen._windowWidth;
299 }
300
301
302 /**
303 * Gets the current window height (vertical resolution). Internally, uses the [window.innerHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight} if possible and fallbacks to [document.documentElement.clientHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight} or [document.body.clientHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight} property otherwise, when possible.
304 * @function
305 * @returns {number} Returns the current window height (vertical resolution) in pixels.
306 */
307 CB_Screen.getWindowHeight = function()
308 {
309 if (window && window.innerHeight && !isNaN(window.innerHeight))
310 {
311 CB_Screen._windowHeight = window.innerHeight;
312 }
313 else if (document && document.documentElement && document.documentElement.clientHeight && !isNaN(document.documentElement.clientHeight) && document.documentElement.clientHeight > 0)
314 {
315 CB_Screen._windowHeight = document.documentElement.clientHeight;
316 }
317 else if (document && document.body && document.body.clientHeight && !isNaN(document.body.clientHeight) && document.body.clientHeight > 0)
318 {
319 CB_Screen._windowHeight = document.body.clientHeight;
320 }
321 return CB_Screen._windowHeight;
322 }
323
324
325 /**
326 * Gets the previous window width (horizontal resolution). Useful when the resolution (screen size and/or orientation) or window size changed. Internally, uses the [window.innerWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth} if possible and fallbacks to [document.documentElement.clientWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth} or [document.body.clientWidth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth} property otherwise, when possible.
327 * @function
328 * @returns {number} Returns the previous window width (horizontal resolution) in pixels.
329 */
330 CB_Screen.getWindowWidthPrevious = function()
331 {
332 if (CB_Screen._previousWindowWidth === 0) { CB_Screen._previousWindowWidth = CB_Screen.getWindowWidth(); }
333 return CB_Screen._previousWindowWidth;
334 }
335
336
337 /**
338 * Gets the previous window height (vertical resolution). Useful when the resolution (screen size and/or orientation) or window size changed. Internally, uses the [window.innerHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight} if possible and fallbacks to [document.documentElement.clientHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight} or [document.body.clientHeight]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight} property otherwise, when possible.
339 * @function
340 * @returns {number} Returns the previous window height (vertical resolution) in pixels.
341 */
342 CB_Screen.getWindowHeightPrevious = function()
343 {
344 if (CB_Screen._previousWindowHeight === 0) { CB_Screen._previousWindowHeight = CB_Screen.getWindowHeight(); }
345 return CB_Screen._previousWindowHeight;
346 }
347
348
349 /**
350 * Gets the current color depth. Uses the [window.screen.colorDepth]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/colorDepth} property internally, when possible.
351 * @function
352 * @returns {number} Returns the current color depth.
353 */
354 CB_Screen.getColorDepth = function()
355 {
356 if (screen && screen.colorDepth && !isNaN(screen.colorDepth))
357 {
358 CB_Screen._screenColorDepth = screen.colorDepth;
359 }
360 return CB_Screen._screenColorDepth;
361 }
362
363
364 /**
365 * Gets the current scroll left position (horizontal scroll) of the screen (main window). Uses the {@link CB_Elements.getScrollLeftById} function internally.
366 * @function
367 * @returns {number|null} Returns the current scroll left position (horizontal scroll) of the screen (main window). It could return null if something fails.
368 */
369 CB_Screen.getScrollLeft = function()
370 {
371 CB_Screen._previousScrollLeft = CB_Screen._scrollLeft;
372 return CB_Elements.getScrollLeftById
373 (
374 window, //elementId
375 function(scrollLeft, scrollLeftPrevious, scrollWidth, visiblePixels, scrollRelative, scrollRelativePrevious) //onScrollLeftChanges:
376 {
377 //If there is any defined function:
378 if (typeof(CB_Screen._eventsHolder["onScrollLeft"]) === "function")
379 {
380 //Sets the new and old positions (just in case the function needs it):
381 CB_Screen._previousScrollLeft = CB_Screen._scrollLeft;
382 CB_Screen._scrollLeft = scrollLeft;
383
384 //Executes the function:
385 CB_Screen._eventsHolder["onScrollLeft"]();
386 }
387 },
388 true, //fireFirstTime
389 false, //fireAlways
390 null, //timeoutMs
391 true, //returnNullOnFail
392 undefined //timeout
393 );
394 }
395
396
397 /**
398 * Gets the current scroll top position (vertical scroll) of the screen (main window). Uses the {@link CB_Elements.getScrollTopById} function internally.
399 * @function
400 * @returns {number|null} Returns the current scroll top position (vertical scroll) of the screen (main window). It could return null if something fails.
401 */
402 CB_Screen.getScrollTop = function()
403 {
404 CB_Screen._previousScrollTop = CB_Screen._scrollTop;
405 return CB_Elements.getScrollTopById
406 (
407 window, //elementId
408 function(scrollTop, scrollTopPrevious, scrollHeight, visiblePixels, scrollRelative, scrollRelativePrevious) //onScrollTopChanges:
409 {
410 //If there is any defined function:
411 if (typeof(CB_Screen._eventsHolder["onScrollTop"]) === "function")
412 {
413 //Sets the new and old positions (just in case the function needs it):
414 CB_Screen._previousScrollTop = CB_Screen._scrollTop;
415 CB_Screen._scrollTop = scrollTop;
416
417 //Executes the function:
418 CB_Screen._eventsHolder["onScrollTop"]();
419 }
420 },
421 true, //fireFirstTime
422 false, //fireAlways
423 null, //timeoutMs
424 true, //returnNullOnFail
425 undefined //timeout
426 );
427 }
428
429
430 /**
431 * Gets the current zoom level of the screen (main window). Uses [detect-zoom]{@link https://github.com/tombigel/detect-zoom} internally.
432 * @function
433 * @returns {number} Returns the current zoom level of the screen (main window). Default zoom level is 1 (one) even when it fails.
434 * @todo Find a better and more-compatible way to detect zoom which supports as many web clients as possible.
435 */
436 CB_Screen.getZoom = function()
437 {
438 //if (typeof(detectZoom) !== "undefined" && detectZoom !== null && detectZoom && typeof(detectZoom.zoom) !== "undefined" && detectZoom.zoom !== null && detectZoom.zoom && typeof(detectZoom.zoom) === "function")
439 if (typeof(detectZoom) !== "undefined" && detectZoom !== null && typeof(detectZoom.zoom) === "function")
440 {
441 CB_Screen._zoom = detectZoom.zoom();
442 //if (CB_Screen._zoom === null || CB_Screen._zoom === 0 || !CB_Screen._zoom) { CB_Screen._zoom = 1; }
443 if (!CB_Screen._zoom) { CB_Screen._zoom = 1; }
444 //} else { CB_Screen._zoom = CB_Screen._previousZoom = 0; }
445 } else { CB_Screen._zoom = CB_Screen._previousZoom = 1; }
446 return CB_Screen._zoom;
447 }
448
449
450 /**
451 * Gets the previous zoom level of the screen (main window). Useful when the zoom changed. Uses [detect-zoom]{@link https://github.com/tombigel/detect-zoom} internally.
452 * @function
453 * @returns {number} Returns the previous zoom level of the screen (main window). Default previous zoom level is 0 (zero) even when it fails.
454 */
455 CB_Screen.getZoomPrevious = function()
456 {
457 return CB_Screen._previousZoom;
458 }
459
460
461 /**
462 * Gets the current pixel ratio of the screen (main window). Uses [detect-zoom]{@link https://github.com/tombigel/detect-zoom} internally.
463 * @function
464 * @returns {number} Returns the current pixel ratio of the screen (main window). Default pixel ratio is 1 (one) even when it fails.
465 * @todo Find a better and more-compatible way to detect pixel ratio which supports as many web clients as possible.
466 */
467 CB_Screen.getPixelRatio = function()
468 {
469 //if (typeof(detectZoom) !== "undefined" && detectZoom !== null && detectZoom && typeof(detectZoom.device) !== "undefined" && detectZoom.device !== null && detectZoom.device && typeof(detectZoom.device) === "function")
470 if (typeof(detectZoom) !== "undefined" && detectZoom !== null && typeof(detectZoom.device) === "function")
471 {
472 CB_Screen._pixelRatio = detectZoom.device();
473 //} else { CB_Screen._pixelRatio = CB_Screen._previousPixelRatio = 0; }
474 //if (CB_Screen._pixelRatio === null || CB_Screen._pixelRatio === 0 || !CB_Screen._pixelRatio) { CB_Screen._pixelRatio = 1; }
475 if (!CB_Screen._pixelRatio) { CB_Screen._pixelRatio = 1; }
476 } else { CB_Screen._pixelRatio = CB_Screen._previousPixelRatio = 1; }
477 return CB_Screen._pixelRatio;
478 }
479
480
481 /**
482 * Gets the previous pixel ratio of the screen (main window). Useful when the zoom/pixel-ratio changed. Uses [detect-zoom]{@link https://github.com/tombigel/detect-zoom} internally.
483 * @function
484 * @returns {number} Returns the previous pixel ratio of the screen (main window). Default previous pixel ratio is 0 (zero) even when it fails.
485 * @todo Find a better and more-compatible way to detect pixel ratio which supports as many web clients as possible.
486 */
487 CB_Screen.getPixelRatioPrevious = function()
488 {
489 return CB_Screen._previousPixelRatio;
490 }
491
492
493 /**
494 * Tells whether the screen (main window) is in landscape position.
495 * @function
496 * @returns {boolean} Returns whether the screen is in landscape position.
497 */
498 CB_Screen.isLandscape = function()
499 {
500 var isLandscape = false;
501
502 if (CB_Screen.getWindowWidth() > CB_Screen.getWindowHeight())
503 {
504 isLandscape = true;
505 }
506
507 //If it's not the first time and position has been changed, calls the onOrientationChange function (if any):
508 if (CB_Screen._isLandscape !== null && CB_Screen._isLandscape !== isLandscape)
509 {
510 //If there is any defined function:
511 if (typeof(CB_Screen._eventsHolder["onOrientationChange"]) === "function")
512 {
513 //Sets the new position (just in case the function needs it):
514 CB_Screen._isLandscape = isLandscape;
515 //Executes the function:
516 CB_Screen._eventsHolder["onOrientationChange"]();
517 }
518 }
519
520 //Sets the new position:
521 CB_Screen._isLandscape = isLandscape;
522
523 return CB_Screen._isLandscape;
524 }
525
526
527 //Sets whether the web is visible (called every time that visibility changes):
528 CB_Screen._visibilityChanged = function(e)
529 {
530 var isVisible = true; //By default is visible.
531
532 //if (!e) { e = window.event; }
533 e = CB_Events.normalize(e);
534
535// if (e.type === "focus" || e.type === "focusin")
536 {
537// isVisible = true;
538 }
539// else if (e.type === "blur" || e.type === "focusout")
540 {
541// isVisible = false;
542 }
543// else
544 {
545 var hidden = "";
546 if (typeof(document.hidden) !== "undefined")
547 {
548 hidden = "hidden";
549 }
550 else if (typeof(document.mozHidden) !== "undefined")
551 {
552 hidden = "mozHidden";
553 }
554 else if (typeof document.msHidden !== "undefined")
555 {
556 hidden = "msHidden";
557 }
558 else if (typeof document.webkitHidden !== "undefined")
559 {
560 hidden = "webkitHidden";
561 }
562 //if (hidden !== "") { isVisible = document[hidden] ? false : true; }
563 if (hidden !== "") { isVisible = !document[hidden]; }
564 }
565
566 //Calls the onVisibilityChange function (if any):
567 if (typeof(CB_Screen._eventsHolder["onVisibilityChange"]) === "function")
568 {
569 //Sets the new visibility (just in case the function needs it):
570 CB_Screen._isVisible = isVisible;
571 //Executes the function:
572 CB_Screen._eventsHolder["onVisibilityChange"]();
573 }
574
575 //Sets the new visibility:
576 CB_Screen._isVisible = isVisible;
577 }
578
579
580 /**
581 * Tells whether the main window is visible or not.
582 * @function
583 * @returns {boolean} Returns whether the main window is visible or not.
584 */
585 CB_Screen.isVisible = function()
586 {
587 return CB_Screen._isVisible;
588 }
589
590
591/*
592 //Called every time that focus is lost:
593 CB_Screen.focusLost = function()
594 {
595 //Focus has not been recovered (yet):
596 CB_Screen.focusRecovered = false;
597
598 //If focus is not recovered, set as not focused:
599 setTimeout(
600 function()
601 {
602 if (!CB_Screen.focusRecovered)
603 {
604 CB_Screen._focusChanged(false);
605 }
606 }, 500);
607
608 return;
609 }
610*/
611
612 //Sets whether the web is focused (called every time that focus changes):
613 CB_Screen._focusChanged = function(isFocused)
614 {
615 //If is focused, the focus has been recovered:
616 //if (isFocused) { CB_Screen.focusRecovered = true; }
617
618 //Calls the onFocusChange function (if any):
619 if (typeof(CB_Screen._eventsHolder["onFocusChange"]) === "function")
620 {
621 //Sets whether is focused or not (just in case the function needs it):
622 CB_Screen._isFocused = isFocused;
623 //Executes the function:
624 CB_Screen._eventsHolder["onFocusChange"]();
625 }
626
627 //Sets whether is focused or not:
628 CB_Screen._isFocused = isFocused;
629 }
630
631
632 /**
633 * Tells whether the main window is focused or not.
634 * @function
635 * @returns {boolean} Returns whether the main window is focused or not.
636 */
637 CB_Screen.isFocused = function()
638 {
639 return CB_Screen._isFocused;
640 }
641
642
643 /**
644 * Sets the focus to the main window (if possible).
645 * @function
646 */
647 CB_Screen.focus = function()
648 {
649 //try { CB_Client.getWindow(true).focus(); } catch(E) {}
650 CB_Client.getWindow(false).focus();
651 }
652
653
654 /**
655 * Sets a function to execute when the left scroll position (horizontal scroll) is changed in the screen (main window) or removes it.
656 * @function
657 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
658 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
659 */
660 CB_Screen.onScrollLeft = function(callbackFunction, keepOldFunction)
661 {
662 return CB_Screen._setSpecialEventFunction("onScrollLeft", callbackFunction, keepOldFunction);
663 }
664
665
666 /**
667 * Sets a function to execute when the top scroll position (vertical scroll) is changed in the screen (main window) or removes it.
668 * @function
669 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
670 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
671 */
672 CB_Screen.onScrollTop = function(callbackFunction, keepOldFunction)
673 {
674 return CB_Screen._setSpecialEventFunction("onScrollTop", callbackFunction, keepOldFunction);
675 }
676
677
678 /**
679 * Sets a function to execute when the screen (main window) orientation is changed (portrait or landscape) or removes it.
680 * @function
681 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
682 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
683 */
684 CB_Screen.onOrientationChange = function(callbackFunction, keepOldFunction)
685 {
686 return CB_Screen._setSpecialEventFunction("onOrientationChange", callbackFunction, keepOldFunction);
687 }
688
689
690 /**
691 * Sets a function to execute when the screen (main window) visibility is changed or removes it.
692 * @function
693 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
694 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
695 */
696 CB_Screen.onVisibilityChange = function(callbackFunction, keepOldFunction)
697 {
698 return CB_Screen._setSpecialEventFunction("onVisibilityChange", callbackFunction, keepOldFunction);
699 }
700
701
702 /**
703 * Sets a function to execute when the screen (main window) focus is changed or removes it.
704 * @function
705 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
706 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
707 */
708 CB_Screen.onFocusChange = function(callbackFunction, keepOldFunction)
709 {
710 return CB_Screen._setSpecialEventFunction("onFocusChange", callbackFunction, keepOldFunction);
711 }
712
713
714 /**
715 * Sets a function to execute when the screen (main window) is resized ([onResize]{@link https://developer.mozilla.org/en-US/docs/Web/Events/resize} event) or removes it.
716 * @function
717 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
718 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
719 */
720 CB_Screen.onResize = function(callbackFunction, keepOldFunction, useCapture)
721 {
722 //If they are not set, use default values for optional parameters:
723 if (typeof(keepOldFunction) === "undefined" || keepOldFunction === null) { keepOldFunction = true; } //If not set, it keeps old function by default.
724
725 //If a function has been sent:
726 if (typeof(callbackFunction) === "function")
727 {
728 //If able, adds the function given to the event:
729 var functionToAdd =
730 function()
731 {
732 CB_Screen.init(); //Updates screen properties.
733 if (typeof(callbackFunction) === "function") { return callbackFunction(); }
734 return true;
735 };
736 ///////CB_Screen._eventsHolder["onResize"] = functionToAdd;
737 CB_Events.add(window, "resize", functionToAdd, useCapture, keepOldFunction, true);
738 }
739 //...but if the function given is null, it will cancel the event:
740 else if (callbackFunction === null)/////// && CB_Screen._eventsHolder["onResize"] !== null)
741 {
742 //CB_Events.remove(window, "resize", CB_Screen._eventsHolder["onResize"], useCapture);
743 CB_Events.removeByName(window, "resize");
744 ////////CB_Screen._eventsHolder["onResize"] = null;
745 }
746 }
747
748
749 //Sets a function to execute when window is resized or zoom is applied:
750 CB_Screen._processOnResizeOrZoomFunction = function()
751 {
752 //If there is no function to process, exits:
753 if (typeof(CB_Screen._eventsHolder["onResizeOrZoom"]) !== "function") { return; }
754
755 //If this is the first time, set stored values:
756 if (CB_Screen._storedScreenWidth === 0) { CB_Screen._storedScreenWidth = CB_Screen.getWidth(); }
757 if (CB_Screen._storedScreenHeight === 0) { CB_Screen._storedScreenHeight = CB_Screen.getHeight(); }
758 if (CB_Screen._storedScreenAvailableWidth === 0) { CB_Screen._storedScreenAvailableWidth = CB_Screen.getAvailableWidth(); }
759 if (CB_Screen._storedScreenAvailableHeight === 0) { CB_Screen._storedScreenAvailableHeight = CB_Screen.getAvailableHeight(); }
760 if (CB_Screen._storedWindowWidth === 0) { CB_Screen._storedWindowWidth = CB_Screen.getWindowWidth(); }
761 if (CB_Screen._storedWindowHeight === 0) { CB_Screen._storedWindowHeight = CB_Screen.getWindowHeight(); }
762 if (CB_Screen._storedZoom === 0) { CB_Screen._storedZoom = CB_Screen.getZoom(); }
763 if (CB_Screen._storedPixelRatio === 0) { CB_Screen._storedPixelRatio = CB_Screen.getPixelRatio(); }
764
765 //If the window has been resized or zoomed, stores the previous values:
766 var windowResizedOrZoomed = false;
767
768 if (CB_Screen._storedWindowWidth !== CB_Screen.getWindowWidth() || CB_Screen._storedWindowHeight !== CB_Screen.getWindowHeight() || CB_Screen._storedZoom !== CB_Screen.getZoom() || CB_Screen._storedPixelRatio !== CB_Screen.getPixelRatio())
769 {
770 //Window has been resized or zoomed:
771 windowResizedOrZoomed = true;
772
773 //Stores the previous width and height:
774 CB_Screen._previousWindowWidth = CB_Screen._storedWindowWidth;
775 CB_Screen._previousWindowHeight = CB_Screen._storedWindowHeight;
776 CB_Screen._previousScreenWidth = CB_Screen._storedScreenWidth;
777 CB_Screen._previousScreenHeight = CB_Screen._storedScreenHeight;
778 CB_Screen._previousScreenAvailableWidth = CB_Screen._storedScreenAvailableWidth;
779 CB_Screen._previousScreenAvailableHeight = CB_Screen._storedScreenAvailableHeight;
780
781
782 //If the zoom has been changed, stores the previous zoom and pixel ratio:
783 if (CB_Screen._storedZoom !== CB_Screen.getZoom() || CB_Screen._storedPixelRatio !== CB_Screen.getPixelRatio())
784 {
785 CB_Screen._previousZoom = CB_Screen._storedZoom;
786 CB_Screen._previousPixelRatio = CB_Screen._storedPixelRatio;
787 }
788
789 //Stores the current window width and height:
790 CB_Screen._storedScreenWidth = CB_Screen.getWidth();
791 CB_Screen._storedScreenHeight = CB_Screen.getHeight();
792 CB_Screen._storedScreenAvailableWidth = CB_Screen.getAvailableWidth();
793 CB_Screen._storedScreenAvailableHeight = CB_Screen.getAvailableHeight();
794 CB_Screen._storedWindowWidth = CB_Screen.getWindowWidth();
795 CB_Screen._storedWindowHeight = CB_Screen.getWindowHeight();
796
797 //Stores the current zoom and pixel ratio:
798 CB_Screen._storedZoom = CB_Screen.getZoom();
799 CB_Screen._storedPixelRatio = CB_Screen.getPixelRatio();
800 }
801
802 //If the window has been resized or zoomed, executes the function:
803 if (windowResizedOrZoomed)
804 {
805 CB_Screen.init(); //It also refresh CB_Screen properties before calling the function.
806 CB_Screen._eventsHolder["onResizeOrZoom"](); //Executes the function.
807 }
808 }
809
810
811 /**
812 * Sets a function to execute when the screen (main window) is resized or the zoom is changed, or removes it.
813 * @function
814 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
815 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
816 */
817 CB_Screen.onResizeOrZoom = function(onResizeOrZoomFunction, keepOldFunction)
818 {
819 return CB_Screen._setSpecialEventFunction("onResizeOrZoom", onResizeOrZoomFunction, keepOldFunction);
820 }
821
822
823 /**
824 * Sets a function to execute when full screen mode is changed (enabled or disabled) or removes it.
825 * @function
826 * @param {function|null} callbackFunction - The function (event listener) that we want to execute when the event is fired, with no parameters. If a null value is used, the event will be removed.
827 * @param {boolean} [keepOldFunction=true] - Defines whether we want to keep any possible previous event listener for the same target and event name or not.
828 */
829 CB_Screen.onFullScreenChange = function(onFullScreenChangeFunction, keepOldFunction)
830 {
831 return CB_Screen._setSpecialEventFunction("onFullScreenChange", onFullScreenChangeFunction, keepOldFunction);
832 }
833
834
835 //Sets a function to execute when an event happens (a non-existing event on JavaScript):
836 CB_Screen._setSpecialEventFunction = function(eventName, eventFunction, keepOldFunction)
837 {
838 //If no function has been sent, cancel all previous functions and exits:
839 if (typeof(eventFunction) !== "function")
840 {
841 if (eventFunction === null) { CB_Screen._eventsHolder[eventName] = null; }
842 return;
843 }
844
845 //If not set, it keeps old function by default:
846 if (typeof(keepOldFunction) === "undefined" || keepOldFunction === null) { keepOldFunction = true; }
847
848 //If we don't want to keep the old function:
849 if (!keepOldFunction)
850 {
851 CB_Screen._eventsHolder[eventName] = eventFunction;
852 }
853 //...otherwise if we want to keep the old function, we keep it:
854 else
855 {
856 //Stores old function:
857 var eventFunctionOld = CB_Screen._eventsHolder[eventName]; //Stores old function of eventFunctionHolder.
858 CB_Screen._eventsHolder[eventName] =
859 function() //TODO: remember to use "e" in the case it uses parameters in the future.
860 {
861 if (typeof(eventFunctionOld) === "function") { eventFunctionOld(); }
862 eventFunction();
863 };
864 }
865 }
866
867
868 /**
869 * Tells whether the web client is compatible with the [FullScreen API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API} or not.
870 * @function
871 * @returns {boolean} Returns whether the web client is compatible with the [FullScreen API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API} or not.
872 */
873 CB_Screen.isFullScreenAPICompatible = function()
874 {
875 if (document.documentElement) { element = document.documentElement; }
876 else { element = document.body; }
877
878 var isFullScreenAPICompatible = false;
879
880 //Gets the function compatible with Fullscreen API (if any):
881 var callFullScreen = element.requestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen
882 || element.oRequestFullScreen || element.msRequestFullscreen || element.msRequestFullScreen || element.webkitEnterFullScreen
883 || element.webkitEnterFullscreen;
884
885 if (typeof(callFullScreen) !== "undefined" && callFullScreen) { isFullScreenAPICompatible = true; }
886
887 return isFullScreenAPICompatible;
888 }
889
890
891 /**
892 * Toggles between full screen and normal mode. Uses the [Fullscreen API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API} and fallbacks to other methods internally, including [NW.js (formerly node-webkit)]{@link https://nwjs.io/} and [Electron (Electron.js)]{@link https://electronjs.org/} ones, when not available. Recommended to be called through an event fired by the user as onclick or ontouchstart, etc.
893 * @function
894 * @param {boolean} [useFullScreen=true] - If set to true, it will try to enable full screen mode. Otherwise, it will try to enable normal mode.
895 * @param {Element} [element=document.documentElement|document.body] - Element which we want to use in full screen mode. By default uses the whole document body. Only used when the "useFullScreen" parameter is set to true. If an element is provided, it will use neither [NW.js (formerly node-webkit)]{@link https://nwjs.io/} nor [Electron (Electron.js)]{@link https://electronjs.org/} methods.
896 * @param {boolean} [allowReload=false] - If set to true and "useFullScreen" is set to true but it fails to enable full screen normally, it will try to reload the entire current document again in a new bigger window. Useful for very old web clients. Only used when the "useFullScreen" parameter is set to true.
897 */
898 CB_Screen.setFullScreen = function(useFullScreen, element, allowReload)
899 {
900 var documentBase = CB_Client.getDocumentBase();
901
902 //Defines default parameters:
903 if (typeof(useFullScreen) === "undefined" || useFullScreen === null) { useFullScreen = true; } //By default, full screen mode will be used:
904 var elementGiven = true; //Tells whether an element was given or not (useful for NW.js and Electron).
905 if (typeof(element) === "undefined" || element === null)
906 {
907 //if (documentBase.documentElement) { element = documentBase.documentElement; }
908 //else { element = documentBase.body; }
909 if (document.documentElement) { element = document.documentElement; }
910 else { element = document.body; }
911 elementGiven = false;
912 }
913
914 //If we want full screen mode:
915 if (useFullScreen)
916 {
917 var fullScreenApplied = false;
918
919 if (!elementGiven)
920 {
921 //Tries to use NW.js (node-webkit) if available to enter full screen:
922 if (CB_Client.isRunningOnNWjs())
923 {
924 if (typeof(nw) !== "undefined" && nw !== null && nw.Window && typeof(nw.Window.get) === "function")
925 {
926 try
927 {
928 var win = nw.Window.get();
929 if (win !== null)
930 {
931 win.enterFullscreen();
932 fullScreenApplied = win.isFullscreen;
933 if (!fullScreenApplied)
934 {
935 win.enterKioskMode();
936 fullScreenApplied = win.isKioskMode;
937 }
938 }
939 } catch(E) { fullScreenApplied = false; }
940 }
941 if (!fullScreenApplied && typeof(require) === "function")
942 {
943 var gui = require("nw.gui");
944 if (typeof(gui) !== "undefined" && gui !== null && typeof(gui.Window) !== "undefined" && gui.Window !== null && typeof(gui.Window.get) === "function")
945 {
946 try
947 {
948 var win = gui.Window.get();
949 if (win !== null)
950 {
951 win.enterFullscreen();
952 fullScreenApplied = win.isFullscreen;
953 if (!fullScreenApplied)
954 {
955 win.enterKioskMode();
956 fullScreenApplied = win.isKioskMode;
957 }
958 }
959 } catch(E) { fullScreenApplied = false; }
960 }
961 }
962 }
963
964 //Tries to use Electron (Electron.js) if available to enter full screen:
965 if (CB_Client.isRunningOnElectron() && typeof(require) === "function")
966 {
967 try
968 {
969 fullScreenApplied = require("electron").remote.getCurrentWindow().setFullScreen(true);
970 }
971 catch(E)
972 {
973 try
974 {
975 fullScreenApplied = require("electron").remote.getCurrentWindow().setSimpleFullScreen(true);
976 }
977 catch(E) { fullScreenApplied = false; }
978 }
979 }
980 }
981
982 if (!fullScreenApplied)
983 {
984 //Gets the function compatible with Fullscreen API (if any):
985 var callFullScreen = element.requestFullscreen || element.mozRequestFullScreen || element.webkitRequestFullscreen
986 || element.oRequestFullScreen || element.msRequestFullscreen || element.msRequestFullScreen || element.webkitEnterFullScreen
987 || element.webkitEnterFullscreen;
988
989 //If there is any function compatible with Fullscreen API, we call it:
990 if (typeof(callFullScreen) !== "undefined" && callFullScreen)
991 {
992 if (Element.ALLOW_KEYBOARD_INPUT)
993 {
994 callFullScreen.call(element, Element.ALLOW_KEYBOARD_INPUT); //For Webkit.
995 }
996 else { callFullScreen.call(element); }
997 fullScreenApplied = true;
998 }
999 //..otherwise, we try to use ActiveX (if it's not already in full screen):
1000 else if (typeof(window.ActiveXObject) !== "undefined")
1001 {
1002 try
1003 {
1004 var wscript = new ActiveXObject("WScript.Shell");
1005 //If ActiveX object has been created:
1006 if (wscript !== null)
1007 {
1008 //ActiveX object working, so we press the key:
1009 wscript.SendKeys("{F11}");
1010 fullScreenApplied = true;
1011 }
1012 }
1013 //If ActiveX is not enabled or has not been accepted:
1014 catch(E) { fullScreenApplied = false; }
1015 }
1016 }
1017
1018 //If the full screen mode has not been applied, we try to resize the window:
1019 if (!fullScreenApplied)
1020 {
1021 /*
1022 if (typeof(window.fullscreen) !== "undefined")
1023 {
1024 try { window.fullscreen = true; }
1025 catch(E) { }
1026 }
1027 */
1028
1029 //if (typeof(top.window.outerWidth) !== "undefined" && typeof(screen.availWidth) !== "undefined" && top.window.outerWidth !== null && screen.availWidth !== null)
1030 var screenAvailableWidth = CB_Screen.getAvailableWidth();
1031 if (typeof(top.window.outerWidth) !== "undefined" && top.window.outerWidth !== null && screenAvailableWidth > 0)
1032 {
1033 //top.window.outerWidth = screen.availWidth;
1034 top.window.outerWidth = screenAvailableWidth;
1035 }
1036 //if (typeof(top.window.outerHeight) !== "undefined" && typeof(screen.availHeight) !== "undefined" && top.window.outerHeight !== null && screen.availHeight !== null)
1037 var screenAvailableHeight = CB_Screen.getAvailableHeight();
1038 if (typeof(top.window.outerHeight) !== "undefined" && top.window.outerHeight !== null && screenAvailableHeight > 0)
1039 {
1040 //top.window.outerHeight = screen.availHeight;
1041 top.window.outerHeight = screenAvailableHeight;
1042 }
1043
1044/*
1045 var hdiff;
1046 window.resizeTo(screen.width/2,screen.height/2)
1047 window.moveTo(0,10)
1048
1049 hdiff=window.screenTop;
1050 window.moveTo(-6,-hdiff+6);
1051 window.resizeTo(screen.width+13,screen.height+hdiff+26)
1052*/
1053
1054 var screenWidth = CB_Screen.getWidth();
1055 var screenHeight = CB_Screen.getHeight();
1056
1057 try
1058 {
1059 //window.moveTo(0, 0); //GIVES PROBLEMS WHEN YOU USE THE SCRIPT ON AN IFRAME!!!
1060 //window.resizeTo(screenWidth, screenHeight); //GIVES PROBLEMS WHEN YOU USE THE SCRIPT ON AN IFRAME!!!
1061 } catch(E) {}
1062
1063//////////////////
1064
1065
1066 //If the full screen mode is still not applied and we allow to reload web:
1067 if (allowReload && !(/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream || navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform))) //Checks whether it is not iOS.
1068 {
1069 var currentWindow = window.self;
1070 currentWindow.opener = window.self;
1071 var newWindow = window.open("" + window.location, "CB_fullScreenWindow", "type=fullWindow, fullscreen=yes, scrollbars=auto, toolbar=no, location=no, directories=no, status=no, menubar=no, resizable=no, channelmode=1");
1072 //currentWindow.location.href = "about:blank";
1073 newWindow.focus();
1074 //currentWindow.close();
1075 //newWindow.focus();
1076 }
1077 }
1078 }
1079 //...otherwise, if we want normal screen mode (not full screen):
1080 else
1081 {
1082 var fullScreenLeft = false;
1083
1084 if (!elementGiven)
1085 {
1086 //Tries to use NW.js (node-webkit) if available to leave full screen:
1087 if (CB_Client.isRunningOnNWjs())
1088 {
1089 if (typeof(nw) !== "undefined" && nw !== null && nw.Window && typeof(nw.Window.get) === "function")
1090 {
1091 try
1092 {
1093 var win = nw.Window.get();
1094 if (win !== null)
1095 {
1096 win.leaveFullscreen();
1097 fullScreenLeft = !win.isFullscreen;
1098 if (!fullScreenLeft)
1099 {
1100 win.leaveKioskMode();
1101 fullScreenLeft = !win.isKioskMode;
1102 }
1103 }
1104 } catch(E) { fullScreenLeft = false; }
1105 }
1106 if (!fullScreenLeft && typeof(require) === "function")
1107 {
1108 var gui = require("nw.gui");
1109 if (typeof(gui) !== "undefined" && gui !== null && typeof(gui.Window) !== "undefined" && gui.Window !== null && typeof(gui.Window.get) === "function")
1110 {
1111 try
1112 {
1113 var win = gui.Window.get();
1114 if (win !== null)
1115 {
1116 win.leaveFullscreen();
1117 fullScreenLeft = !win.isFullscreen;
1118 if (!fullScreenLeft)
1119 {
1120 win.leaveKioskMode();
1121 fullScreenLeft = !win.isKioskMode;
1122 }
1123 }
1124 } catch(E) { fullScreenLeft = false; }
1125 }
1126 }
1127 }
1128
1129
1130 //Tries to use Electron (Electron.js) if available to leave full screen:
1131 if (CB_Client.isRunningOnElectron() && typeof(require) === "function")
1132 {
1133 try
1134 {
1135 fullScreenLeft = require("electron").remote.getCurrentWindow().setFullScreen(true);
1136 }
1137 catch(E)
1138 {
1139 try
1140 {
1141 fullScreenLeft = require("electron").remote.getCurrentWindow().setSimpleFullScreen(true);
1142 }
1143 catch(E) { fullScreenLeft = false; }
1144 }
1145 }
1146 }
1147
1148 if (!fullScreenLeft)
1149 {
1150 //Gets the function compatible with Fullscreen API (if any):
1151 var cancelFullScreen = documentBase.exitFullscreen || documentBase.cancelFullScreen || documentBase.mozCancelFullScreen
1152 || documentBase.webkitCancelFullScreen || documentBase.oCancelFullScreen || documentBase.msExitFullscreen
1153 || documentBase.msExitFullScreen || documentBase.msCancelFullScreen
1154 || documentBase.webkitExitFullScreen || documentBase.webkitExitFullscreen;
1155 var useDocumentBase = true;
1156 if (typeof(cancelFullScreen) === "undefined" || cancelFullScreen === null)
1157 {
1158 cancelFullScreen = document.exitFullscreen || document.cancelFullScreen || document.mozCancelFullScreen
1159 || document.webkitCancelFullScreen || document.oCancelFullScreen || document.msExitFullscreen
1160 || document.msExitFullScreen || document.msCancelFullScreen
1161 || document.webkitExitFullScreen || document.webkitExitFullscreen;
1162 useDocumentBase = false;
1163 }
1164
1165 //If there is any function compatible with Fullscreen API, we call it:
1166 if (typeof(cancelFullScreen) !== "undefined" && cancelFullScreen)
1167 {
1168 cancelFullScreen.call(useDocumentBase ? documentBase : document);
1169 }
1170 //..otherwise, we try to use ActiveX (if it's not in full screen already):
1171 else if (typeof(window.ActiveXObject) !== "undefined" && !CB_Screen.isFullScreen())
1172 {
1173 try
1174 {
1175 var wscript = new ActiveXObject("WScript.Shell");
1176 //If ActiveX object has been created:
1177 if (wscript !== null)
1178 {
1179 //ActiveX object working, so we press the key:
1180 wscript.SendKeys("{F11}");
1181 }
1182 }
1183 //If ActiveX is not enabled or has not been accepted:
1184 catch(E) {}
1185 }
1186 }
1187 }
1188 }
1189
1190 /**
1191 * Tells whether we are in full screen mode or not. Uses the [Fullscreen API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API} and fallbacks to other methods internally, including [NW.js (formerly node-webkit)]{@link https://nwjs.io/} and [Electron (Electron.js)]{@link https://electronjs.org/} ones, when not available.
1192 * @function
1193 * @param {number} [allowedWidthMarginPercentage=CB_Configuration.CrossBase.CB_Screen_isFullScreen_ALLOWED_WIDTH_MARGIN_PERCENTAGE] - Allowed width margin, in percentage, of the total screen available to detect whether it is in full screen or not. Needed by old web clients without [Fullscreen API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API} support, mainly.
1194 * @param {number} [allowedHeightMarginPercentage=CB_Configuration.CrossBase.CB_Screen_isFullScreen_ALLOWED_HEIGHT_MARGIN_PERCENTAGE] - Allowed height margin, in percentage, of the total screen available to detect whether it is in full screen or not. Needed by old web clients without [Fullscreen API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API} support, mainly.
1195 * @returns {boolean} Returns whether we are in full screen mode or not.
1196 */
1197 CB_Screen.isFullScreen = function(allowedWidthMarginPercentage, allowedHeightMarginPercentage)
1198 {
1199 //Gets the variable compatible with Fullscreen API (if any):
1200 var documentBase = CB_Client.getDocumentBase();
1201 /*
1202 var isFullScreenApplied = (documentBase.fullScreenElement && documentBase.fullScreenElement !== null)
1203 || (documentBase.fullscreenElement && documentBase.fullscreenElement !== null)
1204 || (documentBase.msFullscreenElement && documentBase.msFullscreenElement !== null)
1205 || documentBase.fullScreen || documentBase.mozFullScreen || documentBase.fullScreen
1206 || documentBase.webkitIsFullScreen || documentBase.oIsFullScreen || documentBase.msIsFullscreen
1207 || documentBase.msIsFullScreen || documentBase.webkitDisplayingFullScreen
1208 || documentBase.webkitDisplayingFullscreen;
1209 */
1210
1211 var isFullScreenApplied = false;
1212
1213 //Tries to use NW.js (node-webkit) to detect whether we are in fullscreen mode or not:
1214 if (CB_Client.isRunningOnNWjs())
1215 {
1216 if (typeof(nw) !== "undefined" && nw !== null && nw.Window && typeof(nw.Window.get) === "function")
1217 {
1218 try
1219 {
1220 var win = nw.Window.get();
1221 if (win !== null)
1222 {
1223 isFullScreenApplied = win.isFullscreen || win.isKioskMode;
1224 }
1225 } catch(E) { isFullScreenApplied = false; }
1226 }
1227 if (!isFullScreenApplied && typeof(require) === "function")
1228 {
1229 var gui = require("nw.gui");
1230 if (typeof(gui) !== "undefined" && gui !== null && typeof(gui.Window) !== "undefined" && gui.Window !== null && typeof(gui.Window.get) === "function")
1231 {
1232 try
1233 {
1234 var win = gui.Window.get();
1235 if (win !== null)
1236 {
1237 if (win !== null)
1238 {
1239 isFullScreenApplied = win.isFullscreen || win.isKioskMode;
1240 }
1241 }
1242 } catch(E) { isFullScreenApplied = false; }
1243 }
1244 }
1245 }
1246
1247
1248 //Tries to use Electron (Electron.js) to detect whether we are in fullscreen mode or not:
1249 if (CB_Client.isRunningOnElectron() && typeof(require) === "function")
1250 {
1251 try
1252 {
1253 isFullScreenApplied = (require("electron").remote.getCurrentWindow().isFullScreen() === true);
1254 }
1255 catch(E)
1256 {
1257 isFullScreenApplied = false;
1258 }
1259 }
1260
1261
1262 if (!isFullScreenApplied)
1263 {
1264 isFullScreenApplied = documentBase.fullScreenElement || documentBase.fullscreenElement || documentBase.msFullscreenElement
1265 || documentBase.fullScreen || documentBase.mozFullScreen || documentBase.fullScreen
1266 || documentBase.webkitIsFullScreen || documentBase.oIsFullScreen || documentBase.msIsFullscreen
1267 || documentBase.msIsFullScreen || documentBase.webkitDisplayingFullScreen
1268 || documentBase.webkitDisplayingFullscreen;
1269 //var useDocumentBase = true;
1270 if (typeof(isFullScreenApplied) === "undefined" || isFullScreenApplied === null)
1271 {
1272 /*
1273 isFullScreenApplied = (document.fullScreenElement && document.fullScreenElement !== null)
1274 || (document.fullscreenElement && document.fullscreenElement !== null)
1275 || (document.msFullscreenElement && document.msFullscreenElement !== null)
1276 || document.fullScreen || document.mozFullScreen || window.fullScreen
1277 || document.webkitIsFullScreen || document.oIsFullScreen || document.msIsFullscreen
1278 || document.msIsFullScreen || document.webkitDisplayingFullScreen
1279 || document.webkitDisplayingFullscreen;
1280 */
1281 isFullScreenApplied = document.fullScreenElement || document.fullscreenElement || document.msFullscreenElement
1282 || document.fullScreen || document.mozFullScreen || window.fullScreen
1283 || document.webkitIsFullScreen || document.oIsFullScreen || document.msIsFullscreen
1284 || document.msIsFullScreen || document.webkitDisplayingFullScreen
1285 || document.webkitDisplayingFullscreen;
1286 //useDocumentBase = false;
1287 }
1288
1289 //If there is not compatibility with Fullscreen API, we use other methods:
1290 if (typeof(isFullScreenApplied) === "undefined" || isFullScreenApplied === null)
1291 {
1292 if (CB_Client.getBrowser() === "Explorer") //Only IE since Chrome would do false positives:
1293 {
1294 //if (typeof(window.screenTop) !== "undefined" && window.screenTop !== null && !window.screenTop)
1295 if (window.screenTop)
1296 {
1297 isFullScreenApplied = true;
1298 }
1299 //else if (typeof(window.screenY) !== "undefined" && window.screenY !== null && !window.screenY)
1300 else if (window.screenY)
1301 {
1302 isFullScreenApplied = true;
1303 }
1304 }
1305 if ("standalone" in window.navigator && window.navigator.standalone) //For some WebKit browsers.
1306 {
1307 isFullScreenApplied = true;
1308 }
1309
1310 if (typeof(document.fullscreenElement) !== "undefined")
1311 {
1312 if (document.fullscreenElement === null) { isFullScreenApplied = false; }
1313 }
1314 }
1315
1316 //If there is still not compatibility with Fullscreen API and the methods above didn't work, we use other methods:
1317 if (typeof(isFullScreenApplied) === "undefined" || isFullScreenApplied === null)
1318 //if (typeof(isFullScreenApplied) === "undefined" || isFullScreenApplied === null)
1319 {
1320 //If the web client is not compatible with Fullscreen API (except the ones which are but don't detect full screen mode when we press F11):
1321 var element = documentBase.body;
1322
1323 if (documentBase.documentElement) { element = documentBase.documentElement; }
1324 //TODO: Add more browsers in callFullScreen that are compatible with Fullscreen API but don't detect fullscreen when you press F11.
1325 var callFullScreen = element.mozRequestFullScreen;// || element.msRequestFullScreen;
1326 if (typeof(callFullScreen) === "undefined")
1327 {
1328 //We will check the size of the window (having mind zoom level):
1329 var currentZoom = CB_Screen.getZoom();
1330
1331 //Gets the available screen size (or the screen size if it's not avaiable):
1332 var screenAvailableWidth_ = CB_Screen.getAvailableWidth();
1333 //if (typeof(screenAvailableWidth_) === "undefined" || screenAvailableWidth_ === null || screenAvailableWidth_ === 0) { screenAvailableWidth_ = CB_Screen.getWidth(); }
1334 if (!screenAvailableWidth_) { screenAvailableWidth_ = CB_Screen.getWidth(); }
1335 var screenAvailableHeight_ = CB_Screen.getAvailableHeight();
1336 //if (typeof(screenAvailableHeight_) === "undefined" || screenAvailableHeight_ === null || screenAvailableHeight_ === 0) { screenAvailableHeight_ = CB_Screen.getHeight(); }
1337 if (!screenAvailableHeight_) { screenAvailableHeight_ = CB_Screen.getHeight(); }
1338
1339 //Gets the previous available screen size (or the previous screen size if it's not available):
1340 var previousScreenAvailableWidth_ = CB_Screen.getAvailableWidthPrevious();
1341 //if (typeof(previousScreenAvailableWidth_) === "undefined" || previousScreenAvailableWidth_ === null || previousScreenAvailableWidth_ === 0) { previousScreenAvailableWidth_ = CB_Screen.getWidthPrevious(); }
1342 if (!previousScreenAvailableWidth_) { previousScreenAvailableWidth_ = CB_Screen.getWidthPrevious(); }
1343 var previousScreenAvailableHeight_ = CB_Screen.getAvailableHeightPrevious();
1344 //if (typeof(previousScreenAvailableHeight_) === "undefined" || previousScreenAvailableHeight_ === null || previousScreenAvailableHeight_ === 0) { previousScreenAvailableHeight_ = CB_Screen.getHeightPrevious(); }
1345 if (!previousScreenAvailableHeight_) { previousScreenAvailableHeight_ = CB_Screen.getHeightPrevious(); }
1346
1347 /*
1348 //Sets an allowed margin:
1349 var allowedWidthMargin = 0.034 * CB_Screen.getWidth();//50; //Maximum width margin allowed.
1350 if (allowedWidthMargin <= 0) { allowedWidthMargin = 50; }
1351 var allowedHeightMargin = 0.035 * CB_Screen.getHeight(); //42; //Maximum height margin allowed.
1352 if (allowedHeightMargin <= 0) { allowedHeightMargin = 42; }
1353 */
1354
1355 //Determine whether zoom affects screen available size or not:
1356 var zoomAffectsScreenSize = false;
1357 //If the page has not been zoomed yet:
1358 if (CB_Screen.getZoomPrevious() === 0)
1359 {
1360 zoomAffectsScreenSize = false;
1361 }
1362 //...otherwise, if has already been zoomed:
1363 else
1364 {
1365 //If screen available size changed form last time, zoom affects it:
1366 if (screenAvailableWidth_ !== previousScreenAvailableWidth_)// && previousScreenAvailableWidth_ !== CB_Screen._storedScreenAvailableWidth)
1367 {
1368 zoomAffectsScreenSize = true;
1369 }
1370 if (screenAvailableHeight_ !== previousScreenAvailableHeight_)// && previousScreenAvailableHeight_ !== CB_Screen._storedScreenAvailableHeight)
1371 {
1372 zoomAffectsScreenSize = true;
1373 }
1374 }
1375
1376 //If it's Internet Explorer, zooms will affect the screen available size:
1377 //TODO: Add more browsers here that zoom affects the screen available size (specially the ones which are not compatible with the Fullscreen API or they don't think they are in Fullscreen mode when you press F11).
1378 //if (typeof(document.attachEvent) !== "undefined" && document.attachEvent)
1379 if (CB_Client.getBrowser() === "Explorer" || typeof(document.msFullscreenElement) !== "undefined")
1380 {
1381 zoomAffectsScreenSize = true;
1382 }
1383 if (CB_Client.getBrowser() === "Chrome") //Chrome versions not compatible with FullScreen API.
1384 {
1385 zoomAffectsScreenSize = false;//true;
1386 }
1387
1388
1389 //If the zoom affects the screen available size:
1390 if (zoomAffectsScreenSize)
1391 {
1392 //We try to calculate the real screen available size (having in mind the zoom):
1393 screenAvailableWidth_ *= currentZoom;
1394 screenAvailableHeight_ *= currentZoom;
1395 }
1396
1397 //Sets an allowed margin:
1398 if (typeof(allowedWidthMarginPercentage) === "undefined" || allowedWidthMarginPercentage === null || isNaN(allowedWidthMarginPercentage) || allowedWidthMarginPercentage < 0 || allowedWidthMarginPercentage >= 100) { allowedWidthMarginPercentage = CB_Configuration[CB_BASE_NAME].CB_Screen_isFullScreen_ALLOWED_WIDTH_MARGIN_PERCENTAGE; }
1399 if (typeof(allowedHeightMarginPercentage) === "undefined" || allowedHeightMarginPercentage === null || isNaN(allowedHeightMarginPercentage) || allowedHeightMarginPercentage < 0 || allowedHeightMarginPercentage >= 100) { allowedHeightMarginPercentage = CB_Configuration[CB_BASE_NAME].CB_Screen_isFullScreen_ALLOWED_HEIGHT_MARGIN_PERCENTAGE; }
1400 var allowedWidthMargin = (allowedWidthMarginPercentage / 100) * screenAvailableWidth_;//50; //Maximum width margin allowed.
1401 if (allowedWidthMargin <= 0) { allowedWidthMargin = 50; }
1402 var allowedHeightMargin = (allowedHeightMarginPercentage / 100) * screenAvailableHeight_; //42; //Maximum height margin allowed.
1403 if (allowedHeightMargin <= 0) { allowedHeightMargin = 42; }
1404
1405 //We get the real window size (having in mind the zoom):
1406 var windowWidth = CB_Screen.getWindowWidth() * currentZoom;
1407 var windowHeight = CB_Screen.getWindowHeight() * currentZoom;
1408
1409 //We round the sizes having in mind that zoom can be not exact:
1410 windowWidth = Math.ceil(windowWidth);
1411 windowHeight = Math.ceil(windowHeight);
1412 screenAvailableWidth_ = Math.floor(screenAvailableWidth_);
1413 screenAvailableHeight_ = Math.floor(screenAvailableHeight_);
1414
1415 //If it's full screen (having in mind allowed margin):
1416 if (Math.ceil(windowWidth + allowedWidthMargin) >= screenAvailableWidth_ && Math.ceil(windowHeight + allowedHeightMargin) >= screenAvailableHeight_)
1417 {
1418 isFullScreenApplied = true;
1419 }
1420 else
1421 {
1422 isFullScreenApplied = false;
1423 }
1424 }
1425 else { isFullScreenApplied = false; } //If the web client is compatible with Fullscreen API (except the ones which are but don't detect full screen mode when we press F11), it's not in full screen mode.
1426 }
1427
1428 //Casts the variable to boolean type:
1429 //isFullScreenApplied = (isFullScreenApplied) ? true : false;
1430 isFullScreenApplied = !!(isFullScreenApplied);
1431
1432 //If it's not the first time and full screen mode has been changed, calls the onFullScreenChange function (if any):
1433 if (CB_Screen._isFullScreen !== null && CB_Screen._isFullScreen !== isFullScreenApplied)
1434 {
1435 //If there is any defined function:
1436 if (typeof(CB_Screen._eventsHolder["onFullScreenChange"]) === "function")
1437 {
1438 //Sets the new position (just in case the function needs it):
1439 CB_Screen._isFullScreen = isFullScreenApplied;
1440 //Executes the function:
1441 CB_Screen._eventsHolder["onFullScreenChange"]();
1442 }
1443 }
1444 }
1445
1446 //Sets the new position:
1447 CB_Screen._isFullScreen = isFullScreenApplied;
1448
1449 return CB_Screen._isFullScreen;//isFullScreenApplied;
1450 }
1451
1452
1453 CB_Screen.unlockOrientation_unsetOrientation_timeout = null;
1454 /**
1455 * Alias for {@link CB_Screen.unlockOrientation}.
1456 * @function CB_Screen.unsetOrientation
1457 * @see {@link CB_Screen.unlockOrientation}
1458 */
1459 /**
1460 * Function that unlocks the screen orientation. Using the [unlock]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/unlock} function and fallbacks to the [unlockOrientation]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/unlockOrientation} function of the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation} internally.
1461 * @function
1462 * @param {function} [onError] - Callback function that will be called if the orientation has not been unlocked successfully. Unique parameter received will be an error object (probably a [DOMException]{@link https://developer.mozilla.org/en-US/docs/Web/API/DOMException}, depending on the client) with the error.
1463 * @returns {boolean} Returns the same that the [unlock]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/unlock} function returns (undefined, normally), if available. Otherwise, returns the same boolean as the [unlockOrientation]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/unlockOrientation} function (true if the unlocking action has been performed successfully), if available. Otherwise, returns false when the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation} is not available.
1464 */
1465 CB_Screen.unlockOrientation = CB_Screen.unsetOrientation = function(onError)
1466 {
1467 clearTimeout(CB_Screen.unlockOrientation_unsetOrientation_timeout);
1468 var orientationBefore = CB_Screen.getOrientation();
1469 var onErrorCalled = false;
1470 try
1471 {
1472 var returnValue = false;
1473 orientationObject = CB_Screen.getOrientationObject();
1474 if (orientationObject !== null && typeof(orientationObject.unlock) !== "undefined" && orientationObject.unlock !== null)
1475 {
1476 try
1477 {
1478 returnValue = orientationObject.unlock(); //Attaching an error-catch to prevent Chrome desktop exception.
1479 } catch(E) { returnValue = false; }
1480 }
1481 else if (typeof(screen) !== "undefined" && screen !== null)
1482 {
1483 //Using IF because of Firefox bug ("TypeError: 'mozUnlockOrientation' called on an object that does not implement interface Screen."):
1484 if (screen.unlockOrientation)
1485 {
1486 returnValue = screen.unlockOrientation();
1487 }
1488 else if (screen.mozUnlockOrientation)
1489 {
1490 returnValue = screen.mozUnlockOrientation();
1491 }
1492 else if (screen.webkitUnlockOrientation)
1493 {
1494 returnValue = screen.webkitUnlockOrientation();
1495 }
1496 else if (screen.oUnlockOrientation)
1497 {
1498 returnValue = screen.oUnlockOrientation();
1499 }
1500 else if (screen.msUnlockOrientation)
1501 {
1502 returnValue = screen.msUnlockOrientation();
1503 }
1504 else if (typeof(Screen) !== "undefined" && Screen !== null && Screen.msUnlockOrientation)
1505 {
1506 returnValue = Screen.msUnlockOrientation();
1507 }
1508 else if (screen.khtmlUnlockOrientation)
1509 {
1510 returnValue = screen.khtmlUnlockOrientation();
1511 }
1512 /*
1513 else if (screen.orientation && screen.orientation.unlock)
1514 {
1515 return screen.orientation.unlock();
1516 }
1517 */
1518 }
1519
1520 if (returnValue === false && typeof(onError) === "function") { onErrorCalled = true; onError({ message: "Orientation mode could not be unlocked." }); }
1521 }
1522 catch(E)
1523 {
1524 if (!onErrorCalled && typeof(onError) === "function") { onErrorCalled = true; onError({ message: "Orientation mode could not be unlocked." }); }
1525 returnValue = false;
1526 }
1527
1528 //If no error happened, checks after some time:
1529 if (!onErrorCalled)
1530 {
1531 CB_Screen.unlockOrientation_unsetOrientation_timeout = setTimeout
1532 (
1533 function()
1534 {
1535 if (onErrorCalled) { return; }
1536 var orientationNow = CB_Screen.getOrientation();
1537 if (orientationNow === orientationBefore) //Orientation is the same one.
1538 {
1539 if (typeof(onError) === "function") { onErrorCalled = true; onError({ message: "Orientation did not change after trying to unlock it (it is still '" + orientationBefore + "')." }); }
1540 }
1541 }, 1000 //Unlocking process can take some time so it waits a while.
1542 );
1543 }
1544
1545 return returnValue;
1546 }
1547
1548
1549 /**
1550 * Alias for {@link CB_Screen.lockOrientation}.
1551 * @function CB_Screen.setOrientation
1552 * @see {@link CB_Screen.lockOrientation}
1553 */
1554 /**
1555 * Function that forces a desired screen orientation. Using the [lock]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock} function and fallbacks to the [lockOrientation]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation} function of the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation} internally.
1556 * @function
1557 * @param {'default'|'any'|'natural'|'landscape'|'portrait'|'portrait-primary'|'portrait-secondary'|'landscape-primary'|'landscape-secondary'} orientationMode - Desired orientation. Internally, "default" and "natural" will be exchanged and "any" will be transformed to "default", depending on the internal function used. Values "default", "any" and "natural" are not recommended because they are not supported in all web clients.
1558 * @param {function} [onSuccess] - Callback function with no parameters that will be called if the orientation has been set successfully.
1559 * @param {function} [onError] - Callback function that will be called if the orientation has not been set successfully. Unique parameter received will be an error object (probably a [DOMException]{@link https://developer.mozilla.org/en-US/docs/Web/API/DOMException}, depending on the client) with the error.
1560 * @returns {boolean} Returns the same [Promise]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise} that the [lock]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock} function returns, if available. Otherwise, returns the same boolean as the [lockOrientation]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/lockOrientation} function (true if the locking action has been performed successfully), if available. Otherwise, returns false when the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation} is not available.
1561 * @todo Transform values of "default", "any" and "natural" to "portrait", "landscape", etc. when the web clients do not support it (calculating current orientation and trying to guess natural/default one, etc.).
1562 */
1563 CB_Screen.lockOrientation = CB_Screen.setOrientation = function(orientationMode, onSuccess, onError)
1564 {
1565 clearTimeout(CB_Screen.unlockOrientation_unsetOrientation_timeout);
1566 try
1567 {
1568 var returnValue = false;
1569 orientationMode = CB_trim(orientationMode).toLowerCase();
1570 var orientationObject = CB_Screen.getOrientationObject();
1571 if (orientationObject !== null && typeof(orientationObject.lock) !== "undefined" && orientationObject.lock !== null)
1572 {
1573 if (orientationMode === "default") { orientationMode = "natural"; }
1574 //return orientationObject.lock(orientationMode).then(onSuccess)["catch"](typeof(onError) === "function" ? onError : function() {}); //Attaching an error-catch to prevent Chrome desktop exception.
1575
1576 try
1577 {
1578 returnValue = orientationObject.lock(orientationMode).then(onSuccess)["catch"](typeof(onError) === "function" ? onError : function() {}); //Attaching an error-catch to prevent Chrome desktop exception.
1579 return returnValue;
1580 }
1581 catch(E)
1582 {
1583 try
1584 {
1585 returnValue = orientationObject.lock(orientationMode); //Attaching an error-catch to prevent Chrome desktop exception.
1586 } catch(E) { returnValue = false; }
1587 }
1588 }
1589 else if (typeof(screen) !== "undefined" && screen !== null)
1590 {
1591 if (orientationMode === "natural") { orientationMode = "default"; }
1592 else if (orientationMode === "any") { orientationMode = "default"; }
1593
1594 //Using IF because of Firefox bug (TypeError: 'mozLockOrientation' called on an object that does not implement interface Screen.):
1595 if (screen.lockOrientation)
1596 {
1597 returnValue = screen.lockOrientation(orientationMode);
1598 }
1599 else if (screen.mozLockOrientation)
1600 {
1601 returnValue = screen.mozLockOrientation(orientationMode);
1602 }
1603 else if (screen.webkitLockOrientation)
1604 {
1605 returnValue = screen.webkitLockOrientation(orientationMode);
1606 }
1607 else if (screen.oLockOrientation)
1608 {
1609 returnValue = screen.oLockOrientation(orientationMode);
1610 }
1611 else if (screen.msLockOrientation)
1612 {
1613 returnValue = screen.msLockOrientation(orientationMode);
1614 }
1615 else if (typeof(Screen) !== "undefined" && Screen !== null && Screen.msLockOrientation)
1616 {
1617 returnValue = Screen.msLockOrientation(orientationMode);
1618 }
1619 else if (screen.khtmlLockOrientation)
1620 {
1621 returnValue = screen.khtmlLockOrientation(orientationMode);
1622 }
1623
1624 }
1625 if (returnValue === true && typeof(onSucess) === "function") { onSuccess(); }
1626 else if (returnValue === false && typeof(onError) === "function") { onError({ message: "Orientation mode could not be locked." }); }
1627
1628 return returnValue;
1629 }
1630 catch(E)
1631 {
1632 if (typeof(onError) === "function") { onError({ message: "Orientation mode could not be locked." }); }
1633 return false;
1634 }
1635 }
1636
1637
1638 /**
1639 * Gets the [screen orientation object]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation} of the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation}.
1640 * @function
1641 * @returns {Object} Returns the [screen orientation object]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation} of the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation}.
1642 */
1643 CB_Screen.getOrientationObject = function()
1644 {
1645 if (typeof(screen) !== "undefined")
1646 {
1647 var orientationObject = screen.orientation || screen.mozOrientation
1648 || screen.webkitOrientation || screen.oOrientation || screen.msOrientation
1649 || screen.khtmlOrientation || null;
1650 if (typeof(orientationObject) !== "undefined" && orientationObject !== null)
1651 {
1652 return orientationObject;
1653 }
1654 }
1655 return null;
1656 }
1657
1658
1659 CB_Screen.getOrientation_map = //* Map and map implementation by Christian Maniewski from: https://github.com/chmanie/o9n/blob/master/index.js
1660 {
1661 '90': 'landscape-primary',
1662 '-90': 'landscape-secondary',
1663 '0': 'portrait-primary',
1664 '180': 'portrait-secondary'
1665 };
1666 CB_Screen.getOrientation_getMql = function() //* Function by Christian Maniewski from: https://github.com/chmanie/o9n/blob/master/index.js
1667 {
1668 if (typeof(window.matchMedia) !== "function") { return {}; }
1669 return window.matchMedia('(orientation: landscape)');
1670 };
1671 /**
1672 * Gets the current orientation from the [screen orientation object]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation} of the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation}.
1673 * @function
1674 * @returns {string} Returns a string with the current orientation from the [screen orientation object]{@link https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation} of the [Screen Orientation API]{@link https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation}. If cannot be found, it will return "landscape-primary" as default.
1675 */
1676 CB_Screen.getOrientation = function()
1677 {
1678 var orientationObject = CB_Screen.getOrientationObject();
1679 var orientation = "";
1680 if (orientationObject !== null)
1681 {
1682 if (typeof(orientationObject) === "string") { orientation = orientationObject; }
1683 else if (typeof(orientationObject.type) === "string") { orientation = orientationObject.type; }
1684 }
1685 if (orientation === "")
1686 {
1687 orientation = CB_Screen.getOrientation_map[window.orientation + ""] || (CB_Screen.getOrientation_getMql().matches ? "landscape-primary" : "portrait-primary");
1688 }
1689 return orientation; //TODO: Think about using CB_trim();
1690 }
1691
1692
1693 /**
1694 * Sets the [Viewport]{@link https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag} meta tag dynamically with the desired options. If the [Viewport]{@link https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag} meta tag already exists, it will be updated. Otherwise, it will create a new one (and append it to the [HEAD]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head} tag, if found).
1695 * @function
1696 * @param {string|integer} [width] - Desired value for the "width" parameter.
1697 * @param {string|integer} [height] - Desired value for the "height" parameter.
1698 * @param {string|boolean} [userScalable='no'] - Desired value for the "user-scalable" parameter.
1699 * @param {number} [initialScale] - Desired value for the "initial-scale" parameter.
1700 * @param {number} [minimumScale] - Desired value for the "minimum-scale" parameter.
1701 * @param {number} [maximumScale] - Desired value for the "maximum-scale" parameter.
1702 * @param {string} [shrinkToFit] - Desired value for the "shrink-to-fit" parameter.
1703 * @param {string|number} [targetDensityDPI] - Desired value for the "target-densitydpi" parameter.
1704 * @returns {Node|null} Returns the DOM element which belongs to the [Viewport]{@link https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag} meta tag affected (it will have been just created if no one existed before). If a [Viewport]{@link https://developer.mozilla.org/en-US/docs/Mozilla/Mobile/Viewport_meta_tag} meta tag could not be created or modified, returns null.
1705 */
1706 CB_Screen.setViewport = function(width, height, userScalable, initialScale, minimumScale, maximumScale, shrinkToFit, targetDensityDPI)
1707 {
1708 //Finds the meta tag viewport (if any):
1709 var viewport = CB_Elements.id("viewport");
1710 if (typeof(viewport) === "undefined" || viewport === null)
1711 {
1712 //if (typeof(document.querySelector) !== "undefined" && document.querySelector !== null && document.querySelector)
1713 if (document.querySelector)
1714 {
1715 viewport = document.querySelector("meta[name=viewport]");
1716 }
1717 else
1718 {
1719 var metaTags = CB_Elements.tag("meta", document, false);
1720 for (var x = metaTags.length - 1; x >= 0; x--) //Finds the last one.
1721 {
1722 if (metaTags[x].name.toLowerCase() === "viewport")
1723 {
1724 viewport = metaTags[x];
1725 break;
1726 }
1727 }
1728 }
1729 }
1730
1731 //Prepare the given parameters:
1732 width = CB_trim(width);
1733 height = CB_trim(height);
1734 userScalable = CB_trim(userScalable).toLowerCase();
1735 initialScale = CB_trim(initialScale);
1736 minimumScale = CB_trim(minimumScale);
1737 maximumScale = CB_trim(maximumScale);
1738 targetDensityDPI = CB_trim(targetDensityDPI);
1739 if (shrinkToFit === true) { shrinkToFit = "yes"; }
1740 else if (shrinkToFit === false) { shrinkToFit = "no"; }
1741 shrinkToFit = CB_trim(shrinkToFit);
1742
1743 //Sets the content for the meta tag viewport (according to the parameters given):
1744 var viewportContent = "";
1745
1746 if (width !== "") { viewportContent += "width=" + width; }
1747
1748 if (height !== "") { if (CB_trim(viewportContent) !== "") { viewportContent += ", "; } viewportContent += "height=" + height; }
1749
1750 if (userScalable && userScalable !== "no" && userScalable !== 0 && userScalable !== "0") { userScalable = 1; } //userScalable = "yes"; }
1751 else { userScalable = 0; } //userScalable = "no"; }
1752 if (CB_trim(viewportContent) !== "") { viewportContent += ", "; }
1753 viewportContent += "user-scalable=" + (userScalable === 0 ? "no" : "yes");
1754 viewportContent += ", user-scalable=" + userScalable;
1755
1756 if (initialScale !== "") { viewportContent += ", initial-scale=" + initialScale; }
1757
1758 if (minimumScale !== "") { viewportContent += ", minimum-scale=" + minimumScale; }
1759
1760 if (maximumScale !== "") { viewportContent += ", maximum-scale=" + maximumScale; }
1761
1762 if (targetDensityDPI !== "") { viewportContent += ", target-densitydpi=" + targetDensityDPI; }
1763
1764 if (shrinkToFit !== "") { viewportContent += ", shrink-to-fit=" + shrinkToFit; }
1765
1766 //If the meta tag already exists, just updates it:
1767 if (typeof(viewport) !== "undefined" && viewport !== null && typeof(viewport.setAttribute) !== "undefined" && viewport.setAttribute !== null)
1768 {
1769 viewport.setAttribute("content", viewportContent);
1770 return viewport;
1771 }
1772 //...otherwise, it will create it:
1773 else
1774 {
1775 viewport = document.createElement('meta');
1776 viewport.name = "viewport";
1777 viewport.id = "viewport";
1778 viewport.content = viewportContent;
1779 var headTag = CB_Elements.tag("head", document, false);
1780 if (typeof(headTag) !== "undefined" && headTag !== null && typeof(headTag[0]) !== "undefined" && headTag[0] !== null)
1781 {
1782 headTag[0].appendChild(viewport);
1783 return viewport;
1784 }
1785 }
1786 return null;
1787 }
1788
1789
1790 /**
1791 * Keeps the screen awake and prevents it from turning off. Uses different methods internally: [Apache Cordova's Insomnia plugin]{@link https://github.com/EddyVerbruggen/Insomnia-PhoneGap-Plugin}, [Standby API]{@link https://lists.w3.org/Archives/Public/public-device-apis/2014Feb/att-0001/Standby_API_Specification.pdf}, [Mozilla's Wake Lock API]{@link https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/API/Wake_Lock_API}, [new W3C's Wake Lock API]{@link https://w3.org/TR/wake-lock/}, [old W3C's Wake Lock API]{@link https://w3.org/TR/2016/WD-wake-lock-20160714/}, [NoSleep.js library]{@link https://github.com/richtr/NoSleep.js?utm_source=recordnotfound.com} (it should be activated by an event fired by the user as onclick or ontouchstart, etc.)...
1792 * @function
1793 * @param {function} [callbackOk] - Function that will be called if the action has been performed successfully, without parameters.
1794 * @param {function} [callbackError] - Function that will be called if the action has not been performed successfully, without parameters.
1795 * @returns {boolean} If it uses the [new W3C's Wake Lock API]{@link https://w3.org/TR/wake-lock/} internally, it will return a [Promise]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise}. Otherwise, it will return a boolean depending on whether the internal method has been called successfully or not. The fact that the internal method has been called successfully does not mean that the action will perform successfully so it is recommended to relay on the "callbackOk" and "callbackError" functions and ignore this returning value.
1796 * @todo Pay attention since many internal functions as the [new W3C's Wake Lock API]{@link https://w3.org/TR/wake-lock/} are still experimental and not well-tested.
1797 */
1798 CB_Screen.keepAwake = function(callbackOk, callbackError)
1799 {
1800 //Using Apache Cordova's Insomnia plugin:
1801 if (typeof(window.plugins) !== "undefined" && typeof(window.plugins.insomnia) !== "undefined" && typeof(window.plugins.insomnia.allowSleepAgain) !== "undefined")
1802 {
1803 try
1804 {
1805 window.plugins.insomnia.keepAwake(callbackOk, callbackError);
1806 return true;
1807 } catch(E) { }
1808 }
1809 //Using Standby API:
1810 if (window.navigator && typeof(navigator.wakeLock) !== "undefined" && navigator.wakeLock !== null && typeof(navigator.wakeLock.request) !== "undefined")
1811 {
1812 try
1813 {
1814 navigator.wakeLock.request("display").then(callbackOk, callbackError); //navigator.wakeLock.request("screen").then(callbackOk, callbackError);
1815 return true;
1816 } catch(E) { }
1817 }
1818 //Using Mozilla's Wake Lock API:
1819 if (window.navigator && typeof(window.navigator.requestWakeLock) !== "undefined") //So far, only works in Firefox/Firefox OS.
1820 {
1821 try
1822 {
1823 CB_Screen._screenLock = window.navigator.requestWakeLock("screen");
1824 if (typeof(callbackOk) === "function") { callbackOk(); }
1825 return CB_Screen._screenLock;
1826 } catch(E) { }
1827 }
1828 //Using the W3C's Wake Lock API:
1829 if (window.navigator && typeof(window.navigator.getWakeLock) !== "undefined")
1830 {
1831 return navigator.getWakeLock("screen").then
1832 (
1833 function(wakeLock)
1834 {
1835 var request = wakeLock.createRequest();
1836 if (typeof(callbackOk) === "function") { callbackOk(); }
1837 },
1838 callbackError
1839 );
1840 }
1841 //Using the old W3C's Wake Lock API:
1842 if (typeof(screen) !== "undefined" && screen !== null && typeof(screen.keepAwake) !== "undefined")
1843 {
1844 try
1845 {
1846 screen.keepAwake = true;
1847 if (typeof(callbackOk) === "function") { callbackOk(); }
1848 return true;
1849 } catch(E) { }
1850 }
1851 //Using NoSleep.js library (it should be activated by an event fired by the user as onclick or ontouchstart, etc.):
1852 if (typeof(NoSleep) !== "undefined")
1853 {
1854 //if (typeof(CB_Screen._noSleep) === "undefined" || CB_Screen._noSleep === null) { CB_Screen._noSleep = new NoSleep(); }
1855 if (CB_Screen._noSleep) { CB_Screen._noSleep.disable(); } //Fix. Source: https://github.com/richtr/NoSleep.js/issues/75.
1856 CB_Screen._noSleep = new NoSleep(); //Needs to create a new one to work on some iOS versions. Source: https://github.com/richtr/NoSleep.js/issues/75.
1857
1858 //Fix for iOS (Source: https://github.com/richtr/NoSleep.js/issues/75):
1859 if (/iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream || navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)) //Checks whether it is iOS.
1860 {
1861 CB_Events.add
1862 (
1863 document,
1864 "visibilitychange",
1865 function()
1866 {
1867 if (CB_Screen._noSleepEnabled === true && document.visibilityState === "visible")
1868 {
1869 CB_Screen.keepAwake(); //CB_Screen.keepAwake(callbackOk, callbackError);
1870 }
1871 },
1872 true,
1873 true,
1874 false
1875 );
1876 }
1877
1878 try
1879 {
1880 CB_Screen._noSleep.enable();
1881 CB_Screen._noSleepEnabled = true;
1882 if (typeof(callbackOk) === "function") { callbackOk(); }
1883 return true;
1884 } catch(E) {}
1885 }
1886
1887 if (typeof(callbackError) === "function") { callbackError(); }
1888 return false;
1889 }
1890
1891
1892 //Function that lets the screen sleep again (also uses Apache Cordova with insomnia plugin):
1893 /**
1894 * Lets the screen sleep again and stops preventing it from turning off. Uses different methods internally: [Apache Cordova's Insomnia plugin]{@link https://github.com/EddyVerbruggen/Insomnia-PhoneGap-Plugin}, [Standby API]{@link https://lists.w3.org/Archives/Public/public-device-apis/2014Feb/att-0001/Standby_API_Specification.pdf}, [Mozilla's Wake Lock API]{@link https://developer.mozilla.org/en-US/docs/Archive/B2G_OS/API/Wake_Lock_API}, [new W3C's Wake Lock API]{@link https://w3.org/TR/wake-lock/}, [old W3C's Wake Lock API]{@link https://w3.org/TR/2016/WD-wake-lock-20160714/}, [NoSleep.js library]{@link https://github.com/richtr/NoSleep.js?utm_source=recordnotfound.com} (it should be activated by an event fired by the user as onclick or ontouchstart, etc.)...
1895 * @function
1896 * @param {function} [callbackOk] - Function that will be called if the action has been performed successfully, without parameters.
1897 * @param {function} [callbackError] - Function that will be called if the action has not been performed successfully, without parameters.
1898 * @returns {boolean} If it uses the [new W3C's Wake Lock API]{@link https://w3.org/TR/wake-lock/} internally, it will return a [Promise]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise}. Otherwise, it will return a boolean depending on whether the internal method has been called successfully or not. The fact that the internal method has been called successfully does not mean that the action will perform successfully so it is recommended to relay on the "callbackOk" and "callbackError" functions and ignore this returning value.
1899 * @todo Pay attention since many internal functions as the [new W3C's Wake Lock API]{@link https://w3.org/TR/wake-lock/} are still experimental and not well-tested.
1900 */
1901 CB_Screen.keepAwakeDisable = function(callbackOk, callbackError)
1902 {
1903 //Using Apache Cordova's Insomnia plugin:
1904 if (typeof(window.plugins) !== "undefined" && typeof(window.plugins.insomnia) !== "undefined" && typeof(window.plugins.insomnia.allowSleepAgain) !== "undefined")
1905 {
1906 try
1907 {
1908 window.plugins.insomnia.allowSleepAgain(callbackOk, callbackError);
1909 return true;
1910 } catch(E) { }
1911 }
1912 //Using Standby API:
1913 if (window.navigator && typeof(navigator.wakeLock) !== "undefined" && navigator.wakeLock !== null && typeof(navigator.wakeLock.release) !== "undefined")
1914 {
1915 try
1916 {
1917 navigator.wakeLock.release("display"); //navigator.wakeLock.release("screen");
1918 if (typeof(callbackOk) === "function") { callbackOk(); }
1919 return true;
1920 } catch(E) { }
1921 }
1922 //Using Mozilla's Wake Lock API:
1923 if (window.navigator && typeof(window.navigator.requestWakeLock) !== "undefined") //So far, only works in Firefox/Firefox OS.
1924 {
1925 if (typeof(lock) === "undefined" || lock === null)
1926 {
1927 if (typeof(CB_Screen._screenLock) !== "undefined" && CB_Screen._screenLock !== null) { lock = CB_Screen._screenLock; }
1928 }
1929 if (typeof(lock.unlock) !== "undefined")
1930 {
1931 try
1932 {
1933 if (typeof(callbackOk) === "function") { callbackOk(); }
1934 return lock.unlock();
1935 } catch(E) { }
1936 }
1937 }
1938 //Using the W3C's Wake Lock API:
1939 if (window.navigator && typeof(window.navigator.getWakeLock) !== "undefined")
1940 {
1941 navigator.getWakeLock("screen").then
1942 (
1943 function(wakeLock)
1944 {
1945 var request = wakeLock.createRequest();
1946 request.cancel();
1947 //setTimeout(function() { request.cancel(); }, 10);
1948 if (typeof(callbackOk) === "function") { callbackOk(); }
1949 },
1950 callbackError
1951 );
1952 }
1953 //Using the old W3C's Wake Lock API:
1954 if (typeof(screen) !== "undefined" && screen !== null && typeof(screen.keepAwake) !== "undefined")
1955 {
1956 try
1957 {
1958 screen.keepAwake = false;
1959 if (typeof(callbackOk) === "function") { callbackOk(); }
1960 return true;
1961 } catch(E) { }
1962 }
1963 //Using NoSleep.js library:
1964 if (typeof(NoSleep) !== "undefined")
1965 {
1966 //if (typeof(CB_Screen._noSleep) === "undefined" || CB_Screen._noSleep === null) { CB_Screen._noSleep = new NoSleep(); }
1967 CB_Screen._noSleep = new NoSleep(); //Needs to create a new one to work on some iOS versions. Source: https://github.com/richtr/NoSleep.js/issues/75.
1968 try
1969 {
1970 CB_Screen._noSleep.disable();
1971 CB_Screen._noSleepEnabled = false;
1972 if (typeof(callbackOk) === "function") { callbackOk(); }
1973 return true;
1974 } catch(E) { }
1975 }
1976
1977 if (typeof(callbackError) === "function") { callbackError(); }
1978 return false;
1979 }
1980
1981
1982} //End of the static class CB_Screen.
1983
1984//CB_ScreenFileLoaded = true; //This file has been loaded.
1985//CB_filesNeeded["screen/CB_Screen.js"] = true; //This file has been loaded.
\No newline at end of file