(async function () { var theme = window.Shopify.theme; var shop = JSON.parse('{"_id":"6453b5337f975d011ee35cd9","origin":"appelts-diamonds.myshopify.com","customTheme":{"productList":{"classControl":"notrequired","loopOn":"notrequired","pathToPrice":"notrequired","pathToOnSalePrice":"notrequired"},"productDetail":{"classControl":"div.product__form__wrapper","pathToPrice":"span.product__price","after":"div.product__price__wrap"},"cart":{"classControl":"table.total-line-table","pathToPrice":"span.payment-due__price.skeleton-while-loading--lg","before":"td.total-line__price payment-due"}},"theme":"Custom"}'); var flexitiAppServerUrl = 'https://ecomm-monthly-payment-widget.flexiti.com'; var themesConfig = JSON.parse('[{"theme_store_id":null,"name":"Tempest","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":"#product-loop","loopOn":".price","pathToPrice":".prod-price > .money","pathToOnSalePrice":".onsale > .money"},"productDetail":{"classControl":"#product-description","pathToPrice":".product-price > .money","appendTo":"#product-price"},"cart":{"classControl":"#shopping-cart","pathToPrice":"#basket-right > h4 > span.money","appendTo":"#basket-right > h4"}},"css":"tempest"},{"theme_store_id":796,"name":"Debut","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":".grid--view-items","loopOn":".grid--view-items .grid__item","pathToPrice":"dl.price > div.price__regular > dd >span.price-item--regular","pathToOnSalePrice":".price > .price__sale > dd > .price-item--sale"},"productDetail":{"classControl":".product-single","pathToPrice":".price-item--sale","appendTo":"dl.price"},"cart":{"classControl":".template-cart","pathToPrice":".cart-subtotal__price","before":".cart__shipping"}},"css":"debut"},{"theme_store_id":730,"name":"Brooklyn","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":".grid-collage","loopOn":".grid-product__wrapper","pathToPrice":".grid-product__price-wrap > .grid-product__price","pathToOnSalePrice":".grid-product__price-wrap > .grid-product__price"},"productDetail":{"classControl":".product-single","pathToPrice":".product-single__price","appendTo":"div[data-price-container]"},"cart":{"classControl":"#your-shopping-cart","pathToPrice":".cart__subtotal","after":".push--large--seven-twelfths > .grid--full.cart__row--table"}},"css":"brooklyn"},{"theme_store_id":679,"name":"Supply","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":".grid-uniform","loopOn":".product-item--price","pathToPrice":".medium--left > span:last","pathToOnSalePrice":".medium--left > span:last"},"productDetail":{"classControl":"#ProductSection","pathToPrice":"#productPrice-product-template > span:last","after":".product-meta"},"cart":{"classControl":"#your-shopping-cart","pathToPrice":".cart-subtotal--price > span","after":".cart-subtotal"}},"css":"supply"},{"theme_store_id":380,"name":"Minimal","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":".grid-uniform","loopOn":".grid-link","pathToPrice":".grid-link__meta","pathToOnSalePrice":".grid-link__meta"},"productDetail":{"classControl":"#shopify-section-product-template","pathToPrice":"#ProductPrice","after":".product-single__prices"},"cart":{"classControl":"#your-shopping-cart","pathToPrice":".cart__subtotal-price","before":".cart__policies"}},"css":"minimal"},{"theme_store_id":719,"name":"Pop","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":".grid-uniform.product-grid","loopOn":".grid-uniform.product-grid .product-wrapper","pathToPrice":".product__price","pathToOnSalePrice":".grid-link__meta"},"productDetail":{"classControl":"#shopify-section-product-template","pathToPrice":"#shopify-section-product-template","after":".product-single__price"},"cart":{"classControl":"form.cart-wrapper","pathToPrice":"form.cart-wrapper .cart__subtotal","after":".cart__subtotal:parent"}},"css":"pop"},{"theme_store_id":829,"name":"Narrative","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":".collection-template","loopOn":".collection-template .card__info","pathToPrice":".card__price","pathToOnSalePrice":".card__price > .card__price--sale"},"productDetail":{"classControl":"#shopify-section-product-template","pathToPrice":".product__current-price","after":".product__price"},"cart":{"classControl":"form.cart__form","pathToPrice":"form.cart__form .cart-footer__subtotal","after":".cart-footer__subtotal:parent"}},"css":"narrative"},{"theme_store_id":782,"name":"Jumpstart","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":"#shopify-section-collection-template","loopOn":"#shopify-section-collection-template .product","pathToPrice":".price"},"productDetail":{"classControl":".product-template","pathToPrice":"#ProductPrice-product-template","before":".form__row"},"cart":{"classControl":"#cart-form","pathToPrice":"#cart-form .cart__subtotal","after":"#cart-form .cart__subtotal"}},"css":"jumpstart"},{"theme_store_id":578,"name":"Simple","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":"#products","loopOn":".product__prices ","pathToPrice":".product__price","pathToOnSalePrice":".product__price--on-sale"},"productDetail":{"classControl":".template-product","pathToPrice":"#ProductPrice","after":".product-single__prices"},"cart":{"classControl":"#your-shopping-cart","pathToPrice":".cart__subtotal","before":".cart__policies"}},"css":"simple"},{"theme_store_id":775,"name":"Venture","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":".template-collection","loopOn":".template-collection .product-card__info","pathToPrice":".product-card__regular-price","pathToOnSalePrice":".product-card__price"},"productDetail":{"classControl":"#shopify-section-product-template","pathToPrice":"#ProductPrice-product-template","after":".product-single__meta-list"},"cart":{"classControl":".template-cart","pathToPrice":".cart__subtotal","before":".cart__taxes"}},"css":"venture"},{"theme_store_id":766,"name":"Boundless","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":"#products","loopOn":".product-item__meta","pathToPrice":".product-item__price-wrapper","pathToOnSalePrice":".product-item__price-wrapper > span .product-item__price--sale > span .js-price"},"productDetail":{"classControl":"#shopify-section-product-template","pathToPrice":".js-price","after":".product__price"},"cart":{"classControl":"#your-shopping-cart","pathToPrice":".cart__subtotal","before":".cart__policies"}},"css":"boundless"}]'); var selectedTheme = shop.theme; var shopOrigin = shop.origin; var regexpToGetProductPrice = /\d*\,?\d+.?\d*/; var config = null; var myEle = document.getElementById("flexitiModal"); var storeLanguage = document.documentElement.lang === 'fr' ? 'fr' : 'en'; console.log('Shop', shopOrigin); console.log('Flexiti App Server URL', flexitiAppServerUrl); await loadjQuery(); await loadEasyModal(); try { detectThemeAndFlexitiRender(); flexitiCode(); } catch (err) { console.log(err); } console.log("jQuery modal Loaded"); // 10 window.addEventListener("message", function (event) { switch (event.data) { case 'flx-close': console.log('flx-close'); closeFlexitiModal(); break; case 'flx-apply:declined': console.log('flx-apply:declined'); closeFlexitiModal(); break; case 'flx-apply:approved': console.log('flx-apply:approved'); $('.flexitiApplyNowLink').hide(); setCookie('flxHasApplied', true); closeFlexitiModal(); break; } }); function setCookie(cname, cvalue, exdays = null) { if (exdays != null) { var d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toUTCString(); } else { var expires = ""; } document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return false; } function ThemeException(message) { this.message = message; this.name = "ThemeException"; } async function loadEasyModal() { return new Promise((resolve, reject) => { if(!myEle){ var linkElementToAppendScript = document.createElement('script'); linkElementToAppendScript.src = flexitiAppServerUrl + '/static/iziModal.js' linkElementToAppendScript.addEventListener('load', function () { var modalDiv = document.createElement('div'); modalDiv.id = 'flexitiModal'; var body = document.getElementsByTagName('body')[0]; body.insertBefore(modalDiv, body.firstChild); }) document.getElementsByTagName('head')[0].appendChild(linkElementToAppendScript); var linkElementToAppendCSS = document.createElement('link'); linkElementToAppendCSS.href = flexitiAppServerUrl + '/static/iziModal.css'; linkElementToAppendCSS.type = 'text/css'; linkElementToAppendCSS.rel = 'stylesheet'; linkElementToAppendCSS.media = 'screen,print'; document.getElementsByTagName('head')[0].appendChild(linkElementToAppendCSS); console.log('loading jQuery & css via the app .') resolve(); }else{ console.log('not loading jQuery & css via the app .') reject(); } }); } function detectThemeAndFlexitiRender() { return new Promise((resolve) => { if (selectedTheme == "Custom") { try { config = shop.customTheme; } catch (err) { console.log(err); } } else { themesConfig.forEach(function (allowedTheme) { if (selectedTheme == allowedTheme.name) { config = allowedTheme.config; config.css = allowedTheme.css; resolve(config); } }); } if (config == null) { var themeException = new ThemeException("Not allowed Theme."); throw themeException; } }) } function flexitiCode() { loadThemeCss(); if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) { insertPaymentCalculator(); } else { document.addEventListener('DOMContentLoaded', insertPaymentCalculator, false); } function insertPaymentCalculator() { getCalculatorForProductList(); getCalculatorForProductDetail(); getCalculatorForCart(); }; } function getOnlyText($htmlObject) { return $htmlObject .clone() //clone the element .children() //select all the children .remove() //remove all the children .end() //again go back to selected element .text(); } function isGreaterThanZero(regexpMatch) { return regexpMatch !== null && regexpMatch[0] !== '0,00' && regexpMatch[0] !== '0.00' && regexpMatch[0] !== '0' } function getPaymentCalculatorSnippet(productPrice) { var productPriceAsFloat = getPriceAsFloat(productPrice[0]); return getPaymentCalculatorData(productPriceAsFloat).then(function (calculator) { var hasApplied = getCookie('flxHasApplied'); var enabledStyle = (hasApplied == 'true') ? "style='display: none;'" : ""; calculator = calculator.replace("

", "").replace("

", "") .replace("apply_anonymous flx_apply_now_link", "flexitiApplyNowLink") .replace('javascript:;"', 'javascript:;"' + enabledStyle); return "
" + calculator + "
"; }); } function getPriceAsFloat(productPrice) { return Number(productPrice.replace(/[^0-9\.-]+/g, "")); } function getPaymentCalculatorData(productPriceAsFloat) { let methodUrl = flexitiAppServerUrl + '/payment-calculator/get-payment-calculator-plan?shop=' + shopOrigin + '&price=' + productPriceAsFloat + '&language=' + storeLanguage; return $.ajax({ url: methodUrl, xhrFields: { withCredentials: true } }).then(function (response) { return response.htmlRender; }); } function getCalculatorForProductList() { if ($(config.productList.classControl).length) { // Product grid view if ($(config.productList.loopOn).length) { $(config.productList.loopOn).each(function (index, element) { // Closure is added to prevent race condition between loop and the server request (function ($element) { var $priceHtmlElement = $element.find(config.productList.pathToOnSalePrice); if ($priceHtmlElement.text() == '') { $priceHtmlElement = $element.find(config.productList.pathToPrice); } var priceText = getOnlyText($priceHtmlElement); var price = priceText.match(regexpToGetProductPrice); if (isGreaterThanZero(price)) { getPaymentCalculatorSnippet(price).then(function (paymentCalculatorSnippet) { if (!paymentCalculatorSnippet) { return; } $element.append("
" + paymentCalculatorSnippet + "
"); addApplyNowLinkClickListener(); }); } })($(element)); }); } } } function getCalculatorForProductDetail() { if ($(config.productDetail.classControl).length) { var $this = $(this); var $el = $(config.productDetail.pathToPrice); var price = $el.text().match(regexpToGetProductPrice); if (isGreaterThanZero(price)) { getPaymentCalculatorSnippet(price).then(function (paymentCalculatorSnippet) { if (!paymentCalculatorSnippet) { return; } if (typeof config.productDetail.appendTo !== 'undefined') { $(config.productDetail.appendTo).append("
" + paymentCalculatorSnippet + "
"); } else if (typeof config.productDetail.after !== 'undefined') { $(config.productDetail.after).after("
" + paymentCalculatorSnippet + "
"); } else if (typeof config.productDetail.before !== 'undefined') { $(config.productDetail.before).before("
" + paymentCalculatorSnippet + "
"); } addApplyNowLinkClickListener(); }); } } } function getCalculatorForCart() { if ($(config.cart.classControl).length) { var $this = $(this); var $el = $(config.cart.pathToPrice); var price = $el.text().match(regexpToGetProductPrice); if (isGreaterThanZero(price)) { getPaymentCalculatorSnippet(price).then(function (paymentCalculatorSnippet) { if (!paymentCalculatorSnippet) { return; } if (typeof config.cart.appendTo !== 'undefined') { $(config.cart.appendTo).append("
" + paymentCalculatorSnippet + "
"); } else if (typeof config.cart.after !== 'undefined') { $(config.cart.after).after("
" + paymentCalculatorSnippet + "
"); } else if (typeof config.cart.before !== 'undefined') { $(config.cart.before).before("
" + paymentCalculatorSnippet + "
"); } addApplyNowLinkClickListener(); }); } } } function addApplyNowLinkClickListener() { var applyNowLinks = document.getElementsByClassName("flexitiApplyNowLink"); for (var i = 0; i < applyNowLinks.length; i++) { applyNowLinks[i].addEventListener('click', openFlexitiApplyModal, false); } } function openFlexitiApplyModal() { $.ajax({ url: flexitiAppServerUrl + '/payment-calculator/get-apply-url?shop=' + shopOrigin + '&language=' + storeLanguage, xhrFields: { withCredentials: true } }).then(function (response) { $('#flexitiModal').iziModal('destroy'); $('#flexitiModal').iziModal({ zindex: 9999, overlayClose: false, iframe: true, width: '90%', // Seems that height can't be a percentaje iframeHeight: 800, iframeURL: response.learnMoreUrl, }); $('#flexitiModal').iziModal('open'); });; } async function loadjQuery(params) { return new Promise((resolve, reject) => { if (!window.jQuery) { var scriptTagElement = document.createElement('script'); scriptTagElement.type = "text/javascript"; scriptTagElement.src = 'https://code.jquery.com/jquery-2.2.4.min.js'; scriptTagElement.addEventListener('load', async function () { if (window.jQuery) { console.log('jQuery loaded via the app'); resolve(); } else { console.log('not able to load the jQuery via the app'); reject(); } }); document.getElementsByTagName('head')[0].appendChild(scriptTagElement); } else { console.log('not loading jQuery via the app as it seems to be already defined.') resolve(); } }); } function loadThemeCss() { var linkElementToAppendCSS = document.createElement('link'); if (config.css) { linkElementToAppendCSS.href = flexitiAppServerUrl + '/static/theme-assets/' + config.css + '/style.css'; } else { linkElementToAppendCSS.href = flexitiAppServerUrl + '/static/theme-assets/minimal/style.css'; } linkElementToAppendCSS.type = 'text/css'; linkElementToAppendCSS.rel = 'stylesheet'; linkElementToAppendCSS.media = 'screen,print'; document.getElementsByTagName('head')[0].appendChild(linkElementToAppendCSS); } function closeFlexitiModal() { $('#flexitiModal').iziModal('close'); } })();