diff --git a/vendor/magento/module-ui/Controller/Adminhtml/Bookmark/Save.php b/vendor/magento/module-ui/Controller/Adminhtml/Bookmark/Save.php index 1c95428ea93e..d668256847f5 100644 --- a/vendor/magento/module-ui/Controller/Adminhtml/Bookmark/Save.php +++ b/vendor/magento/module-ui/Controller/Adminhtml/Bookmark/Save.php @@ -5,10 +5,13 @@ */ namespace Magento\Ui\Controller\Adminhtml\Bookmark; -use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; use Magento\Authorization\Model\UserContextInterface; use Magento\Backend\App\Action\Context; +use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Json\DecoderInterface; +use Magento\Framework\Serialize\Serializer\Json; use Magento\Framework\View\Element\UiComponentFactory; use Magento\Ui\Api\BookmarkManagementInterface; use Magento\Ui\Api\BookmarkRepositoryInterface; @@ -55,11 +58,12 @@ class Save extends AbstractAction implements HttpPostActionInterface /** * @var DecoderInterface * @deprecated 101.1.0 + * @see Replaced the usage of Magento\Framework\Json\DecoderInterface by Magento\Framework\Serialize\Serializer\Json */ protected $jsonDecoder; /** - * @var \Magento\Framework\Serialize\Serializer\Json + * @var Json */ private $serializer; @@ -71,7 +75,7 @@ class Save extends AbstractAction implements HttpPostActionInterface * @param BookmarkInterfaceFactory $bookmarkFactory * @param UserContextInterface $userContext * @param DecoderInterface $jsonDecoder - * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @param Json|null $serializer * @throws \RuntimeException */ public function __construct( @@ -82,7 +86,7 @@ public function __construct( BookmarkInterfaceFactory $bookmarkFactory, UserContextInterface $userContext, DecoderInterface $jsonDecoder, - \Magento\Framework\Serialize\Serializer\Json $serializer = null + Json $serializer = null ) { parent::__construct($context, $factory); $this->bookmarkRepository = $bookmarkRepository; @@ -90,8 +94,8 @@ public function __construct( $this->bookmarkFactory = $bookmarkFactory; $this->userContext = $userContext; $this->jsonDecoder = $jsonDecoder; - $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() - ->get(\Magento\Framework\Serialize\Serializer\Json::class); + $this->serializer = $serializer ?: ObjectManager::getInstance() + ->get(Json::class); } /** @@ -99,7 +103,7 @@ public function __construct( * * @return void * @throws \InvalidArgumentException - * @throws \LogicException + * @throws \LogicException|LocalizedException */ public function execute() { @@ -126,6 +130,7 @@ public function execute() $bookmark->getTitle(), $jsonData ); + $this->updateCurrentBookmarkConfig($data); break; @@ -134,7 +139,7 @@ public function execute() $this->updateBookmark( $bookmark, $identifier, - isset($data['label']) ? $data['label'] : '', + $data['label'] ?? '', $jsonData ); $this->updateCurrentBookmark($identifier); @@ -176,32 +181,31 @@ protected function updateBookmark(BookmarkInterface $bookmark, $identifier, $tit * * @param string $identifier * @return void + * @throws LocalizedException */ protected function updateCurrentBookmark($identifier) { $bookmarks = $this->bookmarkManagement->loadByNamespace($this->_request->getParam('namespace')); $currentConfig = null; foreach ($bookmarks->getItems() as $bookmark) { - if ($bookmark->getIdentifier() === self::CURRENT_IDENTIFIER) { + if ($bookmark->getIdentifier() == $identifier) { $current = $bookmark->getConfig(); - $currentConfig = $current[self::CURRENT_IDENTIFIER]; - break; + $currentConfig = $current['views'][$bookmark->getIdentifier()]['data']; + $bookmark->setCurrent(true); + } else { + $bookmark->setCurrent(false); } + $this->bookmarkRepository->save($bookmark); } foreach ($bookmarks->getItems() as $bookmark) { - if ($bookmark->getCurrent() && $currentConfig !== null) { + if ($bookmark->getIdentifier() === self::CURRENT_IDENTIFIER && $currentConfig !== null) { $bookmarkConfig = $bookmark->getConfig(); - $bookmarkConfig['views'][$bookmark->getIdentifier()]['data'] = $currentConfig; + $bookmarkConfig[self::CURRENT_IDENTIFIER] = $currentConfig; $bookmark->setConfig($this->serializer->serialize($bookmarkConfig)); + $this->bookmarkRepository->save($bookmark); + break; } - - if ($bookmark->getIdentifier() == $identifier) { - $bookmark->setCurrent(true); - } else { - $bookmark->setCurrent(false); - } - $this->bookmarkRepository->save($bookmark); } } @@ -226,4 +230,33 @@ protected function checkBookmark($identifier) return $result; } + + /** + * Update current bookmark config data + * + * @param array $data + * @return void + * @throws LocalizedException + */ + private function updateCurrentBookmarkConfig(array $data): void + { + $bookmarks = $this->bookmarkManagement->loadByNamespace($this->_request->getParam('namespace')); + foreach ($bookmarks->getItems() as $bookmark) { + if ($bookmark->getCurrent()) { + $bookmarkConfig = $bookmark->getConfig(); + $existingConfig = $bookmarkConfig['views'][$bookmark->getIdentifier()]['data'] ?? null; + $currentConfig = $data[self::CURRENT_IDENTIFIER] ?? null; + if ($existingConfig && $currentConfig) { + if ($existingConfig['filters'] === $currentConfig['filters'] + && $existingConfig['positions'] !== $currentConfig['positions'] + ) { + $bookmarkConfig['views'][$bookmark->getIdentifier()]['data'] = $data[self::CURRENT_IDENTIFIER]; + $bookmark->setConfig($this->serializer->serialize($bookmarkConfig)); + $this->bookmarkRepository->save($bookmark); + } + } + break; + } + } + } } diff --git a/vendor/magento/module-ui/view/base/web/js/grid/controls/bookmarks/bookmarks.js b/vendor/magento/module-ui/view/base/web/js/grid/controls/bookmarks/bookmarks.js index ca3a78a7318b..365d60001110 100644 --- a/vendor/magento/module-ui/view/base/web/js/grid/controls/bookmarks/bookmarks.js +++ b/vendor/magento/module-ui/view/base/web/js/grid/controls/bookmarks/bookmarks.js @@ -58,7 +58,8 @@ define([ activeView: true, hasChanges: true, customLabel: true, - customVisible: true + customVisible: true, + isActiveIndexChanged: false }, listens: { activeIndex: 'onActiveIndexChange', @@ -105,9 +106,9 @@ define([ var data = this.getViewData(this.defaultIndex); if (!_.size(data) && (this.current.columns && this.current.positions)) { - this.setViewData(this.defaultIndex, this.current) - .saveView(this.defaultIndex); - this.defaultDefined = true; + this.setViewData(this.defaultIndex, this.current) + .saveView(this.defaultIndex); + this.defaultDefined = true; } return this; @@ -195,6 +196,7 @@ define([ .remove(viewPath) .removeStored(viewPath) .updateArray(); + this.isActiveIndexChanged = false; return this; }, @@ -446,7 +448,10 @@ define([ * @returns {Bookmarks} Chainable. */ saveState: function () { - this.store('current'); + if (!this.isActiveIndexChanged) { + this.store('current'); + } + this.isActiveIndexChanged = false; return this; }, @@ -554,6 +559,7 @@ define([ this.activeView = this.getActiveView(); this.updateActiveView(); this.store('activeIndex'); + this.isActiveIndexChanged = true; }, /** @@ -566,6 +572,15 @@ define([ if (!this.defaultDefined) { resolver(this.initDefaultView, this); } + + if (!_.isUndefined(this.activeView) + && !_.isUndefined(this.activeView.data) + && !_.isUndefined(this.current)) { + if (JSON.stringify(this.activeView.data.filters) === JSON.stringify(this.current.filters) + && JSON.stringify(this.activeView.data.positions) !== JSON.stringify(this.current.positions)) { + this.updateActiveView(); + } + } } }); });