From 6dc0956253ec3a9d4d2a6840b0d50542049b6330 Mon Sep 17 00:00:00 2001
From: Sergey Semenov <ssemenov@ebay.com>
Date: Fri, 27 Mar 2015 19:08:20 +0200
Subject: [PATCH] MAGETWO-21349: Advanced Mini Cart

---
 .../Controller/Sidebar/RemoveItem.php         | 10 +--
 .../Checkout/view/frontend/web/js/sidebar.js  | 65 ++++++++-----------
 2 files changed, 32 insertions(+), 43 deletions(-)

diff --git a/app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php b/app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php
index f71b4ae64b4..95ec66b764e 100644
--- a/app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php
+++ b/app/code/Magento/Checkout/Controller/Sidebar/RemoveItem.php
@@ -84,11 +84,11 @@ class RemoveItem extends Action
     protected function jsonResponse($error = '')
     {
         $response = $this->sidebar->getResponseData($error);
-        if (isset($response['cleanup']) && (bool)$response['cleanup']) {
-            $resultPage = $this->resultPageFactory->create();
-            $block = $resultPage->getLayout()->getBlock('minicart.content')->toHtml();
-            $response['content'] = $block;
-        }
+
+        $resultPage = $this->resultPageFactory->create();
+        $block = $resultPage->getLayout()->getBlock('minicart.content')->toHtml();
+        $response['content'] = $block;
+
         return $this->getResponse()->representJson(
             $this->jsonHelper->jsonEncode($response)
         );
diff --git a/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js b/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js
index 1c6282150da..d3c3a264b88 100644
--- a/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js
+++ b/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js
@@ -18,22 +18,23 @@ define([
         scrollHeight: 0,
 
         _create: function() {
+            this._initContent();
+        },
+
+        _initContent: function() {
             var self = this;
 
             this.element.decorate('list', this.options.isRecursive);
 
+            $(this.options.button.close).click(function(event) {
+                event.stopPropagation();
+                $(self.options.targetElement).dropdownDialog("close");
+            });
+
             $(this.options.button.checkout).on('click', $.proxy(function() {
                 location.href = this.options.url.checkout;
             }, this));
 
-            $(this.options.item.qty).keyup(function(event) {
-                self._showButton($(this));
-            });
-            $(this.options.item.button).click(function(event) {
-                event.stopPropagation();
-                self._updateQty($(this))
-            });
-
             $(this.options.button.remove).click(function(event) {
                 event.stopPropagation();
                 if (confirm(self.options.confirmMessage)) {
@@ -41,22 +42,16 @@ define([
                 }
             });
 
-            this._initCloseButton();
-            this._calcHeight();
-            this._isOverflowed();
-        },
-
-        /**
-         * Add event on "Close" button click
-         *
-         * @private
-         */
-        _initCloseButton: function() {
-            var self = this;
-            $(this.options.button.close).click(function(event) {
+            $(this.options.item.qty).keyup(function() {
+                self._showItemButton($(this));
+            });
+            $(this.options.item.button).click(function(event) {
                 event.stopPropagation();
-                $(self.options.targetElement).dropdownDialog("close");
+                self._updateItemQty($(this))
             });
+
+            this._calcHeight();
+            this._isOverflowed();
         },
 
         /**
@@ -73,16 +68,16 @@ define([
             }
         },
 
-        _showButton: function(elem) {
+        _showItemButton: function(elem) {
             var itemId = elem.data('cart-item');
             var itemQty = elem.data('item-qty');
             if (this._isValidQty(itemQty, elem.val())) {
                 $('#update-cart-item-' + itemId).show('fade', 300);
             } else if (elem.val() == 0) {
                 elem.val(itemQty);
-                this._hideButton(elem);
+                this._hideItemButton(elem);
             } else {
-                this._hideButton(elem);
+                this._hideItemButton(elem);
             }
         },
 
@@ -99,17 +94,17 @@ define([
                 && (changed - 0 > 0);
         },
 
-        _hideButton: function(elem) {
+        _hideItemButton: function(elem) {
             var itemId = elem.data('cart-item');
             $('#update-cart-item-' + itemId).hide('fade', 300);
         },
 
-        _updateQty: function(elem) {
+        _updateItemQty: function(elem) {
             var itemId = elem.data('cart-item');
             this._ajax(this.options.url.update, {
                 item_id: itemId,
                 item_qty: $('#cart-item-' + itemId + '-qty').val()
-            }, elem, this._updateQtyAfter);
+            }, elem, this._updateItemQtyAfter);
         },
 
         /**
@@ -119,14 +114,14 @@ define([
          * @param response
          * @private
          */
-        _updateQtyAfter: function(elem, response) {
+        _updateItemQtyAfter: function(elem, response) {
             if ($.type(response.data) === 'object') {
                 this._refreshQty(response.data.summary_qty, response.data.summary_text);
                 this._refreshSubtotal(response.data.subtotal);
                 this._refreshShowcart(response.data.summary_qty, response.data.summary_text);
                 this._refreshItemQty(elem, response.data.summary_qty);
             }
-            this._hideButton(elem);
+            this._hideItemButton(elem);
         },
 
         _removeItem: function(elem) {
@@ -145,19 +140,13 @@ define([
          */
         _removeItemAfter: function(elem, response) {
             if ($.type(response.data) === 'object') {
-                this._refreshQty(response.data.summary_qty, response.data.summary_text);
-                this._refreshSubtotal(response.data.subtotal);
                 this._refreshShowcart(response.data.summary_qty, response.data.summary_text);
             }
+            $(this.options.minicart.content).html($.trim(response.content));
             if (response.cleanup === true) {
-                $(this.options.minicart.content).replaceWith($.trim(response.content));
                 $(this.options.showcart.parent).addClass('empty');
-                this._initCloseButton();
-            } else {
-                elem.closest('li').remove();
-                this._calcHeight();
-                this._isOverflowed();
             }
+            this._initContent();
         },
 
         /**
-- 
GitLab