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/SBogers10/farmfun.komma.pro/resources/js/kms/attributes/quantity_discount.js
class QuantityDiscount {
    constructor(wrapper) {
        if (wrapper === undefined || wrapper.tagName !== "DIV") {
            console.error('QuantityDiscount:constructor Missing the expected div element that represents the wrapper.');
            return;
        }

        if(!'key' in wrapper.dataset) {
            console.error('QuantityDiscount:constructor The wrapper did not have a key dataset attribute');
            return;
        }
        this._key = wrapper.dataset.key;

        let realInput = wrapper.querySelector('input[name="'+this._key+'"]');
        if (!realInput) {
            console.error('QuantityDiscount:constructor Did not find a required hidden input with name "'+this._key+'" in the QuantityDiscount wrapper.');
            return;
        }
        this._realInput = realInput;

        let quantityDiscountInput = wrapper.querySelector('input[name="'+this._key+'_discount"]');
        if (!quantityDiscountInput) {
            console.error('QuantityDiscount:constructor Did not find a required input with name "'+this._key+'_discount" in the QuantityDiscount wrapper.');
            return;
        }
        this._quantityDiscountInput = quantityDiscountInput;

        let quantityAmountInput = wrapper.querySelector('input[name="'+this._key+'_quantity"]');
        if (!quantityDiscountInput) {
            console.error('QuantityDiscount:constructor Did not find a required input with name "'+this._key+'_quantity" in the QuantityDiscount wrapper.');
            return;
        }
        this._quantityAmountInput = quantityAmountInput;


        this.initialize();
        this.enableListeners();
        console.log('quantityDiscount init ', this._realInput.value);
    }

    initialize() {
        let data = this._realInput.value.split('|');
        let amount = data[0];
        let discount = data[1];

        if(this._realInput.value === '') this._realInput.value = '|';
        this._quantityAmountInput.value = amount;
        this._quantityDiscountInput.value = discount ? parseInt(discount) / 100 : '';
    }

    enableListeners() {
        this._quantityDiscountInput.addEventListener('change', this.updateRealInput.bind(this));
        this._quantityAmountInput.addEventListener('change', this.updateRealInput.bind(this));
    }


    updateRealInput()
    {
        let amount = this._quantityAmountInput.value;
        let discount = this._quantityDiscountInput.value * 100;
        discount = Math.round(discount);
        this._realInput.value = amount+'|'+discount;
    }
}