(function(){'use strict';$.widget('sidebar',{options:{minicart:{maxItemsVisible:3}},scrollHeight:0,shoppingCartUrl:window.checkout?window.checkout.shoppingCartUrl:'',create:function(){this._addObservers();this._calcHeight();},update:function(){this._calcHeight();},_addObservers:function(){var self=this;this.element.on('click',this.options.button.checkout,function(){var cart=$.sections.get('cart'),customer=$.sections.get('customer');if(!customer().firstname&&cart().isGuestCheckoutAllowed===false){$.cookies.set('login_redirect',self.options.url.checkout);$(this).prop('disabled',true);location.href=self.options.url.loginUrl;return false;} $(this).prop('disabled',true);location.href=self.options.url.checkout;}).on('click',this.options.button.remove,function(event){event.preventDefault();event.stopPropagation();$.confirm({content:self.options.confirmMessage,actions:{confirm:function(){self._removeItem($(event.currentTarget));},always:function(e){e.stopImmediatePropagation();}}});}).on('keyup change',this.options.item.qty,function(event){self._showItemButton($(event.target));}).on('click',this.options.item.button,function(event){event.stopPropagation();self._updateItemQty($(event.currentTarget));}).on('focusout',this.options.item.qty,function(event){self._validateQty($(event.currentTarget));});},_showItemButton:function(elem){var itemId=elem.data('cart-item'),itemQty=elem.data('item-qty');if(this._isValidQty(itemQty,elem.val())){this.element.find('#update-cart-item-'+itemId).show('fade',300);}else if(elem.val()==0){this._hideItemButton(elem);}else{this._hideItemButton(elem);}},_isValidQty:function(origin,changed){return origin!=changed&&changed.length>0&&changed-0==changed&&changed-0>0;},_validateQty:function(elem){var itemQty=elem.data('item-qty');if(!this._isValidQty(itemQty,elem.val())){elem.val(itemQty);}},_hideItemButton:function(elem){this.element.find('#update-cart-item-'+elem.data('cart-item')).hide();},_updateItemQty:function(elem){var itemId=elem.data('cart-item');this._ajax(this.options.url.update,{'item_id':itemId,'item_qty':this.element.find('#cart-item-'+itemId+'-qty').val()},elem,this._updateItemQtyAfter);},_updateItemQtyAfter:function(elem){var productData=this._getProductById(Number(elem.data('cart-item')));if(!_.isUndefined(productData)){$(document).trigger('ajax:updateCartItemQty');if(window.location.href===this.shoppingCartUrl){window.location.reload(false);}} this._hideItemButton(elem);},_removeItem:function(elem){this._ajax(this.options.url.remove,{'item_id':elem.data('cart-item')},elem,this._removeItemAfter);},_removeItemAfter:function(elem){var productData=this._getProductById(Number(elem.data('cart-item')));if(!_.isUndefined(productData)){$(document).trigger('ajax:removeFromCart',{productIds:[productData.product_id],productInfo:[{'id':productData.product_id}]});if(window.location.href.indexOf(this.shoppingCartUrl)===0){window.location.reload();}}},_getProductById:function(productId){return _.find($.sections.get('cart')().items,function(item){return productId===Number(item.item_id);});},_ajax:function(url,data,elem,callback){var self=this;elem.prop('disabled',true);$.request.post({url:url,data:data,type:'form'}).then(function(response){elem.prop('disabled',false);if(response.body.success){callback.call(self,elem,response);}else if(response.body.error_message){$.alert({content:response.body.error_message});}});},_calcHeight:function(){var self=this,height=0,counter=this.options.minicart.maxItemsVisible,target=this.element.find(this.options.minicart.list),outerHeight;if(this.options.calcHeight===false){return;} self.scrollHeight=0;target.children().each(function(){if($(this).find('.options').length>0){$(this).collapsible();} outerHeight=$(this).outerHeight(true);if(counter-->0){height+=outerHeight;} self.scrollHeight+=outerHeight;});target.parent().height(height);}});$.view('minicart',{component:'Magento_Checkout/js/view/minicart',cart:{},shoppingCartUrl:window.checkout?window.checkout.shoppingCartUrl:'',maxItemsToDisplay:window.checkout?window.checkout.maxItemsToDisplay:'',shouldRender:ko.observable(false),isLoading:ko.observable(false),displaySubtotal:ko.observable(true),addToCartCalls:0,minicartSelector:'[data-block="minicart"]',create:function(){var self=this,cartData=$.sections.get('cart');this.update(cartData());this.cartSubscription=cartData.subscribe(function(updatedCart){self.addToCartCalls--;self.isLoading(self.addToCartCalls>0);self.update(updatedCart);self.initSidebar();});this.minicart().one('dropdownDialog:open',function(){self.shouldRender(true);}).on('dropdownDialog:open',function(){self.initSidebar();}).on('contentLoading',function(){self.addToCartCalls++;self.isLoading(true);});if(cartData().website_id!==window.checkout.websiteId&&cartData().website_id!==undefined||cartData().storeId!==window.checkout.storeId&&cartData().storeId!==undefined){$.sections.reload(['cart'],false);}},destroy:function(){this.cartSubscription.dispose();this._super();},minicart:function(){return $(this.minicartSelector);},initSidebar:function(){var minicart=this.minicart(),sidebar=minicart.sidebar('instance');minicart.trigger('contentUpdated');if(sidebar){return sidebar.update();} if(!$('[data-role=product-item]').length){return false;} minicart.sidebar(this.getSidebarSettings());},getSidebarSettings:function(){return{url:{checkout:window.checkout.checkoutUrl,update:window.checkout.updateItemQtyUrl,remove:window.checkout.removeItemUrl,loginUrl:window.checkout.customerLoginUrl,isRedirectRequired:window.checkout.isRedirectRequired},button:{checkout:'.action.checkout',remove:'.action.delete',close:'.action.close'},showcart:{parent:'span.counter',qty:'span.counter-number',label:'span.counter-label'},minicart:{list:'.minicart-items',content:'#minicart-content-wrapper',qty:'div.items-total',subtotal:'div.subtotal span.price',maxItemsVisible:window.checkout.minicartMaxItemsVisible},item:{qty:'.cart-item-qty',button:'.update-cart-item'},confirmMessage:$.__('Are you sure you would like to remove this item from the shopping cart?')};},closeMinicart:function(){this.minicart().find('[data-role="dropdownDialog"]').dropdownDialog('close');},closeMinicartandSendGa:function(){var _this=this;if(typeof sendGaActive!=="undefined"){if(sendGaActive==1){try{var itemGaData=[];var cartItems=customerData.get('cart')().items;var blockItem=globalBlockItem;$.each(cartItems,function(index,value){var itemId=value.item_id;var products={'item_name':value.product_name,'item_id':blockItem[itemId].item_sku,'price':value.product_price_value,'item_brand':blockItem[itemId].item_category,'item_category':blockItem[itemId].item_brand,'item_category2':"Not Defined",'item_category3':"Not Defined",'item_category4':"Not Defined",'item_variant':"Not Defined",'quantity':value.qty};itemGaData.push(products);});_this.pushDataPlaceOrder(itemGaData);}catch(err){console.log('error ga begin checkout : '+err.message);}}} $('[data-block="minicart"]').find('[data-role="dropdownDialog"]').dropdownDialog('close');},pushDataPlaceOrder:function(itemGaData){try{window.dataLayer=window.dataLayer||[];dataLayer.push({event:"begin_checkout",eventCategory:'ecommerce',eventAction:'go_to_checkout',eventLabel:globalStatusUser,ecommerce:{items:itemGaData}});}catch(err){console.log('error ga begin checkout : '+err.message);}},getItemRenderer:function(item){var renderer=this.options.itemRenderer[item.product_type];if(renderer&&document.getElementById(renderer)){return renderer;} return'defaultRenderer';},update:function(updatedCart){_.each(updatedCart,function(value,key){if(!this.cart.hasOwnProperty(key)){this.cart[key]=ko.observable();} this.cart[key](value);},this);},getCartParamUnsanitizedHtml:function(name){if(!_.isUndefined(name)){if(!this.cart.hasOwnProperty(name)){this.cart[name]=ko.observable();}} return this.cart[name]();},getCartParam:function(name){return this.getCartParamUnsanitizedHtml(name);},getCartItems:function(){var items=this.getCartParamUnsanitizedHtml('items')||[];items=items.slice(parseInt(-this.maxItemsToDisplay,10));return items;},getCartLineItemsCount:function(){var items=this.getCartParamUnsanitizedHtml('items')||[];return parseInt(items.length,10);}});ko.components.register('subtotal.totals',{viewModel:function(options){this.cart=$.sections.get('cart');this.displaySubtotal=options.$root.displaySubtotal;$.extend(this,options.$root._option('children/subtotal.container/children/subtotal/children/subtotal.totals/config'));},template:{element:'subtotal.totals'}});})();