mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Source formatting and testing for #24
This commit is contained in:
@ -34,7 +34,15 @@ class Clipboard extends Emitter {
|
|||||||
* @param {String} selector
|
* @param {String} selector
|
||||||
*/
|
*/
|
||||||
delegateClick(selector) {
|
delegateClick(selector) {
|
||||||
Delegate.bind(document.body, selector, 'click', (e) => this.onClick(e));
|
this.binding = Delegate.bind(document.body, selector, 'click', (e) => this.onClick(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undelegates a click event on body.
|
||||||
|
* @param {String} selector
|
||||||
|
*/
|
||||||
|
undelegateClick() {
|
||||||
|
Delegate.unbind(document.body, 'click', this.binding);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -82,6 +90,18 @@ class Clipboard extends Emitter {
|
|||||||
defaultText(trigger) {
|
defaultText(trigger) {
|
||||||
return getAttributeValue('text', trigger);
|
return getAttributeValue('text', trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy lifecycle.
|
||||||
|
*/
|
||||||
|
destroy() {
|
||||||
|
this.undelegateClick();
|
||||||
|
|
||||||
|
if (this.clipboardAction) {
|
||||||
|
this.clipboardAction.destroy();
|
||||||
|
this.clipboardAction = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -69,6 +69,27 @@ describe('Clipboard', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#undelegateClick', function() {
|
||||||
|
before(() => {
|
||||||
|
global.spy = sinon.spy(Delegate, 'unbind');
|
||||||
|
});
|
||||||
|
|
||||||
|
after(() => {
|
||||||
|
global.spy.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should undelegate a click event', () => {
|
||||||
|
let element = document.body;
|
||||||
|
let event = 'click';
|
||||||
|
|
||||||
|
let clipboard = new Clipboard('.btn');
|
||||||
|
clipboard.undelegateClick();
|
||||||
|
|
||||||
|
assert.ok(global.spy.calledOnce);
|
||||||
|
assert.ok(global.spy.calledWith(element, event));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#onClick', () => {
|
describe('#onClick', () => {
|
||||||
it('should create a new instance of ClipboardAction', () => {
|
it('should create a new instance of ClipboardAction', () => {
|
||||||
let clipboard = new Clipboard('.btn');
|
let clipboard = new Clipboard('.btn');
|
||||||
@ -92,4 +113,15 @@ describe('Clipboard', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('#destroy', () => {
|
||||||
|
it('should destroy an existing instance of ClipboardAction', () => {
|
||||||
|
let clipboard = new Clipboard('.btn');
|
||||||
|
|
||||||
|
clipboard.onClick(global.event);
|
||||||
|
clipboard.destroy();
|
||||||
|
|
||||||
|
assert.equal(clipboard.clipboardAction, null);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user