mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Adds support to set action/target/text via function
This commit is contained in:
parent
beab7bc087
commit
b842987292
@ -16,7 +16,7 @@ class ClipboardAction {
|
||||
this.selectedText = '';
|
||||
|
||||
if (this.text && this.target) {
|
||||
throw new Error('Multiple attributes declared, use either "data-clipboard-target" or "data-clipboard-text"');
|
||||
throw new Error('Multiple attributes declared, use either "target" or "text"');
|
||||
}
|
||||
else if (this.text) {
|
||||
this.selectFake();
|
||||
@ -25,7 +25,7 @@ class ClipboardAction {
|
||||
this.selectTarget();
|
||||
}
|
||||
else {
|
||||
throw new Error('Missing required attributes, use either "data-clipboard-target" or "data-clipboard-text"');
|
||||
throw new Error('Missing required attributes, use either "target" or "text"');
|
||||
}
|
||||
}
|
||||
|
||||
@ -144,7 +144,7 @@ class ClipboardAction {
|
||||
this._action = action || 'copy';
|
||||
|
||||
if (this._action !== 'copy' && this._action !== 'cut') {
|
||||
throw new Error('Invalid "data-clipboard-action" value, use either "copy" or "cut"');
|
||||
throw new Error('Invalid "action" value, use either "copy" or "cut"');
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,16 +157,17 @@ class ClipboardAction {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `target` property using the selector of an element
|
||||
* that will be have its content copied.
|
||||
* @param {String} target
|
||||
* Sets the `target` property using an element that will be have its content
|
||||
* copied.
|
||||
* @param {Element} target
|
||||
*/
|
||||
set target(target) {
|
||||
if (target) {
|
||||
this._target = document.querySelector(target);
|
||||
|
||||
if (!this._target) {
|
||||
throw new Error('Invalid "data-clipboard-target" selector, use a value that matches an ID');
|
||||
if (target !== undefined) {
|
||||
if (target && typeof target === 'object' && target.nodeType === 1) {
|
||||
this._target = target;
|
||||
}
|
||||
else {
|
||||
throw new Error('Invalid "target" value, use a valid Element');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,25 +2,42 @@ import ClipboardAction from './clipboard-action';
|
||||
import Delegate from 'delegate-events';
|
||||
import Emitter from 'tiny-emitter';
|
||||
|
||||
const prefix = 'data-clipboard-';
|
||||
|
||||
/**
|
||||
* Base class which takes a selector, delegates a click event to it,
|
||||
* and instantiates a new `ClipboardAction` on each click.
|
||||
*/
|
||||
class Clipboard extends Emitter {
|
||||
|
||||
/**
|
||||
* Delegates a click event on the passed selector.
|
||||
* @param {String} selector
|
||||
* @param {Object} options
|
||||
*/
|
||||
constructor(selector) {
|
||||
constructor(selector, options) {
|
||||
super();
|
||||
|
||||
if (!document.querySelectorAll(selector).length) {
|
||||
throw new Error('No matches were found for the provided selector');
|
||||
}
|
||||
|
||||
this.resolveOptions(options);
|
||||
|
||||
Delegate.bind(document.body, selector, 'click', (e) => this.initialize(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves contructor options.
|
||||
* @param {Object} options
|
||||
*/
|
||||
resolveOptions(options) {
|
||||
options = options || {};
|
||||
this.action = (typeof options.action === 'function') ? options.action : this.setAction;
|
||||
this.target = (typeof options.target === 'function') ? options.target : this.setTarget;
|
||||
this.text = (typeof options.text === 'function') ? options.text : this.setText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines a new `ClipboardAction` on each click event.
|
||||
* @param {Event} e
|
||||
@ -30,16 +47,41 @@ class Clipboard extends Emitter {
|
||||
this.clipboardAction = null;
|
||||
}
|
||||
|
||||
const prefix = 'data-clipboard-';
|
||||
|
||||
this.clipboardAction = new ClipboardAction({
|
||||
action : e.delegateTarget.getAttribute(prefix + 'action'),
|
||||
target : e.delegateTarget.getAttribute(prefix + 'target'),
|
||||
text : e.delegateTarget.getAttribute(prefix + 'text'),
|
||||
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
|
||||
*/
|
||||
setAction(trigger) {
|
||||
return trigger.getAttribute(prefix + 'action');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `target` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
setTarget(trigger) {
|
||||
let target = trigger.getAttribute(prefix + 'target');
|
||||
if (target) {
|
||||
return document.querySelector(target);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `text` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
setText(trigger) {
|
||||
return trigger.getAttribute(prefix + 'text');
|
||||
}
|
||||
}
|
||||
|
||||
export default Clipboard;
|
||||
|
@ -20,34 +20,34 @@ describe('ClipboardAction', () => {
|
||||
});
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error since both "data-clipboard-text" and "data-clipboard-target" were passed', done => {
|
||||
it('should throw an error since both "text" and "target" were passed', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
text: 'foo',
|
||||
target: '#input'
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Multiple attributes declared, use either "data-clipboard-target" or "data-clipboard-text"');
|
||||
assert.equal(e.message, 'Multiple attributes declared, use either "target" or "text"');
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error since neither "data-clipboard-text" nor "data-clipboard-target" were passed', done => {
|
||||
it('should throw an error since neither "text" nor "target" were passed', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
action: ''
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Missing required attributes, use either "data-clipboard-target" or "data-clipboard-text"');
|
||||
assert.equal(e.message, 'Missing required attributes, use either "target" or "text"');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#set action', () => {
|
||||
it('should throw an error since "data-clipboard-action" is invalid', done => {
|
||||
it('should throw an error since "action" is invalid', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
text: 'foo',
|
||||
@ -55,21 +55,21 @@ describe('ClipboardAction', () => {
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Invalid "data-clipboard-action" value, use either "copy" or "cut"');
|
||||
assert.equal(e.message, 'Invalid "action" value, use either "copy" or "cut"');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#set target', () => {
|
||||
it('should throw an error since "data-clipboard-target" do not match any element', done => {
|
||||
it('should throw an error since "target" do not match any element', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
target: '#foo'
|
||||
target: document.querySelector('#foo')
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Invalid "data-clipboard-target" selector, use a value that matches an ID');
|
||||
assert.equal(e.message, 'Invalid "target" value, use a valid Element');
|
||||
done();
|
||||
}
|
||||
});
|
||||
@ -103,7 +103,7 @@ describe('ClipboardAction', () => {
|
||||
it('should select text from editable element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: '#input'
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.target.value);
|
||||
@ -112,7 +112,7 @@ describe('ClipboardAction', () => {
|
||||
it('should select text from non-editable element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: '#paragraph'
|
||||
target: document.querySelector('#paragraph')
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.target.textContent);
|
||||
@ -139,7 +139,7 @@ describe('ClipboardAction', () => {
|
||||
|
||||
let clip = new ClipboardAction({
|
||||
emitter: emitter,
|
||||
target: '#input'
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
});
|
||||
|
||||
@ -154,7 +154,7 @@ describe('ClipboardAction', () => {
|
||||
|
||||
let clip = new ClipboardAction({
|
||||
emitter: emitter,
|
||||
target: '#input'
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -163,7 +163,7 @@ describe('ClipboardAction', () => {
|
||||
it('should fire a success event with certain properties', done => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: '#input'
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
clip.emitter.on('success', (e) => {
|
||||
@ -181,7 +181,7 @@ describe('ClipboardAction', () => {
|
||||
it('should fire a error event with certain properties', done => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: '#input'
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
clip.emitter.on('error', (e) => {
|
||||
@ -200,7 +200,7 @@ describe('ClipboardAction', () => {
|
||||
it('should remove focus from target and text selection', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: '#input'
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
clip.clearSelection();
|
||||
|
@ -2,6 +2,21 @@ import Clipboard from '../src/clipboard';
|
||||
import ClipboardAction from '../src/clipboard-action';
|
||||
|
||||
describe('Clipboard', () => {
|
||||
before(() => {
|
||||
global.button = document.createElement('button');
|
||||
global.button.setAttribute('class', 'btn');
|
||||
global.button.setAttribute('data-clipboard-text', 'foo');
|
||||
document.body.appendChild(global.button);
|
||||
|
||||
global.event = {
|
||||
delegateTarget: global.button
|
||||
};
|
||||
});
|
||||
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error since there was no arguments passed', done => {
|
||||
try {
|
||||
@ -24,27 +39,53 @@ describe('Clipboard', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#resolveOptions', function() {
|
||||
it('should set action as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
action: fn
|
||||
});
|
||||
assert.equal(fn, clipboard.action);
|
||||
});
|
||||
|
||||
it('should set target as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
target: fn
|
||||
});
|
||||
assert.equal(fn, clipboard.target);
|
||||
});
|
||||
|
||||
it('should set text as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
text: fn
|
||||
});
|
||||
assert.equal(fn, clipboard.text);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#initialize', () => {
|
||||
before(() => {
|
||||
global.button = document.createElement('button');
|
||||
global.button.setAttribute('class', 'btn');
|
||||
global.button.setAttribute('data-clipboard-text', 'foo');
|
||||
document.body.appendChild(global.button);
|
||||
|
||||
global.event = {
|
||||
delegateTarget: global.button
|
||||
};
|
||||
});
|
||||
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
it('should create a new instance of ClipboardAction', () => {
|
||||
let clipboard = new Clipboard('.btn');
|
||||
|
||||
clipboard.initialize(global.event);
|
||||
assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
|
||||
});
|
||||
|
||||
it('should throws exception target', done => {
|
||||
try {
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
target: function() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
clipboard.initialize(global.event);
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Invalid "target" value, use a valid Element');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user