WebOrbiton
v1.1.0.0

StocketBase

643 lines · 17.8 KB
  1. (function () {
  2. 'use strict';
  3. ​
  4. var THEME_KEY = 'stocketbase_theme';
  5. ​
  6. function applyTheme(theme) {
  7. document.body.setAttribute('data-theme', theme);
  8. }
  9. ​
  10. function initTheme() {
  11. var stored = localStorage.getItem(THEME_KEY);
  12. if (stored === 'dark' || stored === 'light') {
  13. applyTheme(stored);
  14. return;
  15. }
  16. var prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
  17. applyTheme(prefersDark ? 'dark' : 'light');
  18. }
  19. ​
  20. function toggleTheme() {
  21. var current = document.body.getAttribute('data-theme') || 'light';
  22. var next = current === 'dark' ? 'light' : 'dark';
  23. applyTheme(next);
  24. localStorage.setItem(THEME_KEY, next);
  25. }
  26. ​
  27. function initConsent() {
  28. var banner = document.getElementById('consent-banner');
  29. if (!banner) {
  30. return;
  31. }
  32. ​
  33. var consentKey = 'stocketbase_consent';
  34. var categories = ['analytics', 'marketing', 'preferences'];
  35. var acceptBtn = document.getElementById('consent-accept');
  36. var declineBtn = document.getElementById('consent-decline');
  37. var manageBtn = document.getElementById('consent-manage');
  38. var saveBtn = document.getElementById('consent-save');
  39. var options = document.getElementById('consent-options');
  40. var reopenBtn = document.getElementById('consent-reopen-btn');
  41. var ran = {};
  42. ​
  43. function allAs(value) {
  44. var state = {};
  45. categories.forEach(function (category) {
  46. state[category] = value;
  47. });
  48. return state;
  49. }
  50. ​
  51. function readConsent() {
  52. var raw = null;
  53. try {
  54. raw = localStorage.getItem(consentKey);
  55. } catch (error) {
  56. return null;
  57. }
  58. if (raw === 'accepted') {
  59. return allAs(true);
  60. }
  61. if (raw === 'declined') {
  62. return allAs(false);
  63. }
  64. if (!raw) {
  65. return null;
  66. }
  67. try {
  68. var parsed = JSON.parse(raw);
  69. if (!parsed || typeof parsed !== 'object') {
  70. return null;
  71. }
  72. var state = {};
  73. categories.forEach(function (category) {
  74. state[category] = parsed[category] === true;
  75. });
  76. return state;
  77. } catch (error) {
  78. return null;
  79. }
  80. }
  81. ​
  82. function writeConsent(state) {
  83. var record = { v: 2, at: Date.now() };
  84. categories.forEach(function (category) {
  85. record[category] = state[category] === true;
  86. });
  87. try {
  88. localStorage.setItem(consentKey, JSON.stringify(record));
  89. } catch (error) {
  90. }
  91. }
  92. ​
  93. function activate(node) {
  94. if (node.nodeType !== 1) {
  95. return node;
  96. }
  97. if (node.tagName === 'SCRIPT') {
  98. var fresh = document.createElement('script');
  99. Array.prototype.forEach.call(node.attributes, function (attr) {
  100. fresh.setAttribute(attr.name, attr.value);
  101. });
  102. if (fresh.src) {
  103. fresh.async = node.hasAttribute('async');
  104. }
  105. fresh.text = node.text;
  106. return fresh;
  107. }
  108. Array.prototype.forEach.call(node.querySelectorAll('script'), function (inner) {
  109. inner.parentNode.replaceChild(activate(inner), inner);
  110. });
  111. return node;
  112. }
  113. ​
  114. function insertInOrder(nodes, parent, anchor, done) {
  115. if (!nodes.length) {
  116. done();
  117. return;
  118. }
  119. var node = activate(nodes.shift());
  120. var waits = node.nodeType === 1 && node.tagName === 'SCRIPT' && node.src && !node.async && !node.defer;
  121. var next = function () {
  122. insertInOrder(nodes, parent, anchor, done);
  123. };
  124. if (waits) {
  125. node.addEventListener('load', next);
  126. node.addEventListener('error', next);
  127. }
  128. parent.insertBefore(node, anchor);
  129. if (!waits) {
  130. next();
  131. }
  132. }
  133. ​
  134. function runCategory(category) {
  135. if (ran[category]) {
  136. return;
  137. }
  138. ran[category] = true;
  139. document.querySelectorAll('template.consent-gated[data-category="' + category + '"]').forEach(function (template) {
  140. var nodes = Array.prototype.slice.call(template.content.cloneNode(true).childNodes);
  141. var parent = template.parentNode;
  142. insertInOrder(nodes, parent, template, function () {
  143. if (template.parentNode) {
  144. template.parentNode.removeChild(template);
  145. }
  146. });
  147. });
  148. }
  149. ​
  150. function apply(state) {
  151. var revoked = categories.some(function (category) {
  152. return ran[category] && !state[category];
  153. });
  154. writeConsent(state);
  155. if (revoked) {
  156. window.location.reload();
  157. return;
  158. }
  159. categories.forEach(function (category) {
  160. if (state[category]) {
  161. runCategory(category);
  162. }
  163. });
  164. closeBanner();
  165. }
  166. ​
  167. function syncCheckboxes(state) {
  168. if (!options) {
  169. return;
  170. }
  171. options.querySelectorAll('input[data-consent-category]').forEach(function (input) {
  172. input.checked = !!(state && state[input.dataset.consentCategory]);
  173. });
  174. }
  175. ​
  176. function setManageMode(on) {
  177. if (!options) {
  178. return;
  179. }
  180. options.hidden = !on;
  181. if (manageBtn) {
  182. manageBtn.hidden = on;
  183. }
  184. if (saveBtn) {
  185. saveBtn.hidden = !on;
  186. }
  187. }
  188. ​
  189. function openBanner(manage) {
  190. syncCheckboxes(readConsent());
  191. setManageMode(!!manage);
  192. banner.hidden = false;
  193. }
  194. ​
  195. function closeBanner() {
  196. banner.hidden = true;
  197. setManageMode(false);
  198. }
  199. ​
  200. var stored = readConsent();
  201. if (stored) {
  202. categories.forEach(function (category) {
  203. if (stored[category]) {
  204. runCategory(category);
  205. }
  206. });
  207. } else {
  208. openBanner(false);
  209. }
  210. ​
  211. if (acceptBtn) {
  212. acceptBtn.addEventListener('click', function () {
  213. apply(allAs(true));
  214. });
  215. }
  216. if (declineBtn) {
  217. declineBtn.addEventListener('click', function () {
  218. apply(allAs(false));
  219. });
  220. }
  221. if (manageBtn) {
  222. manageBtn.addEventListener('click', function () {
  223. setManageMode(true);
  224. });
  225. }
  226. if (saveBtn) {
  227. saveBtn.addEventListener('click', function () {
  228. var state = allAs(false);
  229. options.querySelectorAll('input[data-consent-category]').forEach(function (input) {
  230. state[input.dataset.consentCategory] = input.checked;
  231. });
  232. apply(state);
  233. });
  234. }
  235. if (reopenBtn) {
  236. reopenBtn.addEventListener('click', function () {
  237. openBanner(!!options);
  238. });
  239. }
  240. }
  241. ​
  242. function initPagination() {
  243. var list = document.getElementById('article-list');
  244. if (!list) {
  245. return;
  246. }
  247. ​
  248. var source = list.dataset.paginationSource;
  249. if (source !== 'home' && source !== 'category' && source !== 'search') {
  250. return;
  251. }
  252. ​
  253. var categorySlug = list.dataset.categorySlug || '';
  254. var searchQuery = list.dataset.searchQuery || '';
  255. var searchCategory = list.dataset.searchCategory || '';
  256. var searchTag = list.dataset.searchTag || '';
  257. var searchSort = list.dataset.searchSort || '';
  258. var loading = false;
  259. ​
  260. function fetchPage(page) {
  261. if (loading) {
  262. return;
  263. }
  264. loading = true;
  265. ​
  266. var url = 'products-fragment.php?source=' + encodeURIComponent(source) + '&page=' + encodeURIComponent(page);
  267. if (source === 'category') {
  268. url += '&slug=' + encodeURIComponent(categorySlug);
  269. }
  270. if (source === 'search') {
  271. url += '&q=' + encodeURIComponent(searchQuery);
  272. if (searchCategory && searchCategory !== '0') {
  273. url += '&category=' + encodeURIComponent(searchCategory);
  274. }
  275. if (searchTag && searchTag !== '0') {
  276. url += '&tag=' + encodeURIComponent(searchTag);
  277. }
  278. if (searchSort) {
  279. url += '&sort=' + encodeURIComponent(searchSort);
  280. }
  281. }
  282. ​
  283. fetch(url)
  284. .then(function (response) {
  285. if (!response.ok) {
  286. throw new Error('Request failed');
  287. }
  288. return response.text();
  289. })
  290. .then(function (html) {
  291. var temp = document.createElement('div');
  292. temp.innerHTML = html;
  293. ​
  294. var newCards = temp.querySelectorAll('.product-card');
  295. newCards.forEach(function (card) {
  296. list.appendChild(card);
  297. });
  298. ​
  299. var container = document.getElementById('pagination-container');
  300. if (container) {
  301. var newControl = temp.querySelector('.pagination-load-more, .pagination-infinite-marker, .pagination-end');
  302. container.innerHTML = '';
  303. if (newControl) {
  304. container.appendChild(newControl);
  305. bindControl(newControl);
  306. }
  307. }
  308. ​
  309. loading = false;
  310. })
  311. .catch(function () {
  312. loading = false;
  313. });
  314. }
  315. ​
  316. function bindControl(control) {
  317. if (!control) {
  318. return;
  319. }
  320. ​
  321. if (control.classList.contains('pagination-load-more')) {
  322. var btn = control.querySelector('.pagination-load-more-btn');
  323. if (btn) {
  324. btn.addEventListener('click', function () {
  325. fetchPage(control.dataset.nextPage);
  326. });
  327. }
  328. return;
  329. }
  330. ​
  331. if (control.classList.contains('pagination-infinite-marker')) {
  332. if (!('IntersectionObserver' in window)) {
  333. return;
  334. }
  335. var observer = new IntersectionObserver(function (entries) {
  336. entries.forEach(function (entry) {
  337. if (entry.isIntersecting) {
  338. observer.disconnect();
  339. fetchPage(control.dataset.nextPage);
  340. }
  341. });
  342. }, { rootMargin: '200px' });
  343. observer.observe(control);
  344. }
  345. }
  346. ​
  347. var container = document.getElementById('pagination-container');
  348. if (container) {
  349. bindControl(container.querySelector('.pagination-load-more, .pagination-infinite-marker'));
  350. }
  351. }
  352. ​
  353. function initBrandShortName() {
  354. var brand = document.querySelector('.site-brand[data-short-name]');
  355. var header = document.querySelector('.site-header');
  356. ​
  357. if (!brand || !header) {
  358. return;
  359. }
  360. ​
  361. function overflows() {
  362. return brand.scrollWidth > brand.clientWidth + 1 || header.scrollWidth > header.clientWidth + 1;
  363. }
  364. ​
  365. function update() {
  366. brand.classList.remove('use-short');
  367. if (overflows()) {
  368. brand.classList.add('use-short');
  369. }
  370. }
  371. ​
  372. update();
  373. window.addEventListener('resize', update);
  374. window.addEventListener('load', update);
  375. }
  376. ​
  377. function initMobileNav() {
  378. var toggle = document.getElementById('site-menu-toggle');
  379. var nav = document.getElementById('site-nav');
  380. var backdrop = document.getElementById('site-nav-backdrop');
  381. var header = document.querySelector('.site-header');
  382. ​
  383. if (!toggle || !nav || !backdrop) {
  384. return;
  385. }
  386. ​
  387. var lockedScrollY = 0;
  388. ​
  389. function openNav() {
  390. nav.classList.add('is-open');
  391. backdrop.classList.add('is-open');
  392. toggle.setAttribute('aria-expanded', 'true');
  393. lockedScrollY = window.scrollY || window.pageYOffset || 0;
  394. document.body.style.position = 'fixed';
  395. document.body.style.top = -lockedScrollY + 'px';
  396. document.body.style.left = '0';
  397. document.body.style.right = '0';
  398. if (header) {
  399. header.classList.add('site-nav-drawer-open');
  400. }
  401. }
  402. ​
  403. function closeNav() {
  404. nav.classList.remove('is-open');
  405. backdrop.classList.remove('is-open');
  406. toggle.setAttribute('aria-expanded', 'false');
  407. document.body.style.position = '';
  408. document.body.style.top = '';
  409. document.body.style.left = '';
  410. document.body.style.right = '';
  411. window.scrollTo(0, lockedScrollY);
  412. if (header) {
  413. header.classList.remove('site-nav-drawer-open');
  414. }
  415. }
  416. ​
  417. toggle.addEventListener('click', function () {
  418. if (nav.classList.contains('is-open')) {
  419. closeNav();
  420. } else {
  421. openNav();
  422. }
  423. });
  424. ​
  425. backdrop.addEventListener('click', closeNav);
  426. ​
  427. nav.querySelectorAll('a').forEach(function (link) {
  428. link.addEventListener('click', closeNav);
  429. });
  430. ​
  431. window.addEventListener('resize', function () {
  432. if (window.innerWidth > 860) {
  433. closeNav();
  434. }
  435. });
  436. }
  437. ​
  438. function initShare() {
  439. var toggle = document.getElementById('share-toggle-btn');
  440. var panel = document.getElementById('share-panel');
  441. var copyBtn = document.getElementById('share-copy-btn');
  442. ​
  443. if (!toggle || !panel) {
  444. return;
  445. }
  446. ​
  447. function closePanel() {
  448. panel.classList.remove('is-open');
  449. toggle.setAttribute('aria-expanded', 'false');
  450. }
  451. ​
  452. function openPanel() {
  453. panel.classList.add('is-open');
  454. toggle.setAttribute('aria-expanded', 'true');
  455. }
  456. ​
  457. toggle.addEventListener('click', function (event) {
  458. event.stopPropagation();
  459. if (panel.classList.contains('is-open')) {
  460. closePanel();
  461. } else {
  462. openPanel();
  463. }
  464. });
  465. ​
  466. document.addEventListener('click', function (event) {
  467. if (panel.classList.contains('is-open') && !panel.contains(event.target) && event.target !== toggle) {
  468. closePanel();
  469. }
  470. });
  471. ​
  472. document.addEventListener('keydown', function (event) {
  473. if (event.key === 'Escape') {
  474. closePanel();
  475. }
  476. });
  477. ​
  478. if (copyBtn) {
  479. copyBtn.addEventListener('click', function () {
  480. var url = copyBtn.getAttribute('data-share-url') || window.location.href;
  481. var label = copyBtn.querySelector('.js-share-copy-label');
  482. var defaultText = copyBtn.getAttribute('data-default-text') || '';
  483. var copiedText = copyBtn.getAttribute('data-copied-text') || '';
  484. ​
  485. function showCopied() {
  486. if (label && copiedText) {
  487. label.textContent = copiedText;
  488. setTimeout(function () {
  489. label.textContent = defaultText;
  490. }, 2000);
  491. }
  492. }
  493. ​
  494. if (navigator.clipboard && navigator.clipboard.writeText) {
  495. navigator.clipboard.writeText(url).then(showCopied);
  496. } else {
  497. var tempInput = document.createElement('input');
  498. tempInput.value = url;
  499. document.body.appendChild(tempInput);
  500. tempInput.select();
  501. document.execCommand('copy');
  502. document.body.removeChild(tempInput);
  503. showCopied();
  504. }
  505. });
  506. }
  507. }
  508. ​
  509. function initAppBanner() {
  510. var banner = document.getElementById('app-banner');
  511. if (!banner) {
  512. return;
  513. }
  514. ​
  515. var DISMISS_KEY = 'stocketbase_app_banner_dismissed';
  516. ​
  517. try {
  518. if (localStorage.getItem(DISMISS_KEY) === '1') {
  519. banner.remove();
  520. return;
  521. }
  522. } catch (e) {}
  523. ​
  524. var userAgent = navigator.userAgent || '';
  525. var isAndroid = /Android/i.test(userAgent);
  526. var isIOS = /iPhone|iPad|iPod/i.test(userAgent);
  527. var androidUrl = banner.getAttribute('data-android-url') || '';
  528. var iosUrl = banner.getAttribute('data-ios-url') || '';
  529. ​
  530. var targetUrl = '';
  531. if (isAndroid && androidUrl) {
  532. targetUrl = androidUrl;
  533. } else if (isIOS && iosUrl) {
  534. targetUrl = iosUrl;
  535. }
  536. ​
  537. if (!targetUrl) {
  538. banner.remove();
  539. return;
  540. }
  541. ​
  542. var link = document.getElementById('app-banner-link');
  543. if (link) {
  544. link.setAttribute('href', targetUrl);
  545. }
  546. ​
  547. banner.classList.add('is-visible');
  548. ​
  549. var closeBtn = document.getElementById('app-banner-close');
  550. if (closeBtn) {
  551. closeBtn.addEventListener('click', function () {
  552. banner.classList.remove('is-visible');
  553. try {
  554. localStorage.setItem(DISMISS_KEY, '1');
  555. } catch (e) {}
  556. });
  557. }
  558. }
  559. ​
  560. function initBackToTop() {
  561. var button = document.getElementById('back-to-top');
  562. if (!button) {
  563. return;
  564. }
  565. ​
  566. var label = button.querySelector('.back-to-top-progress');
  567. var content = button.getAttribute('data-progress') === '1' ? document.querySelector('.article-content') : null;
  568. var template = button.getAttribute('data-left-text') || '{percent}%';
  569. var reduceMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  570. var ticking = false;
  571. ​
  572. if (content && label) {
  573. button.classList.add('has-progress');
  574. label.hidden = false;
  575. }
  576. ​
  577. function update() {
  578. ticking = false;
  579. ​
  580. var visible = (window.pageYOffset || 0) > 400;
  581. button.classList.toggle('is-visible', visible);
  582. button.tabIndex = visible ? 0 : -1;
  583. ​
  584. if (content && label) {
  585. var rect = content.getBoundingClientRect();
  586. var read = rect.height > 0 ? Math.min(1, Math.max(0, (window.innerHeight - rect.top) / rect.height)) : 1;
  587. var percentRead = Math.round(read * 100);
  588. label.textContent = template.replace('{percent}', String(100 - percentRead));
  589. button.style.setProperty('--btt-read', percentRead + '%');
  590. }
  591. }
  592. ​
  593. function schedule() {
  594. if (!ticking) {
  595. ticking = true;
  596. requestAnimationFrame(update);
  597. }
  598. }
  599. ​
  600. window.addEventListener('scroll', schedule, { passive: true });
  601. window.addEventListener('resize', schedule);
  602. button.addEventListener('click', function () {
  603. window.scrollTo({ top: 0, behavior: reduceMotion ? 'auto' : 'smooth' });
  604. });
  605. update();
  606. }
  607. ​
  608. document.addEventListener('DOMContentLoaded', function () {
  609. initTheme();
  610. initBackToTop();
  611. var toggleBtn = document.getElementById('theme-toggle-btn');
  612. if (toggleBtn) {
  613. toggleBtn.addEventListener('click', toggleTheme);
  614. }
  615. initConsent();
  616. initPagination();
  617. initMobileNav();
  618. initBrandShortName();
  619. initShare();
  620. initAppBanner();
  621. });
  622. })();
  623. ​
  624. (function () {
  625. var switcher = document.querySelector('[data-language-switcher]');
  626. if (!switcher) {
  627. return;
  628. }
  629. ​
  630. document.addEventListener('click', function (event) {
  631. if (switcher.open && !switcher.contains(event.target)) {
  632. switcher.open = false;
  633. }
  634. });
  635. ​
  636. document.addEventListener('keydown', function (event) {
  637. if (event.key === 'Escape' && switcher.open) {
  638. switcher.open = false;
  639. switcher.querySelector('summary').focus();
  640. }
  641. });
  642. })();
  643. ​