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/shop.komma.nl/resources/js/components/productShoppingcartController.js
/**
 * Shopping cart form controller for product detail pages
 */
import {QuantityController} from "./quantityController";

class ProductShoppingCartController {
    /**
     * @param {HTMLDivElement} wrapper
     * @param {ShoppingCartService} shoppingCartService
     */
    constructor(wrapper, shoppingCartService)
    {
        this._wrapper = wrapper;
        if(!this._wrapper) {
            console.error('ProductShoppingCartController: The wrapper wasn\'t specified. ProductShoppingCartController stopped working');
            return;
        }
        this._quantityControls = wrapper.querySelectorAll('.js-quantity-control');
        this._addToCartButton = document.querySelector('.js-add-product-to-shoppingcart');

        this._shoppingCartService = shoppingCartService;
        if(!shoppingCartService) {
            console.error('ShoppingCartController: The shoppingCartController was expected to get an instance of ShoppingCartService, but did not get one.');
            return;
        }

        this.addToCartButtonClicked = this.addToCartButtonClicked.bind(this);
        this.quantityUpdated = this.quantityUpdated.bind(this);

        this.setupQuantityControls();
        this.controlListeners(true);
    }

    /**
     * Enable or disable event listeners
     *
     * @param enable
     */
    controlListeners(enable = true) {
        this._addToCartButton.removeEventListener('click', this.addToCartButtonClicked);

        if(enable) {
            this._addToCartButton.addEventListener('click', this.addToCartButtonClicked);
        }
    }

    /**
     * @param {MouseEvent} event
     */
    addToCartButtonClicked(event) {
        event.preventDefault();
        this._shoppingCartService._addProductToShoppingcart(this._addToCartButton.dataset.productId, this._addToCartButton.dataset.productType, this._addToCartButton.dataset.productQuantity);
    }

    /**
     * Triggered when the quantity control did change.
     */
    quantityUpdated(newValue) {
        this._addToCartButton.dataset.productQuantity = parseInt(newValue);
    }

    /**
     * Set up quantity control buttons. So that when they change they update the backend with the new quantity for the product.
     */
    setupQuantityControls() {
        let self = this;

        this._quantityControls.forEach(function(quantityControlWrapper) {
           const quantityController = new QuantityController(quantityControlWrapper);
            //The code gets to this point when you are in the shopping cart itself
            quantityController.on('update', self.quantityUpdated);
        });
    }

    updateCartButtonCounter()
    {
        let count = this._wrapper.querySelectorAll('.shoppingcartItemList ul li').length;
        let counter = document.querySelector('.shoppingCartButton span.shoppingCartButtonCounter');
        counter.innerText(count);
    }
}

export { ProductShoppingCartController }