From 8ad74583947953ec29aaa074db8e60dd22e26896 Mon Sep 17 00:00:00 2001 From: Alexandre Stanislawski Date: Sat, 13 Aug 2016 13:03:36 +0200 Subject: [PATCH] (feat) Add 'show' and 'hide' methods to the API Resolves #121, #122. --- src/baguetteBox.js | 90 +++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 33 deletions(-) diff --git a/src/baguetteBox.js b/src/baguetteBox.js index 29a575d6..9f37e569 100644 --- a/src/baguetteBox.js +++ b/src/baguetteBox.js @@ -58,6 +58,8 @@ var currentGallery = []; // Current image index inside the slider var currentIndex = 0; + // Visibility of the overlay + var isOverlayVisible = false; // Touch event start position (for slide gesture) var touch = {}; // If set to true ignore touch events because animation was already fired @@ -167,7 +169,7 @@ buildOverlay(); removeFromCache(selector); - bindImageClickListeners(selector, userOptions); + return bindImageClickListeners(selector, userOptions); } function bindImageClickListeners(selector, userOptions) { @@ -218,6 +220,8 @@ }); selectorData.galleries.push(gallery); }); + + return selectorData.galleries; } function clearCachedData() { @@ -433,6 +437,7 @@ } documentLastFocus = document.activeElement; initFocus(); + isOverlayVisible = true; } function initFocus() { @@ -485,6 +490,7 @@ options.afterHide(); } documentLastFocus && documentLastFocus.focus(); + isOverlayVisible = false; }, 500); } @@ -584,46 +590,62 @@ // Return false at the right end of the gallery function showNextImage() { - var returnValue; - // Check if next image exists - if (currentIndex <= imagesElements.length - 2) { - currentIndex++; - updateOffset(); - preloadNext(currentIndex); - returnValue = true; - } else if (options.animation) { - slider.className = 'bounce-from-right'; - setTimeout(function() { - slider.className = ''; - }, 400); - returnValue = false; - } - if (options.onChange) { - options.onChange(currentIndex, imagesElements.length); - } - return returnValue; + return show(currentIndex + 1); } // Return false at the left end of the gallery function showPreviousImage() { - var returnValue; - // Check if previous image exists - if (currentIndex >= 1) { - currentIndex--; - updateOffset(); - preloadPrev(currentIndex); - returnValue = true; - } else if (options.animation) { - slider.className = 'bounce-from-left'; - setTimeout(function() { - slider.className = ''; - }, 400); - returnValue = false; + return show(currentIndex - 1); + } + + /** + * Move the gallery to a specific index + * @param {number} index the position of the image + * @param {array} gallery which should be opened + * @return {boolean} the status of the operation + */ + function show(index, gallery) { + if(!isOverlayVisible && index >= 0 && index < gallery.length) { + prepareOverlay(gallery, options); + showOverlay(index); + return true; + } + if(index < 0) { + if (options.animation) { + bounceAnimation('left'); + } + return false; } + if(index >= imagesElements.length) { + if (options.animation) { + bounceAnimation('right'); + } + return false; + } + + currentIndex = index; + loadImage(currentIndex, function() { + preloadNext(currentIndex); + preloadPrev(currentIndex); + }); + updateOffset(); + if (options.onChange) { options.onChange(currentIndex, imagesElements.length); } - return returnValue; + + return true; + } + + /** + * Triggers the bounce animation + * @param {('left'|'right')} direction - Direction of the movement + */ + function bounceAnimation(direction) { + slider.className = 'bounce-from-' + direction; + setTimeout(function() { + slider.className = ''; + }, 400); } function updateOffset() { @@ -732,8 +754,10 @@ return { run: run, + show: show, showNext: showNextImage, showPrevious: showPreviousImage, + hide: hideOverlay, destroy: destroyPlugin }; }));