luv-assets 1.2.11 → 1.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/js/product.min.js +1 -1
- package/package.json +1 -1
package/dist/js/product.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function saveProductToLocalStorage(product_spu,maxHistoryLength=30){let productHistory=JSON.parse(localStorage.getItem(product_history_key))||[];const index=productHistory.indexOf(product_spu);if(index>-1){productHistory.splice(index,1)}productHistory.unshift(product_spu);if(productHistory.length>maxHistoryLength){productHistory=productHistory.slice(0,maxHistoryLength)}localStorage.setItem(product_history_key,JSON.stringify(productHistory))}function fetchAndDisplayViewedProducts(history_url){let productHistory=JSON.parse(localStorage.getItem(product_history_key)||"[]");if(productHistory.length===0){return}const viewedProductsElement=$("#viewed-products-js");$.ajax({url:history_url,method:"POST",data:{productIds:productHistory},success:function(response){viewedProductsElement.html(response.html);if(typeof lazyLoadInstance!=="undefined"){lazyLoadInstance.update()}},error:function(){viewedProductsElement.html("<p>Failed to load viewed products.</p>")}})}function scrollToElement(targetElement,header_height){const headerHeight=header_height||60;const targetOffsetTop=targetElement.offset().top-headerHeight;$("html, body").animate({scrollTop:targetOffsetTop},{duration:300,easing:"swing"})}function renderSizeChartEle(){const sizes=Object.keys($size_charts);for(i=0;i<sizes.length;i++){const attr_code=sizes[i];const data=$size_charts[attr_code];renderTableEle(data,attr_code,attr_code+"Table"+default_size_unit,default_size_unit);renderTableEle(data,attr_code,attr_code+"Table"+second_size_unit,second_size_unit)}}function renderTableEle(data,attr_code,tableId,system){const $table=$(`#${tableId}`);const $thead=$table.find("thead tr");const $tbody=$table.find("tbody");const key=system.toLowerCase();const attrNames=[];for(const size in data){data[size].forEach(item=>{if(!attrNames.includes(item.attr_name)){attrNames.push(item.attr_name)}});break}$thead.empty().append(`<th>${translations.size}</th>`);attrNames.forEach(name=>{$thead.append(`<th>${name}</th>`)});$tbody.empty();for(const value_code in data){let value_name;if(typeof $sku_map[attr_code]!=="undefined"&&typeof $sku_map[attr_code]["values"]!=="undefined"&&typeof $sku_map[attr_code]["values"][value_code]!=="undefined"){value_name=$sku_map[attr_code]["values"][value_code]["value_name"]}else{continue}const row=["<tr><td>"+value_name+"</td>"];const measurements={};data[value_code].forEach(m=>{let val;if(key==="inch"){if(m.ft!==""){val=m.ft}else{val=m.inch}}else{val=m[key]}measurements[m.attr_name]=`${val}`});attrNames.forEach(name=>{row.push(`<td>${measurements[name]||"—"}</td>`)});row.push("</tr>");$tbody.append(row.join(""))}}function loadProductDetail(productInfo){let rows="";if(productInfo.spu_attr.length>0){for(let index=0;index<productInfo.spu_attr.length;index++){const detail=productInfo.spu_attr[index];const row=`\n <tr>\n <td>${detail.attr_name}</td>\n <td>${detail.value_name}</td>\n </tr>`;rows+=row}}if(productInfo.custom_attr.length>0){for(let index=0;index<productInfo.custom_attr.length;index++){const detail=productInfo.custom_attr[index];const tr="<tr>";const td=`<td>${detail.attr_name}</td>`;let lists="<td><ul>";for(let i=0;i<detail.list.length;i++){const value=detail.list[i];const listItem=`<li>${value.measurements_name}: ${value.value_with_unit}</li>`;lists+=listItem}lists+="</ul></td>";rows+=tr+td+lists+"</tr>"}}$(".product_detail_img").attr("src",productInfo["image"]);$(".product_detail_name").html(productInfo["name"]);$(".product_detail_price").html(`${ecommerce_product.symbol}${productInfo["price"]}`);$(".product_qty_val").html(productInfo["qty"]);$(".product-options-js tbody").html(rows);return}function parseHeight(heightStr){const match=heightStr.match(/^(\d+)'?(\d*)"?$/);if(!match){throw new Error("Invalid height format. Expected format like '5'6 or 5'6\"")}const feet=parseInt(match[1],10)||0;const inches=parseInt(match[2],10)||0;return{feet:feet,inches:inches}}function initCustomize(relatedAttr,sizeSystem,attrValue){let sizeUnit;if(sizeSystem==="imperial"){sizeUnit="inch"}else{sizeUnit="cm"}let selectedAttr={};let pointCode;for(let i=0;i<$size_charts[relatedAttr][attrValue].length;i++){pointCode=$size_charts[relatedAttr][attrValue][i]["point_code"];if(pointCode==="height"){if(sizeUnit==="inch"){const{feet:feet,inches:inches}=parseHeight($size_charts[relatedAttr][attrValue][i]["ft"]);$("#feet").val(feet);$("#inches").val(inches)}else{$("#cm").val($size_charts[relatedAttr][attrValue][i][sizeUnit])}}else{selectedAttr[pointCode]=$size_charts[relatedAttr][attrValue][i][sizeUnit]}}const $inputs=$(`#${relatedAttr}-custom-form-container input.custom-input-js`);$inputs.each(function(){const name=$(this).attr("name");if(selectedAttr[name]){$(this).val(selectedAttr[name])}})}function calculateProductPrice(productInfo){let totalPrice=parseFloat(ecommerce_product.price);if(productInfo["spu_attr"]&&typeof productInfo["spu_attr"]==="object"&&productInfo["spu_attr"]!==null){for(let key in productInfo["spu_attr"]){if(productInfo["spu_attr"][key].value_price>0){totalPrice+=parseFloat(productInfo["spu_attr"][key].value_price)}}}if(productInfo["custom_attr"]&&typeof productInfo["custom_attr"]==="object"&&productInfo["custom_attr"]!==null){for(let key in productInfo["custom_attr"]){if(productInfo["custom_attr"][key].value_price>0){totalPrice+=parseFloat(productInfo["custom_attr"][key].value_price)}}}totalPrice=totalPrice.toFixed(2);product_info.price=totalPrice;return totalPrice}function checkCustomInfo($input){const value=$input.val();const required=$input.prop("required");if(required&&(value===""||value===0)){return false}const min=parseFloat($input.attr("min"));if(!isNaN(min)&&value<min){return false}const max=parseFloat($input.attr("max"));if(!isNaN(max)&&value>max){return false}return true}function getSpuAttrValueInfo(attr,attrValue){const attrValueInfo={attr_code:attr,value_code:attrValue,attr_name:$sku_map[attr].attr_name,value_name:$sku_map[attr]["values"][attrValue].value_name,value_price:$sku_map[attr]["values"][attrValue].value_price,base_value_price:$sku_map[attr]["values"][attrValue].base_value_price};return attrValueInfo}function getCustomAttrValueInfo(attr,attrValue,value_price,base_value_price){let value_code,value_name;if(!attrValue){value_code="";value_name=""}else{value_code=attrValue;value_name=$sku_map[attr]["values"][attrValue].value_name}const attrValueInfo={attr_code:attr,value_code:value_code,attr_name:$sku_map[attr].attr_name,value_name:value_name,value_price:value_price,base_value_price:base_value_price};return attrValueInfo}function setProductSpuAttr(attr,attrValue,options){const attrValueInfo=getSpuAttrValueInfo(attr,attrValue);const newSpuAttr={...product_info.spu_attr,[attr]:attrValueInfo};if(product_info.custom_attr&&product_info.custom_attr.hasOwnProperty(attr)){delete product_info.custom_attr[attr]}const updates={spu_attr:newSpuAttr};if(options&&options.attrImg){updates.image=options.imgSrc}updateProductInfo(updates)}function setProductCustomAttr(attr,attrValue,value_price,base_value_price){const attrValueInfo=getCustomAttrValueInfo(attr,attrValue,value_price,base_value_price);const newCustomAttr={...product_info.custom_attr,[attr]:attrValueInfo};if(product_info.spu_attr&&product_info.spu_attr.hasOwnProperty(attr)){delete product_info.spu_attr[attr]}updateProductInfo({custom_attr:newCustomAttr})}function switchToSpuAttr(attr,attrValue){if(product_info.custom_attr&&product_info.custom_attr.hasOwnProperty(attr)){delete product_info.custom_attr[attr]}if(attrValue){setProductSpuAttr(attr,attrValue)}}function initProductAttr(){product_info["name"]=ecommerce_product.name;product_info["price"]=ecommerce_product.price;product_info["image"]=$("#product-main-image-js").val();product_info["product_id"]=ecommerce_product.id;product_info["spu"]=ecommerce_product.spu;product_info["qty"]=1;product_info["spu_attr"]={};product_info["custom_attr"]={};if($selected_sku_list.length>0){for(let index=0;index<$selected_sku_list.length;index++){const attr=$selected_sku_list[index].attr_code;const attrValue=$selected_sku_list[index].value_code;setProductSpuAttr(attr,attrValue)}}}function getFilenameFromImgSrc(imgSrc){const filename=imgSrc.split("/").splice(-1).toString();return filename}function getIndexByImgSrc(imgSrc){let index=-1;$("#main-swiper-js .swiper-slide img").each(function(i,element){const currentImgSrc=$(this).data("src");const filename=getFilenameFromImgSrc(currentImgSrc);if(filename===getFilenameFromImgSrc(imgSrc)){index=i;return}});return index}function goToSlide(index,speed=null){if(isMobile){swiper.slideTo(index,speed,false)}else{thumbSwiper.slideTo(index,speed,false);mainSwiper.slideTo(index,speed,false)}}function updateProductInfo(updates){const keys=Object.keys(updates);for(let index=0;index<keys.length;index++){const key=keys[index];if(key==="image"){const imgSrc=updates[key];const index=getIndexByImgSrc(imgSrc);goToSlide(index)}}Object.assign(product_info,updates);$(document).trigger("product:updated",[product_info])}function renderProductUI(info){const price=calculateProductPrice(info);$("#total-price").text(`${I18nHelper.formatCurrency(price,ecommerce_product.currency)}`)}function showAttrErrorMessage(attr){$(`#${attr}-error-message`).addClass("d-block")}function hideAttrErrorMessage(attr){$(`#${attr}-error-message`).removeClass("d-block")}function isAllRequiredAttrsSelected(requiredAttrs=$spu_attr){return requiredAttrs.every(attr=>product_info.spu_attr[attr]||product_info.custom_attr[attr])}function getProductInfo(){var result={success:false,data:{name:product_info.name,product_id:product_info.product_id,spu:product_info.spu,qty:product_info.qty,image:product_info.image,price:product_info.price,spu_attr:[],size_chart:{},custom_attr:[]},errors:[]};var $selected_size_charts={};for(var index=0;index<$spu_attr.length;index++){var errror={type:null,key:null,jQueryElement:null};var attr=$spu_attr[index];if(product_info.spu_attr&&typeof product_info.spu_attr==="object"&&product_info.spu_attr!==null&&product_info.spu_attr[attr]&&typeof product_info.spu_attr[attr]==="object"&&product_info.spu_attr[attr]!==null){result.data.spu_attr.push(product_info.spu_attr[attr]);var value_code=product_info.spu_attr[attr].value_code;if($size_charts.hasOwnProperty(attr)&&$size_charts[attr].hasOwnProperty(value_code)){$selected_size_charts[attr]=$size_charts[attr][value_code]}}else if(product_info.custom_attr&&typeof product_info.custom_attr==="object"&&product_info.custom_attr!==null&&product_info.custom_attr[attr]&&typeof product_info.custom_attr[attr]==="object"&&product_info.custom_attr[attr]!==null){product_info.custom_attr[attr].list=[];cusResult=collectCustomMeasurements(attr);if(cusResult.errors.length>0){errror.type="custom_attr";errror.jQueryElement=cusResult.errors[0];result.errors.push(errror);return result}else{cusResult.data.forEach(function(item){product_info.custom_attr[attr].list.push(item)})}result.data.custom_attr.push(product_info.custom_attr[attr])}else{errror.type="spu_attr";errror.key=attr;errror.jQueryElement=$(`#${attr}`);result.errors.push(errror);return result}}if(Object.keys($selected_size_charts).length>0){product_info.size_chart=$selected_size_charts;result.data.size_chart=product_info.size_chart}result.success=true;return result}function renderErrorMsg(err){scrollToElement(err.jQueryElement);if(err.type==="spu_attr"){showAttrErrorMessage(err.key)}else if(err.type==="custom_attr"){err.jQueryElement.addClass("is-invalid");err.jQueryElement.focus()}}function collectCustomMeasurements(attr){var result={success:true,data:[],errors:[]};var $container=$("#"+attr+"-custom-form-container");$container.find("div.input-group").each(function(){var $group=$(this);if(!$group.is(":visible")){return true}var $input=$group.find("input").first();if(!checkCustomInfo($input)){result.success=false;result.errors.push($input);return false}var name=$input.attr("name");var $labelSpan=$group.find(".measurements-label-js");var measurements_name=$labelSpan.length?$labelSpan.text().trim():"Unknown";if(name==="feet"){var feet=$input.val();var $inchesEle=$group.find("#inches");if(!checkCustomInfo($inchesEle)){result.success=false;result.errors.push($inchesEle);return false}var inches=$inchesEle.val();if(feet!==""&&inches!==""){var measurements_code=$group.data("measurements-code");result.data.push({measurements_code:measurements_code,measurements_name:measurements_name,value_with_unit:feet+"'"+inches+'"'})}}else{var value=$input.val();if(value!==""){var measurements_code=name;var $unitSpan=$group.find(".custom-input-unit-js");var unit=$unitSpan.length?$unitSpan.text().trim():"";result.data.push({measurements_code:measurements_code,measurements_name:measurements_name,value_with_unit:value+" "+unit})}}});return result}function init_share(){var ogUrl=$('meta[property="og:url"]').attr("content");var ogTitle=$('meta[property="og:title"]').attr("content");var elems=$("[data-sharer]");for(var i=0;i<elems.length;i++){elems[i].setAttribute("data-url",ogUrl);elems[i].setAttribute("data-title",ogTitle)}$("#product-link-url-input-js").val(ogUrl);return ogUrl}function init_share_m(aff){var ogUrl=init_share();const aff_key=aff.affiliate_keyword;const aff_uuid=aff.affiliate_uuid;if(aff_key&&aff_uuid){$("#affiliate-link-container-js").removeClass("d-none");ogUrl=UrlUtils.addQueryParam(aff_key,aff_uuid,ogUrl)}$("#affiliate-link-url-input-js").val(ogUrl)}function init_share_pc(){var ogUrl=init_share();const aff_key=$("#aff-key-js").val();const aff_uuid=$("#aff-uuid-js").val();if(aff_key&&aff_uuid){ogUrl=UrlUtils.addQueryParam(aff_key,aff_uuid,ogUrl)}$("#affiliate-link-url-input-js").val(ogUrl)}function handleFavoriteClickThrottle(e){e.preventDefault();var self=$(this);$.ajax({url:productFavoriteUrl,method:"GET",data:{spu:ecommerce_product.spu},dataType:"json",timeout:5e3}).done(function(response){loginStatus=response.loginStatus;if(!loginStatus){window.location.href=memberLoginUrl;return}if(response.favoriteStatus){self.addClass("text-danger")}else{self.removeClass("text-danger")}showToast(response.content,"#toast-js")}).fail(function(){alert("Operation failed. Please try again later.")}).always(function(){})}function handleFavoriteClickA(e){e.preventDefault();e.stopPropagation();var self=$(this);if(self.prop("disabled")){return}self.prop("disabled",true);self.addClass("opacity-50");$.ajax({url:productFavoriteUrl,method:"GET",data:{spu:ecommerce_product.spu},dataType:"json",timeout:5e3}).done(function(response){loginStatus=response.loginStatus;if(!loginStatus){window.location.href=memberLoginUrl;return}if(response.favoriteStatus){self.addClass("text-danger")}else{self.removeClass("text-danger")}showToast(response.content,"#toast-js")}).fail(function(){alert("Operation failed. Please try again later.")}).always(function(){self.prop("disabled",false);self.removeClass("opacity-50")})}function handleFavoriteClickB(e){e.preventDefault();e.stopPropagation();var self=$(this);self.toggleClass("text-danger");$.ajax({url:productFavoriteUrl,method:"GET",data:{spu:ecommerce_product.spu},dataType:"json",timeout:5e3}).done(function(response){loginStatus=response.loginStatus;if(!loginStatus){window.location.href=memberLoginUrl;return}if(response.favoriteStatus){self.addClass("text-danger")}else{self.removeClass("text-danger")}showToast(response.content,"#toast-js")}).fail(function(){self.toggleClass("text-danger");showToast("Operation failed","#toast-js")}).always(function(){})}function checkRemark(customer_remark){if(customer_remark.length>order_remark_max_length){return false}return true}function handleAddToCartClick(e){e.preventDefault();e.stopPropagation();var self=$(this);if(self.prop("disabled")){return}self.prop("disabled",true);self.addClass("opacity-50");var result=getProductInfo();if(!result.success){self.prop("disabled",false);self.removeClass("opacity-50");showToast("Failed to obtain product information. Please refresh and try again.","#toast-js");return}var remark=$("#customer-remark-js").val();if(!checkRemark(remark)){self.prop("disabled",false);self.removeClass("opacity-50");showToast(order_remark_message,"#toast-js")}var data_params=result.data;data_params["customer_remark"]=remark;$.ajax({async:true,timeout:6e3,dataType:"json",type:"post",data:JSON.stringify(data_params),url:addToCartUrl,success:function(response){if(response.status=="success"){var items_count=response.items_count;$(".cart-item-count-js").text(items_count);var $modal=$("#productBottomSheet");var modalInstance=bootstrap.Modal.getOrCreateInstance($modal[0]);modalInstance.hide()}showToast(response.message,"#toast-js")},error:function(XMLHttpRequest,textStatus,errorThrown){let message="Operation failed. Please try again later.";if(textStatus==="timeout"){message="Request timed out. Please check your network and try again."}else if(XMLHttpRequest.status===0){message="Network connection failed. Please check your internet."}else if(XMLHttpRequest.status===401){window.location.href=memberLoginUrl;return}else if(XMLHttpRequest.status>=500){message="Server is busy. Please try again later."}else if(XMLHttpRequest.status>=400){message="Invalid request. Please try again."}else{console.error("AJAX Error:",textStatus,errorThrown)}},complete:function(){self.prop("disabled",false);self.removeClass("opacity-50")}})}function paypalButton(){var FUNDING_SOURCES=[paypal.FUNDING.PAYPAL];FUNDING_SOURCES.forEach(function(fundingSource){var button=paypal.Buttons({fundingSource:fundingSource,style:{height:40,label:"buynow"},onClick:function(){var remark=$("#customer-remark-js").val();if(!checkRemark(remark)){showToast(order_remark_message,"#toast-js");return false}if(!isMobile){var result=getProductInfo();if(!result.success){renderErrorMsg(result.errors[0]);return false}}return true},createOrder:(data,actions)=>{var result=getProductInfo();var data_params=result.data;data_params["customer_remark"]=$("#customer-remark-js").val();return fetch(paypalCreateOrderUrl,{method:"post",body:JSON.stringify(data_params),headers:{"content-type":"application/json"}}).then(response=>{if(!response.ok){throw new Error("Network response was not ok")}return response.json()}).then(order=>{if(order.id=="-1"){throw new Error(order.message)}return order.id}).catch(error=>{alert(error)})},onCancel:data=>{},onApprove:(data,actions)=>fetch(paypalCaptureOrderUrl,{method:"post",body:JSON.stringify(data),headers:{"content-type":"application/json"}}).then(response=>{if(!response.ok){throw new Error("Network response was not ok!")}return response.json()}).then(orderData=>{const order_sn=orderData.order_sn;if(order_sn=="-1"){if(!orderData.is_local){if(orderData.error_issue==="INSTRUMENT_DECLINED"){alert(orderData.message);return actions.restart()}else{throw new Error(orderData.message)}}else{}}actions.redirect(`${paypalFinishOrderUrl}?order_sn=${order_sn}`)}).catch(error=>{alert(error)})});if(button.isEligible()){button.render("#product-paypal-container-js")}})}function paypalBuynowCreatePayment(){const container=document.getElementById("product-paypal-container-js");if(container.dataset.rendered==="1")return;paypalButton();container.dataset.rendered="1"}function switchSizeUnit(unit){currentUnit=unit;const activeTabContentPrefix=$("#sizeTabs .nav-link.active").data("content-prefix");const contentId=activeTabContentPrefix+unit;$("#"+contentId).addClass("show active").siblings(".tab-pane").removeClass("show active");if(unit===default_size_unit){$("#switchToDefault-js").addClass("btn-danger").removeClass("btn-secondary");$("#switchToSecond-js").addClass("btn-secondary").removeClass("btn-danger")}else{$("#switchToSecond-js").addClass("btn-danger").removeClass("btn-secondary");$("#switchToDefault-js").addClass("btn-secondary").removeClass("btn-danger")}}var lazyLoadInstance=null;var swiper;var thumbSwiper;var mainSwiper;$(function(){lazyLoadInstance=new LazyLoad({});initProductAttr();fetchAndDisplayViewedProducts(historyUrl);saveProductToLocalStorage(ecommerce_product.spu);$(document).on("product:updated",(e,info)=>{renderProductUI(info)});if(isMobile){if(products_more_count>products_more_count_min){$("#product-list-more").infiniteScroll({path:function(){if(this.loadCount<products_more_max_page-1){return UrlUtils.mergeParams(productsMoreUrl,{p:this.loadCount+1})}},append:false,history:false,responseBody:"json",fetchOptions:{method:"GET",headers:{"X-Requested-With":"XMLHttpRequest","Content-Type":"application/json",Accept:"application/json"}},checkLastPage:true,scrollThreshold:100,status:".page-load-status",prefill:false,debug:false});$("#product-list-more").on("load.infiniteScroll",function(event,body,path){if(body&&body.html&&body.html.trim()!==""){$(this).append(body.html);if(typeof lazyLoadInstance!=="undefined"){lazyLoadInstance.update()}}});$("#product-list-more").on("error.infiniteScroll",function(event,error,path){alert("Failed to load more products.")});$("#product-list-more").on("append.infiniteScroll",function(event,body,path,items){});$("#product-list-more").on("last.infiniteScroll",function(){})}swiper=new Swiper(".swiper",{autoplay:{delay:5e3},pagination:{el:".swiper-pagination",type:"fraction",renderFraction:function(currentClass,totalClass){return'<span class="swiper-pagination-current">'+currentClass+'</span> / <span class="swiper-pagination-total">'+totalClass+"</span>"}}});$("#product-detail-icon-share-js").click(function(e){e.preventDefault();let requestData={};$.get(shareUrl,requestData).done(function(res){if(res.success){init_share_m(res);var $modal=$("#shareBottomSheet");var modalInstance=new bootstrap.Modal($modal[0]);modalInstance.show()}}).fail(function(){})});$("#shareBottomSheet").on("hidden.bs.modal",function(){});$(".product-view-js").on("click","button",function(e){e.preventDefault();var result=getProductInfo();if(!result.success){renderErrorMsg(result.errors[0]);return false}loadProductDetail(result.data);var trigger=$(this).data("trigger");$("#productBottomSheetModalLabel").text($(this).text());if(trigger=="product-add-cart-view-js"){$("#product-add-cart-js").removeClass("d-none");$("#product-paypal-container-js").addClass("d-none")}else if(trigger=="product-buy-now-view-js"){if(typeof paypal=="undefined"){webLoadScript(paypalSdkUrl,function(){paypalBuynowCreatePayment()})}else{paypalBuynowCreatePayment()}$("#product-add-cart-js").addClass("d-none");$("#product-paypal-container-js").removeClass("d-none")}var $modal=$("#productBottomSheet");$modal.data("trigger-source",trigger);var modalInstance=bootstrap.Modal.getOrCreateInstance($modal[0]);modalInstance.show()});$("#productBottomSheet").on("hidden.bs.modal",function(){var triggerSource=$(this).data("trigger-source");if(triggerSource==="product-buy-now-view-js"){$("#paypal-button-container").empty()}$(this).removeData("trigger-source")})}else{thumbSwiper=new Swiper("#thumb-swiper-js",{autoplay:false,direction:"vertical",slidesPerView:5,spaceBetween:5,navigation:{nextEl:"#swiper-button-next-js",prevEl:"#swiper-button-prev-js"},mousewheel:{invert:true},lazy:false,autoHeight:false,allowTouchMove:false,watchOverflow:true});mainSwiper=new Swiper("#main-swiper-js",{autoplay:false,lazy:false,thumbs:{swiper:thumbSwiper}});if(!showArrows){$("#swiper-button-prev-js, #swiper-button-next-js").addClass("d-none")}mainSwiper.on("slideChange",function(){const color=mainSwiper.slides[mainSwiper.activeIndex]?.dataset.color||"";$(window).trigger("productImageColorChange",{colorCode:color})});$("#thumb-swiper-js .swiper-slide").on("mouseenter",function(){const index=$(this).index();mainSwiper.slideTo(index,0)});$("#main-swiper-js .zoom-js").ezPlus({attrImageZoomSrc:"zoom-image"});const $containerShare=$("#sharejs-container-js");if($containerShare.length>0){let requestData={};$.get(shareUrl,requestData).done(function(res){if(res.success&&res.html){$containerShare.empty().html(res.html);init_share_pc()}}).fail(function(){})}if(typeof paypal=="undefined"){webLoadScript(paypalSdkUrl,function(){paypalButton()})}else{paypalButton()}}$(document).on("click",".btn-copy-link-js",function(e){e.preventDefault();const $btn=$(this);const targetId=$btn.data("clipboard-target");const $input=$("#"+targetId);if(!$input.length){return}const textToCopy=$input.val().trim();if(!textToCopy){return}copyTextToClipboard(textToCopy).then(function(success){$btn.find(".copy-label-js").addClass("d-none");$btn.find(".copy-label-copied-js").removeClass("d-none");setTimeout(function(){$btn.find(".copy-label-js").removeClass("d-none");$btn.find(".copy-label-copied-js").addClass("d-none")},2e3);if(!success){}})});$("#panelsStayOpen-sizeChart").on("show.bs.collapse",function(){if(!$(this).data("rendered")){renderSizeChartEle();$(this).data("rendered",true)}});let currentUnit=default_size_unit;$("#switchToDefault-js").on("click",function(){switchSizeUnit(default_size_unit)});$("#switchToSecond-js").on("click",function(){switchSizeUnit(second_size_unit)});$("#sizeTabs .nav-link").on("shown.bs.tab",function(){switchSizeUnit(currentUnit)});$(".product-info-attr-js").on("click",".attr-value-js button",function(e){e.preventDefault();const self=$(this);const $fieldset=self.closest("fieldset");const attr=$fieldset.attr("id");const attrValue=self.data("attr-value");self.closest("div").find("button").removeClass("border-danger");self.removeClass("border-light");self.addClass("border-danger");hideAttrErrorMessage(attr);$fieldset.find(".product-selected-attr-value-js").data("value",attrValue).data("value-for-restore",attrValue).text($sku_map[attr]["values"][attrValue].value_name);const attrHasPrice=$sku_map[attr].attr_has_price;const attrImg=self.data("attr-img");const options={attrHasPrice:attrHasPrice,attrImg:attrImg};if(attrImg===1){var img_src=self.find("img").attr("src");options.imgSrc=img_src}setProductSpuAttr(attr,attrValue,options);const showSizeChart=$sku_map[attr].show_size_value_chart;if(showSizeChart===1){const $chartContainer=$fieldset.find(".product-selected-chart-js");const $chartBody=$chartContainer.find("tbody");if($size_charts[attr][attrValue]){let rows="";$.each($size_charts[attr][attrValue],function(index,detail){const imperialValue=detail.ft?detail.ft:detail.inch?detail.inch+" in":"";const row=`\n <tr>\n <td>${detail.attr_name}</td>\n <td>${imperialValue}</td>\n <td>${detail.cm} cm</td>\n </tr>`;rows+=row});$chartBody.html(rows);$chartContainer.removeClass("d-none")}}const showSizeCustom=$sku_map[attr].show_size_custom;if(showSizeCustom===1){const $customCheckbox=$fieldset.find(".custom-toggle-checkbox-js");if($customCheckbox.is(":checked")){$customCheckbox.prop("checked",false).trigger("change")}}});$(".product-info-attr-js").on("change",".custom-toggle-checkbox-js",function(){const self=$(this);const customPrice=self.data("custom_price");const baseCustomPrice=self.data("base-custom-price");const $fieldset=self.closest("fieldset");const targetFormId=self.attr("aria-controls");const isChecked=self.is(":checked");$("#"+targetFormId).toggleClass("d-none",!isChecked).attr("aria-hidden",!isChecked);self.attr("aria-expanded",isChecked);const relatedAttr=$fieldset.attr("id");const relatedAttrValue=$fieldset.find(".product-selected-attr-value-js").data("value-for-restore");if(isChecked){$fieldset.find(".d-flex button").removeClass("border-danger");$fieldset.find(".product-selected-attr-value-js").data("value","").text("");hideAttrErrorMessage(relatedAttr);if(relatedAttrValue){const size_unit=$("#"+targetFormId).find(".unit-selector-js").val();initCustomize(relatedAttr,size_unit,relatedAttrValue)}$fieldset.find(".product-selected-chart-js").addClass("d-none");setProductCustomAttr(relatedAttr,relatedAttrValue,customPrice,baseCustomPrice)}else{$fieldset.find(".d-flex button").each(function(){if($(this).data("attr-value")==relatedAttrValue){$(this).removeClass("border-light");$(this).addClass("border-danger")}});if(relatedAttrValue){$fieldset.find(".product-selected-attr-value-js").data("value",relatedAttrValue).data("value-for-restore",relatedAttrValue).text($sku_map[relatedAttr]["values"][relatedAttrValue].value_name)}$fieldset.find(".product-selected-chart-js").removeClass("d-none");switchToSpuAttr(relatedAttr,relatedAttrValue)}});$(".product-info-attr-js").on("change",".unit-selector-js",function(){const $this=$(this);const sizeUnit=$this.val();const $fieldset=$this.closest("fieldset");const relatedAttr=$fieldset.attr("id");const relatedAttrValue=$fieldset.find(".product-selected-attr-value-js").data("value-for-restore");const imperialHeight=$("#imperial-height");const metricHeight=$("#metric-height");const weightUnitLabel=$("#weight-unit-label");const $cus_units=$fieldset.find(".custom-input-unit-js");if(sizeUnit==="imperial"){if(imperialHeight){imperialHeight.removeClass("d-none");metricHeight.addClass("d-none");weightUnitLabel.text("lbs")}$cus_units.each(function(){$(this).text("in")})}else{if(imperialHeight){imperialHeight.addClass("d-none");metricHeight.removeClass("d-none");weightUnitLabel.text("kg")}$cus_units.each(function(){$(this).text("cm")})}if(relatedAttrValue){initCustomize(relatedAttr,sizeUnit,relatedAttrValue)}});$(".product-info-attr-js").on("focus",".custom-form-container-js input",function(){$(this).removeClass("is-invalid")}).on("blur",".custom-form-container-js input",function(){if(!checkCustomInfo($(this))){$(this).addClass("is-invalid")}});const $quantityInput=$("#quantity");const $minusBtn=$("#qty-minus-js");const $plusBtn=$("#qty-plus-js");function updateQuantity(newQty){newQty=Math.max(1,Math.min(newQty,max_qty));$quantityInput.val(newQty);product_info.qty=newQty;$minusBtn.toggleClass("disabled",newQty<=1);$plusBtn.toggleClass("disabled",newQty>=max_qty)}$minusBtn.on("click",function(){if($(this).hasClass("disabled"))return;const current=parseInt($quantityInput.val(),10)||1;updateQuantity(current-1)});$plusBtn.on("click",function(){if($(this).hasClass("disabled"))return;const current=parseInt($quantityInput.val(),10)||1;updateQuantity(current+1)});$quantityInput.on("input",function(){let val=parseInt($(this).val(),10);if(isNaN(val)){return}val=Math.max(1,Math.min(val,max_qty));updateQuantity(val)});$quantityInput.on("blur",function(){let val=parseInt($(this).val(),10);if(isNaN(val)||val<1){updateQuantity(1)}else if(val>max_qty){updateQuantity(max_qty)}else{updateQuantity(val)}});$(".product-info-attr-js").on("click","button.measurement-trigger-js",function(e){e.preventDefault();var code=$(this).data("measurements-code");var name=$size_custom_image[code]["local_img_title"];$("#measurementBottomSheetModalLabel").text(name);const imagePath=$size_custom_image[code]["local_img"];if(imagePath){$("#modalImage").attr("src",imagePath)}const htmlContent=$size_custom_image[code]["local_img_desc"];if(htmlContent){$("#modalHtml").html(htmlContent)}var trigger=code;var $modal=$("#measurementBottomSheet");$modal.data("trigger-source",trigger);var modalInstance=new bootstrap.Modal($modal[0]);modalInstance.show()});$("#measurementBottomSheet").on("hidden.bs.modal",function(){var triggerSource=$(this).data("trigger-source");$(this).removeData("trigger-source")});$("#product-favorite-js").click(handleFavoriteClickA);$("#product-add-cart-js").click(handleAddToCartClick);$("#customer-remark-js").on("focus",function(){$(this).removeClass("is-invalid")}).on("blur",function(){if(!checkRemark($(this).val())){$(this).addClass("is-invalid");showToast(order_remark_message,"#toast-js")}})});
|
|
1
|
+
function saveProductToLocalStorage(product_spu,maxHistoryLength=30){let productHistory=JSON.parse(localStorage.getItem(product_history_key))||[];const index=productHistory.indexOf(product_spu);if(index>-1){productHistory.splice(index,1)}productHistory.unshift(product_spu);if(productHistory.length>maxHistoryLength){productHistory=productHistory.slice(0,maxHistoryLength)}localStorage.setItem(product_history_key,JSON.stringify(productHistory))}function fetchAndDisplayViewedProducts(history_url){let productHistory=JSON.parse(localStorage.getItem(product_history_key)||"[]");if(productHistory.length===0){return}const viewedProductsElement=$("#viewed-products-js");$.ajax({url:history_url,method:"POST",data:{productIds:productHistory},success:function(response){viewedProductsElement.html(response.html);if(typeof lazyLoadInstance!=="undefined"){lazyLoadInstance.update()}},error:function(){viewedProductsElement.html("<p>Failed to load viewed products.</p>")}})}function scrollToElement(targetElement,header_height){const headerHeight=header_height||60;const targetOffsetTop=targetElement.offset().top-headerHeight;$("html, body").animate({scrollTop:targetOffsetTop},{duration:300,easing:"swing"})}function renderSizeChartEle(){const sizes=Object.keys($size_charts);for(i=0;i<sizes.length;i++){const attr_code=sizes[i];const data=$size_charts[attr_code];renderTableEle(data,attr_code,attr_code+"Table"+default_size_unit,default_size_unit);renderTableEle(data,attr_code,attr_code+"Table"+second_size_unit,second_size_unit)}}function renderTableEle(data,attr_code,tableId,system){const $table=$(`#${tableId}`);const $thead=$table.find("thead tr");const $tbody=$table.find("tbody");const key=system.toLowerCase();const attrNames=[];for(const size in data){data[size].forEach(item=>{if(!attrNames.includes(item.attr_name)){attrNames.push(item.attr_name)}});break}$thead.empty().append(`<th>${translations.size}</th>`);attrNames.forEach(name=>{$thead.append(`<th>${name}</th>`)});$tbody.empty();for(const value_code in data){let value_name;if(typeof $sku_map[attr_code]!=="undefined"&&typeof $sku_map[attr_code]["values"]!=="undefined"&&typeof $sku_map[attr_code]["values"][value_code]!=="undefined"){value_name=$sku_map[attr_code]["values"][value_code]["value_name"]}else{continue}const row=["<tr><td>"+value_name+"</td>"];const measurements={};data[value_code].forEach(m=>{let val;if(key==="inch"){if(m.ft!==""){val=m.ft}else{val=m.inch}}else{val=m[key]}measurements[m.attr_name]=`${val}`});attrNames.forEach(name=>{row.push(`<td>${measurements[name]||"—"}</td>`)});row.push("</tr>");$tbody.append(row.join(""))}}function loadProductDetail(productInfo){let rows="";if(productInfo.spu_attr.length>0){for(let index=0;index<productInfo.spu_attr.length;index++){const detail=productInfo.spu_attr[index];const row=`\n <tr>\n <td>${detail.attr_name}</td>\n <td>${detail.value_name}</td>\n </tr>`;rows+=row}}if(productInfo.custom_attr.length>0){for(let index=0;index<productInfo.custom_attr.length;index++){const detail=productInfo.custom_attr[index];const tr="<tr>";const td=`<td>${detail.attr_name}</td>`;let lists="<td><ul>";for(let i=0;i<detail.list.length;i++){const value=detail.list[i];const listItem=`<li>${value.measurements_name}: ${value.value_with_unit}</li>`;lists+=listItem}lists+="</ul></td>";rows+=tr+td+lists+"</tr>"}}$(".product_detail_img").attr("src",productInfo["image"]);$(".product_detail_name").html(productInfo["name"]);$(".product_detail_price").html(`${ecommerce_product.symbol}${productInfo["price"]}`);$(".product_qty_val").html(productInfo["qty"]);$(".product-options-js tbody").html(rows);return}function parseHeight(heightStr){const match=heightStr.match(/^(\d+)'?(\d*)"?$/);if(!match){throw new Error("Invalid height format. Expected format like '5'6 or 5'6\"")}const feet=parseInt(match[1],10)||0;const inches=parseInt(match[2],10)||0;return{feet:feet,inches:inches}}function initCustomize(relatedAttr,sizeSystem,attrValue){let sizeUnit;if(sizeSystem==="imperial"){sizeUnit="inch"}else{sizeUnit="cm"}let selectedAttr={};let pointCode;for(let i=0;i<$size_charts[relatedAttr][attrValue].length;i++){pointCode=$size_charts[relatedAttr][attrValue][i]["point_code"];if(pointCode==="height"){if(sizeUnit==="inch"){const{feet:feet,inches:inches}=parseHeight($size_charts[relatedAttr][attrValue][i]["ft"]);$("#feet").val(feet);$("#inches").val(inches)}else{$("#cm").val($size_charts[relatedAttr][attrValue][i][sizeUnit])}}else{selectedAttr[pointCode]=$size_charts[relatedAttr][attrValue][i][sizeUnit]}}const $inputs=$(`#${relatedAttr}-custom-form-container input.custom-input-js`);$inputs.each(function(){const name=$(this).attr("name");if(selectedAttr[name]){$(this).val(selectedAttr[name])}})}function calculateProductPrice(productInfo){let totalPrice=parseFloat(ecommerce_product.price);if(productInfo["spu_attr"]&&typeof productInfo["spu_attr"]==="object"&&productInfo["spu_attr"]!==null){for(let key in productInfo["spu_attr"]){if(productInfo["spu_attr"][key].value_price>0){totalPrice+=parseFloat(productInfo["spu_attr"][key].value_price)}}}if(productInfo["custom_attr"]&&typeof productInfo["custom_attr"]==="object"&&productInfo["custom_attr"]!==null){for(let key in productInfo["custom_attr"]){if(productInfo["custom_attr"][key].value_price>0){totalPrice+=parseFloat(productInfo["custom_attr"][key].value_price)}}}totalPrice=totalPrice.toFixed(2);product_info.price=totalPrice;return totalPrice}function checkCustomInfo($input){const value=$input.val();const required=$input.prop("required");if(required&&(value===""||value===0)){return false}const min=parseFloat($input.attr("min"));if(!isNaN(min)&&value<min){return false}const max=parseFloat($input.attr("max"));if(!isNaN(max)&&value>max){return false}return true}function getSpuAttrValueInfo(attr,attrValue){const attrValueInfo={attr_code:attr,value_code:attrValue,attr_name:$sku_map[attr].attr_name,value_name:$sku_map[attr]["values"][attrValue].value_name,value_price:$sku_map[attr]["values"][attrValue].value_price,base_value_price:$sku_map[attr]["values"][attrValue].base_value_price};return attrValueInfo}function getCustomAttrValueInfo(attr,attrValue,value_price,base_value_price){let value_code,value_name;if(!attrValue){value_code="";value_name=""}else{value_code=attrValue;value_name=$sku_map[attr]["values"][attrValue].value_name}const attrValueInfo={attr_code:attr,value_code:value_code,attr_name:$sku_map[attr].attr_name,value_name:value_name,value_price:value_price,base_value_price:base_value_price};return attrValueInfo}function setProductSpuAttr(attr,attrValue,options){const attrValueInfo=getSpuAttrValueInfo(attr,attrValue);const newSpuAttr={...product_info.spu_attr,[attr]:attrValueInfo};if(product_info.custom_attr&&product_info.custom_attr.hasOwnProperty(attr)){delete product_info.custom_attr[attr]}const updates={spu_attr:newSpuAttr};if(options&&options.attrImg){updates.image=options.imgSrc}updateProductInfo(updates)}function setProductCustomAttr(attr,attrValue,value_price,base_value_price){const attrValueInfo=getCustomAttrValueInfo(attr,attrValue,value_price,base_value_price);const newCustomAttr={...product_info.custom_attr,[attr]:attrValueInfo};if(product_info.spu_attr&&product_info.spu_attr.hasOwnProperty(attr)){delete product_info.spu_attr[attr]}updateProductInfo({custom_attr:newCustomAttr})}function switchToSpuAttr(attr,attrValue){if(product_info.custom_attr&&product_info.custom_attr.hasOwnProperty(attr)){delete product_info.custom_attr[attr]}if(attrValue){setProductSpuAttr(attr,attrValue)}}function initProductAttr(){product_info["name"]=ecommerce_product.name;product_info["price"]=ecommerce_product.price;product_info["image"]=$("#product-main-image-js").val();product_info["product_id"]=ecommerce_product.id;product_info["spu"]=ecommerce_product.spu;product_info["qty"]=1;product_info["spu_attr"]={};product_info["custom_attr"]={};if($selected_sku_list.length>0){for(let index=0;index<$selected_sku_list.length;index++){const attr=$selected_sku_list[index].attr_code;const attrValue=$selected_sku_list[index].value_code;setProductSpuAttr(attr,attrValue)}}}function getFilenameFromImgSrc(imgSrc){const filename=imgSrc.split("/").splice(-1).toString();return filename}function getIndexByImgSrc(imgSrc){let index=-1;$("#main-swiper-js .swiper-slide img").each(function(i,element){const currentImgSrc=$(this).data("src");const filename=getFilenameFromImgSrc(currentImgSrc);if(filename===getFilenameFromImgSrc(imgSrc)){index=i;return}});return index}function goToSlide(index,speed=null){if(isMobile){swiper.slideTo(index,speed,false)}else{thumbSwiper.slideTo(index,speed,false);mainSwiper.slideTo(index,speed,false)}}function updateProductInfo(updates){const keys=Object.keys(updates);for(let index=0;index<keys.length;index++){const key=keys[index];if(key==="image"){const imgSrc=updates[key];const index=getIndexByImgSrc(imgSrc);goToSlide(index)}}Object.assign(product_info,updates);$(document).trigger("product:updated",[product_info])}function renderProductUI(info){const price=calculateProductPrice(info);$("#total-price").text(`${I18nHelper.formatCurrency(price,ecommerce_product.currency)}`)}function showAttrErrorMessage(attr){$(`#${attr}-error-message`).addClass("d-block")}function hideAttrErrorMessage(attr){$(`#${attr}-error-message`).removeClass("d-block")}function isAllRequiredAttrsSelected(requiredAttrs=$spu_attr){return requiredAttrs.every(attr=>product_info.spu_attr[attr]||product_info.custom_attr[attr])}function getProductInfo(){var result={success:false,data:{name:product_info.name,product_id:product_info.product_id,spu:product_info.spu,qty:product_info.qty,image:product_info.image,price:product_info.price,spu_attr:[],size_chart:{},custom_attr:[]},errors:[]};var $selected_size_charts={};for(var index=0;index<$spu_attr.length;index++){var errror={type:null,key:null,jQueryElement:null};var attr=$spu_attr[index];if(product_info.spu_attr&&typeof product_info.spu_attr==="object"&&product_info.spu_attr!==null&&product_info.spu_attr[attr]&&typeof product_info.spu_attr[attr]==="object"&&product_info.spu_attr[attr]!==null){result.data.spu_attr.push(product_info.spu_attr[attr]);var value_code=product_info.spu_attr[attr].value_code;if($size_charts.hasOwnProperty(attr)&&$size_charts[attr].hasOwnProperty(value_code)){$selected_size_charts[attr]=$size_charts[attr][value_code]}}else if(product_info.custom_attr&&typeof product_info.custom_attr==="object"&&product_info.custom_attr!==null&&product_info.custom_attr[attr]&&typeof product_info.custom_attr[attr]==="object"&&product_info.custom_attr[attr]!==null){product_info.custom_attr[attr].list=[];cusResult=collectCustomMeasurements(attr);if(cusResult.errors.length>0){errror.type="custom_attr";errror.jQueryElement=cusResult.errors[0];result.errors.push(errror);return result}else{cusResult.data.forEach(function(item){product_info.custom_attr[attr].list.push(item)})}result.data.custom_attr.push(product_info.custom_attr[attr])}else{errror.type="spu_attr";errror.key=attr;errror.jQueryElement=$(`#${attr}`);result.errors.push(errror);return result}}if(Object.keys($selected_size_charts).length>0){product_info.size_chart=$selected_size_charts;result.data.size_chart=product_info.size_chart}result.success=true;return result}function renderErrorMsg(err){scrollToElement(err.jQueryElement);if(err.type==="spu_attr"){showAttrErrorMessage(err.key)}else if(err.type==="custom_attr"){err.jQueryElement.addClass("is-invalid");err.jQueryElement.focus()}}function collectCustomMeasurements(attr){var result={success:true,data:[],errors:[]};var $container=$("#"+attr+"-custom-form-container");$container.find("div.input-group").each(function(){var $group=$(this);if(!$group.is(":visible")){return true}var $input=$group.find("input").first();if(!checkCustomInfo($input)){result.success=false;result.errors.push($input);return false}var name=$input.attr("name");var $labelSpan=$group.find(".measurements-label-js");var measurements_name=$labelSpan.length?$labelSpan.text().trim():"Unknown";if(name==="feet"){var feet=$input.val();var $inchesEle=$group.find("#inches");if(!checkCustomInfo($inchesEle)){result.success=false;result.errors.push($inchesEle);return false}var inches=$inchesEle.val();if(feet!==""&&inches!==""){var measurements_code=$group.data("measurements-code");result.data.push({measurements_code:measurements_code,measurements_name:measurements_name,value_with_unit:feet+"'"+inches+'"'})}}else{var value=$input.val();if(value!==""){var measurements_code=name;var $unitSpan=$group.find(".custom-input-unit-js");var unit=$unitSpan.length?$unitSpan.text().trim():"";result.data.push({measurements_code:measurements_code,measurements_name:measurements_name,value_with_unit:value+" "+unit})}}});return result}function init_share(){var ogUrl=$('meta[property="og:url"]').attr("content");var ogTitle=$('meta[property="og:title"]').attr("content");var elems=$("[data-sharer]");for(var i=0;i<elems.length;i++){elems[i].setAttribute("data-url",ogUrl);elems[i].setAttribute("data-title",ogTitle)}$("#product-link-url-input-js").val(ogUrl);return ogUrl}function init_share_m(aff){var ogUrl=init_share();const aff_key=aff.affiliate_keyword;const aff_uuid=aff.affiliate_uuid;if(aff_key&&aff_uuid){$("#affiliate-link-container-js").removeClass("d-none");ogUrl=UrlUtils.addQueryParam(aff_key,aff_uuid,ogUrl)}$("#affiliate-link-url-input-js").val(ogUrl)}function init_share_pc(){var ogUrl=init_share();const aff_key=$("#aff-key-js").val();const aff_uuid=$("#aff-uuid-js").val();if(aff_key&&aff_uuid){ogUrl=UrlUtils.addQueryParam(aff_key,aff_uuid,ogUrl)}$("#affiliate-link-url-input-js").val(ogUrl)}function handleFavoriteClickThrottle(e){e.preventDefault();var self=$(this);$.ajax({url:productFavoriteUrl,method:"GET",data:{spu:ecommerce_product.spu},dataType:"json",timeout:5e3}).done(function(response){loginStatus=response.loginStatus;if(!loginStatus){window.location.href=memberLoginUrl;return}if(response.favoriteStatus){self.addClass("text-danger")}else{self.removeClass("text-danger")}showToast(response.content,"#toast-js")}).fail(function(){alert("Operation failed. Please try again later.")}).always(function(){})}function handleFavoriteClickA(e){e.preventDefault();e.stopPropagation();var self=$(this);if(self.prop("disabled")){return}self.prop("disabled",true);self.addClass("opacity-50");$.ajax({url:productFavoriteUrl,method:"GET",data:{spu:ecommerce_product.spu},dataType:"json",timeout:5e3}).done(function(response){loginStatus=response.loginStatus;if(!loginStatus){window.location.href=memberLoginUrl;return}if(response.favoriteStatus){self.addClass("text-danger")}else{self.removeClass("text-danger")}showToast(response.content,"#toast-js")}).fail(function(){alert("Operation failed. Please try again later.")}).always(function(){self.prop("disabled",false);self.removeClass("opacity-50")})}function handleFavoriteClickB(e){e.preventDefault();e.stopPropagation();var self=$(this);self.toggleClass("text-danger");$.ajax({url:productFavoriteUrl,method:"GET",data:{spu:ecommerce_product.spu},dataType:"json",timeout:5e3}).done(function(response){loginStatus=response.loginStatus;if(!loginStatus){window.location.href=memberLoginUrl;return}if(response.favoriteStatus){self.addClass("text-danger")}else{self.removeClass("text-danger")}showToast(response.content,"#toast-js")}).fail(function(){self.toggleClass("text-danger");showToast("Operation failed","#toast-js")}).always(function(){})}function checkRemark(customer_remark){if(customer_remark.length>order_remark_max_length){return false}return true}function handleAddToCartClick(e){e.preventDefault();e.stopPropagation();var self=$(this);if(self.prop("disabled")){return}self.prop("disabled",true);self.addClass("opacity-50");var result=getProductInfo();if(!result.success){if(!isMobile){renderErrorMsg(result.errors[0])}self.prop("disabled",false);self.removeClass("opacity-50");showToast("Failed to obtain product information. Please refresh and try again.","#toast-js");return}var remark=$("#customer-remark-js").val();if(!checkRemark(remark)){self.prop("disabled",false);self.removeClass("opacity-50");showToast(order_remark_message,"#toast-js")}var data_params=result.data;data_params["customer_remark"]=remark;$.ajax({async:true,timeout:6e3,dataType:"json",type:"post",data:JSON.stringify(data_params),url:addToCartUrl,success:function(response){if(response.status=="success"){var items_count=response.items_count;$(".cart-item-count-js").text(items_count);if(isMobile){var $modal=$("#productBottomSheet");var modalInstance=bootstrap.Modal.getOrCreateInstance($modal[0]);modalInstance.hide()}}showToast(response.message,"#toast-js")},error:function(XMLHttpRequest,textStatus,errorThrown){let message="Operation failed. Please try again later.";if(textStatus==="timeout"){message="Request timed out. Please check your network and try again."}else if(XMLHttpRequest.status===0){message="Network connection failed. Please check your internet."}else if(XMLHttpRequest.status===401){window.location.href=memberLoginUrl;return}else if(XMLHttpRequest.status>=500){message="Server is busy. Please try again later."}else if(XMLHttpRequest.status>=400){message="Invalid request. Please try again."}else{console.error("AJAX Error:",textStatus,errorThrown)}},complete:function(){self.prop("disabled",false);self.removeClass("opacity-50")}})}function paypalButton(){var FUNDING_SOURCES=[paypal.FUNDING.PAYPAL];FUNDING_SOURCES.forEach(function(fundingSource){var button=paypal.Buttons({fundingSource:fundingSource,style:{height:40,label:"buynow"},onClick:function(){var remark=$("#customer-remark-js").val();if(!checkRemark(remark)){showToast(order_remark_message,"#toast-js");return false}if(!isMobile){var result=getProductInfo();if(!result.success){renderErrorMsg(result.errors[0]);return false}}return true},createOrder:(data,actions)=>{var result=getProductInfo();var data_params=result.data;data_params["customer_remark"]=$("#customer-remark-js").val();return fetch(paypalCreateOrderUrl,{method:"post",body:JSON.stringify(data_params),headers:{"content-type":"application/json"}}).then(response=>{if(!response.ok){throw new Error("Network response was not ok")}return response.json()}).then(order=>{if(order.id=="-1"){throw new Error(order.message)}return order.id}).catch(error=>{alert(error)})},onCancel:data=>{},onApprove:(data,actions)=>fetch(paypalCaptureOrderUrl,{method:"post",body:JSON.stringify(data),headers:{"content-type":"application/json"}}).then(response=>{if(!response.ok){throw new Error("Network response was not ok!")}return response.json()}).then(orderData=>{const order_sn=orderData.order_sn;if(order_sn=="-1"){if(!orderData.is_local){if(orderData.error_issue==="INSTRUMENT_DECLINED"){alert(orderData.message);return actions.restart()}else{throw new Error(orderData.message)}}else{}}actions.redirect(`${paypalFinishOrderUrl}?order_sn=${order_sn}`)}).catch(error=>{alert(error)})});if(button.isEligible()){button.render("#product-paypal-container-js")}})}function paypalBuynowCreatePayment(){const container=document.getElementById("product-paypal-container-js");if(container.dataset.rendered==="1")return;paypalButton();container.dataset.rendered="1"}function switchSizeUnit(unit){currentUnit=unit;const activeTabContentPrefix=$("#sizeTabs .nav-link.active").data("content-prefix");const contentId=activeTabContentPrefix+unit;$("#"+contentId).addClass("show active").siblings(".tab-pane").removeClass("show active");if(unit===default_size_unit){$("#switchToDefault-js").addClass("btn-danger").removeClass("btn-secondary");$("#switchToSecond-js").addClass("btn-secondary").removeClass("btn-danger")}else{$("#switchToSecond-js").addClass("btn-danger").removeClass("btn-secondary");$("#switchToDefault-js").addClass("btn-secondary").removeClass("btn-danger")}}var lazyLoadInstance=null;var swiper;var thumbSwiper;var mainSwiper;$(function(){lazyLoadInstance=new LazyLoad({});initProductAttr();fetchAndDisplayViewedProducts(historyUrl);saveProductToLocalStorage(ecommerce_product.spu);$(document).on("product:updated",(e,info)=>{renderProductUI(info)});if(isMobile){if(products_more_count>products_more_count_min){$("#product-list-more").infiniteScroll({path:function(){if(this.loadCount<products_more_max_page-1){return UrlUtils.mergeParams(productsMoreUrl,{p:this.loadCount+1})}},append:false,history:false,responseBody:"json",fetchOptions:{method:"GET",headers:{"X-Requested-With":"XMLHttpRequest","Content-Type":"application/json",Accept:"application/json"}},checkLastPage:true,scrollThreshold:100,status:".page-load-status",prefill:false,debug:false});$("#product-list-more").on("load.infiniteScroll",function(event,body,path){if(body&&body.html&&body.html.trim()!==""){$(this).append(body.html);if(typeof lazyLoadInstance!=="undefined"){lazyLoadInstance.update()}}});$("#product-list-more").on("error.infiniteScroll",function(event,error,path){alert("Failed to load more products.")});$("#product-list-more").on("append.infiniteScroll",function(event,body,path,items){});$("#product-list-more").on("last.infiniteScroll",function(){})}swiper=new Swiper(".swiper",{autoplay:{delay:5e3},pagination:{el:".swiper-pagination",type:"fraction",renderFraction:function(currentClass,totalClass){return'<span class="swiper-pagination-current">'+currentClass+'</span> / <span class="swiper-pagination-total">'+totalClass+"</span>"}}});$("#product-detail-icon-share-js").click(function(e){e.preventDefault();let requestData={};$.get(shareUrl,requestData).done(function(res){if(res.success){init_share_m(res);var $modal=$("#shareBottomSheet");var modalInstance=new bootstrap.Modal($modal[0]);modalInstance.show()}}).fail(function(){})});$("#shareBottomSheet").on("hidden.bs.modal",function(){});$(".product-view-js").on("click","button",function(e){e.preventDefault();var result=getProductInfo();if(!result.success){renderErrorMsg(result.errors[0]);return false}loadProductDetail(result.data);var trigger=$(this).data("trigger");$("#productBottomSheetModalLabel").text($(this).text());if(trigger=="product-add-cart-view-js"){$("#product-add-cart-js").removeClass("d-none");$("#product-paypal-container-js").addClass("d-none")}else if(trigger=="product-buy-now-view-js"){if(typeof paypal=="undefined"){webLoadScript(paypalSdkUrl,function(){paypalBuynowCreatePayment()})}else{paypalBuynowCreatePayment()}$("#product-add-cart-js").addClass("d-none");$("#product-paypal-container-js").removeClass("d-none")}var $modal=$("#productBottomSheet");$modal.data("trigger-source",trigger);var modalInstance=bootstrap.Modal.getOrCreateInstance($modal[0]);modalInstance.show()});$("#productBottomSheet").on("hidden.bs.modal",function(){var triggerSource=$(this).data("trigger-source");if(triggerSource==="product-buy-now-view-js"){$("#paypal-button-container").empty()}$(this).removeData("trigger-source")})}else{thumbSwiper=new Swiper("#thumb-swiper-js",{autoplay:false,direction:"vertical",slidesPerView:5,spaceBetween:5,navigation:{nextEl:"#swiper-button-next-js",prevEl:"#swiper-button-prev-js"},mousewheel:{invert:true},autoHeight:false,allowTouchMove:false,watchOverflow:true});mainSwiper=new Swiper("#main-swiper-js",{autoplay:false,thumbs:{swiper:thumbSwiper}});if(!showArrows){$("#swiper-button-prev-js, #swiper-button-next-js").addClass("d-none")}mainSwiper.on("slideChange",function(){const color=mainSwiper.slides[mainSwiper.activeIndex]?.dataset.color||"";$(window).trigger("productImageColorChange",{colorCode:color})});$("#thumb-swiper-js .swiper-slide").on("mouseenter",function(){const index=$(this).index();mainSwiper.slideTo(index,0)});$("#main-swiper-js .zoom-js").ezPlus({attrImageZoomSrc:"zoom-image"});const $containerShare=$("#sharejs-container-js");if($containerShare.length>0){let requestData={};$.get(shareUrl,requestData).done(function(res){if(res.success&&res.html){$containerShare.empty().html(res.html);init_share_pc()}}).fail(function(){})}if(typeof paypal=="undefined"){webLoadScript(paypalSdkUrl,function(){paypalButton()})}else{paypalButton()}}$("#product-add-cart-js").click(handleAddToCartClick);$(document).on("click",".btn-copy-link-js",function(e){e.preventDefault();const $btn=$(this);const targetId=$btn.data("clipboard-target");const $input=$("#"+targetId);if(!$input.length){return}const textToCopy=$input.val().trim();if(!textToCopy){return}copyTextToClipboard(textToCopy).then(function(success){$btn.find(".copy-label-js").addClass("d-none");$btn.find(".copy-label-copied-js").removeClass("d-none");setTimeout(function(){$btn.find(".copy-label-js").removeClass("d-none");$btn.find(".copy-label-copied-js").addClass("d-none")},2e3);if(!success){}})});$("#panelsStayOpen-sizeChart").on("show.bs.collapse",function(){if(!$(this).data("rendered")){renderSizeChartEle();$(this).data("rendered",true)}});let currentUnit=default_size_unit;$("#switchToDefault-js").on("click",function(){switchSizeUnit(default_size_unit)});$("#switchToSecond-js").on("click",function(){switchSizeUnit(second_size_unit)});$("#sizeTabs .nav-link").on("shown.bs.tab",function(){switchSizeUnit(currentUnit)});$(".product-info-attr-js").on("click",".attr-value-js button",function(e){e.preventDefault();const self=$(this);const $fieldset=self.closest("fieldset");const attr=$fieldset.attr("id");const attrValue=self.data("attr-value");self.closest("div").find("button").removeClass("border-danger");self.removeClass("border-light");self.addClass("border-danger");hideAttrErrorMessage(attr);$fieldset.find(".product-selected-attr-value-js").data("value",attrValue).data("value-for-restore",attrValue).text($sku_map[attr]["values"][attrValue].value_name);const attrHasPrice=$sku_map[attr].attr_has_price;const attrImg=self.data("attr-img");const options={attrHasPrice:attrHasPrice,attrImg:attrImg};if(attrImg===1){var img_src=self.find("img").attr("src");options.imgSrc=img_src}setProductSpuAttr(attr,attrValue,options);const showSizeChart=$sku_map[attr].show_size_value_chart;if(showSizeChart===1){const $chartContainer=$fieldset.find(".product-selected-chart-js");const $chartBody=$chartContainer.find("tbody");if($size_charts[attr][attrValue]){let rows="";$.each($size_charts[attr][attrValue],function(index,detail){const imperialValue=detail.ft?detail.ft:detail.inch?detail.inch+" in":"";const row=`\n <tr>\n <td>${detail.attr_name}</td>\n <td>${imperialValue}</td>\n <td>${detail.cm} cm</td>\n </tr>`;rows+=row});$chartBody.html(rows);$chartContainer.removeClass("d-none")}}const showSizeCustom=$sku_map[attr].show_size_custom;if(showSizeCustom===1){const $customCheckbox=$fieldset.find(".custom-toggle-checkbox-js");if($customCheckbox.is(":checked")){$customCheckbox.prop("checked",false).trigger("change")}}});$(".product-info-attr-js").on("change",".custom-toggle-checkbox-js",function(){const self=$(this);const customPrice=self.data("custom_price");const baseCustomPrice=self.data("base-custom-price");const $fieldset=self.closest("fieldset");const targetFormId=self.attr("aria-controls");const isChecked=self.is(":checked");$("#"+targetFormId).toggleClass("d-none",!isChecked).attr("aria-hidden",!isChecked);self.attr("aria-expanded",isChecked);const relatedAttr=$fieldset.attr("id");const relatedAttrValue=$fieldset.find(".product-selected-attr-value-js").data("value-for-restore");if(isChecked){$fieldset.find(".d-flex button").removeClass("border-danger");$fieldset.find(".product-selected-attr-value-js").data("value","").text("");hideAttrErrorMessage(relatedAttr);if(relatedAttrValue){const size_unit=$("#"+targetFormId).find(".unit-selector-js").val();initCustomize(relatedAttr,size_unit,relatedAttrValue)}$fieldset.find(".product-selected-chart-js").addClass("d-none");setProductCustomAttr(relatedAttr,relatedAttrValue,customPrice,baseCustomPrice)}else{$fieldset.find(".d-flex button").each(function(){if($(this).data("attr-value")==relatedAttrValue){$(this).removeClass("border-light");$(this).addClass("border-danger")}});if(relatedAttrValue){$fieldset.find(".product-selected-attr-value-js").data("value",relatedAttrValue).data("value-for-restore",relatedAttrValue).text($sku_map[relatedAttr]["values"][relatedAttrValue].value_name)}$fieldset.find(".product-selected-chart-js").removeClass("d-none");switchToSpuAttr(relatedAttr,relatedAttrValue)}});$(".product-info-attr-js").on("change",".unit-selector-js",function(){const $this=$(this);const sizeUnit=$this.val();const $fieldset=$this.closest("fieldset");const relatedAttr=$fieldset.attr("id");const relatedAttrValue=$fieldset.find(".product-selected-attr-value-js").data("value-for-restore");const imperialHeight=$("#imperial-height");const metricHeight=$("#metric-height");const weightUnitLabel=$("#weight-unit-label");const $cus_units=$fieldset.find(".custom-input-unit-js");if(sizeUnit==="imperial"){if(imperialHeight){imperialHeight.removeClass("d-none");metricHeight.addClass("d-none");weightUnitLabel.text("lbs")}$cus_units.each(function(){$(this).text("in")})}else{if(imperialHeight){imperialHeight.addClass("d-none");metricHeight.removeClass("d-none");weightUnitLabel.text("kg")}$cus_units.each(function(){$(this).text("cm")})}if(relatedAttrValue){initCustomize(relatedAttr,sizeUnit,relatedAttrValue)}});$(".product-info-attr-js").on("focus",".custom-form-container-js input",function(){$(this).removeClass("is-invalid")}).on("blur",".custom-form-container-js input",function(){if(!checkCustomInfo($(this))){$(this).addClass("is-invalid")}});const $quantityInput=$("#quantity");const $minusBtn=$("#qty-minus-js");const $plusBtn=$("#qty-plus-js");function updateQuantity(newQty){newQty=Math.max(1,Math.min(newQty,max_qty));$quantityInput.val(newQty);product_info.qty=newQty;$minusBtn.toggleClass("disabled",newQty<=1);$plusBtn.toggleClass("disabled",newQty>=max_qty)}$minusBtn.on("click",function(){if($(this).hasClass("disabled"))return;const current=parseInt($quantityInput.val(),10)||1;updateQuantity(current-1)});$plusBtn.on("click",function(){if($(this).hasClass("disabled"))return;const current=parseInt($quantityInput.val(),10)||1;updateQuantity(current+1)});$quantityInput.on("input",function(){let val=parseInt($(this).val(),10);if(isNaN(val)){return}val=Math.max(1,Math.min(val,max_qty));updateQuantity(val)});$quantityInput.on("blur",function(){let val=parseInt($(this).val(),10);if(isNaN(val)||val<1){updateQuantity(1)}else if(val>max_qty){updateQuantity(max_qty)}else{updateQuantity(val)}});$(".product-info-attr-js").on("click","button.measurement-trigger-js",function(e){e.preventDefault();var code=$(this).data("measurements-code");var name=$size_custom_image[code]["local_img_title"];$("#measurementBottomSheetModalLabel").text(name);const imagePath=$size_custom_image[code]["local_img"];if(imagePath){$("#modalImage").attr("src",imagePath)}const htmlContent=$size_custom_image[code]["local_img_desc"];if(htmlContent){$("#modalHtml").html(htmlContent)}var trigger=code;var $modal=$("#measurementBottomSheet");$modal.data("trigger-source",trigger);var modalInstance=new bootstrap.Modal($modal[0]);modalInstance.show()});$("#measurementBottomSheet").on("hidden.bs.modal",function(){var triggerSource=$(this).data("trigger-source");$(this).removeData("trigger-source")});$("#product-favorite-js").click(handleFavoriteClickA);$("#customer-remark-js").on("focus",function(){$(this).removeClass("is-invalid")}).on("blur",function(){if(!checkRemark($(this).val())){$(this).addClass("is-invalid");showToast(order_remark_message,"#toast-js")}})});
|