Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

115 wiersze
3.6 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore',
  7. 'jquery',
  8. './scripts'
  9. ], function (_, $, processScripts) {
  10. 'use strict';
  11. var dataAttr = 'data-mage-init',
  12. nodeSelector = '[' + dataAttr + ']';
  13. /**
  14. * Initializes components assigned to a specified element via data-* attribute.
  15. *
  16. * @param {HTMLElement} el - Element to initialize components with.
  17. * @param {Object|String} config - Initial components' config.
  18. * @param {String} component - Components' path.
  19. */
  20. function init(el, config, component) {
  21. require([component], function (fn) {
  22. var $el;
  23. if (typeof fn === 'object') {
  24. fn = fn[component].bind(fn);
  25. }
  26. if (_.isFunction(fn)) {
  27. fn = fn.bind(null, config, el);
  28. } else {
  29. $el = $(el);
  30. if ($el[component]) {
  31. // eslint-disable-next-line jquery-no-bind-unbind
  32. fn = $el[component].bind($el, config);
  33. }
  34. }
  35. // Init module in separate task to prevent blocking main thread.
  36. setTimeout(fn);
  37. }, function (error) {
  38. if ('console' in window && typeof window.console.error === 'function') {
  39. console.error(error);
  40. }
  41. return true;
  42. });
  43. }
  44. /**
  45. * Parses elements 'data-mage-init' attribute as a valid JSON data.
  46. * Note: data-mage-init attribute will be removed.
  47. *
  48. * @param {HTMLElement} el - Element whose attribute should be parsed.
  49. * @returns {Object}
  50. */
  51. function getData(el) {
  52. var data = el.getAttribute(dataAttr);
  53. el.removeAttribute(dataAttr);
  54. return {
  55. el: el,
  56. data: JSON.parse(data)
  57. };
  58. }
  59. return {
  60. /**
  61. * Initializes components assigned to HTML elements via [data-mage-init].
  62. *
  63. * @example Sample 'data-mage-init' declaration.
  64. * data-mage-init='{"path/to/component": {"foo": "bar"}}'
  65. */
  66. apply: function (context) {
  67. var virtuals = processScripts(!context ? document : context),
  68. nodes = document.querySelectorAll(nodeSelector);
  69. _.toArray(nodes)
  70. .map(getData)
  71. .concat(virtuals)
  72. .forEach(function (itemContainer) {
  73. var element = itemContainer.el;
  74. _.each(itemContainer.data, function (obj, key) {
  75. if (obj.mixins) {
  76. require(obj.mixins, function () { //eslint-disable-line max-nested-callbacks
  77. var i, len;
  78. for (i = 0, len = arguments.length; i < len; i++) {
  79. $.extend(
  80. true,
  81. itemContainer.data[key],
  82. arguments[i](itemContainer.data[key], element)
  83. );
  84. }
  85. delete obj.mixins;
  86. init.call(null, element, obj, key);
  87. });
  88. } else {
  89. init.call(null, element, obj, key);
  90. }
  91. }
  92. );
  93. });
  94. },
  95. applyFor: init
  96. };
  97. });