mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Removes code from #constructor to separate functions
This commit is contained in:
parent
3394f59691
commit
a5e29bd420
@ -1,12 +1,21 @@
|
||||
/**
|
||||
* Inner class which performs selection and copy operations.
|
||||
* Inner class which performs selection from either `text` or `target`
|
||||
* properties and then executes copy or cut operations.
|
||||
*/
|
||||
class ClipboardAction {
|
||||
/**
|
||||
* Initializes selection from either `text` or `target` property.
|
||||
* @param {Object} options
|
||||
*/
|
||||
constructor(options = {}) {
|
||||
constructor(options) {
|
||||
this.resolveOptions(options);
|
||||
this.initSelection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines base properties passed from constructor.
|
||||
* @param {Object} options
|
||||
*/
|
||||
resolveOptions(options = {}) {
|
||||
this.action = options.action;
|
||||
this.emitter = options.emitter;
|
||||
this.target = options.target;
|
||||
@ -14,7 +23,13 @@ class ClipboardAction {
|
||||
this.trigger = options.trigger;
|
||||
|
||||
this.selectedText = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Decides which selection strategy is going to be applied based
|
||||
* on the existence of `text` and `target` properties.
|
||||
*/
|
||||
initSelection() {
|
||||
if (this.text && this.target) {
|
||||
throw new Error('Multiple attributes declared, use either "target" or "text"');
|
||||
}
|
||||
@ -157,8 +172,8 @@ class ClipboardAction {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `target` property using an element that will be have its content
|
||||
* copied.
|
||||
* Sets the `target` property using an element
|
||||
* that will be have its content copied.
|
||||
* @param {Element} target
|
||||
*/
|
||||
set target(target) {
|
||||
|
@ -10,7 +10,6 @@ const prefix = 'data-clipboard-';
|
||||
*/
|
||||
class Clipboard extends Emitter {
|
||||
/**
|
||||
* Delegates a click event on the passed selector.
|
||||
* @param {String} selector
|
||||
* @param {Object} options
|
||||
*/
|
||||
@ -18,13 +17,12 @@ class Clipboard extends Emitter {
|
||||
super();
|
||||
|
||||
this.resolveOptions(options);
|
||||
|
||||
Delegate.bind(document.body, selector, 'click', (e) => this.initialize(e));
|
||||
this.delegateClick(selector);
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines if attributes would be resolved using an internal setter function
|
||||
* or a custom function that was passed in the constructor.
|
||||
* Defines if attributes would be resolved using internal setter functions
|
||||
* or custom functions that were passed in the constructor.
|
||||
* @param {Object} options
|
||||
*/
|
||||
resolveOptions(options = {}) {
|
||||
@ -33,6 +31,32 @@ class Clipboard extends Emitter {
|
||||
this.text = (typeof options.text === 'function') ? options.text : this.setText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates a click event on the passed selector.
|
||||
* @param {String} selector
|
||||
*/
|
||||
delegateClick(selector) {
|
||||
Delegate.bind(document.body, selector, 'click', (e) => this.initialize(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a new `ClipboardAction` on each click event.
|
||||
* @param {Event} e
|
||||
*/
|
||||
initialize(e) {
|
||||
if (this.clipboardAction) {
|
||||
this.clipboardAction = null;
|
||||
}
|
||||
|
||||
this.clipboardAction = new ClipboardAction({
|
||||
action : this.action(e.delegateTarget),
|
||||
target : this.target(e.delegateTarget),
|
||||
text : this.text(e.delegateTarget),
|
||||
trigger : e.delegateTarget,
|
||||
emitter : this
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `action` lookup function.
|
||||
* @param {Element} trigger
|
||||
@ -69,24 +93,6 @@ class Clipboard extends Emitter {
|
||||
|
||||
return trigger.getAttribute(prefix + 'text');
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a new `ClipboardAction` on each click event.
|
||||
* @param {Event} e
|
||||
*/
|
||||
initialize(e) {
|
||||
if (this.clipboardAction) {
|
||||
this.clipboardAction = null;
|
||||
}
|
||||
|
||||
this.clipboardAction = new ClipboardAction({
|
||||
action : this.action(e.delegateTarget),
|
||||
target : this.target(e.delegateTarget),
|
||||
text : this.text(e.delegateTarget),
|
||||
trigger : e.delegateTarget,
|
||||
emitter : this
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default Clipboard;
|
||||
|
@ -19,7 +19,23 @@ describe('ClipboardAction', () => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
describe('#constructor', () => {
|
||||
describe('#resolveOptions', () => {
|
||||
it('should set base properties', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
text: 'foo'
|
||||
});
|
||||
|
||||
assert.property(clip, 'action');
|
||||
assert.property(clip, 'emitter');
|
||||
assert.property(clip, 'target');
|
||||
assert.property(clip, 'text');
|
||||
assert.property(clip, 'trigger');
|
||||
assert.property(clip, 'selectedText');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#initSelection', () => {
|
||||
it('should throw an error since both "text" and "target" were passed', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
|
@ -1,5 +1,6 @@
|
||||
import Clipboard from '../src/clipboard';
|
||||
import ClipboardAction from '../src/clipboard-action';
|
||||
import Delegate from 'delegate-events';
|
||||
|
||||
describe('Clipboard', () => {
|
||||
before(() => {
|
||||
@ -18,28 +19,53 @@ describe('Clipboard', () => {
|
||||
});
|
||||
|
||||
describe('#resolveOptions', function() {
|
||||
before(() => {
|
||||
global.fn = function() {};
|
||||
});
|
||||
|
||||
it('should set action as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
action: fn
|
||||
let clipboard = new Clipboard('.btn', {
|
||||
action: global.fn
|
||||
});
|
||||
assert.equal(fn, clipboard.action);
|
||||
|
||||
assert.equal(global.fn, clipboard.action);
|
||||
});
|
||||
|
||||
it('should set target as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
target: fn
|
||||
let clipboard = new Clipboard('.btn', {
|
||||
target: global.fn
|
||||
});
|
||||
assert.equal(fn, clipboard.target);
|
||||
|
||||
assert.equal(global.fn, clipboard.target);
|
||||
});
|
||||
|
||||
it('should set text as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
text: fn
|
||||
let clipboard = new Clipboard('.btn', {
|
||||
text: global.fn
|
||||
});
|
||||
assert.equal(fn, clipboard.text);
|
||||
|
||||
assert.equal(global.fn, clipboard.text);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#delegateClick', function() {
|
||||
before(() => {
|
||||
global.spy = sinon.spy(Delegate, 'bind');
|
||||
});
|
||||
|
||||
after(() => {
|
||||
global.spy.restore();
|
||||
});
|
||||
|
||||
it('should delegate a click event to the passed selector', () => {
|
||||
let element = document.body;
|
||||
let selector = '.btn'
|
||||
let event = 'click';
|
||||
|
||||
let clipboard = new Clipboard(selector);
|
||||
|
||||
assert.ok(global.spy.calledOnce);
|
||||
assert.ok(global.spy.calledWith(element, selector, event));
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user