"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") + '
' + defaultVal(JsBase.gM["empty_wishlist"], "Your wishlist is empty") + '
' + defaultVal(JsBase.gM["continue_shopping"], "Continue shopping") + '