Comparing version 0.0.1 to 0.0.2
12
index.js
@@ -30,2 +30,10 @@ var flatten = require('flatten-list'); | ||
function trigger(nodes, event, detail) { | ||
var e = document.createEvent('CustomEvent'); | ||
e.initCustomEvent(event, true, true, detail); | ||
nodes.forEach(function(node) { | ||
node.dispatchEvent(e); | ||
}); | ||
} | ||
function Events(nodes) { | ||
@@ -43,2 +51,6 @@ this.nodes = nodes; | ||
Events.prototype.trigger = function(event, detail) { | ||
return trigger(this.nodes, event, detail); | ||
}; | ||
function events() { | ||
@@ -45,0 +57,0 @@ return new Events(flatten(arguments)); |
{ | ||
"name": "chi-events", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Easily manage DOM events", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,2 +30,6 @@ # chi-events | ||
// Triggering an event | ||
// Triggers the 'click' event with event.detail == 'test' | ||
events(button).trigger('click', 'test'); | ||
// Use multiple DOM nodes | ||
@@ -54,2 +58,8 @@ events(document.querySelectorAll('a')).on('click', function(e) { | ||
called one time at most. The first time the event is triggered, the listener | ||
will be removed. Return value is the same as `#on`. | ||
will be removed. Return value is the same as `#on`. | ||
### `trigger(event, [detail])` | ||
Creates an event with the event name `event` and dispatches the event to all the | ||
DOM nodes. If `detail` is provided, the `detail` property of the event is set to | ||
this value. |
67
test.js
var test = require('tape'), | ||
events = require('./'); | ||
function trigger(el) { | ||
function triggerTest(el) { | ||
var ev = document.createEvent('Event'); | ||
@@ -21,3 +21,3 @@ ev.initEvent('test', true, true); | ||
trigger(el); | ||
triggerTest(el); | ||
t.ok(called, 'handler should be called'); | ||
@@ -33,3 +33,3 @@ t.end(); | ||
.remove(); | ||
trigger(el); | ||
triggerTest(el); | ||
@@ -46,5 +46,5 @@ t.notOk(called, 'handler should not be called after it is removed'); | ||
events(a, [b, c]).on('test', function() { called++; }); | ||
trigger(a); | ||
trigger(b); | ||
trigger(c); | ||
triggerTest(a); | ||
triggerTest(b); | ||
triggerTest(c); | ||
@@ -68,4 +68,4 @@ t.equal(called, 3); | ||
}); | ||
trigger(el); | ||
trigger(el); | ||
triggerTest(el); | ||
triggerTest(el); | ||
@@ -82,3 +82,3 @@ t.equal(called, 1); | ||
.remove(); | ||
trigger(el); | ||
triggerTest(el); | ||
@@ -94,4 +94,4 @@ t.notOk(called, 'handler should not be called'); | ||
events(a, b).once('test', function() { called++; }); | ||
trigger(b); | ||
trigger(a); | ||
triggerTest(b); | ||
triggerTest(a); | ||
@@ -103,2 +103,47 @@ t.equal(called, 1); | ||
t.end(); | ||
}); | ||
test('trigger', function(t) { | ||
t.test('trigger DOM event', function(t) { | ||
var el = document.createElement('div'), | ||
called = false; | ||
el.addEventListener('click', function() { called = true; }, false); | ||
events(el).trigger('click'); | ||
t.ok(called, 'event should be triggered'); | ||
t.end(); | ||
}); | ||
t.test('set detail value on event', function(t) { | ||
var el = document.createElement('div'), | ||
detail = {}, | ||
called = false; | ||
el.addEventListener('click', function(e) { | ||
t.equal(detail, e.detail); | ||
called = true; | ||
}); | ||
events(el).trigger('click', detail); | ||
t.ok(called, 'event should be triggered'); | ||
t.end(); | ||
}); | ||
t.test('multiple nodes', function(t) { | ||
var a = document.createElement('div'), | ||
b = document.createElement('div'), | ||
results = []; | ||
function handle() { | ||
results.push(this); | ||
} | ||
a.addEventListener('click', handle, false); | ||
b.addEventListener('click', handle, false); | ||
events(a, b).trigger('click'); | ||
t.deepEqual(results, [a, b]); | ||
t.end(); | ||
}); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9029
159
63