/** * Created by bender on 17.09.16. */ var appname = 'ruspost'; function log(message){ console.log(appname+": "+message); } function setRequiredFields() { jQuery("input.ecwid-AddressForm-zip").css("border", "2px solid #a20505"); jQuery("input.ecwid-AddressForm-zip").prop('required',true); jQuery("button.ecwid-btn.ecwid-btn--primary.ecwid-btn--continue").hide(); } function unsetRequiredFields() { jQuery("input.ecwid-AddressForm-zip").css("border", ""); jQuery("input.ecwid-AddressForm-zip").prop('required',false); } function hideNonUsedMethod() { var shippingMethod = getSelectedShipping().text(); jQuery("span.ecwid-shippingOption label:contains('nonusedtestingmethod')").parent().hide(); if(shippingMethod.indexOf('nonusedtestingmethod') !== -1) { jQuery("button.ecwid-btn.ecwid-btn--primary.ecwid-btn--continue").hide(); return true; }else{ jQuery("button.ecwid-btn.ecwid-btn--primary.ecwid-btn--continue").show(); return false; } } function isIndexOK(value){ var chk_str = value.substring(0, 17); return !(chk_str == "Почта России: зап" || chk_str == "Почта России: неи"); } function isOurShippingMethod(value){ return value.substring(0, 14) == "Почта России: " } function getSelectedShipping(){ return jQuery(".ecwid-shippingOption-checked input:checked").next("label"); } function detectShippingMethod(action) { //Если помимо Почты России подгружен Mail.ru (переменная is_dropshipp_io_storefront_js_loaded), то отдаем приоритет валидации им if ("is_dropshipp_io_storefront_js_loaded" in window && window["is_dropshipp_io_storefront_js_loaded"]) return; if(action!='quite'){log("detectShippingMethod: "+action);} var isHideButton = hideNonUsedMethod(); var shippingMethod = getSelectedShipping().text(); if (isOurShippingMethod(shippingMethod) || isHideButton){ if(action!='quite'){log("detectShippingMethod: selected our method: "+shippingMethod+". country_box_exists: "+!!jQuery("select#select-Country-Box option:selected").length+". selected country: "+jQuery("select#select-Country-Box option:selected").val());} if(!jQuery("select#select-Country-Box option:selected").length || jQuery("select#select-Country-Box option:selected").val() == "RU") { if(action!='quite'){log("detectShippingMethod: selected our method, and country is Russia. Enabling zipcode check");} // Проверяем индекс, если выбрана Россия if (isIndexOK(shippingMethod)) { unsetRequiredFields(); jQuery("button.ecwid-btn.ecwid-btn--primary.ecwid-btn--continue").show(); } else { setRequiredFields(); jQuery("button.ecwid-btn.ecwid-btn--primary.ecwid-btn--continue").hide(); } } else { if (action != 'quite') { log("detectShippingMethod: selected our method, but country is not Russia. Skipping zipcode check"); } unsetRequiredFields(); } }else{ if(action!='quite'){log("detectShippingMethod: selected not our method: "+shippingMethod);} if(!isHideButton && jQuery("button.ecwid-btn.ecwid-btn--primary.ecwid-btn--continue").is(":hidden")) { jQuery("button.ecwid-btn.ecwid-btn--primary.ecwid-btn--continue").show(); } unsetRequiredFields(); } } ////////////////////// // Init ////////////////////// Ecwid.OnPageLoaded.add(function(page) { widgets = Ecwid.getInitializedWidgets(); // if storefront widget is present on this page for (i = 0; i < widgets.length ; i++) { if (widgets[i] == "ProductBrowser") { if ('CHECKOUT_SHIPPING_ADDRESS' == page.type) { log("frontend JS loaded"); detectShippingMethod("load"); } } } }); ////////////////////// // Listeners ////////////////////// Ecwid.OnCartChanged.add(function(cart){ detectShippingMethod("fill"); }); var timerCheckShipping = null; Ecwid.OnPageLoaded.add(function(page) { log("Checking if it's checkout page: "+page.type); if(page.type == 'CHECKOUT_SHIPPING_ADDRESS') { log("Set timer "+page.type); var timerCheckShipping = window.setInterval(function () { detectShippingMethod("quite")}, 300); } else { if(timerCheckShipping != null) { log("Remove timer, because page type is "+page.type); window.clearInterval(timerCheckShipping); } } }); !(function(){ // new checkout support var errorSvg = ''; var currentPage = null; var currentShippingMethod = null; Ecwid.OnPageLoaded.add(function(page){ currentPage = page.type; checkVisibility(3); }); Ecwid.OnCartChanged.add(function(cart){ currentShippingMethod = cart.shippingMethod; checkVisibility(3); }); function isShippingDeliveryPage(pageName) { return pageName === 'CHECKOUT_DELIVERY'; } function isPostalCodeRequired(shippingMethod) { if (!isOurShippingMethod(shippingMethod)) { return false; } return !isIndexOK(shippingMethod); } function findButton() { return document.querySelector('.ec-cart-step--delivery .ec-cart-step__wrap .form-control--button.form-control--done'); } function findButtonRow() { var button = findButton(); if (!button || !button.parentNode) { return null; } return button.parentNode.parentNode; } function findButtonForm() { var buttonRow = findButtonRow(); if (!buttonRow) { return null; } return buttonRow.parentNode; } function noticeVisibility(visible) { var noticeClass = 'rusian-post-error-notice-row'; var form = findButtonForm(); if (!form) { return; } var noticeRow = document.querySelector('.' + noticeClass); if (!noticeRow && !visible) { // мы не нашли созданный элемент, и нам не надо его показывать, давай отложим это на потом return; } if (!noticeRow) { noticeRow = createNode(['ec-form__row', noticeClass] , createNode('ec-form__cell', createNotice(errorSvg, createNoticeErrorMessage()))); form.appendChild(noticeRow); } setVisibility(noticeRow, visible); } function setVisibility(node, visible) { if (visible) { node.setAttribute('style', ''); } else { node.setAttribute('style', 'display: none'); } } function goToPaymentButtonVisibility(visible) { var buttonRow = findButtonRow(); if (!buttonRow) { return; } setVisibility(buttonRow, visible); } function checkVisibility(retry) { if (!currentPage || !currentShippingMethod) { if (retry > 0) { setTimeout(function(){ checkVisibility(retry - 1) }, 100); } return; } if (!isShippingDeliveryPage(currentPage)) { return; } if (!findButton()) { if (retry > 0) { setTimeout(function(){ checkVisibility(retry - 1) }, 100); } return; } var postalCodeRequired = isPostalCodeRequired(currentShippingMethod); goToPaymentButtonVisibility(!postalCodeRequired); noticeVisibility(postalCodeRequired); } function createNode(className /*,..children*/) { var node = document.createElement('div'); if (Array.isArray(className)) { for (var i = 0; i < className.length; i++) { node.classList.add(className[i]); } } else { node.classList.add(className); } var children = Array.prototype.slice.call(arguments, 1); for (var i = 0; i < children.length; i++) { var child = children[i]; if (typeof child === 'string') { node.innerHTML = node.innerHTML + child; } else { node.appendChild(child); } } return node; } function createSpan(className, innerHtml) { var node = document.createElement('span'); node.classList.add(className); node.innerHTML = innerHtml; return node; } function createButton(label, callback) { var button = document.createElement('button'); button.classList.add('form-control__button'); button.type = 'button'; button.appendChild(createSpan('form-control__button-text', label)); button.onclick = function(evt) { evt.preventDefault(); callback(); return false; } return createNode(['form-control', 'form-control--button', 'form-control--medium', 'form-control--secondary'] , button); } function goToShippingAddressPage() { Ecwid.openPage('checkout/address', { zipError: 'Заполните индекс' }); } function isInvalidIndex() { var chk_str = currentShippingMethod.substring(0, 17); return chk_str == "Почта России: неи"; } function getErrorMessage() { if (isInvalidIndex()) { return 'Для доставки с помощью Почты России вам необходимо исправить индекс на предыдущем шаге.'; } else { return 'Для доставки с помощью Почты России вам необходимо заполнить индекс на предыдущем шаге.'; } } function getButtonLabel() { if (isInvalidIndex()) { return 'Изменить индекс'; } else { return 'Заполнить индекс'; } } function createNoticeErrorMessage() { var info = createNode([], getErrorMessage()) info.setAttribute('style', 'margin-bottom: 10px'); var action = createButton(getButtonLabel(), goToShippingAddressPage); return createNode([], info, action); } function createNotice(svg, noticeInner) { return createNode(['ec-notice', 'ec-notice--error'] , createNode('ec-notice__wrap' , createNode('ec-notice__icon', svg) , createNode('ec-notice__text' , createNode('ec-notice__text-inner', noticeInner)))); } })();