";
html += '
';
html += '
' + escapeHtml(product.title) + "
";
if (multi) {
html += '
';
html += '';
html += '' + money(first.price) + "
";
html += '
';
variants.forEach(function (variant) {
var active = variant.id === first.id;
html += '";
});
html += '
";
} else {
html += '
';
html += '
';
html += '';
html += '' + money(first.price) + "
";
}
html += '
';
html += '
view details';
html += "
";
return html;
}
function reinitWidget(root) {
delete root.dataset.sbInitialized;
var fire = function () {
document.dispatchEvent(
new CustomEvent("shopify:section:load", { bubbles: true })
);
};
fire();
window.setTimeout(fire, 200);
window.setTimeout(fire, 800);
}
function applyEmbeddedDesign(root, bootEl) {
if (!root || !bootEl) return;
for (var i = 0; i < bootEl.style.length; i += 1) {
var prop = bootEl.style[i];
if (prop.indexOf("--sb-") === 0) {
root.style.setProperty(prop, bootEl.style.getPropertyValue(prop));
}
}
try {
var vars = JSON.parse(bootEl.getAttribute("data-design-vars") || "{}");
Object.keys(vars).forEach(function (name) {
if (name.indexOf("--sb-") === 0 && vars[name]) {
root.style.setProperty(name, String(vars[name]));
}
});
} catch (error) {
console.warn("[SupaBundles] embedded design", error);
}
try {
var fonts = JSON.parse(bootEl.getAttribute("data-font-stylesheets") || "[]");
fonts.forEach(function (href) {
if (
typeof href !== "string" ||
href.indexOf("https://fonts.googleapis.com/css2?") !== 0
) {
return;
}
if (document.querySelector('link[data-sb-font="' + href + '"]')) return;
var link = document.createElement("link");
link.rel = "stylesheet";
link.href = href;
link.setAttribute("data-sb-font", href);
document.head.appendChild(link);
});
} catch (error) {
console.warn("[SupaBundles] embedded fonts", error);
}
}
function whenReady(callback) {
if (window.SupaBundlesPlacement) {
callback();
return;
}
var attempts = 0;
var timer = window.setInterval(function () {
attempts += 1;
if (window.SupaBundlesPlacement || attempts > 40) {
window.clearInterval(timer);
callback();
}
}, 50);
}
async function boot() {
moveIntoMain();
var root = mount.querySelector("[data-supabundles]");
var empty = mount.querySelector("[data-sb-empty]");
var grid = mount.querySelector("[data-sb-grid]");
if (!root || !grid) return;
var params = new URLSearchParams(window.location.search);
var bootEl = document.getElementById("sb-preview-boot");
var collectionHandle =
params.get("sb_collection") ||
(bootEl && bootEl.getAttribute("data-collection-handle")) ||
"";
var collectionId =
params.get("sb_collection_id") ||
(bootEl && bootEl.getAttribute("data-collection-id")) ||
"";
var bundleHandle =
params.get("sb_bundle") ||
(bootEl && bootEl.getAttribute("data-bundle-handle")) ||
"";
var proxy =
params.get("sb_proxy") ||
(bootEl && bootEl.getAttribute("data-proxy-subpath")) ||
(root.dataset.configUrl || "").split("/")[2] ||
"supabundles";
root.dataset.bundleCollectionId = (collectionId || "").split("/").pop() || collectionId;
root.dataset.bundleHandle = bundleHandle || collectionHandle;
root.dataset.configUrl = "/apps/" + proxy + "/config?preview=1";
applyEmbeddedDesign(root, bootEl);
var cfgEl = document.getElementById("sb-preview-config");
if (cfgEl) {
try {
window.__SBC = JSON.parse(cfgEl.textContent || "");
} catch (error) {
window.__SBC = null;
}
}
if (!collectionHandle) {
if (empty) {
empty.innerHTML =
"Open Try in store from SupaBundles Admin to load an offer.
";
}
return;
}
try {
var response = await fetch(
"/collections/" + encodeURIComponent(collectionHandle) + "/products.json?limit=100",
{ credentials: "same-origin" }
);
if (!response.ok) throw new Error("products " + response.status);
var payload = await response.json();
var products = payload.products || [];
if (!products.length) {
if (empty) empty.innerHTML = "This collection has no products.
";
return;
}
grid.innerHTML = products.map(renderCard).join("");
grid.hidden = false;
if (empty) empty.hidden = true;
root.removeAttribute("data-sb-boot-pending");
delete root.dataset.sbInitialized;
whenReady(function () {
reinitWidget(root);
});
} catch (error) {
console.error("[SupaBundles] preview boot failed", error);
if (empty) {
empty.innerHTML =
"Could not load products for this offer. Check the collection handle and try again.
";
}
}
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", boot);
} else {
boot();
}
})();