1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * @namespace The fullscreen API provides an easy way for web content to be presented using the user's entire screen.
 29  * It's invalid on safari,QQbrowser and android browser
 30  * @name cc.screen
 31  */
 32 cc.screen = /** @lends cc.screen# */{
 33     _supportsFullScreen: false,
 34 	// the pre fullscreenchange function
 35     _preOnFullScreenChange: null,
 36     _touchEvent: "",
 37 	_fn: null,
 38 	// Function mapping for cross browser support
 39 	_fnMap: [
 40 		[
 41 			'requestFullscreen',
 42 			'exitFullscreen',
 43 			'fullscreenchange',
 44 			'fullscreenEnabled',
 45 			'fullscreenElement'
 46 		],
 47 		[
 48 			'requestFullScreen',
 49 			'exitFullScreen',
 50 			'fullScreenchange',
 51 			'fullScreenEnabled',
 52 			'fullScreenElement'
 53 		],
 54 		[
 55 			'webkitRequestFullScreen',
 56 			'webkitCancelFullScreen',
 57 			'webkitfullscreenchange',
 58 			'webkitIsFullScreen',
 59 			'webkitCurrentFullScreenElement'
 60 		],
 61 		[
 62 			'mozRequestFullScreen',
 63 			'mozCancelFullScreen',
 64 			'mozfullscreenchange',
 65 			'mozFullScreen',
 66 			'mozFullScreenElement'
 67 		],
 68 		[
 69 			'msRequestFullscreen',
 70 			'msExitFullscreen',
 71 			'MSFullscreenChange',
 72 			'msFullscreenEnabled',
 73 			'msFullscreenElement'
 74 		]
 75 	],
 76 
 77     init: function () {
 78 	    this._fn = {};
 79 	    var i, val, map = this._fnMap, valL;
 80 	    for (i = 0, l = map.length; i < l; i++ ) {
 81 		    val = map[ i ];
 82 		    if ( val && val[1] in document ) {
 83 			    for ( i = 0, valL = val.length; i < valL; i++ ) {
 84 				    this._fn[ map[0][ i ] ] = val[ i ];
 85 			    }
 86 			    break;
 87 		    }
 88 	    }
 89 
 90 		this._supportsFullScreen = (this._fn.requestFullscreen != undefined);
 91         this._touchEvent = ('ontouchstart' in window) ? 'touchstart' : 'mousedown';
 92     },
 93 
 94     /**
 95      * return true if it's full now.
 96      * @returns {Boolean}
 97      */
 98     fullScreen: function() {
 99 	    return this._supportsFullScreen && document[ this._fn.fullscreenEnabled ];
100     },
101 
102     /**
103      * change the screen to full mode.
104      * @param {Element} element
105      * @param {Function} onFullScreenChange
106      */
107     requestFullScreen: function (element, onFullScreenChange) {
108 	    if (!this._supportsFullScreen) return;
109 
110 	    element = element || document.documentElement;
111 	    element[ this._fn.requestFullscreen ]();
112 
113 	    if (onFullScreenChange) {
114 		    var eventName = this._fn.fullscreenchange;
115 		    if (this._preOnFullScreenChange)
116 			    document.removeEventListener(eventName, this._preOnFullScreenChange);
117 		    this._preOnFullScreenChange = onFullScreenChange;
118             cc._addEventListener(document, eventName, onFullScreenChange, false);
119 	    }
120 
121         return element[ this._fn.requestFullscreen ]();
122     },
123 
124     /**
125      * exit the full mode.
126      */
127     exitFullScreen: function () {
128         return this._supportsFullScreen ? document[ this._fn.exitFullscreen ]() : true;
129     },
130 
131     /**
132      * Automatically request full screen with a touch/click event
133      * @param {Element} element
134      * @param {Function} onFullScreenChange
135      */
136     autoFullScreen: function (element, onFullScreenChange) {
137 	    element = element || document.body;
138 	    var touchTarget = cc._canvas || element;
139         var theScreen = this;
140         // Function bind will be too complicated here because we need the callback function's reference to remove the listener
141         function callback() {
142             theScreen.requestFullScreen(element, onFullScreenChange);
143             touchTarget.removeEventListener(theScreen._touchEvent, callback);
144         }
145         this.requestFullScreen(element, onFullScreenChange);
146         cc._addEventListener(touchTarget, this._touchEvent, callback);
147     }
148 };
149 cc.screen.init();