|
| 1 | +/** |
| 2 | + * Copyright © Magento, Inc. All rights reserved. |
| 3 | + * See COPYING.txt for license details. |
| 4 | + */ |
| 5 | + |
| 6 | +define([ |
| 7 | + 'underscore', |
| 8 | + 'jquery' |
| 9 | +], function (_, $) { |
| 10 | + 'use strict'; |
| 11 | + |
| 12 | + var scriptSelector = 'script[type="text/x-magento-init"]', |
| 13 | + dataAttr = 'data-mage-init', |
| 14 | + virtuals = []; |
| 15 | + |
| 16 | + /** |
| 17 | + * Adds components to the virtual list. |
| 18 | + * |
| 19 | + * @param {Object} components |
| 20 | + */ |
| 21 | + function addVirtual(components) { |
| 22 | + virtuals.push({ |
| 23 | + el: false, |
| 24 | + data: components |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Merges provided data with a current data |
| 30 | + * of a elements' "data-mage-init" attribute. |
| 31 | + * |
| 32 | + * @param {Object} components - Object with components and theirs configuration. |
| 33 | + * @param {HTMLElement} elem - Element whose data should be modified. |
| 34 | + */ |
| 35 | + function setData(components, elem) { |
| 36 | + var data = elem.getAttribute(dataAttr); |
| 37 | + |
| 38 | + data = data ? JSON.parse(data) : {}; |
| 39 | + _.each(components, function (obj, key) { |
| 40 | + if (_.has(obj, 'mixins')) { |
| 41 | + data[key] = data[key] || {}; |
| 42 | + data[key].mixins = data[key].mixins || []; |
| 43 | + data[key].mixins = data[key].mixins.concat(obj.mixins); |
| 44 | + delete obj.mixins; |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + data = $.extend(true, data, components); |
| 49 | + data = JSON.stringify(data); |
| 50 | + elem.setAttribute(dataAttr, data); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Search for the elements by privded selector and extends theirs data. |
| 55 | + * |
| 56 | + * @param {Object} components - Object with components and theirs configuration. |
| 57 | + * @param {String} selector - Selector for the elements. |
| 58 | + */ |
| 59 | + function processElems(components, selector) { |
| 60 | + var elems, |
| 61 | + iterator; |
| 62 | + |
| 63 | + if (selector === '*') { |
| 64 | + addVirtual(components); |
| 65 | + |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + elems = document.querySelectorAll(selector); |
| 70 | + iterator = setData.bind(null, components); |
| 71 | + |
| 72 | + _.toArray(elems).forEach(iterator); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parses content of a provided script node. |
| 77 | + * Note: node will be removed from DOM. |
| 78 | + * |
| 79 | + * @param {HTMLScriptElement} node - Node to be processed. |
| 80 | + * @returns {Object} |
| 81 | + */ |
| 82 | + function getNodeData(node) { |
| 83 | + var data = node.textContent; |
| 84 | + |
| 85 | + node.parentNode.removeChild(node); |
| 86 | + |
| 87 | + return JSON.parse(data); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Parses 'script' tags with a custom type attribute and moves it's data |
| 92 | + * to a 'data-mage-init' attribute of an element found by provided selector. |
| 93 | + * Note: All found script nodes will be removed from DOM. |
| 94 | + * |
| 95 | + * @returns {Array} An array of components not assigned to the specific element. |
| 96 | + * |
| 97 | + * @example Sample declaration. |
| 98 | + * <script type="text/x-magento-init"> |
| 99 | + * { |
| 100 | + * "body": { |
| 101 | + * "path/to/component": {"foo": "bar"} |
| 102 | + * } |
| 103 | + * } |
| 104 | + * </script> |
| 105 | + * |
| 106 | + * @example Providing data without selector. |
| 107 | + * { |
| 108 | + * "*": { |
| 109 | + * "path/to/component": {"bar": "baz"} |
| 110 | + * } |
| 111 | + * } |
| 112 | + */ |
| 113 | + return function () { |
| 114 | + var nodes = document.querySelectorAll(scriptSelector); |
| 115 | + |
| 116 | + _.toArray(nodes) |
| 117 | + .map(getNodeData) |
| 118 | + .forEach(function (item) { |
| 119 | + _.each(item, processElems); |
| 120 | + }); |
| 121 | + |
| 122 | + return virtuals.splice(0, virtuals.length); |
| 123 | + }; |
| 124 | +}); |
0 commit comments