Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion demo/delegate.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ul>

<!-- 2. Include library -->
<script src="../dist/delegate.js"></script>
<script src="../lib/delegate-umd.js"></script>

<!-- 3. Add event delegation -->
<script>
Expand Down
2 changes: 1 addition & 1 deletion demo/multiple.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ul>

<!-- 2. Include library -->
<script src="../dist/delegate.js"></script>
<script src="../lib/delegate-umd.js"></script>

<!-- 3. Add event delegation -->
<script>
Expand Down
2 changes: 1 addition & 1 deletion demo/undelegate.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ul>

<!-- 2. Include library -->
<script src="../dist/delegate.js"></script>
<script src="../lib/delegate-umd.js"></script>

<!-- 3. Remove event delegation -->
<script>
Expand Down
20 changes: 14 additions & 6 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { nodeResolve } = require('@rollup/plugin-node-resolve')

module.exports = function(karma) {
karma.set({
plugins: ['karma-browserify', 'karma-chai', 'karma-sinon', 'karma-mocha', 'karma-phantomjs-launcher'],
plugins: ['karma-rollup-preprocessor', 'karma-chai', 'karma-sinon', 'karma-mocha', 'karma-phantomjs-launcher'],

frameworks: ['browserify', 'chai', 'sinon', 'mocha'],
frameworks: ['chai', 'sinon', 'mocha'],

files: [
'src/**/*.js',
Expand All @@ -11,12 +13,18 @@ module.exports = function(karma) {
],

preprocessors: {
'src/**/*.js' : ['browserify'],
'test/**/*.js': ['browserify']
'src/**/*.js' : ['rollup'],
'test/**/*.js': ['rollup']
},

browserify: {
debug: true
rollupPreprocessor: {
output: {
format: 'iife',
name: 'delegate'
},
plugins: [
nodeResolve({mainFields: ['browser', 'jsnext:main', 'module', 'main']})
]
},

browsers: ['PhantomJS']
Expand Down
116 changes: 116 additions & 0 deletions lib/delegate-umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.delegate = factory());
}(this, (function () { 'use strict';

var DOCUMENT_NODE_TYPE = 9;

/**
* A polyfill for Element.matches()
*/
if (typeof Element !== 'undefined' && !Element.prototype.matches) {
var proto = Element.prototype;

proto.matches = proto.matchesSelector ||
proto.mozMatchesSelector ||
proto.msMatchesSelector ||
proto.oMatchesSelector ||
proto.webkitMatchesSelector;
}

/**
* Finds the closest parent that matches a selector.
*
* @param {Element} element
* @param {String} selector
* @return {Function}
*/
function closest (element, selector) {
while (element && element.nodeType !== DOCUMENT_NODE_TYPE) {
if (typeof element.matches === 'function' &&
element.matches(selector)) {
return element;
}
element = element.parentNode;
}
}

/**
* Delegates event to a selector.
*
* @param {Element} element
* @param {String} selector
* @param {String} type
* @param {Function} callback
* @param {Boolean} useCapture
* @return {Object}
*/
function _delegate(element, selector, type, callback, useCapture) {
var listenerFn = listener.apply(this, arguments);

element.addEventListener(type, listenerFn, useCapture);

return {
destroy: function() {
element.removeEventListener(type, listenerFn, useCapture);
}
}
}

/**
* Delegates event to a selector.
*
* @param {Element|String|Array} [elements]
* @param {String} selector
* @param {String} type
* @param {Function} callback
* @param {Boolean} useCapture
* @return {Object}
*/
function delegate(elements, selector, type, callback, useCapture) {
// Handle the regular Element usage
if (typeof elements.addEventListener === 'function') {
return _delegate.apply(null, arguments);
}

// Handle Element-less usage, it defaults to global delegation
if (typeof type === 'function') {
// Use `document` as the first parameter, then apply arguments
// This is a short way to .unshift `arguments` without running into deoptimizations
return _delegate.bind(null, document).apply(null, arguments);
}

// Handle Selector-based usage
if (typeof elements === 'string') {
elements = document.querySelectorAll(elements);
}

// Handle Array-like based usage
return Array.prototype.map.call(elements, function (element) {
return _delegate(element, selector, type, callback, useCapture);
});
}

/**
* Finds closest match and invokes callback.
*
* @param {Element} element
* @param {String} selector
* @param {String} type
* @param {Function} callback
* @return {Function}
*/
function listener(element, selector, type, callback) {
return function(e) {
e.delegateTarget = closest(e.target, selector);

if (e.delegateTarget) {
callback.call(element, e);
}
}
}

return delegate;

})));
52 changes: 41 additions & 11 deletions dist/delegate.js → lib/delegate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.delegate = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';

var DOCUMENT_NODE_TYPE = 9;

/**
Expand All @@ -23,16 +24,14 @@ if (typeof Element !== 'undefined' && !Element.prototype.matches) {
*/
function closest (element, selector) {
while (element && element.nodeType !== DOCUMENT_NODE_TYPE) {
if (element.matches(selector)) return element;
if (typeof element.matches === 'function' &&
element.matches(selector)) {
return element;
}
element = element.parentNode;
}
}

module.exports = closest;

},{}],2:[function(require,module,exports){
var closest = require('./closest');

/**
* Delegates event to a selector.
*
Expand All @@ -43,7 +42,7 @@ var closest = require('./closest');
* @param {Boolean} useCapture
* @return {Object}
*/
function delegate(element, selector, type, callback, useCapture) {
function _delegate(element, selector, type, callback, useCapture) {
var listenerFn = listener.apply(this, arguments);

element.addEventListener(type, listenerFn, useCapture);
Expand All @@ -55,6 +54,40 @@ function delegate(element, selector, type, callback, useCapture) {
}
}

/**
* Delegates event to a selector.
*
* @param {Element|String|Array} [elements]
* @param {String} selector
* @param {String} type
* @param {Function} callback
* @param {Boolean} useCapture
* @return {Object}
*/
function delegate(elements, selector, type, callback, useCapture) {
// Handle the regular Element usage
if (typeof elements.addEventListener === 'function') {
return _delegate.apply(null, arguments);
}

// Handle Element-less usage, it defaults to global delegation
if (typeof type === 'function') {
// Use `document` as the first parameter, then apply arguments
// This is a short way to .unshift `arguments` without running into deoptimizations
return _delegate.bind(null, document).apply(null, arguments);
}

// Handle Selector-based usage
if (typeof elements === 'string') {
elements = document.querySelectorAll(elements);
}

// Handle Array-like based usage
return Array.prototype.map.call(elements, function (element) {
return _delegate(element, selector, type, callback, useCapture);
});
}

/**
* Finds closest match and invokes callback.
*
Expand All @@ -75,6 +108,3 @@ function listener(element, selector, type, callback) {
}

module.exports = delegate;

},{"./closest":1}]},{},[2])(2)
});
Loading