(function () { if ( typeof window.CustomEvent === "function" ) return false; //If not IE function CustomEvent ( event, params ) { params = params || { bubbles: false, cancelable: false, detail: undefined }; var evt = document.createEvent( 'CustomEvent' ); evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); return evt; } CustomEvent.prototype = window.Event.prototype; window.CustomEvent = CustomEvent; // Create Element.remove() function if not exist if (!('remove' in Element.prototype)) { Element.prototype.remove = function() { if (this.parentNode) { this.parentNode.removeChild(this); } }; } })(); var tomitStorageSystem = { now: function() { var date = new Date().getTime(); return parseInt(date / 1000); }, get : function(key) { var entry = JSON.parse(localStorage.getItem(key)||"0"); if (!entry) return null; if (entry.ttl && entry.ttl + entry.now < tomitStorageSystem.now()) { localStorage.removeItem(key); return null; } return entry.value; }, set : function( key, value, ttl ) { localStorage.setItem( key, JSON.stringify({ ttl : ttl || 0, now : tomitStorageSystem.now(), value : value }) ); } }; var tomitProductInventoryInfo = { activeProduct: {}, buttonDisabled : false, originalButton : "", selectedVariantId: "", customerPos : "0,0", dispatchLoadedEvent : function() { var ev = new CustomEvent("tomitLoaded", { detail: { message:'TomIT Loaded' }, bubbles: true, cancelable: true }); document.dispatchEvent(ev); },dispatchProductLoadedEvent : function() { var ev = new CustomEvent("tomitProductLoaded", { detail: { message:'product Loaded' }, bubbles: true, cancelable: true }); document.dispatchEvent(ev); }, listInventory: function(t, useDropdown) { var n = document.createElement("ul"); n.id = "tomit_inventory_list"; var hasPickupOptions = false; var options = []; var selectoption; for (var o = 0; o < t.length; o++) { if(useDropdown){ if(t[o].pickupOption == true) { selectoption = document.createElement('option'); selectoption.text = t[o].name+' - '+t[o].available; selectoption.value = "pickup " + t[o].name; if(t[o].available <= 0) { selectoption.disabled = true; } options.push(selectoption); } if(t[o].pickupOption == true) { hasPickupOptions = true; } }else{ var e = document.createElement("li"); e.setAttribute('data-loc', t[o].id); var radioSelectBtn = ""; if(t[o].pickupOption == true) { hasPickupOptions = true; var pickupDisabled = ''; if(t[o].inStock == 0) { pickupDisabled = 'disabled'; } radioSelectBtn = ' '; e.innerHTML = "", n.appendChild(e) }else{ e.innerHTML = "" + t[o].name + ": " + t[o].available + "", n.appendChild(e) } } } if(hasPickupOptions){ if(useDropdown){ var e = document.createElement("select"); e.name = 'properties[Shipping Option]'; var firstOption = document.createElement('option'); firstOption.text = 'ship to me'; e.appendChild(firstOption); for (var i = 0; i < options.length; ++i) { e.appendChild(options[i]); } n.prepend(e); } else{ var e = document.createElement("li"); radioSelectBtn = ''; e.innerHTML = radioSelectBtn; n.prepend(e); } } return n }, listInventoryPopup: function(t, useDropdown) { var n = document.createElement("ul"); n.id = "tomit_inventory_list"; var hasPickupOptions = false; var options = []; var selectoption; for (var o = 0; o < t.length; o++) { if(useDropdown){ if(t[o].pickupOption == true) { selectoption = document.createElement('option'); selectoption.text = t[o].name+' - '+t[o].available; selectoption.value = "pickup " + t[o].name; if(t[o].available <= 0) { selectoption.disabled = true; } options.push(selectoption); } if(t[o].pickupOption == true) { hasPickupOptions = true; } }else{ var e = document.createElement("li"); e.setAttribute('data-loc', t[o].id); var radioSelectBtn = ""; if(t[o].pickupOption == true) { hasPickupOptions = true; var pickupDisabled = ''; if(t[o].inStock == 0) { pickupDisabled = 'disabled'; } radioSelectBtn = ' '; e.innerHTML = "", n.appendChild(e) }else{ e.innerHTML = "" + t[o].name + ": " + t[o].available + "", n.appendChild(e) } } } if(hasPickupOptions){ if(useDropdown){ var e = document.createElement("select"); e.name = 'properties[Shipping Option]'; var firstOption = document.createElement('option'); firstOption.text = 'ship to me'; e.appendChild(firstOption); for (var i = 0; i < options.length; ++i) { e.appendChild(options[i]); } n.prepend(e); } else{ var e = document.createElement("li"); radioSelectBtn = ''; e.innerHTML = radioSelectBtn; n.prepend(e); } } return n }, appendVariantInformationOnGoogleMapStyle: function() { var css = `.inventoryLocation-information-modal { display: none; position: fixed; z-index: 1; padding-top: 100px; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.4); } #select-store{ position:absolute; bottom:20px; right:20px; } [data-inventory-map] { height:400px; } .inventoryLocation-information-modal-content { background-color: #fefefe; margin: auto; padding: 20px; border: 1px solid #888; width: 80%; position:relative; bottom:20px; right:20px; } .inventoryLocation-information-modal-close { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; position: absolute; top: 0%; right: 0.75%; } .inventoryLocation-information-modal-close:hover, .inventoryLocation-information-modal-close:focus { color: #000; text-decoration: none; cursor: pointer; } .map-section-wrapper{ padding-top:15px; } @media only screen and (max-width: 600px) { #inventoryLocationInformationModalBody{ margin-left: 10%; width: 90%; } #select-store{ position:absolute; width: 90%; } #tomit_inventory_list{ padding-bottom:40px; } .inventoryLocation-information-modal-close{ position: absolute; top:0%; right: 5%; } .map-section-wrapper{ padding-top: 5%; } }`, head = document.head || document.getElementsByTagName('head')[0]; if(!document.getElementById('inventoryLocation-information-modal-style')) { var style = document.createElement('style'); head.appendChild(style); style.type = 'text/css'; style.id = 'inventoryLocation-information-modal-style'; if (style.styleSheet){ style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } } }, getProductsLocations: function() { return new Promise(function (n, o) { let e = "https://inventorylocations.checkmyapp.net/api/locations/information/" + Shopify.shop, r = new XMLHttpRequest(); r.open("GET", e, !0), r.onload = function () { if (200 === r.status) { var t = JSON.parse(r.responseText); n(t); } else o("error"); }, r.send(); }); }, listLocationsInformation: function(t) { let obj = document.getElementById('tomit_inventory_locations_list') if(obj) obj.remove(); let n = document.createElement("ul"); n.id = "tomit_inventory_locations_list"; for (var o = 0; o < t.length; o++) { if(t[o].pickupOption == true) { var e = document.createElement("li"); e.setAttribute('data-location', t[o].locationId); var radioSelectBtn = ""; radioSelectBtn = ''; e.innerHTML = ""; n.appendChild(e); } } return n }, listInventoryCart: function(t, useDropdown) { var n = document.getElementById("tomit_inventory_list_cart"); if( n === null) { n = document.createElement("ul"); n.id = "tomit_inventory_list_cart"; } var options = []; var selectoption; t = Object.values(t); for (var o = 0; o < t.length; o++) { if(useDropdown){ if(t[o].pickupOption == true) { selectoption = document.createElement('option'); selectoption.text = t[o].displayName; selectoption.value = "pickup " + t[o].displayName; selectoption.setAttribute('data-loc', t[o].locationId); options.push(selectoption); } }else{ if(t[o].pickupOption == true) { var e = document.createElement("li"); e.setAttribute('data-loc', t[o].locationId); var radioSelectBtn = ""; radioSelectBtn = ''; e.innerHTML = ""; n.appendChild(e); } } } if(useDropdown){ var e = document.getElementsByName('properties_shipping_option'); if(!e.length) { e = document.createElement("select"); e.name = 'properties_shipping_option'; } var blankOption = document.createElement('option'); blankOption.text = 'Select location'; blankOption.selected = 'selected'; blankOption.disabled = true; e = typeof e[0] !== "undefined" ? e[0] : e; e.appendChild(blankOption); var firstOption = document.createElement('option'); firstOption.text = 'ship to me'; e.appendChild(firstOption); for (var i = 0; i < options.length; ++i) { e.appendChild(options[i]); } n.prepend(e); } else { var e = document.createElement("li"); if(n.childNodes.length === 1) { radioSelectBtn = ''; e.innerHTML = radioSelectBtn; n.prepend(e); } } return n }, getAvailablePickupOptionsCart : t => new Promise((n, o) => { var e = "https://inventorylocations.checkmyapp.net/pickup/cart/" + Shopify.shop + "/" + JSON.stringify(t), r = new XMLHttpRequest; r.open("GET", e, !0), r.onload = function() { if (200 === r.status) { var t = JSON.parse(r.responseText); n(t) } else o("error") }, r.send() }), requestLocation : function() { if(tomitProductInventoryInfo.customerPos != '0,0'){ if (typeof ShopifyAnalytics != "undefined" && "product" == ShopifyAnalytics.meta.page.pageType && null != document.getElementById("inventoryLocationInformation")) { var product = ShopifyAnalytics.meta.product; getProductInventoryInformation(product.id); } }else{ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position, that){ tomitStorageSystem.set( "tomitProductInventoryInfo.customerPos", position.coords.latitude + "," + position.coords.longitude, 86400 ); tomitProductInventoryInfo.customerPos = position.coords.latitude + "," + position.coords.longitude; if (typeof ShopifyAnalytics != "undefined" && "product" == ShopifyAnalytics.meta.page.pageType && null != document.getElementById("inventoryLocationInformation")) { var product = ShopifyAnalytics.meta.product; getProductInventoryInformation(product.id); } }, function(){ if (typeof ShopifyAnalytics != "undefined" && "product" == ShopifyAnalytics.meta.page.pageType && null != document.getElementById("inventoryLocationInformation")) { var product = ShopifyAnalytics.meta.product; getProductInventoryInformation(product.id); } }); } }}, showCurrentVariantInformation: function(t) { var n = document.getElementById("inventoryLocationInformation"); if (typeof ShopifyAnalytics != "undefined" && ShopifyAnalytics.meta.hasOwnProperty("selectedVariantId") && "" != ShopifyAnalytics.meta.selectedVariantId && t.variants[ShopifyAnalytics.meta.selectedVariantId] != null && typeof window.location.search.split('variant=')[1] == 'undefined') var o = t.variants[ShopifyAnalytics.meta.selectedVariantId]; else if (typeof window.location.search.split('variant=')[1] != 'undefined') var o = t.variants[window.location.search.split('variant=')[1]]; else if (typeof ShopifyAnalytics != "undefined" && ShopifyAnalytics.meta.page.pageType == 'product') o = t.variants[ShopifyAnalytics.meta.product.variants[0].id]; else return false; this.selectedVariantId = o.id; var e = document.getElementById("tomit_inventory_list"); if (e && e.remove(), null != o.inventoryItem && null == o.inventoryItem.locations || null != o.inventoryItem && 0 == o.inventoryItem.locations.length) { (r = n.getElementsByClassName("inventoryLocationLoading")[0]) && r.remove(); if(document.body.contains(document.getElementById('no_inventory_found_message'))) { document.getElementById('no_inventory_found_message').remove(); } var infoText = document.createElement("span"); infoText.id = 'no_inventory_found_message'; infoText.innerHTML = 'Online only'; n.appendChild(infoText), n.style.display = 'block' } else { if(document.body.contains(document.getElementById('no_inventory_found_message'))) { document.getElementById('no_inventory_found_message').remove(); } var r, i = this.listInventory(o.inventoryItem.locations, t.useDropdown); (r = n.getElementsByClassName("inventoryLocationLoading")[0]) && r.remove(), n.appendChild(i), n.style.display = 'block' } var ev = new CustomEvent("tomitVariantChanged", { detail: { message:'variant changed' }, bubbles: true, cancelable: true }); document.dispatchEvent(ev); }, attachVariantInformationOnGoogleMapEvents: function() { if(document.getElementById("inventoryLocation-store-pickup-button")){ document.getElementById("inventoryLocation-store-pickup-button").addEventListener("click", function() { var n = document.getElementById("inventoryLocationInformationModal"); n.style.display = 'block' var button = document.createElement("div"); button.innerHTML = "Select store"; button.id ='select-store'; button.className = "btn"; button.setAttribute("onclick", "onStoreSelect()"); document.getElementById("inventoryLocationInformationModalBody").appendChild(button); }); } if(document.querySelector('.inventoryLocation-information-modal-close')) { document.querySelector('.inventoryLocation-information-modal-close').addEventListener("click", function() { var n = document.getElementById("inventoryLocationInformationModal"); n.style.display = 'none' }); } }, showCurrentVariantInformationOnPopupModal: function(t) { var n = document.getElementById("inventoryLocationInformation"); if (typeof ShopifyAnalytics != "undefined" && "" != ShopifyAnalytics.meta.selectedVariantId && t.variants[ShopifyAnalytics.meta.selectedVariantId] != null && typeof window.location.search.split('variant=')[1] == 'undefined') var o = t.variants[ShopifyAnalytics.meta.selectedVariantId]; else if (typeof window.location.search.split('variant=')[1] != 'undefined') var o = t.variants[window.location.search.split('variant=')[1]]; else o = t.variants[ShopifyAnalytics.meta.product.variants[0].id]; this.selectedVariantId = o.id; var e = document.getElementById("tomit_inventory_list"); if (e && e.remove(), null != o.inventoryItem && null == o.inventoryItem.locations || null != o.inventoryItem && 0 == o.inventoryItem.locations.length) { (r = n.getElementsByClassName("inventoryLocationLoading")[0]) && r.remove(); if(document.body.contains(document.getElementById('no_inventory_found_message'))) { document.getElementById('no_inventory_found_message').remove(); } var infoText = document.createElement("span"); infoText.id = 'no_inventory_found_message'; infoText.innerHTML = 'Online only'; n.appendChild(infoText), n.style.display = 'none' } else { if(document.body.contains(document.getElementById('no_inventory_found_message'))) { document.getElementById('no_inventory_found_message').remove(); } var inventoryLocationStorePickupButtonText = "Select Store for Pickup"; var modalHtml = `
${inventoryLocationStorePickupButtonText}
×
`; n.innerHTML = modalHtml; var useDropDown = t.useDropdown; var r, i = this.listInventoryPopup(o.inventoryItem.locations, 0); var k = document.getElementById("inventoryLocationInformationModalBody"); (r = n.getElementsByClassName("inventoryLocationLoading")[0]) && r.remove(), k.appendChild(i), n.style.display = 'block'; } this.attachVariantInformationOnGoogleMapEvents(); var ev = new CustomEvent("tomitVariantChanged", { detail: { message:'variant changed' }, bubbles: true, cancelable: true }); document.dispatchEvent(ev); }, getProductsInventoryInformation: t => new Promise((n, o) => { var e = "https://inventorylocations.checkmyapp.net/inventory/products/" + Shopify.shop + "/" + JSON.stringify(t), r = new XMLHttpRequest; r.open("GET", e, !0), r.onload = function() { if (200 === r.status) { var t = JSON.parse(r.responseText); n(t) } else o("error") }, r.send() }) }; function getProductInventoryInformation(t) { var n = "https://inventorylocations.checkmyapp.net/product/" + Shopify.shop + "/" + t + "/"+ tomitProductInventoryInfo.customerPos, o = new XMLHttpRequest; o.open("GET", n, !0), o.onload = function() { if (200 === o.status) { var t = JSON.parse(o.responseText); tomitProductInventoryInfo.activeProduct = t.product, tomitProductInventoryInfo.showCurrentVariantInformation(tomitProductInventoryInfo.activeProduct); tomitProductInventoryInfo.dispatchProductLoadedEvent(); } }, o.send() } function getProductInventoryInformationOnPopupModal(t) { var n = "https://inventorylocations.checkmyapp.net/product/" + Shopify.shop + "/" + t + "/"+ tomitProductInventoryInfo.customerPos; o = new XMLHttpRequest; o.open("GET", n, !0), o.onload = function() { if (200 === o.status) { var t = JSON.parse(o.responseText); tomitProductInventoryInfo.activeProduct = t.product, tomitProductInventoryInfo.showCurrentVariantInformationOnPopupModal(tomitProductInventoryInfo.activeProduct); tomitProductInventoryInfo.dispatchProductLoadedEvent(); } }, o.send() } if (typeof ShopifyAnalytics != "undefined" && "product" == ShopifyAnalytics.meta.page.pageType && null != document.getElementById("inventoryLocationInformation")) { tomitProductInventoryInfo.customerPos = tomitStorageSystem.get("tomitProductInventoryInfo.customerPos") || '0,0'; tomitProductInventoryInfo.requestLocation(); } tomitProductInventoryInfo.dispatchLoadedEvent(); /* These are the modifications: */ history.pushState = ( f => function pushState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('pushState')); window.dispatchEvent(new Event('locationchange')); return ret; })(history.pushState); history.replaceState = ( f => function replaceState(){ var ret = f.apply(this, arguments); window.dispatchEvent(new Event('replaceState')); window.dispatchEvent(new Event('locationchange')); return ret; })(history.replaceState); window.addEventListener('popstate',()=>{ window.dispatchEvent(new Event('locationchange')); }); window.addEventListener('locationchange', function(e){ tomitProductInventoryInfo.showCurrentVariantInformation(tomitProductInventoryInfo.activeProduct) }); function onStoreSelect(){ var selectedOption = document.getElementById('tomit_inventory_list').querySelector('input[type="radio"]:checked').value; var selectedStore = document.getElementById('selectedStore'); selectedStore.innerHTML = "Selected option : " + selectedOption; document.getElementById("inventoryLocationInformation").appendChild(selectedStore); var modal = document.getElementById("inventoryLocationInformationModal"); modal.style.display = 'none'; }