Skip to content

Commit

Permalink
添加浏览器对 passive 支持的判断;在addEvent和removeEvent中设置相应默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
huzunjie committed Nov 24, 2017
1 parent 7f94dc7 commit ffa0f82
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 38 deletions.
34 changes: 25 additions & 9 deletions lib/index.browser.js
@@ -1,6 +1,6 @@

/**
* chimee-helper-dom v0.1.4
* chimee-helper-dom v0.1.6
* (c) 2017 huzunjie
* Released under MIT
*/
Expand Down Expand Up @@ -2219,6 +2219,22 @@ function hasClassName(el, className) {
return new RegExp('(?:^|\\s)' + className + '(?=\\s|$)').test(el.className);
}

// Test via a getter in the options object to see
// if the passive property is accessed
exports.supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function get() {
exports.supportsPassive = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) {}

// Use our detect's results.
// passive applied if supported, capture will be false either way.
var defaultCapture = exports.supportsPassive ? { passive: true } : false;

/**
* 为HTML元素移除事件监听
* @param {HTMLElement} el 目标元素
Expand All @@ -2229,7 +2245,7 @@ function hasClassName(el, className) {
*/
function removeEvent(el, type, handler) {
var once = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

if (once) {
/* 尝试从缓存中读取包装后的方法 */
Expand All @@ -2251,7 +2267,7 @@ function removeEvent(el, type, handler) {
*/
function addEvent(el, type, handler) {
var once = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

if (once) {
var oldHandler = handler;
Expand Down Expand Up @@ -2281,7 +2297,7 @@ function addEvent(el, type, handler) {
* @param {Boolean} capture 是否在捕获阶段监听
*/
function addDelegate(el, selector, type, handler) {
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;


var handlerWrap = function handlerWrap(e) {
Expand Down Expand Up @@ -2320,7 +2336,7 @@ function addDelegate(el, selector, type, handler) {
* @param {Boolean} capture 是否在捕获阶段监听
*/
function removeDelegate(el, selector, type, handler) {
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

/* 尝试从缓存中读取包装后的方法 */
var handlerWrap = removeEventCache(el, type + '_delegate_' + selector, handler);
Expand Down Expand Up @@ -2703,7 +2719,7 @@ var NodeWrap = function () {
key: 'on',
value: function on(type, handler) {
var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return addEvent(el, type, handler, once, capture);
Expand All @@ -2723,7 +2739,7 @@ var NodeWrap = function () {
key: 'off',
value: function off(type, handler) {
var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return removeEvent(el, type, handler, once, capture);
Expand All @@ -2742,7 +2758,7 @@ var NodeWrap = function () {
}, {
key: 'delegate',
value: function delegate(selector, type, handler) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return addDelegate(el, selector, type, handler, capture);
Expand All @@ -2761,7 +2777,7 @@ var NodeWrap = function () {
}, {
key: 'undelegate',
value: function undelegate(selector, type, handler) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return removeDelegate(el, selector, type, handler, capture);
Expand Down
34 changes: 25 additions & 9 deletions lib/index.js
@@ -1,6 +1,6 @@

/**
* chimee-helper-dom v0.1.4
* chimee-helper-dom v0.1.6
* (c) 2017 huzunjie
* Released under MIT
*/
Expand Down Expand Up @@ -384,6 +384,22 @@ function hasClassName(el, className) {
return new RegExp('(?:^|\\s)' + className + '(?=\\s|$)').test(el.className);
}

// Test via a getter in the options object to see
// if the passive property is accessed
exports.supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function get() {
exports.supportsPassive = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) {}

// Use our detect's results.
// passive applied if supported, capture will be false either way.
var defaultCapture = exports.supportsPassive ? { passive: true } : false;

/**
* 为HTML元素移除事件监听
* @param {HTMLElement} el 目标元素
Expand All @@ -394,7 +410,7 @@ function hasClassName(el, className) {
*/
function removeEvent(el, type, handler) {
var once = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

if (once) {
/* 尝试从缓存中读取包装后的方法 */
Expand All @@ -416,7 +432,7 @@ function removeEvent(el, type, handler) {
*/
function addEvent(el, type, handler) {
var once = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

if (once) {
var oldHandler = handler;
Expand Down Expand Up @@ -446,7 +462,7 @@ function addEvent(el, type, handler) {
* @param {Boolean} capture 是否在捕获阶段监听
*/
function addDelegate(el, selector, type, handler) {
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;


var handlerWrap = function handlerWrap(e) {
Expand Down Expand Up @@ -485,7 +501,7 @@ function addDelegate(el, selector, type, handler) {
* @param {Boolean} capture 是否在捕获阶段监听
*/
function removeDelegate(el, selector, type, handler) {
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

/* 尝试从缓存中读取包装后的方法 */
var handlerWrap = removeEventCache(el, type + '_delegate_' + selector, handler);
Expand Down Expand Up @@ -868,7 +884,7 @@ var NodeWrap = function () {
key: 'on',
value: function on(type, handler) {
var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return addEvent(el, type, handler, once, capture);
Expand All @@ -888,7 +904,7 @@ var NodeWrap = function () {
key: 'off',
value: function off(type, handler) {
var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return removeEvent(el, type, handler, once, capture);
Expand All @@ -907,7 +923,7 @@ var NodeWrap = function () {
}, {
key: 'delegate',
value: function delegate(selector, type, handler) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return addDelegate(el, selector, type, handler, capture);
Expand All @@ -926,7 +942,7 @@ var NodeWrap = function () {
}, {
key: 'undelegate',
value: function undelegate(selector, type, handler) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return removeDelegate(el, selector, type, handler, capture);
Expand Down
2 changes: 1 addition & 1 deletion lib/index.min.js

Large diffs are not rendered by default.

36 changes: 26 additions & 10 deletions lib/index.mjs
@@ -1,6 +1,6 @@

/**
* chimee-helper-dom v0.1.4
* chimee-helper-dom v0.1.6
* (c) 2017 huzunjie
* Released under MIT
*/
Expand Down Expand Up @@ -378,6 +378,22 @@ function hasClassName(el, className) {
return new RegExp('(?:^|\\s)' + className + '(?=\\s|$)').test(el.className);
}

// Test via a getter in the options object to see
// if the passive property is accessed
var supportsPassive = false;
try {
var opts = Object.defineProperty({}, 'passive', {
get: function get() {
supportsPassive = true;
}
});
window.addEventListener("test", null, opts);
} catch (e) {}

// Use our detect's results.
// passive applied if supported, capture will be false either way.
var defaultCapture = supportsPassive ? { passive: true } : false;

/**
* 为HTML元素移除事件监听
* @param {HTMLElement} el 目标元素
Expand All @@ -388,7 +404,7 @@ function hasClassName(el, className) {
*/
function removeEvent(el, type, handler) {
var once = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

if (once) {
/* 尝试从缓存中读取包装后的方法 */
Expand All @@ -410,7 +426,7 @@ function removeEvent(el, type, handler) {
*/
function addEvent(el, type, handler) {
var once = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

if (once) {
var oldHandler = handler;
Expand Down Expand Up @@ -440,7 +456,7 @@ function addEvent(el, type, handler) {
* @param {Boolean} capture 是否在捕获阶段监听
*/
function addDelegate(el, selector, type, handler) {
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;


var handlerWrap = function handlerWrap(e) {
Expand Down Expand Up @@ -479,7 +495,7 @@ function addDelegate(el, selector, type, handler) {
* @param {Boolean} capture 是否在捕获阶段监听
*/
function removeDelegate(el, selector, type, handler) {
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : false;
var capture = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : defaultCapture;

/* 尝试从缓存中读取包装后的方法 */
var handlerWrap = removeEventCache(el, type + '_delegate_' + selector, handler);
Expand Down Expand Up @@ -862,7 +878,7 @@ var NodeWrap = function () {
key: 'on',
value: function on(type, handler) {
var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return addEvent(el, type, handler, once, capture);
Expand All @@ -882,7 +898,7 @@ var NodeWrap = function () {
key: 'off',
value: function off(type, handler) {
var once = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return removeEvent(el, type, handler, once, capture);
Expand All @@ -901,7 +917,7 @@ var NodeWrap = function () {
}, {
key: 'delegate',
value: function delegate(selector, type, handler) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return addDelegate(el, selector, type, handler, capture);
Expand All @@ -920,7 +936,7 @@ var NodeWrap = function () {
}, {
key: 'undelegate',
value: function undelegate(selector, type, handler) {
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
var capture = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : defaultCapture;

return this.each(function (el) {
return removeDelegate(el, selector, type, handler, capture);
Expand All @@ -944,4 +960,4 @@ var NodeWrap = function () {
return NodeWrap;
}();

export { getAttr, setAttr, addClassName, removeClassName, hasClassName, removeEvent, addEvent, addDelegate, removeDelegate, getStyle, setStyle, query, removeEl, findParents, $, NodeWrap };
export { getAttr, setAttr, addClassName, removeClassName, hasClassName, supportsPassive, removeEvent, addEvent, addDelegate, removeDelegate, getStyle, setStyle, query, removeEl, findParents, $, NodeWrap };
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "chimee-helper-dom",
"version": "0.1.5",
"version": "0.1.6",
"description": "dom hanlders of chimee",
"main": "lib/index.js",
"module": "lib/index.mjs",
Expand Down

0 comments on commit ffa0f82

Please sign in to comment.