|
| 1 | +var registerComponent = require('../../core/component').registerComponent; |
| 2 | +var isIframed = require('../../utils/').isIframed; |
| 3 | + |
| 4 | +module.exports.Component = registerComponent('fullscreen', { |
| 5 | + dependencies: [ 'canvas' ], |
| 6 | + |
| 7 | + init: function () { |
| 8 | + var scene = this.el; |
| 9 | + this.isMobile = scene.isMobile; |
| 10 | + this.handler = this.fullscreenChangeHandler.bind(this); |
| 11 | + document.addEventListener('mozfullscreenchange', this.handler); |
| 12 | + document.addEventListener('webkitfullscreenchange', this.handler); |
| 13 | + |
| 14 | + // Handles fullscreen behavior when inside an iframe. |
| 15 | + if (isIframed()) { |
| 16 | + window.addEventListener('message', this.iframedFullscreenChangeHandler.bind(this)); |
| 17 | + } |
| 18 | + |
| 19 | + scene.addEventListener('vr-mode-enter', function () { |
| 20 | + setFullscreen(scene.canvas); |
| 21 | + }); |
| 22 | + }, |
| 23 | + |
| 24 | + fullscreenChangeHandler: function (event) { |
| 25 | + var fullscreenElement = document.fullscreenElement || |
| 26 | + document.mozFullScreenElement || |
| 27 | + document.webkitFullscreenElement; |
| 28 | + |
| 29 | + // Lock to landscape orientation on mobile. |
| 30 | + if (window.screen.orientation) { |
| 31 | + if (fullscreenElement && this.isMobile) { |
| 32 | + window.screen.orientation.lock('landscape'); |
| 33 | + } else { |
| 34 | + window.screen.orientation.unlock(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + if (fullscreenElement) { |
| 39 | + this.enterFullscreenHandler(); |
| 40 | + } else { |
| 41 | + this.exitFullscreenHandler(); |
| 42 | + } |
| 43 | + }, |
| 44 | + |
| 45 | + iframedFullscreenChangeHandler: function (event) { |
| 46 | + if (!event.data) { return; } |
| 47 | + switch (event.data.type) { |
| 48 | + case 'fullscreen': { |
| 49 | + switch (event.data.data) { |
| 50 | + case 'enter': |
| 51 | + this.enterFullscreenHandler(); |
| 52 | + break; |
| 53 | + case 'exit': |
| 54 | + this.exitFullscreenHandler(); |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + }, |
| 60 | + |
| 61 | + enterFullscreenHandler: function () { |
| 62 | + var scene = this.el; |
| 63 | + scene.addState('fullscreen'); |
| 64 | + scene.emit('fullscreen-enter', { target: scene }); |
| 65 | + }, |
| 66 | + |
| 67 | + exitFullscreenHandler: function () { |
| 68 | + var scene = this.el; |
| 69 | + scene.removeState('fullscreen'); |
| 70 | + scene.emit('fullscreen-exit', { target: scene }); |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +/** |
| 75 | + * Manually handles fullscreen for non-VR mobile where the renderer' VR |
| 76 | + * display is not polyfilled. |
| 77 | + * |
| 78 | + * Desktop just works so use the renderer.setFullScreen in that case. |
| 79 | + */ |
| 80 | +function setFullscreen (canvas) { |
| 81 | + if (canvas.requestFullscreen) { |
| 82 | + canvas.requestFullscreen(); |
| 83 | + } else if (canvas.mozRequestFullScreen) { |
| 84 | + canvas.mozRequestFullScreen(); |
| 85 | + } else if (canvas.webkitRequestFullscreen) { |
| 86 | + canvas.webkitRequestFullscreen(); |
| 87 | + } |
| 88 | +} |
0 commit comments