2016-12-05 18:34:30 +00:00
|
|
|
/*global wc_single_product_params, PhotoSwipe, PhotoSwipeUI_Default */
|
2014-03-01 20:10:25 +00:00
|
|
|
jQuery( function( $ ) {
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-12-04 19:15:24 +00:00
|
|
|
// wc_single_product_params is required to continue, ensure the object exists
|
2014-03-19 22:56:07 +00:00
|
|
|
if ( typeof wc_single_product_params === 'undefined' ) {
|
2013-12-04 19:15:24 +00:00
|
|
|
return false;
|
2014-03-19 22:56:07 +00:00
|
|
|
}
|
2013-12-04 19:15:24 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
// Tabs
|
2016-04-18 10:08:42 +00:00
|
|
|
$( 'body' )
|
|
|
|
.on( 'init', '.wc-tabs-wrapper, .woocommerce-tabs', function() {
|
2015-08-11 09:46:21 +00:00
|
|
|
$( '.wc-tab, .woocommerce-tabs .panel:not(.panel .panel)' ).hide();
|
2015-04-23 11:24:15 +00:00
|
|
|
|
|
|
|
var hash = window.location.hash;
|
|
|
|
var url = window.location.href;
|
2015-07-10 05:13:30 +00:00
|
|
|
var $tabs = $( this ).find( '.wc-tabs, ul.tabs' ).first();
|
2015-04-23 11:24:15 +00:00
|
|
|
|
2016-04-15 17:25:16 +00:00
|
|
|
if ( hash.toLowerCase().indexOf( 'comment-' ) >= 0 || hash === '#reviews' || hash === '#tab-reviews' ) {
|
2015-07-10 05:13:30 +00:00
|
|
|
$tabs.find( 'li.reviews_tab a' ).click();
|
|
|
|
} else if ( url.indexOf( 'comment-page-' ) > 0 || url.indexOf( 'cpage=' ) > 0 ) {
|
|
|
|
$tabs.find( 'li.reviews_tab a' ).click();
|
2015-04-23 11:24:15 +00:00
|
|
|
} else {
|
2015-07-10 05:13:30 +00:00
|
|
|
$tabs.find( 'li:first a' ).click();
|
2015-04-23 11:24:15 +00:00
|
|
|
}
|
2017-03-13 16:58:39 +00:00
|
|
|
} )
|
2016-03-04 18:06:25 +00:00
|
|
|
.on( 'click', '.wc-tabs li a, ul.tabs li a', function( e ) {
|
|
|
|
e.preventDefault();
|
2015-04-23 11:24:15 +00:00
|
|
|
var $tab = $( this );
|
|
|
|
var $tabs_wrapper = $tab.closest( '.wc-tabs-wrapper, .woocommerce-tabs' );
|
|
|
|
var $tabs = $tabs_wrapper.find( '.wc-tabs, ul.tabs' );
|
|
|
|
|
2015-07-10 05:13:30 +00:00
|
|
|
$tabs.find( 'li' ).removeClass( 'active' );
|
2015-08-12 11:05:31 +00:00
|
|
|
$tabs_wrapper.find( '.wc-tab, .panel:not(.panel .panel)' ).hide();
|
2015-04-23 11:24:15 +00:00
|
|
|
|
2015-07-10 05:13:30 +00:00
|
|
|
$tab.closest( 'li' ).addClass( 'active' );
|
2015-04-23 11:24:15 +00:00
|
|
|
$tabs_wrapper.find( $tab.attr( 'href' ) ).show();
|
2017-03-13 16:58:39 +00:00
|
|
|
} )
|
2016-04-19 02:00:16 +00:00
|
|
|
// Review link
|
2016-04-18 10:08:42 +00:00
|
|
|
.on( 'click', 'a.woocommerce-review-link', function() {
|
|
|
|
$( '.reviews_tab a' ).click();
|
2016-04-19 02:00:16 +00:00
|
|
|
return true;
|
2017-03-13 16:58:39 +00:00
|
|
|
} )
|
2016-04-19 02:00:16 +00:00
|
|
|
// Star ratings for comments
|
2016-04-18 10:08:42 +00:00
|
|
|
.on( 'init', '#rating', function() {
|
|
|
|
$( '#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>' );
|
2017-03-13 16:58:39 +00:00
|
|
|
} )
|
2014-03-01 20:10:25 +00:00
|
|
|
.on( 'click', '#respond p.stars a', function() {
|
2015-10-08 10:27:25 +00:00
|
|
|
var $star = $( this ),
|
|
|
|
$rating = $( this ).closest( '#respond' ).find( '#rating' ),
|
|
|
|
$container = $( this ).closest( '.stars' );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2013-02-11 15:37:26 +00:00
|
|
|
$rating.val( $star.text() );
|
2014-03-01 20:10:25 +00:00
|
|
|
$star.siblings( 'a' ).removeClass( 'active' );
|
|
|
|
$star.addClass( 'active' );
|
2015-10-08 10:27:25 +00:00
|
|
|
$container.addClass( 'selected' );
|
2012-12-10 13:11:05 +00:00
|
|
|
|
2012-07-17 14:09:18 +00:00
|
|
|
return false;
|
2017-03-13 16:58:39 +00:00
|
|
|
} )
|
2014-03-01 20:10:25 +00:00
|
|
|
.on( 'click', '#respond #submit', function() {
|
|
|
|
var $rating = $( this ).closest( '#respond' ).find( '#rating' ),
|
|
|
|
rating = $rating.val();
|
|
|
|
|
2016-03-01 13:08:25 +00:00
|
|
|
if ( $rating.length > 0 && ! rating && wc_single_product_params.review_rating_required === 'yes' ) {
|
2015-07-10 05:13:30 +00:00
|
|
|
window.alert( wc_single_product_params.i18n_required_rating_text );
|
2013-02-11 15:37:26 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-13 16:58:39 +00:00
|
|
|
} );
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
//Init Tabs and Star Ratings
|
2016-04-19 02:00:16 +00:00
|
|
|
$( '.wc-tabs-wrapper, .woocommerce-tabs, #rating' ).trigger( 'init' );
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Product gallery class.
|
|
|
|
*/
|
2017-03-14 10:55:25 +00:00
|
|
|
var Product_Gallery = function( $el, args ) {
|
2017-03-13 16:58:39 +00:00
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
var gallery = this;
|
|
|
|
|
|
|
|
this.$el = $el;
|
|
|
|
this.$images = $( '.woocommerce-product-gallery__image', $el );
|
|
|
|
|
|
|
|
// Make this object available.
|
|
|
|
$el.data['product_gallery'] = this;
|
|
|
|
|
|
|
|
// Pick functionality to initialize...
|
|
|
|
this.flexslider_enabled = $.isFunction( $.fn.flexslider ) && wc_single_product_params.flexslider_enabled;
|
|
|
|
this.zoom_enabled = $.isFunction( $.fn.zoom ) && wc_single_product_params.zoom_enabled;
|
|
|
|
this.photoswipe_enabled = typeof PhotoSwipe !== 'undefined' && wc_single_product_params.photoswipe_enabled;
|
|
|
|
|
|
|
|
// ...also taking args into account.
|
|
|
|
if ( args ) {
|
|
|
|
this.flexslider_enabled = false === args.photoswipe_enabled ? false : this.flexslider_enabled;
|
|
|
|
this.zoom_enabled = false === args.zoom_enabled ? false : this.zoom_enabled;
|
|
|
|
this.photoswipe_enabled = false === args.photoswipe_enabled ? false : this.photoswipe_enabled;
|
|
|
|
}
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize gallery actions and events.
|
|
|
|
*/
|
2017-03-13 16:58:39 +00:00
|
|
|
this.init = function() {
|
|
|
|
|
|
|
|
this.init_flexslider();
|
|
|
|
this.init_zoom();
|
|
|
|
this.init_photoswipe();
|
2017-03-14 11:52:48 +00:00
|
|
|
|
|
|
|
$el.on( 'woocommerce_gallery_init_zoom', this.init_zoom );
|
|
|
|
$el.on( 'woocommerce_gallery_reset_slide_position', this.reset_slide_position );
|
2017-03-13 16:58:39 +00:00
|
|
|
};
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize flexSlider.
|
|
|
|
*/
|
2017-03-13 16:58:39 +00:00
|
|
|
this.init_flexslider = function() {
|
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
if ( ! this.flexslider_enabled ) {
|
2017-03-13 16:58:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$el.flexslider( {
|
2016-10-13 14:25:42 +00:00
|
|
|
selector: '.woocommerce-product-gallery__wrapper > .woocommerce-product-gallery__image',
|
2016-12-05 18:34:30 +00:00
|
|
|
animation: wc_single_product_params.flexslider.animation,
|
|
|
|
smoothHeight: wc_single_product_params.flexslider.smoothHeight,
|
|
|
|
directionNav: wc_single_product_params.flexslider.directionNav,
|
|
|
|
controlNav: wc_single_product_params.flexslider.controlNav,
|
|
|
|
slideshow: wc_single_product_params.flexslider.slideshow,
|
|
|
|
animationSpeed: wc_single_product_params.flexslider.animationSpeed,
|
2017-01-26 14:57:51 +00:00
|
|
|
animationLoop: wc_single_product_params.flexslider.animationLoop, // Breaks photoswipe pagination if true.
|
2017-01-23 23:35:30 +00:00
|
|
|
start: function() {
|
2017-03-14 10:55:25 +00:00
|
|
|
|
2017-01-23 23:35:30 +00:00
|
|
|
var largest_height = 0;
|
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
gallery.$images.each( function() {
|
2017-01-23 23:35:30 +00:00
|
|
|
var height = $( this ).height();
|
|
|
|
|
|
|
|
if ( height > largest_height ) {
|
|
|
|
largest_height = height;
|
|
|
|
}
|
2017-03-13 16:58:39 +00:00
|
|
|
} );
|
2017-01-23 23:35:30 +00:00
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
gallery.$images.each( function() {
|
2017-01-23 23:35:30 +00:00
|
|
|
$( this ).css( 'min-height', largest_height );
|
2017-03-13 16:58:39 +00:00
|
|
|
} );
|
2017-01-23 23:35:30 +00:00
|
|
|
}
|
2017-03-01 19:42:41 +00:00
|
|
|
} );
|
2017-03-13 16:58:39 +00:00
|
|
|
|
|
|
|
};
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init zoom.
|
|
|
|
*/
|
2017-03-13 16:58:39 +00:00
|
|
|
this.init_zoom = function() {
|
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
if ( ! gallery.zoom_enabled ) {
|
2017-03-13 16:58:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
var zoom_target = gallery.$images,
|
2017-02-23 12:29:05 +00:00
|
|
|
enable_zoom = false;
|
2017-01-27 15:35:29 +00:00
|
|
|
|
2017-03-14 11:44:45 +00:00
|
|
|
if ( ! gallery.flexslider_enabled ) {
|
2017-01-27 15:35:29 +00:00
|
|
|
zoom_target = zoom_target.first();
|
|
|
|
}
|
|
|
|
|
2017-02-23 12:29:05 +00:00
|
|
|
$( zoom_target ).each( function( index, target ) {
|
|
|
|
var image = $( target ).find( 'img' );
|
|
|
|
|
2017-03-13 16:58:39 +00:00
|
|
|
if ( image.attr( 'width' ) > $el.width() ) {
|
2017-02-23 12:29:05 +00:00
|
|
|
enable_zoom = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} );
|
2017-01-27 15:35:29 +00:00
|
|
|
|
2016-11-14 11:50:37 +00:00
|
|
|
// But only zoom if the img is larger than its container.
|
2017-02-23 12:29:05 +00:00
|
|
|
if ( enable_zoom ) {
|
2017-01-27 15:35:29 +00:00
|
|
|
zoom_target.trigger( 'zoom.destroy' );
|
2017-03-14 10:55:25 +00:00
|
|
|
zoom_target.zoom( {
|
2016-11-14 11:50:37 +00:00
|
|
|
touch: false
|
2017-03-13 16:58:39 +00:00
|
|
|
} );
|
2016-10-13 14:25:42 +00:00
|
|
|
}
|
2017-03-13 16:58:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.reset_slide_position = function() {
|
2017-03-14 11:52:48 +00:00
|
|
|
|
|
|
|
if ( ! gallery.flexslider_enabled ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-13 16:58:39 +00:00
|
|
|
$el.flexslider( 0 );
|
|
|
|
};
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get product gallery image items.
|
|
|
|
*/
|
2017-03-13 16:58:39 +00:00
|
|
|
this.get_gallery_items = function() {
|
2017-03-14 10:55:25 +00:00
|
|
|
|
|
|
|
var $slides = this.$images,
|
|
|
|
items = [];
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
if ( $slides.length > 0 ) {
|
|
|
|
$slides.each( function( i, el ) {
|
|
|
|
var img = $( el ).find( 'img' ),
|
2017-03-14 11:33:05 +00:00
|
|
|
large_image_src = img.attr( 'data-large_image' ),
|
|
|
|
large_image_w = img.attr( 'data-large_image_width' ),
|
|
|
|
large_image_h = img.attr( 'data-large_image_height' ),
|
2016-10-13 14:25:42 +00:00
|
|
|
item = {
|
|
|
|
src: large_image_src,
|
|
|
|
w: large_image_w,
|
|
|
|
h: large_image_h,
|
|
|
|
title: img.attr( 'title' )
|
|
|
|
};
|
|
|
|
items.push( item );
|
2017-03-13 16:58:39 +00:00
|
|
|
} );
|
2016-10-13 14:25:42 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
return items;
|
2017-03-13 16:58:39 +00:00
|
|
|
};
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Init PhotoSwipe.
|
|
|
|
*/
|
2017-03-13 16:58:39 +00:00
|
|
|
this.init_photoswipe = function() {
|
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
if ( ! this.photoswipe_enabled ) {
|
2017-03-13 16:58:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-14 10:55:25 +00:00
|
|
|
if ( this.zoom_enabled && this.$images.length > 0 ) {
|
|
|
|
$el.prepend( '<a href="#" class="woocommerce-product-gallery__trigger">🔍</a>' );
|
2017-03-13 16:58:39 +00:00
|
|
|
$el.on( 'click', '.woocommerce-product-gallery__trigger', { gallery: this }, this.trigger_photoswipe );
|
2017-01-27 15:35:29 +00:00
|
|
|
}
|
2017-03-14 10:55:25 +00:00
|
|
|
|
2017-03-13 16:58:39 +00:00
|
|
|
$el.on( 'click', '.woocommerce-product-gallery__image a', { gallery: this }, this.trigger_photoswipe );
|
|
|
|
};
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise photoswipe.
|
|
|
|
*/
|
2017-03-13 16:58:39 +00:00
|
|
|
this.trigger_photoswipe = function( e ) {
|
|
|
|
|
2016-11-03 11:35:55 +00:00
|
|
|
e.preventDefault();
|
|
|
|
|
2017-01-27 15:35:29 +00:00
|
|
|
var pswpElement = $( '.pswp' )[0],
|
2017-03-13 16:58:39 +00:00
|
|
|
items = e.data.gallery.get_gallery_items(),
|
2017-01-27 15:35:29 +00:00
|
|
|
target = $( e.target ),
|
2017-02-21 08:11:39 +00:00
|
|
|
clicked;
|
2016-10-13 14:25:42 +00:00
|
|
|
|
2017-01-27 15:35:29 +00:00
|
|
|
if ( ! target.is( '.woocommerce-product-gallery__trigger' ) ) {
|
2017-03-14 10:55:25 +00:00
|
|
|
clicked = e.target.closest( '.woocommerce-product-gallery__image' );
|
2017-02-21 08:11:39 +00:00
|
|
|
} else {
|
2017-03-13 16:58:39 +00:00
|
|
|
clicked = e.data.gallery.$el.find( '.flex-active-slide' );
|
2017-01-27 15:35:29 +00:00
|
|
|
}
|
2016-10-13 14:25:42 +00:00
|
|
|
|
|
|
|
var options = {
|
2017-02-21 10:35:11 +00:00
|
|
|
index: $( clicked ).index(),
|
2016-10-13 14:25:42 +00:00
|
|
|
shareEl: false,
|
|
|
|
closeOnScroll: false,
|
|
|
|
history: false,
|
|
|
|
hideAnimationDuration: 0,
|
|
|
|
showAnimationDuration: 0
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initializes and opens PhotoSwipe.
|
2017-03-14 10:55:25 +00:00
|
|
|
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options );
|
2016-10-13 14:25:42 +00:00
|
|
|
gallery.init();
|
2017-03-13 16:58:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.init();
|
2016-10-13 14:25:42 +00:00
|
|
|
};
|
|
|
|
|
2017-03-13 16:58:39 +00:00
|
|
|
/**
|
|
|
|
* Function to call wc_product_gallery on jquery selector.
|
|
|
|
*/
|
2017-03-14 10:55:25 +00:00
|
|
|
$.fn.wc_product_gallery = function( args ) {
|
|
|
|
new Product_Gallery( this, args );
|
2017-03-13 16:58:39 +00:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize all galleries on page.
|
|
|
|
*/
|
|
|
|
$( '.woocommerce-product-gallery' ).each( function() {
|
2017-03-14 10:55:25 +00:00
|
|
|
$( this ).wc_product_gallery();
|
2017-03-13 16:58:39 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
} );
|