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:
@ -16,7 +16,7 @@ class ClipboardAction {
|
|||||||
this.selectedText = '';
|
this.selectedText = '';
|
||||||
|
|
||||||
if (this.text && this.target) {
|
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) {
|
else if (this.text) {
|
||||||
this.selectFake();
|
this.selectFake();
|
||||||
@ -25,7 +25,7 @@ class ClipboardAction {
|
|||||||
this.selectTarget();
|
this.selectTarget();
|
||||||
}
|
}
|
||||||
else {
|
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';
|
this._action = action || 'copy';
|
||||||
|
|
||||||
if (this._action !== 'copy' && this._action !== 'cut') {
|
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
|
* Sets the `target` property using an element that will be have its content
|
||||||
* that will be have its content copied.
|
* copied.
|
||||||
* @param {String} target
|
* @param {Element} target
|
||||||
*/
|
*/
|
||||||
set target(target) {
|
set target(target) {
|
||||||
if (target) {
|
if (target !== undefined) {
|
||||||
this._target = document.querySelector(target);
|
if (target && typeof target === 'object' && target.nodeType === 1) {
|
||||||
|
this._target = target;
|
||||||
if (!this._target) {
|
}
|
||||||
throw new Error('Invalid "data-clipboard-target" selector, use a value that matches an ID');
|
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 Delegate from 'delegate-events';
|
||||||
import Emitter from 'tiny-emitter';
|
import Emitter from 'tiny-emitter';
|
||||||
|
|
||||||
|
const prefix = 'data-clipboard-';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class which takes a selector, delegates a click event to it,
|
* Base class which takes a selector, delegates a click event to it,
|
||||||
* and instantiates a new `ClipboardAction` on each click.
|
* and instantiates a new `ClipboardAction` on each click.
|
||||||
*/
|
*/
|
||||||
class Clipboard extends Emitter {
|
class Clipboard extends Emitter {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegates a click event on the passed selector.
|
* Delegates a click event on the passed selector.
|
||||||
* @param {String} selector
|
* @param {String} selector
|
||||||
|
* @param {Object} options
|
||||||
*/
|
*/
|
||||||
constructor(selector) {
|
constructor(selector, options) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
if (!document.querySelectorAll(selector).length) {
|
if (!document.querySelectorAll(selector).length) {
|
||||||
throw new Error('No matches were found for the provided selector');
|
throw new Error('No matches were found for the provided selector');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.resolveOptions(options);
|
||||||
|
|
||||||
Delegate.bind(document.body, selector, 'click', (e) => this.initialize(e));
|
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.
|
* Defines a new `ClipboardAction` on each click event.
|
||||||
* @param {Event} e
|
* @param {Event} e
|
||||||
@ -30,16 +47,41 @@ class Clipboard extends Emitter {
|
|||||||
this.clipboardAction = null;
|
this.clipboardAction = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const prefix = 'data-clipboard-';
|
|
||||||
|
|
||||||
this.clipboardAction = new ClipboardAction({
|
this.clipboardAction = new ClipboardAction({
|
||||||
action : e.delegateTarget.getAttribute(prefix + 'action'),
|
action : this.action(e.delegateTarget),
|
||||||
target : e.delegateTarget.getAttribute(prefix + 'target'),
|
target : this.target(e.delegateTarget),
|
||||||
text : e.delegateTarget.getAttribute(prefix + 'text'),
|
text : this.text(e.delegateTarget),
|
||||||
trigger : e.delegateTarget,
|
trigger : e.delegateTarget,
|
||||||
emitter : this
|
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;
|
export default Clipboard;
|
||||||
|
@ -20,34 +20,34 @@ describe('ClipboardAction', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('#constructor', () => {
|
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 {
|
try {
|
||||||
new ClipboardAction({
|
new ClipboardAction({
|
||||||
text: 'foo',
|
text: 'foo',
|
||||||
target: '#input'
|
target: document.querySelector('#input')
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch(e) {
|
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();
|
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 {
|
try {
|
||||||
new ClipboardAction({
|
new ClipboardAction({
|
||||||
action: ''
|
action: ''
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch(e) {
|
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();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#set action', () => {
|
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 {
|
try {
|
||||||
new ClipboardAction({
|
new ClipboardAction({
|
||||||
text: 'foo',
|
text: 'foo',
|
||||||
@ -55,21 +55,21 @@ describe('ClipboardAction', () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch(e) {
|
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();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#set target', () => {
|
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 {
|
try {
|
||||||
new ClipboardAction({
|
new ClipboardAction({
|
||||||
target: '#foo'
|
target: document.querySelector('#foo')
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch(e) {
|
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();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -103,7 +103,7 @@ describe('ClipboardAction', () => {
|
|||||||
it('should select text from editable element', () => {
|
it('should select text from editable element', () => {
|
||||||
let clip = new ClipboardAction({
|
let clip = new ClipboardAction({
|
||||||
emitter: new Emitter(),
|
emitter: new Emitter(),
|
||||||
target: '#input'
|
target: document.querySelector('#input')
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(clip.selectedText, clip.target.value);
|
assert.equal(clip.selectedText, clip.target.value);
|
||||||
@ -112,7 +112,7 @@ describe('ClipboardAction', () => {
|
|||||||
it('should select text from non-editable element', () => {
|
it('should select text from non-editable element', () => {
|
||||||
let clip = new ClipboardAction({
|
let clip = new ClipboardAction({
|
||||||
emitter: new Emitter(),
|
emitter: new Emitter(),
|
||||||
target: '#paragraph'
|
target: document.querySelector('#paragraph')
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(clip.selectedText, clip.target.textContent);
|
assert.equal(clip.selectedText, clip.target.textContent);
|
||||||
@ -139,7 +139,7 @@ describe('ClipboardAction', () => {
|
|||||||
|
|
||||||
let clip = new ClipboardAction({
|
let clip = new ClipboardAction({
|
||||||
emitter: emitter,
|
emitter: emitter,
|
||||||
target: '#input'
|
target: document.querySelector('#input')
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -154,7 +154,7 @@ describe('ClipboardAction', () => {
|
|||||||
|
|
||||||
let clip = new ClipboardAction({
|
let clip = new ClipboardAction({
|
||||||
emitter: emitter,
|
emitter: emitter,
|
||||||
target: '#input'
|
target: document.querySelector('#input')
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -163,7 +163,7 @@ describe('ClipboardAction', () => {
|
|||||||
it('should fire a success event with certain properties', done => {
|
it('should fire a success event with certain properties', done => {
|
||||||
let clip = new ClipboardAction({
|
let clip = new ClipboardAction({
|
||||||
emitter: new Emitter(),
|
emitter: new Emitter(),
|
||||||
target: '#input'
|
target: document.querySelector('#input')
|
||||||
});
|
});
|
||||||
|
|
||||||
clip.emitter.on('success', (e) => {
|
clip.emitter.on('success', (e) => {
|
||||||
@ -181,7 +181,7 @@ describe('ClipboardAction', () => {
|
|||||||
it('should fire a error event with certain properties', done => {
|
it('should fire a error event with certain properties', done => {
|
||||||
let clip = new ClipboardAction({
|
let clip = new ClipboardAction({
|
||||||
emitter: new Emitter(),
|
emitter: new Emitter(),
|
||||||
target: '#input'
|
target: document.querySelector('#input')
|
||||||
});
|
});
|
||||||
|
|
||||||
clip.emitter.on('error', (e) => {
|
clip.emitter.on('error', (e) => {
|
||||||
@ -200,7 +200,7 @@ describe('ClipboardAction', () => {
|
|||||||
it('should remove focus from target and text selection', () => {
|
it('should remove focus from target and text selection', () => {
|
||||||
let clip = new ClipboardAction({
|
let clip = new ClipboardAction({
|
||||||
emitter: new Emitter(),
|
emitter: new Emitter(),
|
||||||
target: '#input'
|
target: document.querySelector('#input')
|
||||||
});
|
});
|
||||||
|
|
||||||
clip.clearSelection();
|
clip.clearSelection();
|
||||||
|
@ -2,6 +2,21 @@ import Clipboard from '../src/clipboard';
|
|||||||
import ClipboardAction from '../src/clipboard-action';
|
import ClipboardAction from '../src/clipboard-action';
|
||||||
|
|
||||||
describe('Clipboard', () => {
|
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', () => {
|
describe('#constructor', () => {
|
||||||
it('should throw an error since there was no arguments passed', done => {
|
it('should throw an error since there was no arguments passed', done => {
|
||||||
try {
|
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', () => {
|
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', () => {
|
it('should create a new instance of ClipboardAction', () => {
|
||||||
let clipboard = new Clipboard('.btn');
|
let clipboard = new Clipboard('.btn');
|
||||||
|
|
||||||
clipboard.initialize(global.event);
|
clipboard.initialize(global.event);
|
||||||
assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user