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

210 lines
6.2 KiB
JavaScript
Raw Normal View History

Add/product galleries (#11665) * register prettyPhoto but don't enqueue. #10721 * Remove lightbox option. #10721 Also registers the prettyPhoto styles instead of enqueueing * Stip all prettyPhoto related stuff from the templates and tidy up some logic. #10721 * Add flexslider assets #10721 * styling and re-adds `woocommerce_single_product_image_thumbnail_html`. #10721. * Add zoom functionality. #10721 * Move js to single-product.js. #10721 * styling of the gallery thumbnails when using the `product_page` shortcode. #10721 * Only enable the zoom if the image is large enough. #10721 * Use a 4 column layout by default. #10721 * Make the carousel options filterable. #10721 * rtl styles for gallery. #10721 * Don't zoom on handheld devices. #10721 * markup tweaks in prep for photoswipe * disable zoom for now * Add and enqueue photoswipe assets * add the photoswipe template * initialise photoswipe - it's alive! * tidy up js. add title. add separate trigger. #10721 * Move photoswipe functions to single-product.js. #10721 * reactivate the zoom! #10721 * style the photoswipe trigger. #10721 * disable flexslider animation loop. #10721 * js tidy up. #10721 * Fix jshint * Abstract the product gallery scripts * Minify * Fixed conflict with the admin bar * Photoswipe conflict with admin bar * Index. #10721 * photoswipe button styles. #10721 * Styling. #10721 * No animation on zoom * Image width on mobile * No shadows please. #10721 Looking at you, Twenty Twelve. * code tidy and add class to placeholder figure. #10721 * simplify rtl query * photoswipe button styles * Comma should not be here * zoom icon #10721 * gallery thumb styles. #10721 * trigger icon. #10721 * Image margins. #10721 * icon hover states. #10721 * Variation handling * Fix zoom and heights * Resize after timeout
2016-10-13 14:25:42 +00:00
/*global wc_single_product_params, flexslider_options, PhotoSwipe, PhotoSwipeUI_Default */
jQuery( function( $ ) {
2012-12-10 13:11:05 +00:00
// wc_single_product_params is required to continue, ensure the object exists
if ( typeof wc_single_product_params === 'undefined' ) {
return false;
}
// Tabs
$( 'body' )
.on( 'init', '.wc-tabs-wrapper, .woocommerce-tabs', function() {
$( '.wc-tab, .woocommerce-tabs .panel:not(.panel .panel)' ).hide();
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();
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();
} else {
2015-07-10 05:13:30 +00:00
$tabs.find( 'li:first a' ).click();
}
})
.on( 'click', '.wc-tabs li a, ul.tabs li a', function( e ) {
e.preventDefault();
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' );
$tabs_wrapper.find( '.wc-tab, .panel:not(.panel .panel)' ).hide();
2015-07-10 05:13:30 +00:00
$tab.closest( 'li' ).addClass( 'active' );
$tabs_wrapper.find( $tab.attr( 'href' ) ).show();
})
// Review link
.on( 'click', 'a.woocommerce-review-link', function() {
$( '.reviews_tab a' ).click();
return true;
})
// Star ratings for comments
.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>' );
})
.on( 'click', '#respond p.stars a', function() {
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() );
$star.siblings( 'a' ).removeClass( 'active' );
$star.addClass( 'active' );
$container.addClass( 'selected' );
2012-12-10 13:11:05 +00:00
return false;
2013-02-11 15:37:26 +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;
}
Add/product galleries (#11665) * register prettyPhoto but don't enqueue. #10721 * Remove lightbox option. #10721 Also registers the prettyPhoto styles instead of enqueueing * Stip all prettyPhoto related stuff from the templates and tidy up some logic. #10721 * Add flexslider assets #10721 * styling and re-adds `woocommerce_single_product_image_thumbnail_html`. #10721. * Add zoom functionality. #10721 * Move js to single-product.js. #10721 * styling of the gallery thumbnails when using the `product_page` shortcode. #10721 * Only enable the zoom if the image is large enough. #10721 * Use a 4 column layout by default. #10721 * Make the carousel options filterable. #10721 * rtl styles for gallery. #10721 * Don't zoom on handheld devices. #10721 * markup tweaks in prep for photoswipe * disable zoom for now * Add and enqueue photoswipe assets * add the photoswipe template * initialise photoswipe - it's alive! * tidy up js. add title. add separate trigger. #10721 * Move photoswipe functions to single-product.js. #10721 * reactivate the zoom! #10721 * style the photoswipe trigger. #10721 * disable flexslider animation loop. #10721 * js tidy up. #10721 * Fix jshint * Abstract the product gallery scripts * Minify * Fixed conflict with the admin bar * Photoswipe conflict with admin bar * Index. #10721 * photoswipe button styles. #10721 * Styling. #10721 * No animation on zoom * Image width on mobile * No shadows please. #10721 Looking at you, Twenty Twelve. * code tidy and add class to placeholder figure. #10721 * simplify rtl query * photoswipe button styles * Comma should not be here * zoom icon #10721 * gallery thumb styles. #10721 * trigger icon. #10721 * Image margins. #10721 * icon hover states. #10721 * Variation handling * Fix zoom and heights * Resize after timeout
2016-10-13 14:25:42 +00:00
})
.on( 'woocommerce_init_gallery', function() {
if ( $.isFunction( $.fn.zoom ) ) {
wc_product_gallery.init_zoom();
}
2013-02-11 15:37:26 +00:00
});
Add/product galleries (#11665) * register prettyPhoto but don't enqueue. #10721 * Remove lightbox option. #10721 Also registers the prettyPhoto styles instead of enqueueing * Stip all prettyPhoto related stuff from the templates and tidy up some logic. #10721 * Add flexslider assets #10721 * styling and re-adds `woocommerce_single_product_image_thumbnail_html`. #10721. * Add zoom functionality. #10721 * Move js to single-product.js. #10721 * styling of the gallery thumbnails when using the `product_page` shortcode. #10721 * Only enable the zoom if the image is large enough. #10721 * Use a 4 column layout by default. #10721 * Make the carousel options filterable. #10721 * rtl styles for gallery. #10721 * Don't zoom on handheld devices. #10721 * markup tweaks in prep for photoswipe * disable zoom for now * Add and enqueue photoswipe assets * add the photoswipe template * initialise photoswipe - it's alive! * tidy up js. add title. add separate trigger. #10721 * Move photoswipe functions to single-product.js. #10721 * reactivate the zoom! #10721 * style the photoswipe trigger. #10721 * disable flexslider animation loop. #10721 * js tidy up. #10721 * Fix jshint * Abstract the product gallery scripts * Minify * Fixed conflict with the admin bar * Photoswipe conflict with admin bar * Index. #10721 * photoswipe button styles. #10721 * Styling. #10721 * No animation on zoom * Image width on mobile * No shadows please. #10721 Looking at you, Twenty Twelve. * code tidy and add class to placeholder figure. #10721 * simplify rtl query * photoswipe button styles * Comma should not be here * zoom icon #10721 * gallery thumb styles. #10721 * trigger icon. #10721 * Image margins. #10721 * icon hover states. #10721 * Variation handling * Fix zoom and heights * Resize after timeout
2016-10-13 14:25:42 +00:00
//Init Tabs and Star Ratings
$( '.wc-tabs-wrapper, .woocommerce-tabs, #rating' ).trigger( 'init' );
Add/product galleries (#11665) * register prettyPhoto but don't enqueue. #10721 * Remove lightbox option. #10721 Also registers the prettyPhoto styles instead of enqueueing * Stip all prettyPhoto related stuff from the templates and tidy up some logic. #10721 * Add flexslider assets #10721 * styling and re-adds `woocommerce_single_product_image_thumbnail_html`. #10721. * Add zoom functionality. #10721 * Move js to single-product.js. #10721 * styling of the gallery thumbnails when using the `product_page` shortcode. #10721 * Only enable the zoom if the image is large enough. #10721 * Use a 4 column layout by default. #10721 * Make the carousel options filterable. #10721 * rtl styles for gallery. #10721 * Don't zoom on handheld devices. #10721 * markup tweaks in prep for photoswipe * disable zoom for now * Add and enqueue photoswipe assets * add the photoswipe template * initialise photoswipe - it's alive! * tidy up js. add title. add separate trigger. #10721 * Move photoswipe functions to single-product.js. #10721 * reactivate the zoom! #10721 * style the photoswipe trigger. #10721 * disable flexslider animation loop. #10721 * js tidy up. #10721 * Fix jshint * Abstract the product gallery scripts * Minify * Fixed conflict with the admin bar * Photoswipe conflict with admin bar * Index. #10721 * photoswipe button styles. #10721 * Styling. #10721 * No animation on zoom * Image width on mobile * No shadows please. #10721 Looking at you, Twenty Twelve. * code tidy and add class to placeholder figure. #10721 * simplify rtl query * photoswipe button styles * Comma should not be here * zoom icon #10721 * gallery thumb styles. #10721 * trigger icon. #10721 * Image margins. #10721 * icon hover states. #10721 * Variation handling * Fix zoom and heights * Resize after timeout
2016-10-13 14:25:42 +00:00
/**
* Product gallery class.
*/
var wc_product_gallery = {
/**
* Initialize gallery actions and events.
*/
init: function() {
// Init FlexSlider if present.
if ( $.isFunction( $.fn.flexslider ) ) {
this.init_flexslider();
}
// Init Zoom if present.
if ( $.isFunction( $.fn.zoom ) ) {
this.init_zoom();
}
// Init PhotoSwipe if present.
if ( typeof PhotoSwipe !== 'undefined' ) {
this.init_photoswipe();
// Trigger photoswipe.
$( document ).on( 'click', '.woocommerce-product-gallery__trigger', this.trigger_photoswipe );
}
},
/**
* Detect if the visitor is using a touch device.
*
* @return bool
*/
is_touch_device: function() {
return 'ontouchstart' in window || navigator.maxTouchPoints;
},
/**
* Initialize flexSlider.
*/
init_flexslider: function() {
$( '.woocommerce-product-gallery' ).flexslider({
selector: '.woocommerce-product-gallery__wrapper > .woocommerce-product-gallery__image',
animation: flexslider_options.animation,
smoothHeight: flexslider_options.smoothHeight,
directionNav: flexslider_options.directionNav,
controlNav: flexslider_options.controlNav,
slideshow: flexslider_options.slideshow,
animationSpeed: flexslider_options.animationSpeed,
animationLoop: false // Breaks photoswipe pagination if true. It's hard disabled because we don't need it anyway (no next/prev enabled in flex).
});
},
/**
* Init zoom.
*/
init_zoom: function() {
2016-11-14 11:50:37 +00:00
// But only zoom if the img is larger than its container.
if ( ( $( '.woocommerce-product-gallery__image img' ).attr( 'width' ) > $( '.woocommerce-product-gallery' ).width() ) ) {
$( '.woocommerce-product-gallery__image' ).trigger( 'zoom.destroy' );
$( '.woocommerce-product-gallery__image' ).zoom({
touch: false
});
Add/product galleries (#11665) * register prettyPhoto but don't enqueue. #10721 * Remove lightbox option. #10721 Also registers the prettyPhoto styles instead of enqueueing * Stip all prettyPhoto related stuff from the templates and tidy up some logic. #10721 * Add flexslider assets #10721 * styling and re-adds `woocommerce_single_product_image_thumbnail_html`. #10721. * Add zoom functionality. #10721 * Move js to single-product.js. #10721 * styling of the gallery thumbnails when using the `product_page` shortcode. #10721 * Only enable the zoom if the image is large enough. #10721 * Use a 4 column layout by default. #10721 * Make the carousel options filterable. #10721 * rtl styles for gallery. #10721 * Don't zoom on handheld devices. #10721 * markup tweaks in prep for photoswipe * disable zoom for now * Add and enqueue photoswipe assets * add the photoswipe template * initialise photoswipe - it's alive! * tidy up js. add title. add separate trigger. #10721 * Move photoswipe functions to single-product.js. #10721 * reactivate the zoom! #10721 * style the photoswipe trigger. #10721 * disable flexslider animation loop. #10721 * js tidy up. #10721 * Fix jshint * Abstract the product gallery scripts * Minify * Fixed conflict with the admin bar * Photoswipe conflict with admin bar * Index. #10721 * photoswipe button styles. #10721 * Styling. #10721 * No animation on zoom * Image width on mobile * No shadows please. #10721 Looking at you, Twenty Twelve. * code tidy and add class to placeholder figure. #10721 * simplify rtl query * photoswipe button styles * Comma should not be here * zoom icon #10721 * gallery thumb styles. #10721 * trigger icon. #10721 * Image margins. #10721 * icon hover states. #10721 * Variation handling * Fix zoom and heights * Resize after timeout
2016-10-13 14:25:42 +00:00
}
},
/**
* Get product gallery image items.
*/
get_gallery_items: function() {
var $slides = $( '.woocommerce-product-gallery__wrapper' ).children(),
items = [],
index = $slides.filter( '.' + 'flex-active-slide' ).index();
if ( $slides.length > 0 ) {
$slides.each( function( i, el ) {
var img = $( el ).find( 'img' ),
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' ),
item = {
src: large_image_src,
w: large_image_w,
h: large_image_h,
title: img.attr( 'title' )
};
items.push( item );
});
}
return {
index: index,
items: items
};
},
/**
* Init PhotoSwipe.
*/
init_photoswipe: function() {
$( '.woocommerce-product-gallery' ).prepend( '<a href="#" class="woocommerce-product-gallery__trigger">🔍</a>' );
},
/**
* Initialise photoswipe.
*/
trigger_photoswipe: function( e ) {
e.preventDefault();
Add/product galleries (#11665) * register prettyPhoto but don't enqueue. #10721 * Remove lightbox option. #10721 Also registers the prettyPhoto styles instead of enqueueing * Stip all prettyPhoto related stuff from the templates and tidy up some logic. #10721 * Add flexslider assets #10721 * styling and re-adds `woocommerce_single_product_image_thumbnail_html`. #10721. * Add zoom functionality. #10721 * Move js to single-product.js. #10721 * styling of the gallery thumbnails when using the `product_page` shortcode. #10721 * Only enable the zoom if the image is large enough. #10721 * Use a 4 column layout by default. #10721 * Make the carousel options filterable. #10721 * rtl styles for gallery. #10721 * Don't zoom on handheld devices. #10721 * markup tweaks in prep for photoswipe * disable zoom for now * Add and enqueue photoswipe assets * add the photoswipe template * initialise photoswipe - it's alive! * tidy up js. add title. add separate trigger. #10721 * Move photoswipe functions to single-product.js. #10721 * reactivate the zoom! #10721 * style the photoswipe trigger. #10721 * disable flexslider animation loop. #10721 * js tidy up. #10721 * Fix jshint * Abstract the product gallery scripts * Minify * Fixed conflict with the admin bar * Photoswipe conflict with admin bar * Index. #10721 * photoswipe button styles. #10721 * Styling. #10721 * No animation on zoom * Image width on mobile * No shadows please. #10721 Looking at you, Twenty Twelve. * code tidy and add class to placeholder figure. #10721 * simplify rtl query * photoswipe button styles * Comma should not be here * zoom icon #10721 * gallery thumb styles. #10721 * trigger icon. #10721 * Image margins. #10721 * icon hover states. #10721 * Variation handling * Fix zoom and heights * Resize after timeout
2016-10-13 14:25:42 +00:00
var pswpElement = $( '.pswp' )[0];
// Build items array.
var items = wc_product_gallery.get_gallery_items();
// Define options.
var options = {
index: items.index,
shareEl: false,
closeOnScroll: false,
history: false,
hideAnimationDuration: 0,
showAnimationDuration: 0
};
// Initializes and opens PhotoSwipe.
var gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items.items, options );
gallery.init();
}
};
wc_product_gallery.init();
});