From cead303e53297817ddd36b6cdc4c3f12cd03be06 Mon Sep 17 00:00:00 2001 From: Vitor Cortez Date: Fri, 2 Oct 2015 19:56:10 -0300 Subject: [PATCH 1/3] Add helper method to dry default set functions --- src/clipboard.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/clipboard.js b/src/clipboard.js index 5f2bcd0..ca35011 100644 --- a/src/clipboard.js +++ b/src/clipboard.js @@ -4,6 +4,21 @@ import Emitter from 'tiny-emitter'; 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, * 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 */ setAction(trigger) { - if (!trigger.hasAttribute(prefix + 'action')) { - return; - } - - return trigger.getAttribute(prefix + 'action'); + return getAttributeValue('action', trigger); } /** - * Sets the `target` lookup function. + * Default `target` lookup function. * @param {Element} trigger */ setTarget(trigger) { - if (!trigger.hasAttribute(prefix + 'target')) { - return; - } + let selector = getAttributeValue('target', trigger); - let target = trigger.getAttribute(prefix + 'target'); - return document.querySelector(target); + if ('string' === typeof selector) { + return document.querySelector(selector); + } } /** - * Sets the `text` lookup function. + * Defualt `text` lookup function. * @param {Element} trigger */ setText(trigger) { - if (!trigger.hasAttribute(prefix + 'text')) { - return; - } - - return trigger.getAttribute(prefix + 'text'); + return getAttributeValue('text', trigger); } } From c66615027896bbeabb1edb3fb944efad531ce90c Mon Sep 17 00:00:00 2001 From: Vitor Cortez Date: Fri, 2 Oct 2015 20:46:02 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Edit=20=C2=B4getAttributeValue=C2=B4=20to?= =?UTF-8?q?=20a=20function=20declaration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Favoring function declaration over a function expression in this case to follow style guidelines. --- src/clipboard.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/clipboard.js b/src/clipboard.js index ca35011..f599e0e 100644 --- a/src/clipboard.js +++ b/src/clipboard.js @@ -9,7 +9,7 @@ const prefix = 'data-clipboard-'; * @param {String} suffix * @param {Element} element */ -var getAttributeValue = (suffix, element) => { +function getAttributeValue(suffix, element) { let attribute = prefix + suffix; if (!element.hasAttribute(attribute)) { @@ -17,7 +17,7 @@ var getAttributeValue = (suffix, element) => { } return element.getAttribute(attribute); -}; +} /** * Base class which takes a selector, delegates a click event to it, From 0b9a0402b9530cceac4d56b4d8e0e3f1af84156b Mon Sep 17 00:00:00 2001 From: Zeno Rocha Date: Sat, 3 Oct 2015 16:25:06 -0700 Subject: [PATCH 3/3] Source formatting #63 --- src/clipboard.js | 49 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/clipboard.js b/src/clipboard.js index f599e0e..c28d631 100644 --- a/src/clipboard.js +++ b/src/clipboard.js @@ -2,23 +2,6 @@ import ClipboardAction from './clipboard-action'; import Delegate from 'delegate-events'; import Emitter from 'tiny-emitter'; -const prefix = 'data-clipboard-'; - -/** - * Helper function to retrieve attribute value. - * @param {String} suffix - * @param {Element} element - */ -function 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, * and instantiates a new `ClipboardAction` on each click. @@ -41,9 +24,9 @@ class Clipboard extends Emitter { * @param {Object} options */ resolveOptions(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; + this.action = (typeof options.action === 'function') ? options.action : this.defaultAction; + this.target = (typeof options.target === 'function') ? options.target : this.defaultTarget; + this.text = (typeof options.text === 'function') ? options.text : this.defaultText; } /** @@ -76,7 +59,7 @@ class Clipboard extends Emitter { * Default `action` lookup function. * @param {Element} trigger */ - setAction(trigger) { + defaultAction(trigger) { return getAttributeValue('action', trigger); } @@ -84,21 +67,37 @@ class Clipboard extends Emitter { * Default `target` lookup function. * @param {Element} trigger */ - setTarget(trigger) { + defaultTarget(trigger) { let selector = getAttributeValue('target', trigger); - if ('string' === typeof selector) { + if (selector) { return document.querySelector(selector); } } /** - * Defualt `text` lookup function. + * Default `text` lookup function. * @param {Element} trigger */ - setText(trigger) { + defaultText(trigger) { return getAttributeValue('text', trigger); } } + +/** + * Helper function to retrieve attribute value. + * @param {String} suffix + * @param {Element} element + */ +function getAttributeValue(suffix, element) { + let attribute = `data-clipboard-${suffix}`; + + if (!element.hasAttribute(attribute)) { + return; + } + + return element.getAttribute(attribute); +} + export default Clipboard;