Added Input.js

Got rid of the "on" files, refactored the rest of the code to use the functions declared in Input.js
This commit is contained in:
unsettledgames
2021-07-12 11:36:30 +02:00
parent 6072528ad2
commit 4457d2178c
12 changed files with 54 additions and 92 deletions

26
js/Input.js Normal file
View File

@ -0,0 +1,26 @@
class Input {
static on(event, elementId, functionCallback, ...args) {
//if element provided is string, get the actual element
const element = Util.getElement(elementId);
element.addEventListener(event,
function (e) {
// e = event
//this = element clicked
functionCallback(e, this, args);
//if you need to access the event or this variable, you need to add them
//when you define the callback, but you cant use the word this, eg:
//on('click', menuButton, function (e, button) {});
});
}
static onChildren(event, parentElement, functionCallback, ...args) {
parentElement = Util.getElement(parentElement);
const children = parentElement.children;
//loop through children and add onClick listener
for (var i = 0; i < children.length; i++) {
on(event, children[i], functionCallback, args);
}
}
}