HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/pietvanmierlo/stempelbv.nl/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);
        }
    }
}