Fixed Util static method issue

Also fixed bug in Util.getElement
This commit is contained in:
unsettledgames 2021-07-10 11:05:56 +02:00
parent f81019830b
commit dbffc0b9da
6 changed files with 38 additions and 53 deletions

View File

@ -1,35 +1,41 @@
// Acts as a public static class
class Util {
static getElement(elementOrElementId) {
return typeof elementOrElementId
? document.getElementById(elementOrElementId)
: elementOrElementId;
if (typeof(elementOrElementId) == "object") {
return elementOrElementId;
}
else if (typeof(elementOrElementId) == "string") {
return document.getElementById(elementOrElementId);
}
else {
console.log("Type not supported: " + typeof(elementOrElementId));
}
}
static getText(elementId) {
return this.getElement(elementId).textContent;
return Util.getElement(elementId).textContent;
}
static setText(elementId, text) {
this.getElement(elementId).textContent = text;
Util.getElement(elementId).textContent = text;
}
static getValue(elementId) {
return this.getElement(elementId).value;
return Util.getElement(elementId).value;
}
static setValue(elementId, value) {
this.getElement(elementId).value = value;
Util.getElement(elementId).value = value;
}
//add class .selected to specified element
static select(elementId) {
this.getElement(elementId).classList.add('selected');
Util.getElement(elementId).classList.add('selected');
}
//remove .selected class from specified element
static deselect(elementId) {
this.getElement(elementId).classList.remove('selected');
Util.getElement(elementId).classList.remove('selected');
}
//toggle the status of the .selected class on the specified element
static toggle(elementId) {
this.getElement(elementId).classList.toggle('selected');
Util.getElement(elementId).classList.toggle('selected');
}
}

View File

@ -9,7 +9,7 @@ for (var i = 1; i < mainMenuItems.length; i++) {
//when you click a main menu items button
on('click', menuButton, function (e, button) {
select(button.parentElement);
Util.select(button.parentElement);
});
var subMenu = menuItem.children[1];
@ -191,7 +191,7 @@ for (var i = 1; i < mainMenuItems.length; i++) {
function closeMenu () {
//remove .selected class from all menu buttons
for (var i = 0; i < mainMenuItems.length; i++) {
deselect(mainMenuItems[i]);
Util.deselect(mainMenuItems[i]);
}
}

View File

@ -84,10 +84,10 @@ palettes["Gameboy Color"] = {"name":"Nintendo Gameboy (Black Zero)","author":"",
const buttonEvent = () => {
//hide the dropdown menu
deselect('palette-menu');
deselect('palette-button');
deselect('palette-menu-splash');
deselect('palette-button-splash');
Util.deselect('palette-menu');
Util.deselect('palette-button');
Util.deselect('palette-menu-splash');
Util.deselect('palette-button-splash');
//show empty palette option
noPaletteButton.style.display = 'block';
@ -115,15 +115,15 @@ palettes["Gameboy Color"] = {"name":"Nintendo Gameboy (Black Zero)","author":"",
document.getElementById('load-palette-browse-holder').click();
}
const clickPaletteButtonEvent = (e) => {
toggle('palette-button');
toggle('palette-menu');
Util.toggle('palette-button');
Util.toggle('palette-menu');
deselect('preset-button');
deselect('preset-menu');
Util.deselect('preset-button');
Util.deselect('preset-menu');
// Splash version
toggle('palette-button-splash');
toggle('palette-menu-splash');
Util.toggle('palette-button-splash');
Util.toggle('palette-menu-splash');
e.stopPropagation();
}
@ -141,13 +141,13 @@ palettes["Gameboy Color"] = {"name":"Nintendo Gameboy (Black Zero)","author":"",
})
newPixelElement.addEventListener('click', () => {
deselect('preset-button');
deselect('preset-menu');
deselect('palette-button');
deselect('palette-menu');
Util.deselect('preset-button');
Util.deselect('preset-menu');
Util.deselect('palette-button');
Util.deselect('palette-menu');
// Splash version
deselect('palette-button-splash');
deselect('palette-menu-splash');
Util.deselect('palette-button-splash');
Util.deselect('palette-menu-splash');
})
})();

View File

@ -25,8 +25,8 @@ const PresetModule = (() => {
Util.setText('palette-button', presets[presetName].palette);
//hide the dropdown menu
deselect('preset-menu');
deselect('preset-button');
Util.deselect('preset-menu');
Util.deselect('preset-button');
//set the text of the dropdown to the newly selected preset
Util.setText('preset-button', presetName);
@ -41,8 +41,8 @@ const PresetModule = (() => {
Util.toggle('preset-menu');
//close the palette menu
deselect('palette-button');
deselect('palette-menu');
Util.deselect('palette-button');
Util.deselect('palette-menu');
e.stopPropagation();
});

View File

@ -3,7 +3,6 @@
//=include util/onChildren.js
//=include util/onClick.js
//=include util/onClickChildren.js
//=include util/select.js
//=include util/hexToRgb.js
//=include util/rgbToHex.js
//=include util/rgbToHsl.js

View File

@ -1,20 +0,0 @@
//add class .selected to specified element
function select(elementId) {
//console.log(arguments.callee.caller.name, 'selected ', elementId);
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
element.classList.add('selected');
}
//remove .selected class from specified element
function deselect(elementId) {
//console.log('deselected ', elementId);
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
element.classList.remove('selected');
}
//toggle the status of the .selected class on the specified element
function toggle(elementId) {
//console.log('toggled ', elementId);
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
element.classList.toggle('selected');
}