import { getCustomizationForShop, getNecessaryPublicSettingsForShop, } from "./apiFunctions.js"; import { insertButtonsOnProductPage } from "./domManiuplationAndHandling.js"; import { updateVariantAndHandleChangeOnProductPage } from "./variantIdentificationAndModification.js"; import { mainFunctionOnCollectionPage } from "./collectionPageFunctions.js"; import { createAndAddButton, createAndAddConfirmationModal, createAndAddErrorModal, createAndAddForm, styleEverything, } from "./domCreation.js"; import { getSelectiveDisplayCalculations } from "./helperFunctions.js"; // -------------------- MAIN -------------------- document.addEventListener("DOMContentLoaded", async function () { // Keep track of which version is currently loaded console.log("RR version: 10 Jun, 01:44"); if (!window.backInStock) { console.warn("RR: Product data not found"); return; } window.backInStock.initialLoadFinished = false; if (Shopify.designMode) { // Get shop customization from api (which communicates with own DB) const shopCustomization = await getCustomizationForShop(); window.backInStock.shopCustomization = shopCustomization; if ( window.backInStock.shopCustomization?.storeSpecificCustomization ?.adminStopAllNotifications === true ) { const isButtonShown = window.backInStock.shopCustomization?.storeSpecificCustomization ?.adminShowButton === true; // Check if the banner was closed within the last 30 minutes const bannerClosedAt = localStorage.getItem("bannerClosedAt"); const THIRTY_MINUTES = 30 * 60 * 1000; if ( !bannerClosedAt || Date.now() - parseInt(bannerClosedAt) > THIRTY_MINUTES ) { showBanner(); } function showBanner() { const banner = document.createElement("div"); banner.textContent = isButtonShown ? "Restock alerts not being sent. Upgrade to a paid plan in Restock Rooster app or contact support@restockrooster.com" : "Notify Me button not showing due to alert limit exceeded. Upgrade to a paid plan in Restock Rooster app or contact support@restockrooster.com"; // Style the banner banner.style.position = "fixed"; banner.style.top = "0"; banner.style.left = "0"; banner.style.width = "100%"; banner.style.padding = "15px"; banner.style.backgroundColor = "red"; banner.style.color = "white"; banner.style.textAlign = "center"; banner.style.fontSize = "18px"; banner.style.zIndex = "2147483647"; // Makes sure it's on top of other elements // Create close button const closeButton = document.createElement("span"); closeButton.textContent = "✖"; closeButton.style.marginLeft = "20px"; closeButton.style.cursor = "pointer"; closeButton.style.color = "#fff"; // Add click event to close button closeButton.addEventListener("click", () => { banner.remove(); // Save the current time in localStorage localStorage.setItem("bannerClosedAt", Date.now().toString()); }); banner.appendChild(closeButton); // Insert the banner at the top of the body document.body.prepend(banner); } } } // ------------------------------------------- // if there are no variants that are out of stock, maybe we are on a collection page instead if ( !window?.backInStock?.productLiquidObject || !window?.backInStock?.productLiquidObject?.variants.some( (variant) => variant.available === false, ) ) { if (!window?.backInStock?.productsInCollectionLiquidObject) { // if we don't have a collection products object, then we don't do anything return false; } else { // we execute the code for the collection page window.backInStock.currentPage = "collection"; mainFunctionOnCollectionPage(); return; } } window.backInStock.currentPage = "product"; // if we are actually on a product page, we continue // Get shop customization from api (which communicates with own DB) const shopCustomization = await getCustomizationForShop(); window.backInStock.shopCustomization = shopCustomization; // if restock alerts are stopped and we aren't in design mode (theme editor), we don't do anything anymore if ( window.backInStock.shopCustomization?.storeSpecificCustomization ?.adminStopAllNotifications === true && window.backInStock.shopCustomization?.storeSpecificCustomization ?.adminShowButton !== true && !Shopify.designMode ) { console.log("RR: alerts stopped by admin, button not displayed anymore"); return; } // Get shop settings from api (similar to above) const shopPublicSettings = await getNecessaryPublicSettingsForShop(); window.backInStock.shopPublicSettings = shopPublicSettings; // if there is already our button on the page, don't do anything if ( shopCustomization?.storeSpecificCustomization?.preventLoadingTwice === true && Array.from(document.querySelectorAll(".back-in-stock-button"))?.length > 0 ) { window.backInStock.initialLoadFinished = true; return true; } const { variantsForWhichToShowButton } = getSelectiveDisplayCalculations({ productLiquidObject: window?.backInStock?.productLiquidObject, }); createAndAddErrorModal(); createAndAddConfirmationModal(); createAndAddForm({ variantsForWhichToShowButton, }); createAndAddButton(); styleEverything(); // TOIMPROVE: 4. Insert the button into the page. // This does NOT mean the button shows to the user. That depends on whether display: none or display: block, computed during showOrHideButtons() // initial variant detection on load updateVariantAndHandleChangeOnProductPage(); insertButtonsOnProductPage({ enableCustomPosition: shopCustomization?.buttonCustomization?.enableCustomPosition, customPositionReferenceElement: shopCustomization?.buttonCustomization?.customPositionReferenceElement, customPositionRelativePositionType: shopCustomization?.buttonCustomization ?.customPositionRelativePositionType, storeSpecificReferenceElementsIfAny: shopCustomization?.storeSpecificCustomization ?.referenceElementsSelectorForButtonInsertion, storeSpecificRelativePositionIfAny: shopCustomization?.storeSpecificCustomization ?.referenceElementsRelativePosition, backInStockButton: window.backInStock.backInStockButton, notifyForm: window.backInStock.notifyForm, }); // "hack" to unfocus all elements if need be if (shopCustomization?.storeSpecificCustomization) { if (shopCustomization.storeSpecificCustomization?.unfocusAllElements) { const temporaryElement = document.createElement("input"); temporaryElement.style.position = "fixed"; document.body.appendChild(temporaryElement); temporaryElement.focus(); document.body.removeChild(temporaryElement); } } window.backInStock.initialLoadFinished = true; });