dbxapp 4.1.3
CMS, Shop, Workflows und modulare Geschäftsanwendungen
Loading...
Searching...
No Matches
shop.js
Go to the documentation of this file.
1(function (window) {
2 "use strict";
3
4 const document = window.document;
5 const shop = window.dbxShop = window.dbxShop || {};
6
7 shop.version = "0.2.1";
8
9 shop.syncCartMenu = function (count) {
10 count = Math.max(0, parseInt(count, 10) || 0);
11 const language = String(document.documentElement.lang || "de").toLowerCase().slice(0, 2);
12 const labels = {
13 de: {
14 empty: "Warenkorb",
15 one: "1 Artikel im Warenkorb",
16 many: count + " Artikel im Warenkorb"
17 },
18 en: {
19 empty: "Cart",
20 one: "1 item in cart",
21 many: count + " items in cart"
22 },
23 es: {
24 empty: "Carrito",
25 one: "1 artículo en el carrito",
26 many: count + " artículos en el carrito"
27 }
28 };
29 const text = labels[language] || labels.de;
30 const label = count === 1 ? text.one : text.many;
31
32 document.querySelectorAll(".dbx-shop-cart-menu-link").forEach(link => {
33 const icon = link.querySelector(".dbx-shop-cart-menu-icon");
34 if (!icon) return;
35
36 let badge = icon.querySelector(".dbx-shop-cart-menu-badge");
37 if (count <= 0) {
38 if (badge) badge.remove();
39 link.setAttribute("title", text.empty);
40 return;
41 }
42
43 if (!badge) {
44 badge = document.createElement("span");
45 badge.className = "dbx-shop-cart-menu-badge";
46 icon.appendChild(badge);
47 }
48
49 badge.textContent = String(count);
50 badge.setAttribute("aria-label", label);
51 link.setAttribute("title", label);
52 });
53 };
54
55 shop.syncCartMarker = function (root) {
56 if (!root || !root.querySelector) return;
57
58 const marker = root.matches && root.matches("[data-dbx-shop-cart-count]")
59 ? root
60 : root.querySelector("[data-dbx-shop-cart-count]");
61
62 if (!marker) return;
63 shop.syncCartMenu(marker.getAttribute("data-dbx-shop-cart-count"));
64 };
65
66 shop.syncCartMarker(document);
67
68 function bindAjaxSync() {
69 if (shop.__ajaxSyncBound) return true;
70 if (!window.dbx || !window.dbx.event || typeof window.dbx.event.on !== "function") {
71 return false;
72 }
73
74 shop.__ajaxSyncBound = true;
75 window.dbx.event.on("ajax:after", data => {
76 shop.syncCartMarker((data && data.targetElement) || document);
77 });
78 return true;
79 }
80
81 if (!bindAjaxSync()) {
82 let attempts = 0;
83 const wait = window.setInterval(() => {
84 if (bindAjaxSync() || ++attempts > 80) {
85 window.clearInterval(wait);
86 }
87 }, 50);
88 }
89})(window);