Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) Add passive mode support for touch events #148

Merged
merged 1 commit into from
Jan 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/baguetteBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@
function run(selector, userOptions) {
// Fill supports object
supports.transforms = testTransformsSupport();
supports.svg = testSVGSupport();
supports.svg = testSvgSupport();
supports.passiveEvents = testPassiveEventsSupport();

buildOverlay();
removeFromCache(selector);
Expand Down Expand Up @@ -306,25 +307,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 @@ -647,12 +650,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 +689,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 +703,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 Down