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 = '';
|
||||
|
||||
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;
|
||||
|
Reference in New Issue
Block a user