From 9774cfa15f6bda16edcf3bc961157e37d51a016b Mon Sep 17 00:00:00 2001 From: Trey Richards Date: Tue, 9 May 2017 16:50:34 -0700 Subject: [PATCH] Added namespace to jQuery events that are removed in creation of a VariationForm. --- assets/js/frontend/add-to-cart-variation.js | 14 ++++++-------- assets/js/frontend/add-to-cart-variation.min.js | 2 +- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/assets/js/frontend/add-to-cart-variation.js b/assets/js/frontend/add-to-cart-variation.js index 6436b7056f8..353e3457e4e 100644 --- a/assets/js/frontend/add-to-cart-variation.js +++ b/assets/js/frontend/add-to-cart-variation.js @@ -16,9 +16,7 @@ // Initial state. this.$singleVariationWrap.show(); - this.$form.unbind( 'check_variations update_variation_values found_variation' ); - this.$resetVariations.unbind( 'click' ); - this.$attributeFields.unbind( 'change ' ); + this.$form.off( '.wc-variation-form' ); // Methods. this.getChosenAttributes = this.getChosenAttributes.bind( this ); @@ -27,17 +25,17 @@ this.toggleResetLink = this.toggleResetLink.bind( this ); // Events. - $form.on( 'click', '.reset_variations', { variationForm: this }, this.onReset ); + $form.on( 'click.wc-variation-form', '.reset_variations', { variationForm: this }, this.onReset ); $form.on( 'reload_product_variations', { variationForm: this }, this.onReload ); $form.on( 'hide_variation', { variationForm: this }, this.onHide ); $form.on( 'show_variation', { variationForm: this }, this.onShow ); $form.on( 'click', '.single_add_to_cart_button', { variationForm: this }, this.onAddToCart ); $form.on( 'reset_data', { variationForm: this }, this.onResetDisplayedVariation ); $form.on( 'reset_image', { variationForm: this }, this.onResetImage ); - $form.on( 'change', '.variations select', { variationForm: this }, this.onChange ); - $form.on( 'found_variation', { variationForm: this }, this.onFoundVariation ); - $form.on( 'check_variations', { variationForm: this }, this.onFindVariation ); - $form.on( 'update_variation_values', { variationForm: this }, this.onUpdateAttributes ); + $form.on( 'change.wc-variation-form', '.variations select', { variationForm: this }, this.onChange ); + $form.on( 'found_variation.wc-variation-form', { variationForm: this }, this.onFoundVariation ); + $form.on( 'check_variations.wc-variation-form', { variationForm: this }, this.onFindVariation ); + $form.on( 'update_variation_values.wc-variation-form', { variationForm: this }, this.onUpdateAttributes ); // Check variations once init. $form.trigger( 'check_variations' ); diff --git a/assets/js/frontend/add-to-cart-variation.min.js b/assets/js/frontend/add-to-cart-variation.min.js index bae2fa0e973..dfadc6f3222 100644 --- a/assets/js/frontend/add-to-cart-variation.min.js +++ b/assets/js/frontend/add-to-cart-variation.min.js @@ -1 +1 @@ -!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('

'+wc_add_to_cart_variation_params.i18n_no_matching_variations_text+"

"),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('

'+wc_add_to_cart_variation_params.i18n_no_matching_variations_text+"

"),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("/**/",""),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(""),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("
").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;d1?(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.thumb_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); \ No newline at end of file