woocommerce/assets/js/frontend/single-product.min.js

1 line
5.9 KiB
JavaScript

jQuery(function(s){if("undefined"==typeof wc_single_product_params)return!1;s("body").on("init",".wc-tabs-wrapper, .woocommerce-tabs",function(){s(".wc-tab, .woocommerce-tabs .panel:not(.panel .panel)").hide();var e=window.location.hash,t=window.location.href,i=s(this).find(".wc-tabs, ul.tabs").first();0<=e.toLowerCase().indexOf("comment-")||"#reviews"===e||"#tab-reviews"===e?i.find("li.reviews_tab a").click():0<t.indexOf("comment-page-")||0<t.indexOf("cpage=")?i.find("li.reviews_tab a").click():"#tab-additional_information"===e?i.find("li.additional_information_tab a").click():i.find("li:first a").click()}).on("click",".wc-tabs li a, ul.tabs li a",function(e){e.preventDefault();var t=s(this),i=t.closest(".wc-tabs-wrapper, .woocommerce-tabs");i.find(".wc-tabs, ul.tabs").find("li").removeClass("active"),i.find(".wc-tab, .panel:not(.panel .panel)").hide(),t.closest("li").addClass("active"),i.find(t.attr("href")).show()}).on("click","a.woocommerce-review-link",function(){return s(".reviews_tab a").click(),!0}).on("init","#rating",function(){s("#rating").hide().before('<p class="stars"><span><a class="star-1" href="#">1</a><a class="star-2" href="#">2</a><a class="star-3" href="#">3</a><a class="star-4" href="#">4</a><a class="star-5" href="#">5</a></span></p>')}).on("click","#respond p.stars a",function(){var e=s(this),t=s(this).closest("#respond").find("#rating"),i=s(this).closest(".stars");return t.val(e.text()),e.siblings("a").removeClass("active"),e.addClass("active"),i.addClass("selected"),!1}).on("click","#respond #submit",function(){var e=s(this).closest("#respond").find("#rating"),t=e.val();if(0<e.length&&!t&&"yes"===wc_single_product_params.review_rating_required)return window.alert(wc_single_product_params.i18n_required_rating_text),!1}),s(".wc-tabs-wrapper, .woocommerce-tabs, #rating").trigger("init");var t=function(e,t){this.$target=e,this.$images=s(".woocommerce-product-gallery__image",e),0!==this.$images.length?(e.data("product_gallery",this),this.flexslider_enabled=s.isFunction(s.fn.flexslider)&&wc_single_product_params.flexslider_enabled,this.zoom_enabled=s.isFunction(s.fn.zoom)&&wc_single_product_params.zoom_enabled,this.photoswipe_enabled="undefined"!=typeof PhotoSwipe&&wc_single_product_params.photoswipe_enabled,t&&(this.flexslider_enabled=!1!==t.flexslider_enabled&&this.flexslider_enabled,this.zoom_enabled=!1!==t.zoom_enabled&&this.zoom_enabled,this.photoswipe_enabled=!1!==t.photoswipe_enabled&&this.photoswipe_enabled),1===this.$images.length&&(this.flexslider_enabled=!1),this.initFlexslider=this.initFlexslider.bind(this),this.initZoom=this.initZoom.bind(this),this.initZoomForTarget=this.initZoomForTarget.bind(this),this.initPhotoswipe=this.initPhotoswipe.bind(this),this.onResetSlidePosition=this.onResetSlidePosition.bind(this),this.getGalleryItems=this.getGalleryItems.bind(this),this.openPhotoswipe=this.openPhotoswipe.bind(this),this.flexslider_enabled?(this.initFlexslider(),e.on("woocommerce_gallery_reset_slide_position",this.onResetSlidePosition)):this.$target.css("opacity",1),this.zoom_enabled&&(this.initZoom(),e.on("woocommerce_gallery_init_zoom",this.initZoom)),this.photoswipe_enabled&&this.initPhotoswipe()):this.$target.css("opacity",1)};t.prototype.initFlexslider=function(){var e=this.$target,t=this,i=s.extend({selector:".woocommerce-product-gallery__wrapper > .woocommerce-product-gallery__image",start:function(){e.css("opacity",1)},after:function(e){t.initZoomForTarget(t.$images.eq(e.currentSlide))}},wc_single_product_params.flexslider);e.flexslider(i),s(".woocommerce-product-gallery__wrapper .woocommerce-product-gallery__image:eq(0) .wp-post-image").one("load",function(){var i=s(this);i&&setTimeout(function(){var e=i.closest(".woocommerce-product-gallery__image").height(),t=i.closest(".flex-viewport");e&&t&&t.height(e)},100)}).each(function(){this.complete&&s(this).trigger("load")})},t.prototype.initZoom=function(){this.initZoomForTarget(this.$images.first())},t.prototype.initZoomForTarget=function(e){if(!this.zoom_enabled)return!1;var i=this.$target.width(),o=!1;if(s(e).each(function(e,t){if(s(t).find("img").data("large_image_width")>i)return!(o=!0)}),o){var t=s.extend({touch:!1},wc_single_product_params.zoom_options);"ontouchstart"in document.documentElement&&(t.on="click"),e.trigger("zoom.destroy"),e.zoom(t)}},t.prototype.initPhotoswipe=function(){this.zoom_enabled&&0<this.$images.length?(this.$target.prepend('<a href="#" class="woocommerce-product-gallery__trigger">🔍</a>'),this.$target.on("click",".woocommerce-product-gallery__trigger",this.openPhotoswipe),this.$target.on("click",".woocommerce-product-gallery__image a",function(e){e.preventDefault()}),this.flexslider_enabled||this.$target.on("click",".woocommerce-product-gallery__image a",this.openPhotoswipe)):this.$target.on("click",".woocommerce-product-gallery__image a",this.openPhotoswipe)},t.prototype.onResetSlidePosition=function(){this.$target.flexslider(0)},t.prototype.getGalleryItems=function(){var e=this.$images,a=[];return 0<e.length&&e.each(function(e,t){var i=s(t).find("img");if(i.length){var o={src:i.attr("data-large_image"),w:i.attr("data-large_image_width"),h:i.attr("data-large_image_height"),title:i.attr("data-caption")?i.attr("data-caption"):i.attr("title")};a.push(o)}}),a},t.prototype.openPhotoswipe=function(e){e.preventDefault();var t,i=s(".pswp")[0],o=this.getGalleryItems(),a=s(e.target);t=a.is(".woocommerce-product-gallery__trigger")||a.is(".woocommerce-product-gallery__trigger img")?this.$target.find(".flex-active-slide"):a.closest(".woocommerce-product-gallery__image");var r=s.extend({index:s(t).index()},wc_single_product_params.photoswipe_options);new PhotoSwipe(i,PhotoSwipeUI_Default,o,r).init()},s.fn.wc_product_gallery=function(e){return new t(this,e),this},s(".woocommerce-product-gallery").each(function(){s(this).trigger("wc-product-gallery-before-init",[this,wc_single_product_params]),s(this).wc_product_gallery(wc_single_product_params),s(this).trigger("wc-product-gallery-after-init",[this,wc_single_product_params])})});