isolated script scope

This commit is contained in:
e7d 2016-11-11 10:01:18 +01:00
parent 76576ef297
commit bf1bbfd141

View File

@ -1,18 +1,19 @@
$.urlParam = function(name) { (function(window, document, undefined) {
$.urlParam = function(name) {
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href); var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results === null) { if (results === null) {
return null; return null;
} else { } else {
return decodeURIComponent(results[1]) || 0; return decodeURIComponent(results[1]) || 0;
} }
}; };
var haveEvents = 'ongamepadconnected' in window; var haveEvents = 'ongamepadconnected' in window;
var gamepads = {}; var gamepads = {};
var $gamepad = $('.gamepad'); var $gamepad = $('.gamepad');
var $nogamepad = $('.no-gamepad'); var $nogamepad = $('.no-gamepad');
var $help = $('.help'); var $help = $('.help');
var gamepadIdentifiers = { var gamepadIdentifiers = {
'ds4': { 'ds4': {
'id': /054c.*?05c4/, 'id': /054c.*?05c4/,
'colors': ['black', 'white', 'red', 'blue'] 'colors': ['black', 'white', 'red', 'blue']
@ -21,44 +22,44 @@ var gamepadIdentifiers = {
'id': /xinput|XInput/, 'id': /xinput|XInput/,
'colors': ['black', 'white'] 'colors': ['black', 'white']
} }
}; };
var gamepadHelpTimeout = null; var gamepadHelpTimeout = null;
var gamepadHelpDelay = 10000; var gamepadHelpDelay = 10000;
var activeGamepadIndex = null; var activeGamepadIndex = null;
var activeGamepadType = null; var activeGamepadType = null;
var activeGamepadIdentifier = null; var activeGamepadIdentifier = null;
var activeGamepadColorIndex = null; var activeGamepadColorIndex = null;
var activeGamepadColorName = null; var activeGamepadColorName = null;
var activeGamepadZoomLevel = 1; var activeGamepadZoomLevel = 1;
var mapping = { var mapping = {
buttons: [], buttons: [],
axes: [] axes: []
}; };
window.addEventListener("gamepadconnected", onGamepadConnect); window.addEventListener("gamepadconnected", onGamepadConnect);
window.addEventListener("gamepaddisconnected", onGamepadDisconnect); window.addEventListener("gamepaddisconnected", onGamepadDisconnect);
window.addEventListener("keydown", onKeyDown); window.addEventListener("keydown", onKeyDown);
displayGamepadHelp(); displayGamepadHelp();
function displayGamepadHelp() { function displayGamepadHelp() {
gamepadHelpTimeout = window.setTimeout(function() { gamepadHelpTimeout = window.setTimeout(function() {
$nogamepad.fadeIn(); $nogamepad.fadeIn();
}, gamepadHelpDelay); }, gamepadHelpDelay);
} }
function hideGamepadHelp() { function hideGamepadHelp() {
window.clearTimeout(gamepadHelpTimeout); window.clearTimeout(gamepadHelpTimeout);
$nogamepad.hide(); $nogamepad.hide();
} }
function onGamepadConnect(e) { function onGamepadConnect(e) {
addGamepad(e.gamepad); addGamepad(e.gamepad);
} }
function onGamepadDisconnect(e) { function onGamepadDisconnect(e) {
removeGamepad(e.gamepad.index); removeGamepad(e.gamepad.index);
} }
function onKeyDown(e) { function onKeyDown(e) {
switch (e.code) { switch (e.code) {
case "Delete": case "Delete":
case "Escape": case "Escape":
@ -83,17 +84,17 @@ function onKeyDown(e) {
changeZoom("0"); changeZoom("0");
break; break;
} }
} }
function getGamepads() { function getGamepads() {
return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []); return navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
} }
function addGamepad(gamepad) { function addGamepad(gamepad) {
gamepads[gamepad.index] = gamepad; gamepads[gamepad.index] = gamepad;
} }
function removeGamepad(gamepadIndex) { function removeGamepad(gamepadIndex) {
if (gamepadIndex === activeGamepadIndex) { if (gamepadIndex === activeGamepadIndex) {
activeGamepadIndex = null; activeGamepadIndex = null;
$gamepad.empty(); $gamepad.empty();
@ -101,10 +102,10 @@ function removeGamepad(gamepadIndex) {
delete gamepads[gamepadIndex]; delete gamepads[gamepadIndex];
displayGamepadHelp(); displayGamepadHelp();
} }
setInterval(scanGamepads, 500); setInterval(scanGamepads, 500);
function scanGamepads() { function scanGamepads() {
if (null !== activeGamepadIndex) { if (null !== activeGamepadIndex) {
return; return;
} }
@ -125,9 +126,9 @@ function scanGamepads() {
} }
} }
} }
} }
function mapGamepad(gamepad) { function mapGamepad(gamepad) {
var button; var button;
var axis; var axis;
@ -178,9 +179,9 @@ function mapGamepad(gamepad) {
updateVisualStatus(); updateVisualStatus();
}); });
} }
function updateVisualStatus() { function updateVisualStatus() {
if (null === activeGamepadIndex) { if (null === activeGamepadIndex) {
return; return;
} }
@ -236,9 +237,9 @@ function updateVisualStatus() {
updateAxis($axis); updateAxis($axis);
} }
} }
} }
function changeGamepadColor(gamepadColor) { function changeGamepadColor(gamepadColor) {
if (! activeGamepadIdentifier) { if (! activeGamepadIdentifier) {
return; return;
} }
@ -267,9 +268,9 @@ function changeGamepadColor(gamepadColor) {
} }
$gamepad.attr('data-color', activeGamepadColorName); $gamepad.attr('data-color', activeGamepadColorName);
} }
function changeZoom(zoomLevel) { function changeZoom(zoomLevel) {
if (! activeGamepadIdentifier) { if (! activeGamepadIdentifier) {
return; return;
} }
@ -295,8 +296,9 @@ function changeZoom(zoomLevel) {
activeGamepadZoomLevel = +activeGamepadZoomLevel.toFixed(1); activeGamepadZoomLevel = +activeGamepadZoomLevel.toFixed(1);
$gamepad.css('transform', 'translate(-50%, -50%) scale(' + activeGamepadZoomLevel + ', ' + activeGamepadZoomLevel + ')'); $gamepad.css('transform', 'translate(-50%, -50%) scale(' + activeGamepadZoomLevel + ', ' + activeGamepadZoomLevel + ')');
} }
function toggleHelp(zoomLevel) { function toggleHelp(zoomLevel) {
$help.toggleClass('active'); $help.toggleClass('active');
} }
})(window, document);