diff options
Diffstat (limited to 'plugins/jetpack/modules/carousel/jetpack-carousel.js')
-rw-r--r-- | plugins/jetpack/modules/carousel/jetpack-carousel.js | 84 |
1 files changed, 66 insertions, 18 deletions
diff --git a/plugins/jetpack/modules/carousel/jetpack-carousel.js b/plugins/jetpack/modules/carousel/jetpack-carousel.js index 0774d5f2..9ed78b76 100644 --- a/plugins/jetpack/modules/carousel/jetpack-carousel.js +++ b/plugins/jetpack/modules/carousel/jetpack-carousel.js @@ -37,18 +37,15 @@ jQuery(document).ready(function($) { break; case 39: // right e.preventDefault(); - gallery.jp_carousel('clearCommentTextAreaValue'); gallery.jp_carousel('next'); break; case 37: // left case 8: // backspace e.preventDefault(); - gallery.jp_carousel('clearCommentTextAreaValue'); gallery.jp_carousel('previous'); break; case 27: // escape e.preventDefault(); - gallery.jp_carousel('clearCommentTextAreaValue'); container.jp_carousel('close'); break; default: @@ -193,13 +190,15 @@ jQuery(document).ready(function($) { .addClass('jp-carousel-next-button') .css({ 'right' : '15px' - }); + }) + .hide(); previousButton = $('<div><span></span></div>') .addClass('jp-carousel-previous-button') .css({ 'left' : 0 - }); + }) + .hide(); nextButton.add( previousButton ).css( { 'position' : 'fixed', @@ -387,6 +386,8 @@ jQuery(document).ready(function($) { $(window).unbind('keydown', keyListener); $(window).unbind('resize', resizeListener); $(window).scrollTop(scroll); + $( '.jp-carousel-previous-button' ).hide(); + $( '.jp-carousel-next-button' ).hide(); }) .bind('jp_carousel.afterClose', function(){ if ( window.location.hash && history.back ) { @@ -460,7 +461,7 @@ jQuery(document).ready(function($) { open: function(options) { var settings = { - 'items_selector' : '.gallery-item [data-attachment-id], .tiled-gallery-item [data-attachment-id]', + 'items_selector' : '.gallery-item [data-attachment-id], .tiled-gallery-item [data-attachment-id], img[data-attachment-id]', 'start_index': 0 }, data = $(this).data('carousel-extra'); @@ -523,6 +524,7 @@ jQuery(document).ready(function($) { // make sure to let the page scroll again $('body').css('overflow', originalOverflow); $('html').css('overflow', originalHOverflow); + this.jp_carousel( 'clearCommentTextAreaValue' ); return container .trigger('jp_carousel.beforeClose') .fadeOut('fast', function(){ @@ -532,21 +534,25 @@ jQuery(document).ready(function($) { }, - next : function(){ - var slide = gallery.jp_carousel( 'nextSlide' ); - container.animate({scrollTop:0}, 'fast'); + next : function() { + this.jp_carousel( 'previousOrNext', 'nextSlide' ); + }, - if ( slide ) { - this.jp_carousel('selectSlide', slide); - } + previous : function() { + this.jp_carousel( 'previousOrNext', 'prevSlide' ); }, - previous : function(){ - var slide = gallery.jp_carousel( 'prevSlide' ); - container.animate({scrollTop:0}, 'fast'); + previousOrNext : function ( slideSelectionMethodName ) { + if ( ! this.jp_carousel( 'hasMultipleImages' ) ) { + return false; + } + + var slide = gallery.jp_carousel( slideSelectionMethodName ); if ( slide ) { - this.jp_carousel('selectSlide', slide); + container.animate( { scrollTop: 0 }, 'fast' ); + this.jp_carousel( 'clearCommentTextAreaValue' ); + this.jp_carousel( 'selectSlide', slide ); } }, @@ -1374,6 +1380,10 @@ jQuery(document).ready(function($) { image.data( 'loaded', 1 ); } + }, + + hasMultipleImages : function () { + return gallery.jp_carousel('slides').length > 1; } }; @@ -1391,7 +1401,7 @@ jQuery(document).ready(function($) { }; // register the event listener for starting the gallery - $( document.body ).on( 'click.jp-carousel', 'div.gallery,div.tiled-gallery', function(e) { + $( document.body ).on( 'click.jp-carousel', 'div.gallery,div.tiled-gallery, a.single-image-gallery', function(e) { if ( ! $(this).jp_carousel( 'testForData', e.currentTarget ) ) { return; } @@ -1406,6 +1416,43 @@ jQuery(document).ready(function($) { $(this).jp_carousel('open', {start_index: $(this).find('.gallery-item, .tiled-gallery-item').index($(e.target).parents('.gallery-item, .tiled-gallery-item'))}); }); + // handle lightbox (single image gallery) for images linking to 'Attachment Page' + if ( 1 === Number( jetpackCarouselStrings.single_image_gallery ) ) { + // process links that contain img tag with attribute data-attachment-id + $( 'a img[data-attachment-id]' ).each(function() { + var container = $( this ).parent(); + + // skip if image was already added to gallery by shortcode + if( container.parent( '.gallery-icon' ).length ) { + return; + } + + var valid = false; + + // if link points to 'Media File' and flag is set allow it + if ( $( container ).attr( 'href' ) === $( this ).attr( 'data-orig-file' ) && + 1 === Number( jetpackCarouselStrings.single_image_gallery_media_file ) + ) { + valid = true; + } + + // if link points to 'Attachment Page' allow it + if( $( container ).attr( 'href' ) === $( this ).attr( 'data-permalink' ) ) { + valid = true; + } + + // links to 'Custom URL' or 'Media File' when flag not set are not valid + if( ! valid ) { + return; + } + + // make this node a gallery recognizable by event listener above + $( container ).addClass( 'single-image-gallery' ) ; + // blog_id is needed to allow posting comments to correct blog + $( container ).data( 'carousel-extra', { blog_id: Number( jetpackCarouselStrings.blog_id ) } ); + }); + } + // Makes carousel work on page load and when back button leads to same URL with carousel hash (ie: no actual document.ready trigger) $( window ).on( 'hashchange.jp-carousel', function () { @@ -1432,7 +1479,7 @@ jQuery(document).ready(function($) { last_known_location_hash = window.location.hash; matches = window.location.hash.match( hashRegExp ); attachmentId = parseInt( matches[1], 10 ); - galleries = $( 'div.gallery, div.tiled-gallery' ); + galleries = $( 'div.gallery, div.tiled-gallery, a.single-image-gallery' ); // Find the first thumbnail that matches the attachment ID in the location // hash, then open the gallery that contains it. @@ -1447,6 +1494,7 @@ jQuery(document).ready(function($) { if ( selectedThumbnail ) { $( selectedThumbnail.gallery ) .jp_carousel( 'openOrSelectSlide', selectedThumbnail.index ); + return false; } }); }); |