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