| 30 | * @param {Phaser.Game} game - The Game instance this Visibility Handler is working on. |
| 31 | */ |
| 32 | var VisibilityHandler = function (game) |
| 33 | { |
| 34 | var hiddenVar; |
| 35 | var eventEmitter = game.events; |
| 36 | |
| 37 | if (document.hidden !== undefined) |
| 38 | { |
| 39 | hiddenVar = 'visibilitychange'; |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | var vendors = [ 'webkit', 'moz', 'ms' ]; |
| 44 | |
| 45 | vendors.forEach(function (prefix) |
| 46 | { |
| 47 | if (document[prefix + 'Hidden'] !== undefined) |
| 48 | { |
| 49 | document.hidden = function () |
| 50 | { |
| 51 | return document[prefix + 'Hidden']; |
| 52 | }; |
| 53 | |
| 54 | hiddenVar = prefix + 'visibilitychange'; |
| 55 | } |
| 56 | |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | var onChange = function (event) |
| 61 | { |
| 62 | if (document.hidden || event.type === 'pause') |
| 63 | { |
| 64 | eventEmitter.emit(Events.HIDDEN); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | eventEmitter.emit(Events.VISIBLE); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | if (hiddenVar) |
| 73 | { |
| 74 | document.addEventListener(hiddenVar, onChange, false); |
| 75 | } |
| 76 | |
| 77 | window.onblur = function () |
| 78 | { |
| 79 | eventEmitter.emit(Events.BLUR); |
| 80 | }; |
| 81 | |
| 82 | window.onfocus = function () |
| 83 | { |
| 84 | eventEmitter.emit(Events.FOCUS); |
| 85 | }; |
| 86 | |
| 87 | // Automatically give the window focus unless config says otherwise |
| 88 | if (window.focus && game.config.autoFocus) |
| 89 | { |