Files
Last update 4 months 2 weeks
by
Arnaud-Apu-Fanovard
CPNComponent.js.téléchargementvar needsClear=new Array;function BindCartMenu(){$(".cpn-comp-group").off("show.bs.dropdown"),$(".cpn-comp-group").on("show.bs.dropdown",function(e){if(pushGA4EventForCPN(e,"pn_dropdown"),$('input[name$="ControllerUrl"]').first().val()==="CART"){var t=parseInt($(e.target).parent().find("input").prop("id").substring(10).split("__")[0]),n=$(".cpn-input").map(function(e,n){return parseInt($(n).prop("id").substring(10).split("__")[0])>t?n:null});n.each(function(e,t){return $(t).parent().css("z-index","0")}),isNaN(t)&&$(".n-cpn-comp",$(e.target).closest(".grid-row").nextAll()).css("z-index","0")}$(".cart-summary > div:nth-child(1) > div").css("position","static"),$(".cart-summary > div:nth-child(1) > div:nth-child(1) > div > div").css("position","static")}),$(".cpn-comp-group").off("hide.bs.dropdown"),$(".cpn-comp-group").on("hide.bs.dropdown",function(){$('input[name$="ControllerUrl"]').first().val()==="CART"&&$(".n-cpn-comp").each(function(e,t){return $(t).css("z-index","")}),$(".cart-summary > div:nth-child(1) > div").css("position",""),$(".cart-summary > div:nth-child(1) > div:nth-child(1) > div > div").css("position","")})}function BindCPNComponents(){$(".n-cpn-comp").each(function(e,t){return BindCPNComponent($(t))})}function BindCPNComponent(e){var t=e.find("input"),n=e.find("button.btn-clear"),a=e.find("button.btn-add"),c=e.find("button.btn-default"),o=e.find("i"),l=e.find(".dropdown-menu"),d=$('input[name$="ControllerUrl"]').first().val(),s=d==="NonMvc",r=e.find(".divCustPartDdl"),i=r.length>0&&r.attr("style")!==void 0&&r.attr("style").length>0;(!s||s&&!i)&&n.on("click",function(e){return ClearCPNEvent(e)}),l.find("a").on("click",function(e){e.preventDefault(),$(e.target).text()!==t.val()&&($(e.target).length>0&&$(e.target).data()!==null&&$(e.target).data().val>1&&pushGA4EventForCPN(e,"pn_selected"),SetCPNEvent(e,$(e.target).text(),$(e.target).attr("data-val")))}),t.on("keyup",function(){var a=t.val().length>0;s?i||(a?ShowClearButton(n,o):HideClearButton(n,o)):a?ShowClearButton(n,o):HideClearButton(n,o)}),t.val().length>0&&ShowClearButton(n,o),t.attr("autocomplete","off"),a.length===0||s&&!i?t.on("blur",function(e){$.when(AsyncBlurEvent(t,c).then(function(){return SetCPNEvent(e,t.val(),"-2")}))}):(!s||s&&i)&&a.on("click",function(e){e.preventDefault(),$(t).val().length>0&&$(t).val()!=="*"&&(a.prop("disabled",!0).html('<i class="fa fa-spinner fa-spin fa-fw" style="position: relative; top: 1px; font-size: 18px"></i>').css("width","45px"),$(t).prop("disabled",!0),SetCPNEvent(e,t.val(),"-2"))})}function AsyncBlurEvent(e,t){var n=$.Deferred(),s=e.val();return setTimeout(function(){if(e.prop("disabled")){n.reject();return}if(t.attr("aria-expanded")==="true"){n.reject();return}if(e.val()===e[0].getAttribute("value")){var i=e.attr("id"),o=$.inArray(i,needsClear);if(o!==-1){needsClear.splice(o,1),n.resolve();return}n.reject();return}if(s===e.val()){n.resolve();return}n.reject()},200),n.promise()}function IsValidNew(e){return typeof e!="undefined"&&e}function ShowClearButton(e,t){e.prop("disabled",!1),t.css("visibility","")}function HideClearButton(e,t){e.prop("disabled",!0),t.css("visibility","hidden")}function IsValidCustPartNum2(e){return new RegExp("^[\x00-)+-\xFF]*$").test(e)}function ClearCPNEvent(e){var n=$(e.target).closest("div.n-cpn-comp"),t=$(n).find("input"),s=$(n).find("button.btn-clear"),o=$(n).find("i"),i=t.attr("id");$.inArray(i,needsClear)===-1&&needsClear.push(t.attr("id")),t.val()>0&&pushGA4EventForCPN(e,"pn_cleared"),ClearCPNValue(t,o,s)}function ClearCPNValue(e,t,n){e.val(""),e.attr("value",""),n.prop("disabled",!0),t.css("visibility","hidden"),e.trigger("focus")}function SetCPNEvent(e,t,n){var s=$(e.target).closest("div.n-cpn-comp"),o=$(s).find("input"),i=$(s).find("button.btn-clear"),a=$(s).find("button.btn-add"),r=$(s).find("button.btn-default"),c=$(s).find("i"),l=$(s).find(".dropdown-menu"),d=$(s).find(".divCustPartDdl");if(t!==void 0&&n&&parseInt(n)>1){if(!CheckForValidCPNAndPushClearedEvent(e,o,t))return;UpdateCPN(o,t,i,a,r,c,l,d)}else if(t!==void 0&&n&&parseInt(n)===-2){if(!CheckForValidCPNAndPushClearedEvent(e,o,t))return;t.length>0&&t.trim()!==""&&pushGA4EventForCPN(e,"pn_added"),UpdateCPN(o,t,i,a,r,c,l,d)}else n&&parseInt(n)===1?UpdateCPN(o,"",i,a,r,c,l,d):n&&parseInt(n)===-1&&ClearCPNEvent(e)}function CheckForValidCPNAndPushClearedEvent(e,t,n){return!!IsValidCustPartNum2(n)&&(n===""&&t[0]&&t[0].defaultValue&&pushGA4EventForCPN(e,"pn_cleared"),!0)}function UpdateCPN(e,t,n,s,o,i,a,r){e.val(t),e.attr("value",t);var u,l=$('input[name$="ControllerUrl"]').first().val(),c=e.closest(".formSaveCustomerPartNumber"),h=$(c).find('input[name$="CartHeaderIdForCustPartNum"]').val(),d=$(c).find('input[name$="ProductIdEncForCustPartNum"]').val(),m=$("input[name=__RequestVerificationToken]").val(),f=$(c).find('input[name$="CustomerPartNumber"]').attr("id").split("_"),p=f[1];IsValidNew(d)&&d.trim()&&(n.prop("disabled",!0).addClass("disableBtnClear"),o.prop("disabled",!0),e.addClass("grey-disabled").prop("disabled",!0),u=r.length>0&&r.attr("style")!==void 0&&r.attr("style").length>0,i.hasClass("fa-times")&&!u&&i.removeClass("fa-times").addClass("fa-spinner fa-spin fa-fw fa-spinner-update"),$.ajax({url:"/Product/Product/SaveCustomerPartNumber2",type:"POST",data:{__RequestVerificationToken:m,productIdEncForCustPartNum:d,customerPartNumber:encodeURIComponent(t),calledFrom:l,itemId:h,cartItemRowId:p}}).done(function(t){if(IsValidNew(t)){var h,m,f,p,d=void 0;try{d=decodeURIComponent($(t)[0].innerHTML)}catch{d=$(t)[0].innerHTML;try{m=$(d).find("input.cpn-input").val(),f=decodeURIComponent(m),p=new RegExp(m,"g"),d=d.replace(p,f)}catch{}}h=$(".dropdown-menu",d),(h.length===0||h.find("a").length===3)&&o.length===0||(h.length===0||h.find("a").length===3)&&l==="NonMvc"&&u?l!=="NonMvc"?($(c).html(d),BindCPNComponent($(".n-cpn-comp",$(c))),BindCartMenu()):(e.off("keyup"),e.off("blur"),n.off("click"),s.off("click"),s.attr("style","display:none"),r.attr("style",""),a.html(h.html()),i.removeClass("fa-spinner fa-spin fa-fw fa-spinner-update").addClass("fa-times").css("color",""),n.prop("disabled",!1).removeClass("disableBtnClear"),o.prop("disabled",!1),e.removeClass("grey-disabled").prop("disabled",!1),BindCPNComponent($(".n-cpn-comp",$(c))),BindCartMenu()):(a.html(h.html()),a.find("a").on("click",function(t){t.preventDefault(),$(t.target).text()!==e.val()&&($(t.target).length>0&&$(t.target).data()!==null&&$(t.target).data().val>1&&pushGA4EventForCPN(t,"pn_selected"),SetCPNEvent(t,$(t.target).text(),$(t.target).attr("data-val")))}))}}).always(function(){i.hasClass("fa-spinner")&&i.removeClass("fa-spinner fa-spin fa-fw fa-spinner-update").addClass("fa-check").css({position:"",top:"",color:"#4E7B0F","font-size":"",visibility:""}).fadeOut(2e3,function(){e.val().length<1?(i.removeClass("fa-check").addClass("fa-times").css({color:"",visibility:"hidden"}).fadeIn(),n.removeClass("disableBtnClear"),o.prop("disabled",!1),e.removeClass("grey-disabled").prop("disabled",!1),e.trigger("focus")):(i.removeClass("fa-check").addClass("fa-times").css("color","").fadeIn(),n.prop("disabled",!1).removeClass("disableBtnClear"),o.prop("disabled",!1),e.removeClass("grey-disabled").prop("disabled",!1))})}))}function pushGA4EventForCPN(e,t){var o=$("#scheduled-form").length>0,i=$("#cart-form").length>0,s="_product_customer_",n="";o?n="schedule_order":i&&(n="cart"),n&&t&&(s=n+s+t,pushGA4Event(e,s))}function pushGA4Event(e,t){dataLayer.push({event:t,event_manufacturer:$(e.target).parents().closest("tr.grid-row").attr("data-mfrName")||"none",event_brand:$(e.target).parents().closest("tr.grid-row").attr("data-mfrName")||"none",event_manufacturerpn:$(e.target).parents().closest("tr.grid-row").attr("data-mfrpartnumber"),event_mouserpn:$(e.target).parents().closest("tr.grid-row").attr("data-partnumber").toLowerCase()})}function pushGA4ForPDClick(e){var t=$("#scheduled-form").length>0,n=$("#cart-form").length>0;t?pushGA4Event(e,"schedule_order_product_click"):n&&pushGA4Event(e,"cart_product_click")}$(function(){BindCPNComponents(),BindCartMenu()})