Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurélien FOUCRET committed Jun 7, 2018
2 parents 49d97bc + 35c30ed commit 11e1df2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
}
},

Expand Down Expand Up @@ -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
});
Expand Down

0 comments on commit 11e1df2

Please sign in to comment.