"use strict"; var PubUsers; (function() { PubUsers = (function() { function PubUsers(){ JsBase.jsModule = this; this.bindActions(); } PubUsers.prototype = { oValidator : {}, check : {}, disabled : false, oInp2FA : false }; PubUsers.prototype.bindActions = function(){ var _this = this; // Rewrite show error function for specific login if(typeof(Validator) !== "undefined"){ Validator.prototype.showError = function(caller, msg){ if(caller && window.JsBase && $.isFunction(JsBase.showFieldError)) JsBase.showFieldError(caller, msg); }; Validator.prototype.hideError = function(caller){ if(caller && window.JsBase && $.isFunction(JsBase.hideFieldError)) JsBase.hideFieldError(caller); }; if($.isDefined(JsBase.dt['valCfg'])){ _this.oValidator = new Validator(JsBase.dt['valCfg'], "body", JsBase.gM); if($.isDefined(JsBase.dt.fData)) _this.oValidator.showErrorsByObject(JsBase.dt.fData.err); } } // Show form setTimeout(function(){ $("#form").fadeIn(); }, 200); // Bind favorite checkbox var jqFav = $(".selmun ul > li > a .star"); if(jqFav.length){ jqFav.click(function(e){ e.stopImmediatePropagation(); var iOffset = 0, bStatus = 0; var jqLi = $(this).closest("li"); // Reset position if(jqLi.parent().attr("id") === "favorite"){ var jqNewLi = $("#frm_input > ul:not(#favorite) #" + jqLi.attr("id")); // Add to favorites }else{ bStatus = 1; var jqFavBlock = $("#favorite"); var jqNewLi = jqFavBlock.find("#" + jqLi.attr("id")); if(!jqNewLi.length){ jqNewLi = jqLi.clone(true, true).hide(); jqFavBlock.append(jqNewLi); } } iOffset = jqNewLi.offset().top; if(iOffset === 0 && jqNewLi.prev().length) iOffset = jqNewLi.prev().offset().top; if(iOffset === 0 && jqNewLi.next().length) iOffset = jqNewLi.next().offset().top - jqNewLi.next().height(); if(iOffset === 0) iOffset = jqNewLi.parent().offset().top; jqLi.hide("slide", {}, 300, function(){ iOffset -= 30; var i = $("html, body").scrollTop(); JsBase.ajax({ status : bStatus, action : "updateFavorite", id : jqLi.find("a").attr("data-id") }, false, function(d){ if(d && d.jsonok){ if(!(iOffset > i && iOffset < (i + window.innerHeight))) $("html, body").animate({scrollTop: iOffset}, "fast", function(){ jqNewLi.show("slide", {}, 300); }); else jqNewLi.show("slide", {}, 300); }else{ jqLi.show("slide", {}, 300); } }); }); return false; }); } // Bind check boxes $(".chk").each(function(){ var chk = $(this); chk.click(function(){ chk.toggleClass("chked"); }); chk.parent().next().bind("click", function(e){ if(!$(e.target).closest(".js-terms-modal").length) chk.toggleClass("chked"); }); }); if(window.JsBase && $.isFunction(JsBase.bindTermsModalLinks)){ JsBase.bindTermsModalLinks(); } var jqCloseIframe = $("#close-iframe"); var jqIframeOverlay = jqCloseIframe.closest(".wrapper"); if (jqCloseIframe.length){ jqCloseIframe.click(function(e){ e.preventDefault(); return false; }); jqIframeOverlay.click(function(e){ if(jqIframeOverlay[0] === e.target || jqIframeOverlay.find("> div > .row")[0] === e.target) { jqCloseIframe.trigger("click"); } }); } // Rewrite ajax overlay JsBase.ajaxLoader.jqObj.hide(); JsBase.ajaxLoader.jqObj = $("#ajax_overlay"); /* Bind form submits */ var jqForms = $("#frm_input, #passRecovery_cnt, #frm_2fa_input, #pwd_cnt, #reg_form, #profile_cnt"); if(jqForms.length){ jqForms.each(function(){ var jqParent = $(this); jqParent.find("input").bind({ focus : function(){ $(this).parent().addClass("active"); }, blur : function(){ $(this).parent().removeClass("active"); } /*keydown : function(e){ // Submit form on enter if(e.which == JsBase.iKeys.ENTER) jqParent.find("button").click(); }*/ }); jqParent.find("button, #submitFrom").click(function(e){ e.preventDefault(); switch(jqParent.attr("id")){ case "frm_input": JsBase.ajaxLoader.jqAjaxSaveCaller = $(this); _this.submitLoginData(jqParent); break; case "passRecovery_cnt": JsBase.ajaxLoader.jqAjaxSaveCaller = $(this); _this.submitPWDRequest(jqParent); break; case "frm_2fa_input": JsBase.ajaxLoader.jqAjaxSaveCaller = $(this); _this.submit2FARequest(jqParent); break; case "pwd_cnt": JsBase.ajaxLoader.jqAjaxSaveCaller = $(this); _this.submitPwdChange(jqParent); break; case "reg_form": JsBase.ajaxLoader.jqAjaxSaveCaller = $(this); _this.submitRegForm(jqParent); break; case "profile_cnt": JsBase.ajaxLoader.jqAjaxSaveCaller = $(this); _this.submitProfileData(jqParent); break; default: if(_this.oValidator && !_this.oValidator.isFormValid(false)) jqParent.submit(); } return false; }); }); } // Profile form actions /*if($("#fsave").length){ _this.bindProfileActions(); }*/ // Profile wishlist _this.bindProfileNavigation(); _this.bindProfileWishlist(); _this.bindWishlistPage(); _this.bindAddressBook(); _this.bindDeleteAccount(); // 2FA script var jqHTML = $("#frm_2fa_input"); if(jqHTML.length){ // Bind 2fa form _this.oInp2FA = new C2FAController(jqHTML); _this.oInp2FA.sCfgKey = "valCfg"; _this.oInp2FA.bindSingleField2FARequestInputs(); _this.oInp2FA.jqSubmitButton = jqHTML.find("button#submit"); } var readMe = $(".read_me"), infoCnt = $("#info_cnt"); if(readMe.length && infoCnt.length){ // Bind info content hide infoCnt.add(infoCnt.find("span.close, div.accept_close > a")).bind("click", function(e){ if(infoCnt.is(":visible") && e.target === this){ infoCnt.hide(); $('html, body').css("overflow","auto"); } }); // Bind info content show $(readMe).bind("click", function(){ if(!infoCnt.is(":visible")){ infoCnt.fadeIn("fast"); $('html').css("overflow","hidden"); $('body').css("overflow-y","scroll"); } }); } }; PubUsers.prototype.shakeCustomCheckbox = function(jqCheckbox){ var jqTarget = $(jqCheckbox).closest(".agree_to"); if(!jqTarget.length) jqTarget = $(jqCheckbox).closest(".validate"); if(jqTarget.length && $.isFunction($.fn.effect)) jqTarget.stop(true, true).effect("shake", {}, 500); }; PubUsers.prototype.shakeInvalidCheckboxes = function(jqForm){ if(window.JsBase && $.isFunction(JsBase.shakeInvalidCheckboxes)) JsBase.shakeInvalidCheckboxes(jqForm); }; PubUsers.prototype.showCleanSuccessState = function(jqForm, msg){ var jqParent = jqForm.parent(); jqParent.find(".status_error").hide(); if(msg) jqParent.find(".status_ok").html(msg); jqForm.hide(); jqParent.find(".status_ok").fadeIn("normal"); }; PubUsers.prototype.resetStatusMessages = function(jqScope){ $(jqScope).find(".status_error, .status_ok").stop(true, true).hide(); }; PubUsers.prototype.showStatusMessage = function(jqScope, type, msg){ var jqNode = $(jqScope).find(type === "success" ? ".status_ok" : ".status_error").first(); if(!jqNode.length) return; if(msg) jqNode.html(msg); jqNode.stop(true, true).hide().fadeIn("normal"); }; PubUsers.prototype.setAjaxContext = function(jqCaller, jqSaveCaller){ JsBase.ajaxLoader.jqAjaxCaller = $(jqCaller); JsBase.ajaxLoader.jqAjaxSaveCaller = $(jqSaveCaller || jqCaller); JsBase.ajaxLoader.disabledFadeOut = true; }; // Submit pwd change request - forgot password PubUsers.prototype.submitPWDRequest = function(jqForm){ var _this = this; if(!_this.oValidator.isFormValid(false)) return false; jqForm.find(":focus").blur(); var data = { action : "recovery", user_data : jqForm.find("#user_data").val() }; JsBase.ajaxLoader.jqAjaxCaller = jqForm; JsBase.ajax(data, false, function(data){ var p = jqForm.parent(); if(data && data.jsonok){ p.find(".status_error, .info p:not(.ok), .validate, button, a.link").hide(); if(data.msg) p.find(".status_ok").html(data.msg); p.find(".status_ok, .info p.ok, a.btn.btn-block").fadeIn("fast"); }else{ if(data.msg) p.find(".status_error").html(data.msg); p.find(".status_error, .info p:not(.ok)").fadeIn("slow"); } }); return true; }; // Submit pwd change request - forgot password PubUsers.prototype.submit2FARequest = function(jqForm){ var sCode, _this = this; if(_this.oInp2FA) sCode = _this.oInp2FA.chk2FACode(); if(sCode.length){ jqForm.find(":focus").blur(); var vData = { action: "2fa", code: sCode }; if(jqForm.find("#user_flogin").length) vData["user_flogin"] = 1; if(jqForm.find("#user_uoa").length) vData["user_uoa"] = 1; var jqErrStatus = jqForm.closest(".container").find(".status_error").hide(); jqForm.find("#close-iframe").hide(); JsBase.ajaxLoader.jqAjaxCaller = jqForm; JsBase.ajaxLoader.disabledFadeOut = true; JsBase.ajax(vData, false, function(d){ if(d){ // Check if session continued if(d.jsonok && d['continue_session']){ window.top.JsBase.oSession.removeIframeForm(); return false; } // Check if session continued if(d.jsonok && d['loginok']){ window.top.JsBase.jsModule.setDataAndCloseIframe(d.data); return false; } if(d.error){ JsBase.ajaxLoader.hideLoader(true); if(d.data && _this.oInp2FA) _this.oInp2FA.show2FAError(d.data, jqErrStatus); } // Redirect if any url passed if(d.url){ if(window.top && window.top.JsBase && window.top.JsBase.oSession){ window.top.JsBase.oSession.hideIframe(); if(d['back2login']) d.url = window.top.JsBase.oSession.sUrlSrc; } window.location = d.url; } } jqForm.find("#close-iframe").show(); }); } return true; }; // Submit system Login data PubUsers.prototype.submitLoginData = function(jqForm){ var _this = this; if(!_this.oValidator.isFormValid(false)) return false; var jqAgree = $("#agree_to_terms"); if(jqAgree.length && !jqAgree.hasClass("chked")){ _this.shakeCustomCheckbox(jqAgree); return false; } jqForm.find(":focus").blur(); var vData = { action : "login", user_login : jqForm.find("#user_login").val(), user_password : jqForm.find("#user_password").val(), chkTerms : jqAgree.hasClass("chked") ? 1 : 0 }; if(jqForm.find("#user_flogin").length) vData["user_flogin"] = 1; if(jqForm.find("#user_uoa").length) vData["user_uoa"] = 1; jqForm.find("#close-iframe").hide(); JsBase.ajaxLoader.jqAjaxCaller = jqForm; JsBase.ajaxLoader.disabledFadeOut = true; JsBase.ajax(vData, false, function(d){ var p = jqForm.parent(); if(d && d.jsonok){ // Check if session continued if(d['continue_session']){ window.top.JsBase.oSession.removeIframeForm(); return false; } p.find(".status_error").hide(); p.find(".status_ok").fadeIn("normal", function(){ if(d.url){ if(window.top && window.top.JsBase && window.top.JsBase.oSession){ window.top.JsBase.oSession.hideIframe(); if(d['back2login']) d.url = window.top.JsBase.oSession.sUrlSrc; } window.location = d.url; } }); }else{ JsBase.ajaxLoader.hideLoader(true); if(d.data.msg) p.find(".status_error").html(d.data.msg); p.find(".status_error").fadeIn("slow"); if(d.data.chkTerms && jqAgree.length){ _this.shakeCustomCheckbox(jqAgree); return false; } } jqForm.find("#close-iframe").show(); }); return true; }; // Force pwd change submit function PubUsers.prototype.submitPwdChange = function(jqForm){ var _this = this; if(!_this.oValidator.isFormValid(false)) return false; jqForm.find(":focus").blur(); var vData = { action : "pwd", old_password : jqForm.find("#old_password").val(), new_password : jqForm.find("#new_password").val(), rnew_password : jqForm.find("#rnew_password").val() }; if(jqForm.find("#user_uoa").length) vData["user_uoa"] = 1; JsBase.ajaxLoader.jqAjaxCaller = jqForm; JsBase.ajaxLoader.disabledFadeOut = true; JsBase.ajax(vData, false, function(d){ var jqParent = jqForm.parent(); jqParent.find("#act_ok").fadeOut(function(){ $(this).remove(); }); if(d){ if(d.jsonok){ jqParent.find(".status_error").hide(); jqParent.find(".status_ok").fadeIn("normal", function(){ // Check if session continued if(d['continue_session']){ window.top.JsBase.oSession.removeIframeForm(); return false; } // Check if session continued if(d.jsonok && d['loginok']){ window.top.JsBase.jsModule.setDataAndCloseIframe(d.data); return false; } }); }else{ JsBase.ajaxLoader.hideLoader(true); if(d.data.msg) jqParent.find(".status_error").html(d.data.msg); if(d.data.fields) jqParent.find(d.data.fields).removeClass("valid").addClass("invalid").attr("title", d.data.msg); if(d.data['hideinputs']){ // TODO BRS Aivars: Kam šis domāts? jqParent.find(".mid,.bot").hide(); } jqParent.find(".status_error").fadeIn("slow"); } if(d.url){ if(window.top && window.top.JsBase && window.top.JsBase.oSession){ window.top.JsBase.oSession.hideIframe(); if(d['back2login']) d.url = window.top.JsBase.oSession.sUrlSrc; } setTimeout(function(){ window.location = d.url; }, 1000); } } }); return true; }; // Force pwd change submit function PubUsers.prototype.submitRegForm = function(jqForm){ var _this = this; if (!_this.oValidator.isFormValid(false)) return false; var jqAgree = $("#agree_to_terms"); if (jqAgree.length && !jqAgree.hasClass("chked")){ _this.shakeCustomCheckbox(jqAgree); return false; } jqForm.find(":focus").blur(); var oData = { action : "cna", data : {}}; jqForm.find("input, select").each(function(){ if($(this).attr("type") === "checkbox"){ oData.data[$(this).attr("id")] = $(this).is(":checked") ? 1 : 0; }else { oData.data[$(this).attr("id")] = $(this).val(); } }); JsBase.ajaxLoader.jqAjaxCaller = jqForm; JsBase.ajaxLoader.disabledFadeOut = true; JsBase.ajax(oData, false, function(data){ if(data && data.jsonok){ _this.showCleanSuccessState(jqForm, data.msg); }else{ JsBase.ajaxLoader.hideLoader(true); if(data && data.data && data.data.fields){ $.each(data.data.fields, function(id, msg){ jqForm.find("#" + id).removeClass("valid").addClass("invalid").attr("title", msg); }); _this.shakeInvalidCheckboxes(jqForm); } if(data && data.data && data.data.msg) jqForm.parent().find(".status_error").html(data.data.msg); jqForm.parent().find(".status_error").fadeIn("slow") } }); return true; }; // Save profile form PubUsers.prototype.submitProfileData = function(jqForm){ var _this = this; if(!_this.oValidator.isFormValid(false)) return false; jqForm.find(":focus").blur(); var oData = { action : "profileSave", data : {}}; jqForm.find("input, select, textarea").each(function(){ if(!$(this).attr("id")) return; if($(this).attr("type") === "checkbox"){ oData.data[$(this).attr("id")] = $(this).is(":checked") ? 1 : 0; }else{ oData.data[$(this).attr("id")] = $(this).val(); } }); _this.resetStatusMessages(jqForm); _this.setAjaxContext(jqForm, jqForm.find("button[type='submit']").first()); JsBase.ajax(oData, false, function(data){ if(data && data.jsonok){ _this.showStatusMessage(jqForm, "success", data.msg); }else{ JsBase.ajaxLoader.hideLoader(true); if(data && data.data && data.data.fields){ $.each(data.data.fields, function(id, msg){ jqForm.find("#" + id).removeClass("valid").addClass("invalid").attr("title", msg); }); } _this.showStatusMessage(jqForm, "error", data && data.data ? data.data.msg : ""); } }); return true; }; // Load and bind profile wishlist PubUsers.prototype.bindProfileNavigation = function(){ var _this = this; let navButtons = $(".ap-profile .profile-nav-btn"); if (!navButtons.length) { return; } let $activeButton = navButtons.filter("[data-nav-active='true']").first(); let initialSection = $activeButton.data("nav-section") || "profile"; // If no active button set, check if there's an active section indicator if (!$activeButton.length && JsBase.dt.activeSection) { initialSection = JsBase.dt.activeSection; } _this.showProfileSection(initialSection); navButtons.on("click", function(e){ e.preventDefault(); let section = $(this).data("nav-section"); if (section) { _this.showProfileSection(section); if (window.history && window.history.replaceState) { let sectionUrls = JsBase.dt.profileSections || {}; if (sectionUrls[section]) { window.history.replaceState({}, "", sectionUrls[section]); } } } }); // Initialize wishlist button states on page load if (window.JsBase && $.isFunction(JsBase.setWishlistButtonState)) { JsBase.ajax({action: "runWishlist", wl_action: "getIds"}, false, function(data){ if (data && data.jsonok && data.ids) { var wishlistIds = Array.isArray(data.ids) ? data.ids.map(String) : []; $(".add-to-wishlist").each(function(){ var $btn = $(this); var productId = String(_this.getProductIdFromButton($btn)); var isInWishlist = wishlistIds.indexOf(productId) !== -1; if (isInWishlist) { JsBase.setWishlistButtonState($btn, productId, true); } }); } }); } }; PubUsers.prototype.getProductIdFromButton = function($btn){ return $btn.attr("data-product-id") || $btn.data("product-id") || $btn.closest("[data-product-id]").attr("data-product-id") || ""; }; PubUsers.prototype.showProfileSection = function(sectionName){ let $container = $("#ap-detail-container"); let $sections = $container.find("[data-nav-content]"); let $navButtons = $(".ap-profile .profile-nav-btn"); // Fade out current content $sections.fadeOut(200, function(){ // Hide all sections $sections.hide(); // Show selected section $container.find("[data-nav-content='" + sectionName + "']").fadeIn(200); // Update active state in navigation $navButtons.removeClass("active"); $navButtons.filter("[data-nav-section='" + sectionName + "']").addClass("active"); }); }; PubUsers.prototype.bindProfileWishlist = function(){ let _this = this; let wishlist = $(".ap-profile .profile-nav-btn[data-nav-section='wishlist']"); if (!wishlist.length) { return; } // Load wishlist AJAX function extracted for reuse var loadWishlist = function() { _this.setAjaxContext($("#wishlist-container"), wishlist); // Load wishlist on profile page JsBase.ajax({action: "runWishlist", wl_action: "load"}, false, function(data){ if (data && data.jsonok){ let html = data.html ? data.html : ''; let count = data.count ? data.count : 0; let $container = $("#wishlist-container"); let wishlist_empty = '

' + defaultVal(JsBase.gM["empty_wishlist"], "Your wishlist is empty") + '

'; $container.html(html || wishlist_empty); // Scroll to wishlist section $('html, body').animate({scrollTop: $("#ap-detail-container").offset().top - 100}, 300); // Show all rows _this.updateWishlistCount(count); // Bind remove buttons after loading $(document) .off("click.pubWishlistRemove", "#wishlist-container .wishlist-remove-btn") .on("click.pubWishlistRemove", "#wishlist-container .wishlist-remove-btn", function(e){ e.preventDefault(); let $btn = $(this); let productId = $btn.data("product-id"); if (!productId) return; _this.setAjaxContext($container, $btn); JsBase.ajax( {action: "runWishlist", wl_action: "remove", product_id: productId}, false, function(removeData){ if (removeData && removeData.error) { JsBase.showErrorModal(removeData.msg || "Wishlist remove failed"); return; } if(removeData && removeData.jsonok){ if(window.JsBase && $.isFunction(JsBase.setWishlistButtonState)) JsBase.setWishlistButtonState($btn, String(productId), false); $btn.closest(".wish-table-info").slideUp(300, function(){ $(this).remove(); _this.updateWishlistCount(defaultVal(removeData.count, 0)); }); } } ); }); // "Add all to cart" button $container.find(".wishlist-add-all-to-cart").on("click", function(e){ e.preventDefault(); var productIds = []; $container.find(".wish-table-info").each(function(){ var productId = $(this).data("product-id"); if(productId) productIds.push(productId); }); if(!productIds.length) return; // Add each product to cart $.each(productIds, function(i, productId){ JsBase.ajax( {action: "addToCart", p: productId, d1: 0, d2: 0, count: 1}, false, function(cartData){ console.log("Added product " + productId + " to cart"); } ); }); }); // "Continue shopping" button $container.find(".wishlist-continue-shopping").on("click", function(e){ e.preventDefault(); window.location.href = JsBase.dt.shopUrl || '/'; }); } }); }; wishlist.click(function(e){ e.preventDefault(); loadWishlist(); }); // Auto-load wishlist if it's the initial section if (JsBase.dt.activeSection === 'wishlist') { setTimeout(loadWishlist, 300); } }; PubUsers.prototype.bindWishlistPage = function(){ let _this = this; let $container = $("#wishlist-page-container"); let wishlistAjaxUrl = (JsBase.dt && JsBase.dt.baseUrl && JsBase.dt.baseUrl.url ? JsBase.dt.baseUrl.url : window.location.pathname); if (!$container.length) { return; } var renderWishlist = function() { _this.setAjaxContext($container, $container); JsBase.ajax({action: "runWishlist", wl_action: "load", simple_view: 1}, wishlistAjaxUrl, function(data){ if (data && data.error) { JsBase.showErrorModal(data.msg || "Wishlist load failed"); return; } if (data && data.jsonok) { let html = data.html ? data.html : ''; let wishlistEmpty = '

' + defaultVal(JsBase.gM["empty_wishlist"], "Your wishlist is empty") + '

' + defaultVal(JsBase.gM["continue_shopping"], "Continue shopping") + '
'; $container.html(html || wishlistEmpty); _this.updateWishlistCount(defaultVal(data.count, 0)); } }, "POST", function(){ JsBase.showErrorModal("Wishlist load failed"); }); }; $(document) .off("click.pubWishlistPageRemove", "#wishlist-page-container .wishlist-remove-btn") .on("click.pubWishlistPageRemove", "#wishlist-page-container .wishlist-remove-btn", function(e){ e.preventDefault(); let $btn = $(this); let productId = $btn.data("product-id"); if (!productId) return; _this.setAjaxContext($container, $btn); JsBase.ajax( {action: "runWishlist", wl_action: "remove", product_id: productId}, wishlistAjaxUrl, function(removeData){ if (removeData && removeData.error) { JsBase.showErrorModal(removeData.msg || "Wishlist remove failed"); return; } if (removeData && removeData.jsonok) { if (window.JsBase && $.isFunction(JsBase.setWishlistButtonState)) { JsBase.setWishlistButtonState($btn, String(productId), false); } $btn.closest(".wish-table-info").slideUp(250, function(){ $(this).remove(); _this.updateWishlistCount(defaultVal(removeData.count, 0)); if (!$container.find(".wish-table-info").length) { renderWishlist(); } }); } }, "POST", function(){ JsBase.showErrorModal("Wishlist remove failed"); } ); }); }; // Update wishlist counter PubUsers.prototype.updateWishlistCount = function(count){ $(".wishlist-count, .wish-counter, .wishlist-counter").text(count); }; PubUsers.prototype.bindAddressBook = function(){ var _this = this; var $section = $("#address-book-section"); var $form = $("#address-book-form"); if (!$section.length || !$form.length) { return; } var resetForm = function() { $form[0].reset(); $form.find("input[type='hidden']").val(""); $form.find(".status_error, .status_ok").hide(); $form.find(".invalid").removeClass("invalid"); }; var fillForm = function(address) { resetForm(); $.each(address || {}, function(key, value) { var id = ""; switch (key) { case "id": id = "address_entry_id"; break; case "label": id = "address_label"; break; case "first_name": id = "address_first_name"; break; case "last_name": id = "address_last_name"; break; case "company": id = "address_company"; break; case "phone": id = "address_phone"; break; case "address1": id = "address_address1"; break; case "address2": id = "address_address2"; break; case "city": id = "address_city"; break; case "state": id = "address_state"; break; case "country": id = "address_country"; break; case "zip": id = "address_zip"; break; case "type": id = "address_type"; break; } if (id) { $form.find("#" + id).val(value); } }); $form.find("#address_is_default_shipping").prop("checked", !!address.is_default_shipping); $form.find("#address_is_default_billing").prop("checked", !!address.is_default_billing); }; $section.on("click", ".js-add-address", function(e){ e.preventDefault(); resetForm(); _this.showProfileSection("addresses"); document.getElementById("address-form-section").style.display = "block"; $("html, body").animate({scrollTop: $("#address-form-section").offset().top - 120}, "fast"); }); $section.on("click", ".js-edit-address", function(e){ e.preventDefault(); var raw = $(this).attr("data-address") || "{}"; fillForm(JSON.parse(raw)); _this.showProfileSection("addresses"); document.getElementById("address-form-section").style.display = "block"; $("html, body").animate({scrollTop: $("#address-form-section").offset().top - 120}, "fast"); }); $section.on("click", ".js-edit-address-inline", function(e){ e.preventDefault(); var raw = $(this).attr("data-address") || "{}"; fillForm(JSON.parse(raw)); document.getElementById("address-form-section").style.display = "block"; $("html, body").animate({scrollTop: $("#address-form-section").offset().top - 120}, "fast"); }); $section.on("click", ".js-delete-address", function(e){ e.preventDefault(); var addressId = $(this).data("addressId"); if (!addressId || !window.confirm(defaultVal(JsBase.gM["Are you sure you want to delete this address?"], "Are you sure you want to delete this address?"))) { return; } _this.setAjaxContext($section, $(this)); JsBase.ajax({action: "addressBookDelete", address_id: addressId}, false, function(resp){ if (resp && resp.jsonok && resp.reload_url) { window.location.href = resp.reload_url; } }); }); $section.on("click", ".js-set-default-address", function(e){ e.preventDefault(); var addressId = $(this).data("addressId"); var type = $(this).data("defaultType"); if (!addressId || !type) { return; } _this.setAjaxContext($section, $(this)); JsBase.ajax({action: "addressBookSetDefault", address_id: addressId, default_type: type}, false, function(resp){ if (resp && resp.jsonok && resp.reload_url) { window.location.href = resp.reload_url; } }); }); $form.on("submit", function(e){ e.preventDefault(); var payload = {action: "addressBookSave", data: {}}; $form.find("input, select, textarea").each(function(){ if (!$(this).attr("id")) { return; } if ($(this).attr("type") === "checkbox") { payload.data[$(this).attr("id")] = $(this).is(":checked") ? 1 : 0; } else { payload.data[$(this).attr("id")] = $(this).val(); } }); _this.resetStatusMessages($form); _this.setAjaxContext($form, $form.find("button[type='submit']").first()); JsBase.ajax(payload, false, function(resp){ if (resp && resp.jsonok && resp.reload_url) { window.location.href = resp.reload_url; return; } if (resp && resp.data && resp.data.fields) { $.each(resp.data.fields, function(id, msg){ $form.find("#" + id).addClass("invalid").attr("title", msg); }); } _this.showStatusMessage($form, "error", resp && resp.data ? resp.data.msg : ""); }); }); $form.on("click", ".js-address-cancel", function(e){ e.preventDefault(); resetForm(); document.getElementById("address-form-section").style.display = "none"; }); }; PubUsers.prototype.bindDeleteAccount = function(){ var _this = this; var $form = $("#delete-account-form"); if (!$form.length) { return; } $form.on("submit", function(e){ e.preventDefault(); var payload = {action: "deleteAccount", data: {}}; $form.find("input").each(function(){ if (!$(this).attr("id")) { return; } payload.data[$(this).attr("id")] = ($(this).attr("type") === "checkbox" ? ($(this).is(":checked") ? 1 : 0) : $(this).val()); }); _this.resetStatusMessages($form); _this.setAjaxContext($form, $form.find("button[type='submit']").first()); JsBase.ajax(payload, false, function(resp){ if (resp && resp.jsonok && resp.url) { window.location.href = resp.url; return; } if (resp && resp.data && resp.data.fields) { $.each(resp.data.fields, function(id, msg){ if (!msg) { return; } $form.find("#" + id).addClass("invalid").attr("title", msg); }); } _this.showStatusMessage($form, "error", resp && resp.data ? resp.data.msg : ""); }); }); }; return PubUsers; })(); })();