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

Add support for passive event listeners #222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
73 changes: 52 additions & 21 deletions responsive-nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
var responsiveNav = function (el, options) {

var computed = !!window.getComputedStyle;

/**
* getComputedStyle polyfill for old browsers
*/
Expand All @@ -38,7 +38,7 @@
};
}
/* exported addEvent, removeEvent, getChildren, setAttributes, addClass, removeClass, forEach */

/**
* Add Event
* fn arg can be an object or a function, thanks to handleEvent
Expand All @@ -50,16 +50,19 @@
* @param {boolean} bubbling
*/
var addEvent = function (el, evt, fn, bubble) {
// set passive events if this is supported
var options = supportsPassiveEvents ? {passive: true, capture: bubble} : bubble;

if ("addEventListener" in el) {
// BBOS6 doesn't support handleEvent, catch and polyfill
try {
el.addEventListener(evt, fn, bubble);
el.addEventListener(evt, fn, options);
} catch (e) {
if (typeof fn === "object" && fn.handleEvent) {
el.addEventListener(evt, function (e) {
// Bind fn as this and set first arg as event object
fn.handleEvent.call(fn, e);
}, bubble);
}, options);
} else {
throw e;
}
Expand All @@ -76,7 +79,7 @@
}
}
},

/**
* Remove Event
*
Expand Down Expand Up @@ -108,7 +111,32 @@
}
}
},


/**
* Check support for passive event listeners
*
* @return {bool} Returns whether {passive: true} can be used
*/
checkPassiveEventSupport = function () {
var supported = false;

try {
var opts = {
get passive() {
supported = true;
}
};

if ("addEventListener" in window) {
window.addEventListener("test", null, opts);
window.removeEventListener("test", null, opts);
}
} catch (e) {
supported = false;
}
return supported;
},

/**
* Get the children of any element
*
Expand All @@ -129,7 +157,7 @@
}
return children;
},

/**
* Sets multiple attributes at once
*
Expand All @@ -141,7 +169,7 @@
el.setAttribute(key, attrs[key]);
}
},

/**
* Adds a class to any element
*
Expand All @@ -154,7 +182,7 @@
el.className = el.className.replace(/(^\s*)|(\s*$)/g,"");
}
},

/**
* Remove a class from any element
*
Expand All @@ -165,7 +193,7 @@
var reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
el.className = el.className.replace(reg, " ").replace(/(^\s*)|(\s*$)/g,"");
},

/**
* forEach method that passes back the stuff we need
*
Expand All @@ -184,6 +212,7 @@
navToggle,
styleElement = document.createElement("style"),
htmlEl = document.documentElement,
supportsPassiveEvents = checkPassiveEventSupport(),
hasAnimFinished,
isMobile,
navOpen;
Expand Down Expand Up @@ -519,17 +548,19 @@
* @param {event} event
*/
_preventDefault: function(e) {
if (e.preventDefault) {
if (e.stopImmediatePropagation) {
e.stopImmediatePropagation();
}
e.preventDefault();
e.stopPropagation();
return false;
if (!supportsPassiveEvents) {
if (e.preventDefault) {
if (e.stopImmediatePropagation) {
e.stopImmediatePropagation();
}
e.preventDefault();
e.stopPropagation();
return false;

// This is strictly for old IE
} else {
e.returnValue = false;
// This is strictly for old IE
} else {
e.returnValue = false;
}
}
},

Expand Down Expand Up @@ -660,4 +691,4 @@
window.responsiveNav = responsiveNav;
}

}(document, window, 0));
}(document, window, 0));