PageSourceSearch

https://vkusblagosti.ru/wp-content/themes/bono_child/js/mobile-menu-actions-fallback.js

js vkusblagosti.ru collected 2026-10-05 00:04:01 UTC 3,707 bytes, 113 lines download raw bytes

1/**
2 * Если .mobile-menu-header-tools__actions пустой после клонирования nav в placeholder — вставляем ссылки из bonoChildMobileNavFallback (см. functions.php).
3 */
4(function () {
5  function payload() {
6    return typeof window.bonoChildMobileNavFallback === 'object' && window.bonoChildMobileNavFallback !== null
7      ? window.bonoChildMobileNavFallback
8      : null;
9  }
10
11  function getCookieValue(name) {
12    // Simple cookie reader for `document.cookie` format: "a=b; c=d".
13    var needle = encodeURIComponent(name) + '=';
14    var parts = ('; ' + document.cookie).split('; ' + needle);
15    if (parts.length < 2) {
16      return '';
17    }
18    return decodeURIComponent(parts.pop().split(';').shift());
19  }
20
21  function getFavoritesCountFromCookie() {
22    var raw = getCookieValue('b:favorites');
23    if (!raw) {
24      return 0;
25    }
26    try {
27      var decoded = JSON.parse(raw);
28      if (Array.isArray(decoded)) {
29        return decoded.length;
30      }
31      // Backward compatibility: cookie might contain object or primitive.
32      if (decoded && typeof decoded === 'object') {
33        return Object.keys(decoded).length;
34      }
35    } catch (e) {
36      // Ignore parse errors.
37    }
38    return 0;
39  }
40
41  function needsFill(box) {
42    return (
43      box &&
44      (!box.querySelector('a.js-header-favorite') || !box.querySelector('a.js-header-customer-account'))
45    );
46  }
47
48  function fillBox(box, d) {
49    if (!d || !d.favoriteUrl || !d.accountUrl || !needsFill(box)) {
50      return;
51    }
52    while (box.firstChild) {
53      box.removeChild(box.firstChild);
54    }
55    var fav = document.createElement('a');
56    fav.href = d.favoriteUrl;
57    fav.setAttribute('class', 'header-cart__link header-favorite header-favorite--vis js-header-favorite');
58    fav.title = d.favoriteTitle || '';
59
60    // If counter badge exists elsewhere (e.g. rendered by PHP but hidden on mobile),
61    // copy it into the placeholder link to keep the same UX.
62    var existingSup = document.querySelector('a.js-header-favorite > sup');
63    if (existingSup) {
64      fav.appendChild(existingSup.cloneNode(true));
65    } else {
66      // Fallback: calculate count from theme cookie and show it when > 0.
67      // (Theme server renders <sup> only when counter is enabled; copying above keeps behavior consistent.)
68      var count = getFavoritesCountFromCookie();
69      if (count > 0) {
70        var sup = document.createElement('sup');
71        sup.style.opacity = '1';
72        sup.textContent = String(count);
73        fav.appendChild(sup);
74      }
75    }
76
77    var acc = document.createElement('a');
78    acc.href = d.accountUrl;
79    acc.setAttribute('class', 'header-customer-account header-customer-account--vis js-header-customer-account');
80    acc.title = d.loggedIn ? d.accountTitle || '' : d.loginTitle || '';
81
82    var span = document.createElement('span');
83    span.setAttribute('class', 'header-customer-account__text');
84    span.textContent = d.loggedIn ? d.accountTextOk || '' : d.loginText || '';
85    acc.appendChild(span);
86
87    box.appendChild(fav);
88    box.appendChild(acc);
89  }
90
91  function run() {
92    var d = payload();
93    if (!d) {
94      return;
95    }
96    var nodes = document.querySelectorAll('.mobile-menu-header-tools__actions');
97    for (var i = 0; i < nodes.length; i++) {
98      fillBox(nodes[i], d);
99    }
100  }
101
102  if (document.readyState === 'loading') {
103    document.addEventListener('DOMContentLoaded', run);
104  } else {
105    run();
106  }
107
108  var ph = document.querySelector('.js-mobile-menu-placeholder');
109  if (ph && typeof MutationObserver !== 'undefined') {
110    var mo = new MutationObserver(run);
111    mo.observe(ph, { childList: true, subtree: true });
112  }
113})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.