Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

96 lignes
2.4 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'mage/apply/main'
  8. ], function ($, mage) {
  9. 'use strict';
  10. /**
  11. * Main namespace for Magento extensions
  12. * @type {Object}
  13. */
  14. $.mage = $.mage || {};
  15. /**
  16. * Plugin mage, initialize components on elements
  17. * @param {String} name - Components' path.
  18. * @param {Object} config - Components' config.
  19. * @returns {JQuery} Chainable.
  20. */
  21. $.fn.mage = function (name, config) {
  22. config = config || {};
  23. this.each(function (index, el) {
  24. mage.applyFor(el, config, name);
  25. });
  26. return this;
  27. };
  28. $.extend($.mage, {
  29. /**
  30. * Handle all components declared via data attribute
  31. * @return {Object} $.mage
  32. */
  33. init: function () {
  34. mage.apply();
  35. return this;
  36. },
  37. /**
  38. * Method handling redirects and page refresh
  39. * @param {String} url - redirect URL
  40. * @param {(undefined|String)} type - 'assign', 'reload', 'replace'
  41. * @param {(undefined|Number)} timeout - timeout in milliseconds before processing the redirect or reload
  42. * @param {(undefined|Boolean)} forced - true|false used for 'reload' only
  43. */
  44. redirect: function (url, type, timeout, forced) {
  45. var _redirect;
  46. forced = !!forced;
  47. timeout = timeout || 0;
  48. type = type || 'assign';
  49. /**
  50. * @private
  51. */
  52. _redirect = function () {
  53. window.location[type](type === 'reload' ? forced : url);
  54. };
  55. timeout ? setTimeout(_redirect, timeout) : _redirect();
  56. },
  57. /**
  58. * Checks if provided string is a valid selector.
  59. * @param {String} selector - Selector to check.
  60. * @returns {Boolean}
  61. */
  62. isValidSelector: function (selector) {
  63. try {
  64. document.querySelector(selector);
  65. return true;
  66. } catch (e) {
  67. return false;
  68. }
  69. }
  70. });
  71. /**
  72. * Init components inside of dynamically updated elements
  73. */
  74. $(document).on('contentUpdated', 'body', function () {
  75. if (mage) {
  76. mage.apply();
  77. }
  78. });
  79. return $.mage;
  80. });