Skip to content

Commit

Permalink
Build update
Browse files Browse the repository at this point in the history
  • Loading branch information
feimosi committed Jan 23, 2018
1 parent 4544d04 commit 3537ca2
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 44 deletions.
128 changes: 85 additions & 43 deletions dist/baguetteBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -162,11 +164,12 @@
function run(selector, userOptions) {
// Fill supports object
supports.transforms = testTransformsSupport();
supports.svg = testSVGSupport();
supports.svg = testSvgSupport();
supports.passiveEvents = testPassiveEventsSupport();

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

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

return selectorData.galleries;
}

function clearCachedData() {
Expand Down Expand Up @@ -306,25 +311,27 @@
}

function bindEvents() {
var options = supports.passiveEvents ? { passive: true } : null;
bind(overlay, 'click', overlayClickHandler);
bind(previousButton, 'click', previousButtonClickHandler);
bind(nextButton, 'click', nextButtonClickHandler);
bind(closeButton, 'click', closeButtonClickHandler);
bind(slider, 'contextmenu', contextmenuHandler);
bind(overlay, 'touchstart', touchstartHandler);
bind(overlay, 'touchmove', touchmoveHandler);
bind(overlay, 'touchstart', touchstartHandler, options);
bind(overlay, 'touchmove', touchmoveHandler, options);
bind(overlay, 'touchend', touchendHandler);
bind(document, 'focus', trapFocusInsideOverlay, true);
}

function unbindEvents() {
var options = supports.passiveEvents ? { passive: true } : null;
unbind(overlay, 'click', overlayClickHandler);
unbind(previousButton, 'click', previousButtonClickHandler);
unbind(nextButton, 'click', nextButtonClickHandler);
unbind(closeButton, 'click', closeButtonClickHandler);
unbind(slider, 'contextmenu', contextmenuHandler);
unbind(overlay, 'touchstart', touchstartHandler);
unbind(overlay, 'touchmove', touchmoveHandler);
unbind(overlay, 'touchstart', touchstartHandler, options);
unbind(overlay, 'touchmove', touchmoveHandler, options);
unbind(overlay, 'touchend', touchendHandler);
unbind(document, 'focus', trapFocusInsideOverlay, true);
}
Expand Down Expand Up @@ -430,6 +437,7 @@
}
documentLastFocus = document.activeElement;
initFocus();
isOverlayVisible = true;
}

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

Expand Down Expand Up @@ -581,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 `index` {number} - the position of the image
* @param `gallery` {array} - gallery which should be opened, if omitted assumes the currently opened one
* @return {boolean} - true on success or false if the index is invalid
*/
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 All @@ -647,12 +672,27 @@
}

// Inline SVG test
function testSVGSupport() {
function testSvgSupport() {
var div = create('div');
div.innerHTML = '<svg/>';
return (div.firstChild && div.firstChild.namespaceURI) === 'http://www.w3.org/2000/svg';
}

// Borrowed from https://github.com/seiyria/bootstrap-slider/pull/680/files
function testPassiveEventsSupport() {
var passiveEvents = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function() {
passiveEvents = true;
}
});
window.addEventListener('test', null, opts);
} catch (e) { /* Silence the error and continue */ }

return passiveEvents;
}

function preloadNext(index) {
if (index - currentIndex >= options.preload) {
return;
Expand All @@ -671,9 +711,9 @@
});
}

function bind(element, event, callback, useCapture) {
function bind(element, event, callback, options) {
if (element.addEventListener) {
element.addEventListener(event, callback, useCapture);
element.addEventListener(event, callback, options);
} else {
// IE8 fallback
element.attachEvent('on' + event, function(event) {
Expand All @@ -685,9 +725,9 @@
}
}

function unbind(element, event, callback, useCapture) {
function unbind(element, event, callback, options) {
if (element.removeEventListener) {
element.removeEventListener(event, callback, useCapture);
element.removeEventListener(event, callback, options);
} else {
// IE8 fallback
element.detachEvent('on' + event, callback);
Expand All @@ -714,8 +754,10 @@

return {
run: run,
show: show,
showNext: showNextImage,
showPrevious: showPreviousImage,
hide: hideOverlay,
destroy: destroyPlugin
};
}));
Loading

0 comments on commit 3537ca2

Please sign in to comment.