File: D:/HostingSpaces/SBogers10/carrot.komma.pro/resources/js/kms/attributes/styleClassController.js
/**
* Helps managing classes on a button.
* You can request to add a class and for each time you request to add the class, it will increment a counter.
* You can also request to remove a class and for each time you do that, it will decrement a counter.
* When that counter hits 0 it will remove the class.
* It will keep counter for each unique class you request to add.
*/
class styleClassController {
/**
* Constructor
*
* @param {HTMLElement} htmlElement
*/
constructor(htmlElement)
{
this.constructedSuccessFully = false;
if(!htmlElement instanceof HTMLElement)
{
console.error('styleClassController: The given htmlElement must be, but was not a HTMLElement');
return;
}
this.htmlElement = htmlElement;
this.classCounts = {};
this.constructedSuccessFully = true;
}
/**
* @param {string} styleClass
*/
requestAddClass(styleClass)
{
if(!this.htmlElement.classList.contains(styleClass)) this.htmlElement.classList.add(styleClass);
if(!this.classCounts.hasOwnProperty(styleClass)) this.classCounts[styleClass] = 0;
this.classCounts[styleClass]++;
return true;
}
requestRemoveClass(styleClass)
{
if(!this.htmlElement.classList.contains(styleClass) || !this.classCounts.hasOwnProperty(styleClass)) return false;
this.classCounts[styleClass]--;
if(this.classCounts[styleClass] === 0) {
delete this.classCounts[styleClass];
this.htmlElement.classList.remove(styleClass);
}
}
}