switcher.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. define([
  2. 'jquery',
  3. 'Magento_Ui/js/modal/confirm',
  4. 'domReady!'
  5. ], function (jQuery, confirm) {
  6. 'use strict';
  7. /**
  8. * Init
  9. * @param {Int} useConfirm
  10. * @param {String} getContent
  11. * @param {Int} objVal
  12. * @param {Boolean} isIframe
  13. * @param {String} switchUrl
  14. */
  15. function init(useConfirm, getContent, objVal, isIframe, switchUrl) {
  16. var scopeSwitcherHandler;
  17. (function ($) {
  18. var $storesList = $('[data-role=stores-list]');
  19. $storesList.on('click', '[data-value]', function (event) {
  20. var val = $(event.target).data('value'),
  21. role = $(event.target).data('role'),
  22. switcher = $('[data-role=' + role + ']');
  23. event.preventDefault();
  24. if (!switcher.val() || val !== switcher.val()) {
  25. switcher.val(val).trigger('change'); // Set the value & trigger event
  26. }
  27. });
  28. })(jQuery);
  29. /**
  30. * Switch scope
  31. * @param {Object} obj
  32. */
  33. function switchScope(obj) {
  34. var switcher = jQuery(obj),
  35. scopeId = switcher.val(),
  36. scopeParams = '',
  37. switcherParams;
  38. /**
  39. * Reload
  40. */
  41. function reload() {
  42. var url;
  43. if (!isIframe) {
  44. url = switchUrl + scopeParams;
  45. window.location.href = url;
  46. } else {
  47. jQuery('#preview_selected_store').val(scopeId);
  48. jQuery('#preview_form').submit();
  49. jQuery('.store-switcher .dropdown-menu li a').each(function () {
  50. var $this = jQuery(this);
  51. if ($this.data('role') === 'store-view-id' && $this.data('value') === scopeId) {
  52. jQuery('#store-change-button').html($this.text());
  53. }
  54. });
  55. jQuery('#store-change-button').click();
  56. }
  57. }
  58. if (scopeId) {
  59. scopeParams = switcher.data('param') + '/' + scopeId + '/';
  60. }
  61. if (obj.switchParams) {
  62. scopeParams += obj.switchParams;
  63. }
  64. if (typeof scopeSwitcherHandler !== 'undefined') {
  65. switcherParams = {
  66. scopeId: scopeId,
  67. scopeParams: scopeParams,
  68. useConfirm: useConfirm
  69. };
  70. scopeSwitcherHandler(switcherParams);
  71. } else if (useConfirm) {
  72. confirm({
  73. content: getContent,
  74. actions: {
  75. /**
  76. * Confirm
  77. */
  78. confirm: function () {
  79. reload();
  80. },
  81. /**
  82. * Cancel
  83. */
  84. cancel: function () {
  85. obj.value = objVal;
  86. }
  87. }
  88. });
  89. } else {
  90. reload();
  91. }
  92. }
  93. window.scopeSwitcherHandler = scopeSwitcherHandler;
  94. window.switchScope = switchScope;
  95. }
  96. /**
  97. *
  98. * @param {Object} switcher
  99. */
  100. return function (switcher) {
  101. init(
  102. switcher.getUseConfirm,
  103. switcher.getContent,
  104. switcher.objVal,
  105. switcher.isUsingIframe,
  106. switcher.getSwitchUrl
  107. );
  108. };
  109. });