mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Replaces every single event listener in favor of event delegation
This commit is contained in:
2
dist/clipboard.min.js
vendored
2
dist/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
@ -9,7 +9,8 @@
|
|||||||
"cut"
|
"cut"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"custom-event": "^1.0.0"
|
"custom-event": "^1.0.0",
|
||||||
|
"delegate-events": "^1.1.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babelify": "^6.3.0",
|
"babelify": "^6.3.0",
|
||||||
|
@ -1,26 +1,20 @@
|
|||||||
var ClipboardAction = require('./clipboard-action');
|
var ClipboardAction = require('./clipboard-action');
|
||||||
|
var Delegate = require('dom-delegate').Delegate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class which takes a selector, binds a click event for
|
* Base class which takes a selector, delegates a click event to it,
|
||||||
* each element found, and instantiates a new `ClipboardAction`.
|
* and instantiates a new `ClipboardAction` on each click.
|
||||||
*/
|
*/
|
||||||
class Clipboard {
|
class Clipboard {
|
||||||
/**
|
/**
|
||||||
* Fetches all elements from a selector and
|
* Delegates a click event to the passed selector.
|
||||||
* calls `bind` method for each element found.
|
* @param {String} selector
|
||||||
* @param {String} triggers
|
|
||||||
*/
|
*/
|
||||||
constructor(triggers) {
|
constructor(selector) {
|
||||||
this.triggers = document.querySelectorAll(triggers);
|
this.selector = selector;
|
||||||
|
|
||||||
[].forEach.call(this.triggers, (trigger) => this.bind(trigger));
|
let delegate = new Delegate(document.body);
|
||||||
}
|
delegate.on('click', this.selector, (e) => this.initialize(e));
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a click event listener for each element.
|
|
||||||
*/
|
|
||||||
bind(trigger) {
|
|
||||||
trigger.addEventListener('click', (e) => this.initialize(e));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,33 +23,12 @@ class Clipboard {
|
|||||||
*/
|
*/
|
||||||
initialize(e) {
|
initialize(e) {
|
||||||
new ClipboardAction({
|
new ClipboardAction({
|
||||||
action : e.currentTarget.getAttribute('data-action'),
|
action : e.target.getAttribute('data-action'),
|
||||||
target : e.currentTarget.getAttribute('data-target'),
|
target : e.target.getAttribute('data-target'),
|
||||||
text : e.currentTarget.getAttribute('data-text'),
|
text : e.target.getAttribute('data-text'),
|
||||||
trigger : e.currentTarget
|
trigger : e.target
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the `triggers` property if there's at least
|
|
||||||
* one match for the provided selector.
|
|
||||||
* @param {NodeList} triggers
|
|
||||||
*/
|
|
||||||
set triggers(triggers) {
|
|
||||||
if (!triggers.length) {
|
|
||||||
throw new Error('No matches were found for the provided selector');
|
|
||||||
}
|
|
||||||
|
|
||||||
this._triggers = triggers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the `triggers` property.
|
|
||||||
* @return {NodeList}
|
|
||||||
*/
|
|
||||||
get triggers() {
|
|
||||||
return this._triggers;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Clipboard;
|
export default Clipboard;
|
||||||
|
@ -25,11 +25,6 @@ describe('Clipboard', () => {
|
|||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create a NodeList from selector and store it in a "triggers" property', () => {
|
|
||||||
let clipboard = new Clipboard('.btn');
|
|
||||||
return assert.instanceOf(clipboard.triggers, NodeList);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
after(() => {
|
after(() => {
|
||||||
|
Reference in New Issue
Block a user