diff --git a/demo/delegate.html b/demo/delegate.html index 3c09921..35abf3c 100644 --- a/demo/delegate.html +++ b/demo/delegate.html @@ -15,7 +15,7 @@ - + + + + ``` ## Usage diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..a50149b --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,18 @@ +import pkg from './package.json'; + +export default { + input: 'src/delegate.js', + output: [ + { + file: pkg.main, + format: 'cjs', + exports: 'auto' + }, + { + file: 'lib/delegate-umd.js', + format: 'umd', + exports: 'auto', + name: 'delegate' + } + ] +}; diff --git a/src/closest.js b/src/closest.js index 842ea07..b26087d 100644 --- a/src/closest.js +++ b/src/closest.js @@ -20,7 +20,7 @@ if (typeof Element !== 'undefined' && !Element.prototype.matches) { * @param {String} selector * @return {Function} */ -function closest (element, selector) { +export default function closest (element, selector) { while (element && element.nodeType !== DOCUMENT_NODE_TYPE) { if (typeof element.matches === 'function' && element.matches(selector)) { @@ -29,5 +29,3 @@ function closest (element, selector) { element = element.parentNode; } } - -module.exports = closest; diff --git a/src/delegate.js b/src/delegate.js index 9d9397d..928f94a 100644 --- a/src/delegate.js +++ b/src/delegate.js @@ -1,4 +1,4 @@ -var closest = require('./closest'); +import closest from './closest'; /** * Delegates event to a selector. @@ -32,7 +32,7 @@ function _delegate(element, selector, type, callback, useCapture) { * @param {Boolean} useCapture * @return {Object} */ -function delegate(elements, selector, type, callback, useCapture) { +export default function delegate(elements, selector, type, callback, useCapture) { // Handle the regular Element usage if (typeof elements.addEventListener === 'function') { return _delegate.apply(null, arguments); @@ -74,5 +74,3 @@ function listener(element, selector, type, callback) { } } } - -module.exports = delegate; diff --git a/test/closest.js b/test/closest.js index 6a9e25f..38dda2f 100644 --- a/test/closest.js +++ b/test/closest.js @@ -1,4 +1,4 @@ -var closest = require('../src/closest'); +import closest from '../src/closest'; describe('closest', function() { before(function() { @@ -9,10 +9,6 @@ describe('closest', function() { ''; document.body.innerHTML += html; - - global.a = document.querySelector('#a'); - global.b = document.querySelector('#b'); - global.c = document.querySelector('#c'); }); after(function() { @@ -20,9 +16,13 @@ describe('closest', function() { }); it('should return the closest parent based on the selector', function() { - assert.ok(closest(global.c, '#b'), global.b); - assert.ok(closest(global.c, '#a'), global.a); - assert.ok(closest(global.b, '#a'), global.a); + var a = document.querySelector('#a'); + var b = document.querySelector('#b'); + var c = document.querySelector('#c'); + + assert.ok(closest(c, '#b'), b); + assert.ok(closest(c, '#a'), a); + assert.ok(closest(b, '#a'), a); }); it('should return itself if the same selector is passed', function() { diff --git a/test/delegate.js b/test/delegate.js index 669738f..1075d12 100644 --- a/test/delegate.js +++ b/test/delegate.js @@ -1,5 +1,5 @@ -var delegate = require('../src/delegate'); -var simulant = require('simulant'); +import delegate from '../src/delegate'; +import simulant from 'simulant'; describe('delegate', function() { before(function() { @@ -12,39 +12,41 @@ describe('delegate', function() { ''; document.body.innerHTML += html; - - global.container = document.querySelector('ul'); - global.anchor = document.querySelector('a'); - - global.spy = sinon.spy(global.container, 'removeEventListener'); }); after(function() { - global.spy.restore(); document.body.innerHTML = ''; }); it('should add an event listener', function(done) { - delegate(global.container, 'a', 'click', function() { + var container = document.querySelector('ul'); + var anchor = document.querySelector('a'); + + delegate(container, 'a', 'click', function() { done(); }); - simulant.fire(global.anchor, simulant('click')); + simulant.fire(anchor, simulant('click')); }); it('should remove an event listener', function() { - var delegation = delegate(global.container, 'a', 'click', function() {}); + var container = document.querySelector('ul'); + var spy = sinon.spy(container, 'removeEventListener'); + var delegation = delegate(container, 'a', 'click', function() {}); delegation.destroy(); - assert.ok(global.spy.calledOnce); + assert.ok(spy.calledOnce); + + spy.restore(); }); it('should use `document` if the element is unspecified', function(done) { + var anchor = document.querySelector('a'); delegate('a', 'click', function() { done(); }); - simulant.fire(global.anchor, simulant('click')); + simulant.fire(anchor, simulant('click')); }); it('should remove an event listener the unspecified base (`document`)', function() {