From ecce2e22a8e6f47472c85ccafb4445427f87e6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20FOUCRET?= Date: Thu, 7 Jun 2018 11:57:37 +0200 Subject: [PATCH 1/2] Ensure one result redirect return type. --- .../Plugin/CatalogSearch/ResultPlugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/module-elasticsuite-catalog/Plugin/CatalogSearch/ResultPlugin.php b/src/module-elasticsuite-catalog/Plugin/CatalogSearch/ResultPlugin.php index 283a2590d..113ab7bdd 100644 --- a/src/module-elasticsuite-catalog/Plugin/CatalogSearch/ResultPlugin.php +++ b/src/module-elasticsuite-catalog/Plugin/CatalogSearch/ResultPlugin.php @@ -95,7 +95,7 @@ public function aroundExecute( \Magento\CatalogSearch\Controller\Result\Index $subject, \Closure $proceed ) { - $proceed(); + $result = $proceed(); if (!$subject->getResponse()->isRedirect() && $this->scopeConfig->isSetFlag(self::REDIRECT_SETTINGS_CONFIG_XML_FLAG)) { $layer = $this->layerResolver->get(); @@ -110,12 +110,12 @@ public function aroundExecute( $this->addRedirectMessage($product); $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); $result->setUrl($product->getProductUrl()); - - return $result; } } } } + + return $result; } /** From 95eb3305a890b85c9bdc056585a3749160df9f63 Mon Sep 17 00:00:00 2001 From: Romain Ruaud Date: Thu, 7 Jun 2018 15:02:56 +0200 Subject: [PATCH 2/2] Fixing default order of categories filterable attributes. --- .../web/js/category/filter-config/dnd.js | 15 +- .../js/category/filter-config/dynamic-rows.js | 128 ++++++------------ .../web/js/category/filter-config/record.js | 18 +-- 3 files changed, 55 insertions(+), 106 deletions(-) diff --git a/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dnd.js b/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dnd.js index f60d7c434..97ffca6c7 100644 --- a/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dnd.js +++ b/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dnd.js @@ -29,21 +29,22 @@ define(['Magento_Ui/js/dynamic-rows/dnd'], function (Dnd) { var plannedPosition; if (dragData.depElement.insert === 'after') { - plannedPosition = depElemPosition + 1; + // hack : ensure sort() call on position will properly compute this item after the other, and before the next. + plannedPosition = depElemPosition + 0.5 ; } else if (dragData.depElement.insert === 'before') { - plannedPosition = dragData.instanceCtx.position = depElemPosition; + plannedPosition = depElemPosition - 1; } if (dragData.instanceCtx.isPinned() === true) { - var maxPinnedPosition = dragData.instanceCtx.parentComponent().getPinnedRecords().length + 1; - if (plannedPosition > maxPinnedPosition) { - // Do nothing in this case, this occurs when dropping a pinned item outside other pinned items. - return; + var pinnedRecords = dragData.instanceCtx.parentComponent().getPinnedRecords(); + var lastPinnedRecord = pinnedRecords[pinnedRecords.length - 1]; + if (plannedPosition > lastPinnedRecord.position) { + plannedPosition = lastPinnedRecord.position + 1; } } dragData.instanceCtx.position = plannedPosition; - dragData.instanceCtx.parentComponent().sortElements(); + dragData.instanceCtx.parentComponent().sort(dragData.instanceCtx.position, dragData.instanceCtx); } }); }); diff --git a/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dynamic-rows.js b/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dynamic-rows.js index 468c36043..093e647bc 100644 --- a/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dynamic-rows.js +++ b/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/dynamic-rows.js @@ -17,106 +17,62 @@ define(['Magento_Ui/js/dynamic-rows/dynamic-rows'], function (DynamicRows) { return DynamicRows.extend({ - defaults: { - unpinnedPositions: [] - }, - - /** - * Pin/Unpin an item to the top of list. - */ - togglePinned: function (record) { - return record.isPinned() ? this.pin(record) : this.unpin(record); - }, - /** - * Pin an item on top of the list - * - * @param record The record being pinned + * Set max element position * - * @returns {exports} + * @param {Number} position - element position + * @param {Object} elem - instance */ - pin : function(record) { - this.sortElements(); - record.position = this.getPinnedRecords().length; // Pin the item at the end of already pinned items. - - return this; + setMaxPosition: function (position, elem) { + if (position || position === 0) { + this.checkMaxPosition(position); + // Discard the legacy call to sort() that was here because it was messed up by pinned items. + } else { + this.maxPosition += 1; + } }, - /** - * Unpin an item. Put it back to his place in the list. - * - * @param record The record being pinned - * - * @returns {exports} - */ - unpin : function(record) { - this.sortElements(); - var pinnedRecords = this.getPinnedRecords(); + sort: function (position, elem) { + var posCounter = 0; - // Items which are pinned and should be normally previous the unpinned one, leaving an hole in the list. - var pinnedBefore = pinnedRecords.filter(function(elem) { - return this.getUnpinnedPosition(elem) < this.getUnpinnedPosition(record) - }.bind(this)); + var sorted = this.elems().sort(function (propOne, propTwo) { + var order = 0; - // We finally add +1 to insert after element instead of replacing it. - record.position = this.getUnpinnedPosition(record) + pinnedRecords.length - pinnedBefore.length + 1; - - this.sortElements(); - return this; - }, + if (propOne.isPinned() && propTwo.isPinned()) { + order = propOne.position - propTwo.position; + } else if (propOne.isPinned() || propTwo.isPinned()) { + order = propOne.isPinned() ? -1 : 1; + } else { + order = propOne.data().default_position - propTwo.data().default_position; - /** - * Retrieve all currently pinned records. - * - * @returns {*} - */ - getPinnedRecords : function() { - return this.elems().filter(function(elem) { return elem.isPinned() === true }); - }, - - /** - * Reapply position correctly according to current order of items. - * Mandatory to avoid gaps in position field. - */ - sortElements : function() { - for (var i = 0; i < this.elems().length; i++) { - this.elems()[i].position = i + 1; - } - }, + if (order === 0) { + order = propOne.recordId - propTwo.recordId; + } + } - /** - * Initialize children - * - * @returns {Object} Chainable. - */ - initChildren: function () { - this._super(); + return order; + }); - if (this.unpinnedPositions.length === 0) { - var unpinnedPositions = this.getChildItems().sort(function (itemA, itemB) { - if (itemA.default_position !== itemB.default_position) { - return parseInt(itemA.default_position, 10) - parseInt(itemB.default_position, 10); - } - return itemA.attribute_label.localeCompare(itemB.attribute_label); - }); + sorted.forEach(function(record) { + posCounter++; + record.position = posCounter; + }); - for (var i = 0; i < unpinnedPositions.length; i++) { - this.unpinnedPositions[unpinnedPositions[i].attribute_id] = i + 1; - } - } + this.elems(sorted); - return this; }, - /** - * Retrieve position of an item when not pinned. - * Unpinned Positions are computed at element first rendering. - * - * @param record - * @returns {Integer} - */ - getUnpinnedPosition: function (record) { - return parseInt(this.unpinnedPositions[record.data().attribute_id], 10); + /** + * Retrieve all currently pinned records. + * + * @returns {*} + */ + getPinnedRecords : function() { + return this.elems().filter( + function(elem) { return elem.isPinned() === true } + ).sort(function(recordA, recordB) { + return recordA.position - recordB.position; + }); } }); }); diff --git a/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/record.js b/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/record.js index e7dbd5c71..b30e89c53 100644 --- a/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/record.js +++ b/src/module-elasticsuite-catalog/view/adminhtml/web/js/category/filter-config/record.js @@ -24,16 +24,6 @@ define(['Magento_Ui/js/dynamic-rows/record'], function (Component) { } }, - /** - * Initialize element - */ - initialize: function() { - this._super(); - this.position = this.recordId + 1; - - return this; - }, - /** * Init bindings. * @@ -50,7 +40,10 @@ define(['Magento_Ui/js/dynamic-rows/record'], function (Component) { onPinChange : function() { if (this.data() && this.data().is_pinned !== undefined) { this.isPinned(this.data().is_pinned); - this.parentComponent().togglePinned(this); + if (this.isPinned()) { + this.position = this.parentComponent().getPinnedRecords().length + 1; + } + this.parentComponent().sort(this.position, this); } }, @@ -87,8 +80,7 @@ define(['Magento_Ui/js/dynamic-rows/record'], function (Component) { * @param name * @returns {Integer} */ - getChildrenIndex: function (name) - { + getChildrenIndex: function (name) { return this.elems().findIndex(function (elem) { return elem.index === name });