Skip to content

Commit

Permalink
(feat) Add 'show' and 'hide' methods to the API
Browse files Browse the repository at this point in the history
Resolves #121, #122.
  • Loading branch information
Alexandre Stanislawski authored and feimosi committed Sep 16, 2017
1 parent f3a6cfc commit c33d9bd
Showing 1 changed file with 58 additions and 34 deletions.
92 changes: 58 additions & 34 deletions src/baguetteBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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
Expand Down Expand Up @@ -164,7 +166,7 @@

buildOverlay();
removeFromCache(selector);
bindImageClickListeners(selector, userOptions);
return bindImageClickListeners(selector, userOptions);
}

function bindImageClickListeners(selector, userOptions) {
Expand Down Expand Up @@ -215,6 +217,8 @@
});
selectorData.galleries.push(gallery);
});

return selectorData.galleries;
}

function clearCachedData() {
Expand Down Expand Up @@ -426,6 +430,7 @@
}
documentLastFocus = document.activeElement;
initFocus();
isOverlayVisible = true;
}

function initFocus() {
Expand Down Expand Up @@ -479,6 +484,7 @@
}
}, 500);
documentLastFocus && documentLastFocus.focus();
isOverlayVisible = false;
}

function loadImage(index, callback) {
Expand Down Expand Up @@ -577,46 +583,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() {
Expand Down Expand Up @@ -711,7 +733,9 @@
return {
run: run,
destroy: destroyPlugin,
show: show,
showNext: showNextImage,
showPrevious: showPreviousImage
showPrevious: showPreviousImage,
hide: hideOverlay
};
}));

0 comments on commit c33d9bd

Please sign in to comment.