(async function () {
let shop = JSON.parse('{"_id":"64beea1e1e5a150129ebc940","origin":"la-zone-musicale.myshopify.com","theme":"Custom","customTheme":{"productList":{"classControl":"notrequired","pathToPrice":"notrequired","pathToOnSalePrice":"notrequired"},"productDetail":{"classControl":"div.product-main","pathToPrice":"div.price.product__price > div.price__current > span.money","after":"div.product-pricing"},"cart":{"classControl":"div.cartitems","pathToPrice":"div.cart-subtotal > span.money","before":"div.cart-subtotal"}}}');
let flexitiAppServerUrl = 'https://ecomm-monthly-payment-widget.flexiti.com';
let 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","variant":".price-item--regular"},"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--regular","appendTo":"dl.price","variant":".price-item--regular"},"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]","variant":".product-single__price"},"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":".h1 > span:last","pathToOnSalePrice":".h1 > span:last"},"productDetail":{"classControl":"#ProductSection","pathToPrice":"#productPrice-product-template > span:last","after":".product-meta","variant":"#productPrice-product-template"},"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","variant":"#ProductPrice"},"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","variant":"#shopify-section-product-template"},"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","variant":".product__current-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","variant":"#ProductPrice-product-template"},"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","variant":"#ProductPrice"},"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","variant":"#ProductPrice-product-template"},"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","variant":".js-price"},"cart":{"classControl":"#your-shopping-cart","pathToPrice":".cart__subtotal","before":".cart__policies"}},"css":"boundless"},{"theme_store_id":1368,"name":"Craft","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":"div.collection","loopOn":"div.card__information","pathToPrice":"div.price","pathToOnSalePrice":"span.price-item.price-item--sale.price-item--last"},"productDetail":{"classControl":"div.product__info-wrapper.grid__item","pathToPrice":".product__info-container","after":"div.price.price--large.price--show-badge","variant":".product__info-container"},"cart":{"classControl":"div.cart__footer","pathToPrice":".totals__total-value","after":".totals"}},"css":"craft"},{"theme_store_id":887,"name":"Dawn","script":"storefront-script-tempest.js.twig","config":{"productList":{"classControl":"div.collection","loopOn":"div.card-information","pathToPrice":"div.price__regular > span.price-item price-item--regular","pathToOnSalePrice":"div.price__sale > span.price-item.price-item--sale.price-item--last"},"productDetail":{"classControl":"div.product__info-wrapper.grid__item","pathToPrice":"div.price__sale > span.price-item.price-item--sale.price-item--last , div.price__regular > span.price-item price-item--regular","after":"div.price__container","variant":".product__info-container"},"cart":{"classControl":"div.cart__footer","pathToPrice":".totals__total-value","after":".totals"}},"css":"dawn"}]');
let selectedTheme = shop.theme;
let shopOrigin = shop.origin;
let regexpToGetProductPrice = /\d*\,?\d+.?\d*/;
let config = null;
let myEle = document.getElementById('flexitiModal');
let 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
function flxCloseDeclinedWithMessage(message) {
console.log(message);
closeFlexitiModal();
}
window.addEventListener('message', function (event) {
switch (event.data) {
case 'flx-close':
flxCloseDeclinedWithMessage('flx-close');
break;
case 'flx-apply:declined':
flxCloseDeclinedWithMessage('flx-apply:declined');
break;
case 'flx-apply:approved':
console.log('flx-apply:approved');
$('.flexitiApplyNowLink').hide();
setCookie('flxHasApplied', true);
closeFlexitiModal();
break;
}
});
function setCookie(cname, cvalue, exdays = null) {
let expires = '';
if (exdays != null) {
let d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
expires = 'expires=' + d.toUTCString();
}
document.cookie = cname + '=' + cvalue + ';' + expires + ';path=/';
}
function getCookie(cname) {
const name = cname + '=';
const ca = document.cookie.split(';');
for (let c of ca) {
if (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return null;
}
function ThemeException(message) {
this.message = message;
this.name = 'ThemeException';
}
async function loadEasyModal() {
return new Promise((resolve, reject) => {
if (!myEle) {
const urlStaticFlxAppServer = flexitiAppServerUrl + '/static';
let linkElementToAppendScript = document.createElement('script');
linkElementToAppendScript.src = urlStaticFlxAppServer + '/iziModal.js';
linkElementToAppendScript.addEventListener('load', function () {
let modalDiv = document.createElement('div');
modalDiv.id = 'flexitiModal';
let body = document.getElementsByTagName('body')[0];
body.insertBefore(modalDiv, body.firstChild);
});
document
.getElementsByTagName('head')[0]
.appendChild(linkElementToAppendScript);
let linkElementToAppendCSS = document.createElement('link');
linkElementToAppendCSS.href = urlStaticFlxAppServer + '/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) {
let 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) {
let productPriceAsFloat = getPriceAsFloat(productPrice[0]);
return getPaymentCalculatorData(productPriceAsFloat).then(function (
calculator,
) {
let strCalculator = calculator || '';
let hasApplied = getCookie('flxHasApplied');
let enabledStyle = hasApplied == 'true' ? "style='display: none;'" : '';
strCalculator = strCalculator
.replace('', '')
.replace('
', '')
.replace('apply_anonymous flx_apply_now_link', 'flexitiApplyNowLink')
.replace('javascript:;"', 'javascript:;"' + enabledStyle);
return (
"
" +
strCalculator +
''
);
});
}
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) {
let $priceHtmlElement = $element.find(
config.productList.pathToOnSalePrice,
);
if ($priceHtmlElement.text() == '') {
$priceHtmlElement = $element.find(config.productList.pathToPrice);
}
let priceText = getOnlyText($priceHtmlElement);
let price = priceText.match(regexpToGetProductPrice);
if (isGreaterThanZero(price)) {
getPaymentCalculatorSnippet(price).then(function (
paymentCalculatorSnippet,
) {
if (!paymentCalculatorSnippet) {
return;
}
$element.append(
"" +
paymentCalculatorSnippet +
'
',
);
addApplyNowLinkClickListener();
});
}
})($(element));
});
}
}
}
function insertPayementCalculatorsnippet(price){
if (isGreaterThanZero(price)) {
getPaymentCalculatorSnippet(price).then(function (
paymentCalculatorSnippet,
) {
if (!paymentCalculatorSnippet) {
return;
}
const divPaymentCalculatorSnippet =
"" +
paymentCalculatorSnippet +
'
';
$('div.flxcalculator-plugin-wrapper').remove();
if (typeof config.productDetail.appendTo !== 'undefined') {
$(config.productDetail.appendTo).append(
divPaymentCalculatorSnippet,
);
} else if (typeof config.productDetail.after !== 'undefined') {
$(config.productDetail.after).after(divPaymentCalculatorSnippet);
} else if (typeof config.productDetail.before !== 'undefined') {
$(config.productDetail.before).before(divPaymentCalculatorSnippet);
}
addApplyNowLinkClickListener();
});
}
}
function getCalculatorForProductDetail() {
if ($(config.productDetail.classControl).length) {
let $el = $(config.productDetail.pathToPrice);
let price = $el.text().match(regexpToGetProductPrice);
const process = () => {
$el = $(config.productDetail.pathToPrice);
price = $el.text().match(regexpToGetProductPrice);
insertPayementCalculatorsnippet(price);
}
const observer = new MutationObserver(process);
if(config.productDetail.variant){
const elementToObserve = document.querySelector(config.productDetail.variant);
observer.observe(elementToObserve, { attributes: true,childList: true,characterData: true });}
insertPayementCalculatorsnippet(price);
}
}
function getCalculatorForCart() {
if ($(config.cart.classControl).length) {
var $el = $(config.cart.pathToPrice);
var price = $el.text().match(regexpToGetProductPrice);
if (isGreaterThanZero(price)) {
getPaymentCalculatorSnippet(price).then(function (
paymentCalculatorSnippet,
) {
if (!paymentCalculatorSnippet) {
return;
}
const divPaymentCalSnippet =
"" +
paymentCalculatorSnippet +
'
';
if (typeof config.cart.appendTo !== 'undefined') {
$(config.cart.appendTo).append(divPaymentCalSnippet);
} else if (typeof config.cart.after !== 'undefined') {
$(config.cart.after).after(divPaymentCalSnippet);
} else if (typeof config.cart.before !== 'undefined') {
$(config.cart.before).before(divPaymentCalSnippet);
}
addApplyNowLinkClickListener();
});
}
}
}
function addApplyNowLinkClickListener() {
let applyNowLinks = document.getElementsByClassName('flexitiApplyNowLink');
for (let link of applyNowLinks) {
link.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: false,
width: '90%',
// Seems that height can't be a percentaje
iframeHeight: 800,
customHtml:String(response.learnMoreHtml).replace('class="container window"','class="container window" style="display: contents;"').replace('class="close" onclick="flxCloseAndRedirect()"','class="close" onclick="flxClose()"'),
top:'0px',
});
$('#flexitiModal').iziModal('open');
});
}
async function loadjQuery(params) {
return new Promise((resolve, reject) => {
if (!window.jQuery) {
let 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() {
let linkElementToAppendCSS = document.createElement('link');
const urlFlxAppServerStatic = flexitiAppServerUrl + '/static';
if (config.css) {
linkElementToAppendCSS.href =
urlFlxAppServerStatic + '/theme-assets/' + config.css + '/style.css';
} else {
linkElementToAppendCSS.href =
urlFlxAppServerStatic + '/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');
}
})();