Skip to content

Commit

Permalink
Small changes to increase performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Galvao da Gama committed Oct 14, 2020
1 parent 8eead95 commit e3fd246
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 66 deletions.
73 changes: 32 additions & 41 deletions app/code/Magento/Wishlist/Block/AddToWishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

namespace Magento\Wishlist\Block;

use Magento\Catalog\Api\Data\ProductTypeInterface;
use Magento\Catalog\Api\ProductTypeListInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

/**
* Wishlist js plugin initialization block
Expand All @@ -24,63 +28,50 @@ class AddToWishlist extends Template
private $productTypes;

/**
* Returns wishlist widget options
*
* @return array
* @since 100.1.0
* @var ProductTypeListInterface
*/
public function getWishlistOptions()
{
return [
'productType' => $this->getProductTypes(),
'isProductList' => (bool)$this->getData('is_product_list')
];
}
private $productTypeList;

/**
* Returns an array of product types
*
* @return array|null
* @throws \Magento\Framework\Exception\LocalizedException
* AddToWishlist constructor.
* @param ProductTypeListInterface $productTypeList
* @param Context $context
* @param array $data
*/
private function getProductTypes()
{
if ($this->productTypes === null) {
$this->productTypes = [];
$block = $this->getLayout()->getBlock($this->getProductListBlockName());
if ($block) {
$productCollection = $block->getLoadedProductCollection();
$productTypes = [];
/** @var $product \Magento\Catalog\Model\Product */
foreach ($productCollection as $product) {
$productTypes[] = $this->escapeHtml($product->getTypeId());
}
$this->productTypes = array_unique($productTypes);
}
}
return $this->productTypes;
public function __construct(
Context $context,
array $data = [],
?ProductTypeListInterface $productTypeList = null
) {
parent::__construct($context, $data);
$this->productTypes = [];
$this->productTypeList = $productTypeList ?: ObjectManager::getInstance()->get(ProductTypeListInterface::class);
}

/**
* Get product list block name in layout
* Returns wishlist widget options
*
* @return string
* @return array
* @since 100.1.0
*/
private function getProductListBlockName(): string
public function getWishlistOptions()
{
return $this->getData('product_list_block') ?: 'category.products.list';
return ['productType' => $this->getProductTypes()];
}

/**
* @inheritDoc
* Returns an array of product types
*
* @since 100.1.0
* @return array
*/
protected function _toHtml()
private function getProductTypes(): array
{
if (!$this->getProductTypes()) {
return '';
if (count($this->productTypes) === 0) {
/** @var ProductTypeInterface productTypes */
$this->productTypes = array_map(function ($productType) {
return $productType->getName();
}, $this->productTypeList->getProductTypes());
}
return parent::_toHtml();
return $this->productTypes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
<referenceContainer name="category.product.list.additional">
<block class="Magento\Wishlist\Block\AddToWishlist"
name="category.product.list.additional.wishlist_addto"
template="Magento_Wishlist::addto.phtml">
<arguments>
<argument name="is_product_list" xsi:type="boolean">true</argument>
</arguments>
</block>
template="Magento_Wishlist::addto.phtml"/>
</referenceContainer>
</referenceContainer>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
<referenceBlock name="wishlist_page_head_components">
<block class="Magento\Wishlist\Block\AddToWishlist"
name="catalogsearch.wishlist_addto"
template="Magento_Wishlist::addto.phtml">
<arguments>
<argument name="is_product_list" xsi:type="boolean">true</argument>
<argument name="product_list_block" xsi:type="string">search_result_list</argument>
</arguments>
</block>
template="Magento_Wishlist::addto.phtml"/>
</referenceBlock>
</body>
</page>
36 changes: 22 additions & 14 deletions app/code/Magento/Wishlist/view/frontend/web/js/add-to-wishlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ define([
customOptionsInfo: '.product-custom-option',
qtyInfo: '#qty',
actionElement: '[data-action="add-to-wishlist"]',
productListItem: '.item.product-item',
isProductList: false
productListWrapper: '.product-item-info',
productPageWrapper: '.product-info-main'
},

/** @inheritdoc */
Expand Down Expand Up @@ -67,23 +67,17 @@ define([
_updateWishlistData: function (event) {
var dataToAdd = {},
isFileUploaded = false,
productItem = null,
handleObjSelector = null,
self = this;

if (event.handleObj.selector == this.options.qtyInfo) { //eslint-disable-line eqeqeq
this._updateAddToWishlistButton({}, productItem);
this._updateAddToWishlistButton({}, event);
event.stopPropagation();

return;
}

if (this.options.isProductList) {
productItem = $(event.target).closest(this.options.productListItem);
handleObjSelector = productItem.find(event.handleObj.selector);
} else {
handleObjSelector = $(event.handleObj.selector);
}
handleObjSelector = $(event.currentTarget).closest('form').find(event.handleObj.selector)

handleObjSelector.each(function (index, element) {
if ($(element).is('input[type=text]') ||
Expand All @@ -110,18 +104,18 @@ define([
if (isFileUploaded) {
this.bindFormSubmit();
}
this._updateAddToWishlistButton(dataToAdd, productItem);
this._updateAddToWishlistButton(dataToAdd, event);
event.stopPropagation();
},

/**
* @param {Object} dataToAdd
* @param {Object} productItem
* @param {jQuery.Event} event
* @private
*/
_updateAddToWishlistButton: function (dataToAdd, productItem) {
_updateAddToWishlistButton: function (dataToAdd, event) {
var self = this,
buttons = productItem ? productItem.find(this.options.actionElement) : $(this.options.actionElement);
buttons = this._getAddToWishlistButton(event);

buttons.each(function (index, element) {
var params = $(element).data('post');
Expand All @@ -139,6 +133,20 @@ define([
});
},

/**
* @param {jQuery.Event} event
* @private
*/
_getAddToWishlistButton: function (event) {
var productListWrapper = $(event.currentTarget).closest(this.options.productListWrapper);

if (productListWrapper.length) {
return productListWrapper.find(this.options.actionElement);
}

return $(event.currentTarget).closest(this.options.productPageWrapper).find(this.options.actionElement);
},

/**
* @param {Object} array1
* @param {Object} array2
Expand Down

0 comments on commit e3fd246

Please sign in to comment.