From 7e47efddd9df6ff0153365aa260ff2a0bdb3f565 Mon Sep 17 00:00:00 2001 From: TuNa Date: Sat, 13 Mar 2021 18:37:12 +0700 Subject: [PATCH] Seperate js out template to avoid render issue update update update improve script load update update update fix static tests fix static tests update update update update update update update update update update update up update fix wrong typo --- .../Magento/Backend/Block/Store/Switcher.php | 4 +- .../adminhtml/templates/store/switcher.phtml | 324 +++++++----------- .../view/adminhtml/web/js/store-switcher.js | 127 +++++++ 3 files changed, 247 insertions(+), 208 deletions(-) create mode 100644 app/code/Magento/Backend/view/adminhtml/web/js/store-switcher.js diff --git a/app/code/Magento/Backend/Block/Store/Switcher.php b/app/code/Magento/Backend/Block/Store/Switcher.php index 1cf58666a47d9..2ae929f9b7398 100644 --- a/app/code/Magento/Backend/Block/Store/Switcher.php +++ b/app/code/Magento/Backend/Block/Store/Switcher.php @@ -3,6 +3,7 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); namespace Magento\Backend\Block\Store; @@ -114,7 +115,8 @@ protected function _construct() { parent::_construct(); - $this->setUseConfirm(true); + $this->setUseConfirm($this->hasData('use_confirm') ? (bool)$this->getData('use_confirm') : true); + $this->setUseAjax(true); $this->setShowManageStoresLink(0); diff --git a/app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml b/app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml index 72a128a23432a..d2bdacda47ebe 100644 --- a/app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml +++ b/app/code/Magento/Backend/view/adminhtml/templates/store/switcher.phtml @@ -8,222 +8,132 @@ /** @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer */ ?> getWebsites()): ?> -
- escapeHtml(__('Scope:')) ?> - + getHintHtml() ?>
- getHintHtml() ?> - - - getUseConfirm(); - $scriptString = << diff --git a/app/code/Magento/Backend/view/adminhtml/web/js/store-switcher.js b/app/code/Magento/Backend/view/adminhtml/web/js/store-switcher.js new file mode 100644 index 0000000000000..2f48000c0d1af --- /dev/null +++ b/app/code/Magento/Backend/view/adminhtml/web/js/store-switcher.js @@ -0,0 +1,127 @@ +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +define([ + 'jquery' +], function ($) { + 'use strict'; + + /** + * @param {Object} storeSwitchConfig + */ + return function (storeSwitchConfig) { + var scopeSwitcherHandler; + + (function () { + var storesList = $('[data-role=stores-list]'); + + storesList.on('click', '[data-value]', function (event) { + var val = $(event.target).data('value'), + role = $(event.target).data('role'), + switcher = $('[data-role=' + role + ']'); + + event.preventDefault(); + + if (!switcher.val() || val !== switcher.val()) { + + /* Set the value & trigger event */ + switcher.val(val).trigger('change'); + } + }); + })($); + + /** + * Switch store scope + * + * @param {Object} obj + * @return void + */ + function switchScope(obj) { + var switcher = $(obj), + scopeId = switcher.val(), + scopeParams = '', + switcherParams = {}; + + if (scopeId) { + scopeParams = switcher.data('param') + '/' + scopeId + '/'; + } + + if (obj.switchParams) { + scopeParams += obj.switchParams; + } + + /** + * Reload function for switcher + */ + function reload() { + var url; + + if (!storeSwitchConfig.isUsingIframe) { + + if (storeSwitchConfig.switchUrl && storeSwitchConfig.switchUrl.length > 0) { + url = storeSwitchConfig.switchUrl + scopeParams; + + /* eslint-disable no-undef */ + setLocation(url); + } + + } else { + $('#preview_selected_store').val(scopeId); + $('#preview_form').submit(); + + $('.store-switcher .dropdown-menu li a').each(function () { + var $this = $(this); + + if ($this.data('role') === 'store-view-id' && $this.data('value') === scopeId) { + $('#store-change-button').html($this.text()); + } + }); + + $('#store-change-button').click(); + } + } + + if (typeof scopeSwitcherHandler !== 'undefined') { + switcherParams = { + scopeId: scopeId, + scopeParams: scopeParams, + useConfirm: storeSwitchConfig.useConfirm + }; + + scopeSwitcherHandler(switcherParams); + } else if (storeSwitchConfig.useConfirm) { + require([ + 'Magento_Ui/js/modal/confirm', + 'mage/translate' + ], function (confirm, $t) { + confirm({ + content: $t('Please confirm scope switching. All data that hasn\'t been saved will be lost.'), + actions: { + + /** + * Confirm action + */ + confirm: function () { + reload(); + }, + + /** + * Cancel action + */ + cancel: function () { + obj.value = storeSwitchConfig.storeId ? storeSwitchConfig.storeId : ''; + } + } + }); + }); + } else { + reload(); + } + } + + window.scopeSwitcherHandler = scopeSwitcherHandler; + window.switchScope = switchScope; + }; +});