mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Add helper method to dry default set functions
This commit is contained in:
@ -4,6 +4,21 @@ import Emitter from 'tiny-emitter';
|
|||||||
|
|
||||||
const prefix = 'data-clipboard-';
|
const prefix = 'data-clipboard-';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to retrieve attribute value.
|
||||||
|
* @param {String} suffix
|
||||||
|
* @param {Element} element
|
||||||
|
*/
|
||||||
|
var getAttributeValue = (suffix, element) => {
|
||||||
|
let attribute = prefix + suffix;
|
||||||
|
|
||||||
|
if (!element.hasAttribute(attribute)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return element.getAttribute(attribute);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
@ -58,40 +73,31 @@ class Clipboard extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the `action` lookup function.
|
* Default `action` lookup function.
|
||||||
* @param {Element} trigger
|
* @param {Element} trigger
|
||||||
*/
|
*/
|
||||||
setAction(trigger) {
|
setAction(trigger) {
|
||||||
if (!trigger.hasAttribute(prefix + 'action')) {
|
return getAttributeValue('action', trigger);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return trigger.getAttribute(prefix + 'action');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the `target` lookup function.
|
* Default `target` lookup function.
|
||||||
* @param {Element} trigger
|
* @param {Element} trigger
|
||||||
*/
|
*/
|
||||||
setTarget(trigger) {
|
setTarget(trigger) {
|
||||||
if (!trigger.hasAttribute(prefix + 'target')) {
|
let selector = getAttributeValue('target', trigger);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let target = trigger.getAttribute(prefix + 'target');
|
if ('string' === typeof selector) {
|
||||||
return document.querySelector(target);
|
return document.querySelector(selector);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the `text` lookup function.
|
* Defualt `text` lookup function.
|
||||||
* @param {Element} trigger
|
* @param {Element} trigger
|
||||||
*/
|
*/
|
||||||
setText(trigger) {
|
setText(trigger) {
|
||||||
if (!trigger.hasAttribute(prefix + 'text')) {
|
return getAttributeValue('text', trigger);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return trigger.getAttribute(prefix + 'text');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user