/** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ define([ 'uiComponent', 'Magento_Customer/js/customer-data', 'jquery', 'ko', 'underscore', 'sidebar', 'mage/translate', 'mage/dropdown' ], function (Component, customerData, $, ko, _) { 'use strict'; var sidebarInitialized = false, addToCartCalls = 0, miniCart= $('[data-block=\'minicart\']'); /** * @return {Boolean} */ function initSidebar() { if (!miniCart || typeof miniCart.sidebar !== 'function') { return false; } if (miniCart.data('mageSidebar')) { miniCart.sidebar('update'); } if (!$('[data-role=product-item]').length) { return false; } miniCart.trigger('contentUpdated'); if (sidebarInitialized) { return false; } sidebarInitialized = true; function getCleanCartUrl(url) { if (!url) { return ''; } return url.replace(/\/default\//, '/'); } if (!window.checkout || !window.checkout.shoppingCartUrl) { return false; } var cleanCartUrl = getCleanCartUrl(window.checkout.shoppingCartUrl); try { miniCart.sidebar({ 'targetElement': 'div.block.block-minicart', 'url': { 'checkout': cleanCartUrl, 'update': window.checkout.updateItemQtyUrl || '', 'remove': window.checkout.removeItemUrl || '' }, 'button': { 'checkout': '#top-cart-btn-checkout', 'remove': '#mini-cart a.action.delete', 'close': '#btn-minicart-close' }, 'showcart': { 'parent': 'span.counter', 'qty': 'span.counter-number', 'label': 'span.counter-label' }, 'minicart': { 'list': '#mini-cart', 'content': '#minicart-content-wrapper', 'qty': 'div.items-total', 'subtotal': 'div.subtotal span.price', 'maxItemsVisible': window.checkout.minicartMaxItemsVisible || 3 }, 'item': { 'qty': ':input.cart-item-qty', 'button': ':button.update-cart-item' }, 'confirmMessage': $.mage ? $.mage.__('Are you sure you would like to remove this item from the shopping cart?') : 'Are you sure you would like to remove this item from the shopping cart?' }); var checkoutButton = $('#top-cart-btn-checkout'); if (checkoutButton.length) { checkoutButton.on('click', function (e) { e.preventDefault(); e.stopImmediatePropagation(); if (cleanCartUrl) { window.location.href = cleanCartUrl; } }); } return true; } catch (error) { return false; } } $(function() { $(document).on('click', '.btn-close-without-product', function() { $('.close-minicart').trigger('click'); }); }); return Component.extend({ shoppingCartUrl: window.checkout.shoppingCartUrl, maxItemsToDisplay: window.checkout.maxItemsToDisplay, cart: {}, gifts: ko.observable(''), freeshipping: ko.observable(''), // jscs:disable requireCamelCaseOrUpperCaseIdentifiers /** * @override */ initialize: function () { var self = this, cartData = customerData.get('cart'); this.update(cartData()); cartData.subscribe(function (updatedCart) { addToCartCalls--; this.isLoading(addToCartCalls > 0); sidebarInitialized = false; this.update(updatedCart); initSidebar(); this.getGifts(); this.getFreeShipping(); }, this); $('[data-block="minicart"]').on('contentLoading', function () { addToCartCalls++; self.isLoading(true); }); if ( cartData().website_id !== window.checkout.websiteId && cartData().website_id !== undefined || cartData().storeId !== window.checkout.storeId && cartData().storeId !== undefined ) { customerData.reload(['cart'], false); } this.getGifts(); this.getFreeShipping(); return this._super(); }, //jscs:enable requireCamelCaseOrUpperCaseIdentifiers isLoading: ko.observable(false), initSidebar: initSidebar, /** * Close mini shopping cart. */ closeMinicart: function () { $('[data-block="minicart"]').find('[data-role="dropdownDialog"]').dropdownDialog('close'); }, getInstallments: function () { $('.minicart-installments').empty(); $.ajax({ url: '/installments/ajax/index', method: 'GET', dataType: 'json', success: function (response) { $('.minicart-installments').append(response); }, error: function (xhr, status, error) { console.error('Erro:', error); } }); }, getGifts: function () { var self = this; $.ajax({ url: '/minicart/cart/gift', method: 'GET', dataType: 'json', success: function (response) { if (response == "") { $('.minicart-gifts').hide(); } else { self.gifts(response); $('.minicart-gifts').show(); } }, error: function (xhr, status, error) { console.error('Erro:', error); } }); }, getFreeShipping: function () { var self = this; $.ajax({ url: '/minicart/cart/freeshipping', method: 'GET', dataType: 'json', success: function (response) { if (response == "") { $('.minicart-shipping').hide(); } else { self.freeshipping(response.message); $('.minicart-shipping .progress-bar').css('width', response.percent + '%'); $('.minicart-shipping').show(); } }, error: function (xhr, status, error) { console.error('Erro:', error); } }); }, /** * @param {String} productType * @return {*|String} */ getItemRenderer: function (productType) { return this.itemRenderer[productType] || 'defaultRenderer'; }, /** * Update mini shopping cart content. * * @param {Object} updatedCart * @returns void */ update: function (updatedCart) { _.each(updatedCart, function (value, key) { if (!this.cart.hasOwnProperty(key)) { this.cart[key] = ko.observable(); } this.cart[key](value); }, this); }, /** * Get cart param by name. * * @param {String} name * @returns {*} */ getCartParamUnsanitizedHtml: function (name) { if (!_.isUndefined(name)) { if (!this.cart.hasOwnProperty(name)) { this.cart[name] = ko.observable(); } } return this.cart[name](); }, /** * @deprecated please use getCartParamUnsanitizedHtml. * @param {String} name * @returns {*} */ getCartParam: function (name) { return this.getCartParamUnsanitizedHtml(name); }, /** * Returns array of cart items, limited by 'maxItemsToDisplay' setting * @returns [] */ getCartItems: function () { var items = this.getCartParamUnsanitizedHtml('items') || []; items = items.slice(parseInt(-this.maxItemsToDisplay, 10)); return items; }, /** * Returns count of cart line items * @returns {Number} */ getCartLineItemsCount: function () { var items = this.getCartParamUnsanitizedHtml('items') || []; return parseInt(items.length, 10); } }); });