function loadScript(url, callback) { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = url; script.onreadystatechange = callback; script.onload = callback; document.getElementsByTagName("head")[0].appendChild(script); } var modal; var shop = 'babywearsmy.myshopify.com'; function createModal(mode, caption, url) { closeModal(); var n = !1, i = !(!window.navigator || !window.navigator.userAgent) && window.navigator.userAgent; i && i.match(/iPhone|iPad|iPod/i) && (n = !0); var s = document.createElement("div"); s.setAttribute("style", "position: fixed; z-index: 2147483648; padding: 20px; top: 0; width: 100%; height: 100%; left: 0; background: rgba(0, 0 , 0, 0.6); box-sizing: border-box;"); var r = document.createElement("div"); r.setAttribute("style", "padding: 15px; overflow:hidden; border-bottom: 1px solid #e5e5e5; position:absolute; top:0; left:0; width:100%; box-sizing: border-box;"); var o = document.createElement("button"); o.onclick = closeModal.bind(this); o.setAttribute("style", "height:30px; width:30px; cursor:pointer; border:0px; background:0 0; padding:0; -webkit-appearance:none; color:#000; float:right; background:none;"); var a = document.createElement("img"); a.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAYAAAA6/NlyAAAAAXNSR0IArs4c6QAAAgVJREFUaAXtmU2KwkAQhZ0hQbyDu7mIB3PhwoN5EXfeIYguJo9JQRMydurnNYxTDTH+dNWr71VD2mSzyZEOpAPpQDqQDqQD6UA6kA6kA80dOJ1O++Px+NVKGFrQ9Oh9WoMhPAzD5fF4XFpAQwNa0PRAf1iABXaMle7e+r4/nM/nqyVfLUZgx3nS3etutzuMddxqsfPf1cALsJKTAr0AK3omaPWSfj6f21ERx3zso5f3C1hob6da5nW8/KzuMLJVCgnpNEvDBMyGZsGibjMwC5oJ6waOhmbDhgBHQbeADQP2QreCDQW2QreEDQfWQreGpQCvhcY8bFTGk2wX8ZWMkGu5JCvPrstSmWj+vta9aX5TWGjSgJG8Ao0p80HrrAhRgSGigKbDoh468EroJrCoRf1vCUF/edA7/K+WtAJWFg19adM6XIGVWzPvcVmqweL+F1r6FhuPNbBys08zV9a89xy6pC0AlhgPdBiwp3BPrBY+BDii4Igca+DdwJGFRub6Dd4FzCiQkbOENwMzC2PmNgEzC5JusDTUwKxCBLQ8M7TU/5a6rruPReGYj/B9MDYo065MtqKl5n2qpfyu+l7dYWRceIIYDltWvtBp05ND5DQBI7CA3jKfDUMLo4C+W58N/2RyvAIahThSqEKhBU1VUE5OB9KBdCAdSAfSgXQgHUgH0oEQB74BG1sUIwNoL3cAAAAASUVORK5CYII="; a.setAttribute("style", "height:30px; width:30px;"); o.appendChild(a); r.appendChild(o); var d = document.createElement("h4"); d.setAttribute("style", "float: left;font-weight:bold;color:#222;line-height:30px;margin:0px;"); d.innerText = caption; r.appendChild(d); var l = document.createElement("div"); l.setAttribute("style", "background-color: #fff; width: 100%; height: 100%;overflow:hidden;position:relative"); l.appendChild(r); var u = document.createElement("style"); u.innerHTML = "@media screen and (max-width: 768px) { .product-customizer__header-title {font-size: 16px !important;} }"; l.appendChild(u); s.appendChild(l); if (mode == 1) { var c = document.createElement('iframe'); if (c.src = url, c.width = '100%', c.height = '100%', n) { var h = document.createElement("div"); h.setAttribute('style', '-webkit-overflow-scrolling: touch; overflow: scroll; height: 100%; top: 61px; box-sizing: border-box; position: absolute; width: 100%; padding-bottom: 60px;'); c.setAttribute('style', 'border: 0; box-sizing: border-box;'); h.appendChild(c); l.appendChild(h); } else { c.setAttribute('style', 'border: 0; padding-top: 60px; box-sizing: border-box;'); l.appendChild(c); } } else { var img = document.createElement('img'); img.src = url; img.setAttribute('style', 'border: 0; padding-top: 60px; box-sizing: border-box; max-width: 100%; max-height: 100%; display: block; margin-left: auto; margin-right: auto;'); l.appendChild(img); } modal = s; return s } function closeModal() { modal && (modal.parentNode.removeChild(modal), modal = null) } function listenForResponse() { var t = window.addEventListener ? "addEventListener" : "attachEvent"; (0, window[t])("attachEvent" === t ? "onmessage" : "message", function (t) { /*t.origin === this.settings.baseUrl && */void 0 !== t.data.action && "customisation_completed" === t.data.action && onCustomDesignSave(t.data.customisation_id, t.data.customisation_texts); }.bind(this)) } function onCustomDesignSave(id, texts) { var form; var f = document.querySelectorAll('main form[action="/cart/add"]'); if (0 !== f.length) { form = f[0]; } else { f = document.querySelectorAll('form[action="/cart/add"]'); form = f[0]; } if (id) { var e, n = form.querySelectorAll('[name="properties[Customisation]"]'); n.length > 0 ? e = n[0] : ((e = document.createElement("input")).type = "hidden", e.name = "properties[Customisation]", form.appendChild(e)), e.value = id } for (var i = 0; i < texts.length; i++) { var e, n = form.querySelectorAll('[name="properties[Text' + (texts.length > 1 ? " " + (i + 1) : "") + ']"]'); n.length > 0 ? e = n[0] : ((e = document.createElement("input")).type = "hidden", e.name = "properties[Text" + (texts.length > 1 ? " " + (i + 1) : "") + "]", form.appendChild(e)), e.value = texts[i] } form.submit(); } var callback = function () { if (window.location.href.indexOf('/products') !== -1) { if (Shopify.shop == 'babywearsmy.myshopify.com' && Shopify.theme.name.toLowerCase() == 'turbomsia/main' && ShopifyAnalytics.meta.product.id == '7714606842068') { $.ajax({ url: 'https://app2.famsystores.com/product/' + ShopifyAnalytics.meta.product.id + '/GetShopifyProductForPersonalisation', type: 'GET', dataType: 'json', cache: false, success: function (response) { if (response != null && response.success && response.result && response.result.length > 0) { $('head').append(''); $('.product_gallery').prepend(''); $('.product_gallery_nav').remove(); $('.purchase-details__buttons').append(''); var canvas_customisations = document.getElementById('canvas_customisations'); var context_customisations = canvas_customisations.getContext('2d'); var canvas_images = document.getElementById('canvas_images'); var context_images = canvas_images.getContext('2d'); var variantCustomisations = response.result; var selectedVariantCustomisation; var selectedPrint; var selectedPrintArea; var selectedPrintAreaID; var resultCustomisations = []; function isCustomisationOn(variant_id) { for (var i = 0; i < variantCustomisations.length; i++) { if (variantCustomisations[i].VariantID == variant_id) { return isAnyCustomisationOn(variantCustomisations[i].CustomisationData.Customisations); } } return false; } function isAnyCustomisationOn(customisations) { for (var i = 0; i < customisations.length; i++) { if (customisations[i].EnableCustomisation) { return true; } } return false; } function getVariantCustomisation(variant_id) { for (var i = 0; i < variantCustomisations.length; i++) { if (variantCustomisations[i].VariantID == variant_id) { return variantCustomisations[i]; } } } function getCustomisations(print_area_id) { var customisations = []; for (var i = 0; i < selectedVariantCustomisation.CustomisationData.Customisations.length; i++) { if (selectedVariantCustomisation.CustomisationData.Customisations[i].PrintAreaID == print_area_id) { customisations.push(selectedVariantCustomisation.CustomisationData.Customisations[i]); } } return customisations; } function getCustomisation(id) { for (var i = 0; i < selectedVariantCustomisation.CustomisationData.Customisations.length; i++) { if (selectedVariantCustomisation.CustomisationData.Customisations[i].PrintAreaID == selectedPrintAreaID && selectedVariantCustomisation.CustomisationData.Customisations[i].ID == id) { return selectedVariantCustomisation.CustomisationData.Customisations[i]; } } } function getDefaultPrintAreaID(variant_id) { var customisation = getVariantCustomisation(variant_id); return customisation.CustomisationData.PrintAreas[0].ID; } function printAreaChanged(print_area_id) { selectedPrintAreaID = print_area_id; selectedPrint = getPrint(print_area_id); selectedPrintArea = getPrintArea(print_area_id); var customisations = getCustomisations(print_area_id); $('.product-block--customisation').remove(); for (var i = 0; i < customisations.length; i++) { (function (i) { var resultCustomisation = getResultCustomisation(customisations[i].ID); $('#product-block--customisations').append(''); $('#text' + customisations[i].ID).on('input', function () { var c = this.selectionStart, r = /[^a-zA-Z0-9\ \.\,\:\;\!\?\+\-\=\<\>\(\)\[\]\{\}\|\\\/\`\~\@@\#\$\%\^\&\*\'\"]/gi, v = $(this).val(); if (r.test(v)) { $(this).val(v.replace(r, '')); c--; } this.setSelectionRange(c, c); var reduced = false; while (customisationOutOfPrintArea($(this).val(), customisations[i])) { reduced = true; customisations[i].FontSize--; } if (!reduced) { while (!customisationOutOfPrintArea($(this).val(), customisations[i]) && customisations[i].FontSize <= customisations[i].OriginalFontSize) { customisations[i].FontSize++; } customisations[i].FontSize--; } editResultCustomisation($(this).attr('data-customisation-id'), $(this).val()); printCustomisations(); }); })(i); } printImages(); printCustomisations(); } function getPrintArea(print_area_id) { for (var i = 0; i < selectedVariantCustomisation.CustomisationData.PrintAreas.length; i++) { if (selectedVariantCustomisation.CustomisationData.PrintAreas[i].ID == print_area_id) { return selectedVariantCustomisation.CustomisationData.PrintAreas[i]; } } } function getPrint(print_area_id) { for (var i = 0; i < selectedVariantCustomisation.CustomisationData.Prints.length; i++) { if (selectedVariantCustomisation.CustomisationData.Prints[i].PrintAreaID == print_area_id) { return selectedVariantCustomisation.CustomisationData.Prints[i]; } } } function variantChanged(variant_id) { initResultCustomisations(variant_id); if (isCustomisationOn(variant_id)) { $('#canvas_images, #canvas_customisations, #product-block--customisations, #add-personalisation').show(); $('.flickity-viewport, .purchase-details__buttons>button[name="add"]').hide(); selectedPrintAreaID = getDefaultPrintAreaID(variant_id); selectedVariantCustomisation = getVariantCustomisation(variant_id); if ($('#product-block--customisations').length == 0) { $('.product-block--form').append('
'); } var printAreasHtml = '
' + '
'; for (var i = 0; i < selectedVariantCustomisation.CustomisationData.PrintAreas.length; i++) { printAreasHtml += '' + '
' + '' + '
'; } printAreasHtml += '
'; $('#product-block--customisations').empty().append(printAreasHtml); $('input[type=radio][name="option-print-area"]').on('change', function () { printAreaChanged(this.value); }); printAreaChanged(selectedPrintAreaID); } else { $('#canvas_images, #canvas_customisations, #product-block--customisations, #add-personalisation').hide(); $('.flickity-viewport, .purchase-details__buttons>button[name="add"]').show(); } } function getResultCustomisation(customisation_id) { for (var i = 0; i < resultCustomisations.length; i++) { if (resultCustomisations[i].ID == customisation_id) { return resultCustomisations[i]; } } } function initResultCustomisations(variant_id) { resultCustomisations = []; if (isCustomisationOn(variant_id)) { var customisation = getVariantCustomisation(variant_id); for (var i = 0; i < customisation.CustomisationData.Customisations.length; i++) { customisation.CustomisationData.Customisations[i].DefaultText = customisation.CustomisationData.Customisations[i].Text; customisation.CustomisationData.Customisations[i].OriginalFontSize = customisation.CustomisationData.Customisations[i].FontSize; resultCustomisations.push(customisation.CustomisationData.Customisations[i]); } } } function editResultCustomisation(customisation_id, text) { var resultCustomisation = getResultCustomisation(customisation_id); resultCustomisation.Text = text; resultCustomisation.Changed = text ? true : false; } function getAlignment(align) { if (align == -1) return "left"; else if (align == 0) return "center"; else if (align == 1) return "right"; else return "center"; } function getTextAlignX(customisation) { var result = 0; context_customisations.save(); context_customisations.font = (customisation.Italic ? 'italic ' : '') + (customisation.Bold ? 'bold ' : '') + customisation.OriginalFontSize + 'px ' + customisation.FontName; if (customisation.Align == -1) result = 0; else if (customisation.Align == 0) result = context_customisations.measureText(customisation.DefaultText).width / 2; else if (customisation.Align == 1) result = context_customisations.measureText(customisation.DefaultText).width; else result = context_customisations.measureText(customisation.DefaultText).width / 2; context_customisations.restore(); return result; } function customisationOutOfPrintArea(text, customisation) { context_customisations.save(); context_customisations.font = (customisation.Italic ? 'italic ' : '') + (customisation.Bold ? 'bold ' : '') + customisation.OriginalFontSize + 'px ' + customisation.FontName; var original_size = context_customisations.measureText(customisation.DefaultText).width context_customisations.font = (customisation.Italic ? 'italic ' : '') + (customisation.Bold ? 'bold ' : '') + customisation.FontSize + 'px ' + customisation.FontName; var current_size = context_customisations.measureText(text).width context_customisations.restore(); return original_size < current_size; } function printImages() { img_mockup = new Image(); img_mockup.src = selectedPrint.GraphicImageSnapshotSource; img_mockup.onload = function () { img_shadow = new Image(); img_shadow.src = selectedPrintArea.ShadowImageUrl; img_shadow.onload = function () { context_images.fillStyle = selectedVariantCustomisation.CustomisationData.Color.Hex; context_images.fillRect(0, 0, canvas_images.width, canvas_images.height); context_images.drawImage( img_mockup, 0, 0, img_mockup.width, img_mockup.height, selectedPrintArea.OffsetLeft + selectedPrint.GraphicImageLeft, selectedPrintArea.OffsetTop + selectedPrint.GraphicImageTop, selectedPrint.GraphicImageWidth, selectedPrint.GraphicImageHeight); context_images.drawImage( img_shadow, 0, 0, img_shadow.width, img_shadow.height, 0, 0, canvas_images.width, canvas_images.height); } } } function printCustomisations() { //context_customisations.translate(newx, newy); context_customisations.clearRect(0, 0, canvas_customisations.width, canvas_customisations.height); context_customisations.save(); for (var i = 0; i < resultCustomisations.length; i++) { (function (i) { var customisation = getCustomisation(resultCustomisations[i].ID); if (customisation) { var myFont = new FontFace(customisation.FontName, 'url(https://app2.famsystores.com/CustomisationFonts/' + customisation.FontName + '/' + customisation.FontName + '.ttf)'); myFont.load().then((font) => { document.fonts.add(font); context_customisations.fillStyle = customisation.Color; context_customisations.textAlign = getAlignment(customisation.Align); context_customisations.textBaseline = 'top'; context_customisations.font = (customisation.Italic ? 'italic ' : '') + (customisation.Bold ? 'bold ' : '') + customisation.FontSize + 'px ' + customisation.FontName; var textAlignX = getTextAlignX(customisation); context_customisations.fillText(resultCustomisations[i].Text || resultCustomisations[i].DefaultText, selectedPrintArea.OffsetLeft + customisation.Left + textAlignX, selectedPrintArea.OffsetTop + customisation.Top); context_customisations.restore(); }); } })(i); } } $('#add-personalisation').on('click', function () { var submitButton = $(this); var submitButtonSpinner = submitButton.find('.spinner'); submitButton.attr('disabled', true); submitButtonSpinner.removeClass('hidden'); var customisation = { HtmlMockupRatio: 1600, Personalisations: [] }; var texts = []; for (var i = 0; i < resultCustomisations.length; i++) { if (resultCustomisations[i].Changed) { var print = getPrint(resultCustomisations[i].PrintAreaID); resultCustomisations[i].ProductVariantID = print.ID; texts.push(resultCustomisations[i].Text); customisation.Personalisations.push(resultCustomisations[i]); } } if (customisation.Personalisations.length > 0) { $.ajax({ url: 'https://app2.famsystores.com/product/AddPersonalisation', type: "POST", dataType: "json", contentType: "application/json; charset=utf-8", cache: false, data: JSON.stringify(customisation), success: function (response) { if (response != null && response.success) { onCustomDesignSave(response.reference, texts) } else { alert(response.responseText); submitButton.attr('disabled', false); submitButtonSpinner.addClass('hidden'); } }, error: function (response) { alert("Something went wrong..."); submitButton.attr('disabled', false); submitButtonSpinner.addClass('hidden'); } }); } else { var main_form = $('main').find('form[action = "/cart/add"]'); if (main_form.length == 0) { main_form = $('form[action = "/cart/add"]'); } main_form.submit(); } }); $('select[name="id"]').on('change', function () { variantChanged(this.value); }); variantChanged($('select[name="id"]').val()); //$('.gallery-cell.product-gallery__thumbnail').on('click', function () { // variantChanged($('select[name = "id"]').val()); //}); } else { console.log(response); } }, error: function (response) { console.log(response); } }); } else { $.ajax({ url: 'https://app2.famsystores.com/product/' + ShopifyAnalytics.meta.product.id + '/IsPersonalisationEnabled', type: 'GET', dataType: 'json', cache: false, success: function (response) { if (response != null && response.success && response.result) { var main_form = $('main').find('form[action = "/cart/add"]'); if (main_form.length == 0) { main_form = $('form[action = "/cart/add"]'); } var submit_button = main_form.find(':submit'); if (submit_button.length == 0) { submit_button = main_form.find('button[name="add"]'); } $('') .insertAfter(submit_button); $('.personalise_product_selector').addClass(submit_button.attr('class')).removeClass('ajax-submit add_to_cart'); $('.personalise_product_selector').on('click', function () { var form = $(this).closest('form[action$="/cart/add"]')[0]; var e = !1, n = form.querySelectorAll('select[name="id"]'); if (n.length > 0) e = n[0].options[n[0].selectedIndex].value; else { var i = form.querySelectorAll('input[name="id"]'); if (0 == i.length) return; e = i[0].value } if (e) { var s = 'https://app2.famsystores.com/product/personalise?shop=' + Shopify.shop + '&variant_id=' + e; document.body.appendChild(createModal(1, 'Create a personalised design', s)); listenForResponse(); } }); } else { console.log(response); } }, error: function (response) { console.log(response); } }); } } if (window.location.href.indexOf('/cart') !== -1) { //for themes that does not work need to determine right class near the image in repeating cart rows $('.cart__image, .CartItem__Image, .cart_page_image').each(function () { var $this = $(this); var parent = $this; var variant = null; var customisation = null; while (parent) { var html = parent.prop('outerHTML'); if (!html) { break; } var match = html.match(/variant=([0-9]+)/); if (match) { variant = match[1]; if (variant) { break; } } parent = parent.parent(); } var customisationHtml = $this.parent().parent().html(); if (customisationHtml.indexOf('custid_') != -1) { var i = customisationHtml.indexOf("custid_"); customisation = customisationHtml.substring(i, i + 7 + 10); } if (variant && customisation) { $.ajax({ url: 'https://app2.famsystores.com/product/GetPersonalisationImage?reference=' + customisation + '&variant_id=' + variant, type: 'GET', dataType: 'json', cache: false, success: function (response) { if (response != null && response.success && response.result) { $this.parent().find('img').removeAttr('srcset').attr('src', response.result); $this.parent().parent().html($this.parent().parent().html().replace(customisation, '' + customisation + '').replace('custid_', '').replace('Customisation:', 'Reference:')); } else { console.log(response); } }, error: function (response) { console.log(response); } }); } }); } }; if (typeof jQuery == 'undefined') { loadScript("//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js", callback); } else { callback(); }