woocommerce/assets/js/frontend/add-to-cart-variation.min.js

1 line
11 KiB
JavaScript

!function(a,b,c,d){var e=function(a){this.$form=a,this.$attributeFields=a.find(".variations select"),this.$singleVariation=a.find(".single_variation"),this.$singleVariationWrap=a.find(".single_variation_wrap"),this.$resetVariations=a.find(".reset_variations"),this.$product=a.closest(".product"),this.variationData=a.data("product_variations"),this.useAjax=!1===this.variationData,this.xhr=!1,this.$singleVariationWrap.show(),this.$form.unbind("check_variations update_variation_values found_variation"),this.$resetVariations.unbind("click"),this.$attributeFields.unbind("change "),this.getChosenAttributes=this.getChosenAttributes.bind(this),this.findMatchingVariations=this.findMatchingVariations.bind(this),this.isMatch=this.isMatch.bind(this),this.toggleResetLink=this.toggleResetLink.bind(this),a.on("click",".reset_variations",{variationForm:this},this.onReset),a.on("reload_product_variations",{variationForm:this},this.onReload),a.on("hide_variation",{variationForm:this},this.onHide),a.on("show_variation",{variationForm:this},this.onShow),a.on("click",".single_add_to_cart_button",{variationForm:this},this.onAddToCart),a.on("reset_data",{variationForm:this},this.onResetDisplayedVariation),a.on("reset_image",{variationForm:this},this.onResetImage),a.on("change",".variations select",{variationForm:this},this.onChange),a.on("found_variation",{variationForm:this},this.onFoundVariation),a.on("check_variations",{variationForm:this},this.onFindVariation),a.on("update_variation_values",{variationForm:this},this.onUpdateAttributes),a.trigger("check_variations"),a.trigger("wc_variation_form")};e.prototype.onReset=function(a){a.preventDefault(),a.data.variationForm.$attributeFields.val("").change(),a.data.variationForm.$form.trigger("reset_data")},e.prototype.onReload=function(a){var b=a.data.variationForm;b.variationData=b.$form.data("product_variations"),b.useAjax=!1===b.variationData,b.$form.trigger("check_variations")},e.prototype.onHide=function(a){a.preventDefault(),a.data.variationForm.$form.find(".single_add_to_cart_button").removeClass("wc-variation-is-unavailable").addClass("disabled wc-variation-selection-needed"),a.data.variationForm.$form.find(".woocommerce-variation-add-to-cart").removeClass("woocommerce-variation-add-to-cart-enabled").addClass("woocommerce-variation-add-to-cart-disabled")},e.prototype.onShow=function(a,b,c){a.preventDefault(),c?(a.data.variationForm.$form.find(".single_add_to_cart_button").removeClass("disabled wc-variation-selection-needed wc-variation-is-unavailable"),a.data.variationForm.$form.find(".woocommerce-variation-add-to-cart").removeClass("woocommerce-variation-add-to-cart-disabled").addClass("woocommerce-variation-add-to-cart-enabled")):(a.data.variationForm.$form.find(".single_add_to_cart_button").removeClass("wc-variation-selection-needed").addClass("disabled wc-variation-is-unavailable"),a.data.variationForm.$form.find(".woocommerce-variation-add-to-cart").removeClass("woocommerce-variation-add-to-cart-enabled").addClass("woocommerce-variation-add-to-cart-disabled"))},e.prototype.onAddToCart=function(c){a(this).is(".disabled")&&(c.preventDefault(),a(this).is(".wc-variation-is-unavailable")?b.alert(wc_add_to_cart_variation_params.i18n_unavailable_text):a(this).is(".wc-variation-selection-needed")&&b.alert(wc_add_to_cart_variation_params.i18n_make_a_selection_text))},e.prototype.onResetDisplayedVariation=function(a){var b=a.data.variationForm;b.$product.find(".product_meta").find(".sku").wc_reset_content(),b.$product.find(".product_weight").wc_reset_content(),b.$product.find(".product_dimensions").wc_reset_content(),b.$form.trigger("reset_image"),b.$singleVariation.slideUp(200).trigger("hide_variation")},e.prototype.onResetImage=function(a){a.data.variationForm.$form.wc_variations_image_update(!1)},e.prototype.onFindVariation=function(b){var c=b.data.variationForm,d=c.getChosenAttributes(),e=d.data;if(d.count===d.chosenCount)if(c.useAjax)c.xhr&&c.xhr.abort(),c.$form.block({message:null,overlayCSS:{background:"#fff",opacity:.6}}),e.product_id=parseInt(c.$form.data("product_id"),10),e.custom_data=c.$form.data("custom_data"),c.xhr=a.ajax({url:wc_cart_fragments_params.wc_ajax_url.toString().replace("%%endpoint%%","get_variation"),type:"POST",data:e,success:function(a){a?c.$form.trigger("found_variation",[a]):(c.$form.trigger("reset_data"),c.$form.find(".single_variation").after('<p class="wc-no-matching-variations woocommerce-info">'+wc_add_to_cart_variation_params.i18n_no_matching_variations_text+"</p>"),c.$form.find(".wc-no-matching-variations").slideDown(200))},complete:function(){c.$form.unblock()}});else{c.$form.trigger("update_variation_values");var f=c.findMatchingVariations(c.variationData,e),g=f.shift();g?c.$form.trigger("found_variation",[g]):(c.$form.trigger("reset_data"),c.$form.find(".single_variation").after('<p class="wc-no-matching-variations woocommerce-info">'+wc_add_to_cart_variation_params.i18n_no_matching_variations_text+"</p>"),c.$form.find(".wc-no-matching-variations").slideDown(200))}else c.$form.trigger("update_variation_values"),c.$form.trigger("reset_data");c.toggleResetLink(d.chosenCount>0)},e.prototype.onFoundVariation=function(b,c){var d=b.data.variationForm,e=d.$product.find(".product_meta").find(".sku"),f=d.$product.find(".product_weight"),g=d.$product.find(".product_dimensions"),h=d.$singleVariationWrap.find(".quantity"),i=!0,j="",k=!1,l="";c.sku?e.wc_set_content(c.sku):e.wc_reset_content(),c.weight?f.wc_set_content(c.weight):f.wc_reset_content(),c.dimensions?g.wc_set_content(c.dimensions):g.wc_reset_content(),d.$form.wc_variations_image_update(c),c.variation_is_visible?(k=wp.template("variation-template"),j=c.variation_id):k=wp.template("unavailable-variation-template"),l=k({variation:c}),l=l.replace("/*<![CDATA[*/",""),l=l.replace("/*]]>*/",""),d.$singleVariation.html(l),d.$form.find('input[name="variation_id"], input.variation_id').val(c.variation_id).change(),"yes"===c.is_sold_individually?(h.find("input.qty").val("1").attr("min","1").attr("max",""),h.hide()):(h.find("input.qty").attr("min",c.min_qty).attr("max",c.max_qty),h.show()),c.is_purchasable&&c.is_in_stock&&c.variation_is_visible||(i=!1),a.trim(d.$singleVariation.text())?d.$singleVariation.slideDown(200).trigger("show_variation",[c,i]):d.$singleVariation.show().trigger("show_variation",[c,i])},e.prototype.onChange=function(b){var c=b.data.variationForm;c.$form.find('input[name="variation_id"], input.variation_id').val("").change(),c.$form.find(".wc-no-matching-variations").remove(),c.useAjax?c.$form.trigger("check_variations"):(c.$form.trigger("woocommerce_variation_select_change"),c.$form.trigger("check_variations"),a(this).blur()),c.$form.trigger("woocommerce_variation_has_changed")},e.prototype.addSlashes=function(a){return a=a.replace(/'/g,"\\'"),a=a.replace(/"/g,'\\"')},e.prototype.onUpdateAttributes=function(b){var c=b.data.variationForm,d=c.getChosenAttributes(),e=d.data;c.useAjax||(c.$attributeFields.each(function(b,d){var f=a(d),g=f.data("attribute_name")||f.attr("name"),h=a(d).data("show_option_none"),i=":gt(0)",j=0,k=a("<select/>"),l=f.val()||"",m=!0;if(!f.data("attribute_html")){var n=f.clone();n.find("option").removeAttr("disabled attached").removeAttr("selected"),f.data("attribute_options",n.find("option"+i).get()),f.data("attribute_html",n.html())}k.html(f.data("attribute_html"));var o=a.extend(!0,{},e);o[g]="";var p=c.findMatchingVariations(c.variationData,o);for(var q in p)if("undefined"!=typeof p[q]){var r=p[q].attributes;for(var s in r)if(r.hasOwnProperty(s)){var t=r[s],u="";s===g&&(p[q].variation_is_active&&(u="enabled"),t?(t=a("<div/>").html(t).text(),k.find('option[value="'+c.addSlashes(t)+'"]').addClass("attached "+u)):k.find("option:gt(0)").addClass("attached "+u))}}j=k.find("option.attached").length,!l||0!==j&&0!==k.find('option.attached.enabled[value="'+c.addSlashes(l)+'"]').length||(m=!1),j>0&&l&&m&&"no"===h&&(k.find("option:first").remove(),i=""),k.find("option"+i+":not(.attached)").remove(),f.html(k.html()),f.find("option"+i+":not(.enabled)").prop("disabled",!0),l?m?f.val(l):f.val("").change():f.val("")}),c.$form.trigger("woocommerce_update_variation_values"))},e.prototype.getChosenAttributes=function(){var b={},c=0,d=0;return this.$attributeFields.each(function(){var e=a(this).data("attribute_name")||a(this).attr("name"),f=a(this).val()||"";f.length>0&&d++,c++,b[e]=f}),{count:c,chosenCount:d,data:b}},e.prototype.findMatchingVariations=function(a,b){for(var c=[],d=0;d<a.length;d++){var e=a[d];this.isMatch(e.attributes,b)&&c.push(e)}return c},e.prototype.isMatch=function(a,b){var c=!0;for(var e in a)if(a.hasOwnProperty(e)){var f=a[e],g=b[e];f!==d&&g!==d&&0!==f.length&&0!==g.length&&f!==g&&(c=!1)}return c},e.prototype.toggleResetLink=function(a){a?"hidden"===this.$resetVariations.css("visibility")&&this.$resetVariations.css("visibility","visible").hide().fadeIn():this.$resetVariations.css("visibility","hidden")},a.fn.wc_variation_form=function(){return new e(this),this},a.fn.wc_set_content=function(a){d===this.attr("data-o_content")&&this.attr("data-o_content",this.text()),this.text(a)},a.fn.wc_reset_content=function(){d!==this.attr("data-o_content")&&this.text(this.attr("data-o_content"))},a.fn.wc_set_variation_attr=function(a,b){d===this.attr("data-o_"+a)&&this.attr("data-o_"+a,this.attr(a)?this.attr(a):""),!1===b?this.removeAttr(a):this.attr(a,b)},a.fn.wc_reset_variation_attr=function(a){d!==this.attr("data-o_"+a)&&this.attr(a,this.attr("data-o_"+a))},a.fn.wc_maybe_trigger_slide_position_reset=function(b){var c=a(this),d=c.closest(".product"),e=d.find(".images"),f=!1,g=b&&b.image_id?b.image_id:"";c.attr("current-image")!==g&&(f=!0),c.attr("current-image",g),f&&e.trigger("woocommerce_gallery_reset_slide_position")},a.fn.wc_variations_image_update=function(c){var d=this,e=d.closest(".product"),f=e.find(".images"),g=e.find(".flex-control-nav li:eq(0) img"),h=f.find(".woocommerce-product-gallery__image, .woocommerce-product-gallery__image--placeholder").eq(0),i=h.find(".wp-post-image"),j=h.find("a").eq(0);c&&c.image&&c.image.src&&c.image.src.length>1?(i.wc_set_variation_attr("src",c.image.src),i.wc_set_variation_attr("height",c.image.src_h),i.wc_set_variation_attr("width",c.image.src_w),i.wc_set_variation_attr("srcset",c.image.srcset),i.wc_set_variation_attr("sizes",c.image.sizes),i.wc_set_variation_attr("title",c.image.title),i.wc_set_variation_attr("alt",c.image.alt),i.wc_set_variation_attr("data-src",c.image.full_src),i.wc_set_variation_attr("data-large_image",c.image.full_src),i.wc_set_variation_attr("data-large_image_width",c.image.full_src_w),i.wc_set_variation_attr("data-large_image_height",c.image.full_src_h),h.wc_set_variation_attr("data-thumb",c.image.src),g.wc_set_variation_attr("src",c.image.src),j.wc_set_variation_attr("href",c.image.full_src)):(i.wc_reset_variation_attr("src"),i.wc_reset_variation_attr("width"),i.wc_reset_variation_attr("height"),i.wc_reset_variation_attr("srcset"),i.wc_reset_variation_attr("sizes"),i.wc_reset_variation_attr("title"),i.wc_reset_variation_attr("alt"),i.wc_reset_variation_attr("data-src"),i.wc_reset_variation_attr("data-large_image"),i.wc_reset_variation_attr("data-large_image_width"),i.wc_reset_variation_attr("data-large_image_height"),h.wc_reset_variation_attr("data-thumb"),g.wc_reset_variation_attr("src"),j.wc_reset_variation_attr("href")),b.setTimeout(function(){f.trigger("woocommerce_gallery_init_zoom"),d.wc_maybe_trigger_slide_position_reset(c),a(b).trigger("resize")},10)},a(function(){"undefined"!=typeof wc_add_to_cart_variation_params&&a(".variations_form").each(function(){a(this).wc_variation_form()})})}(jQuery,window,document);