From 56dd1aac221fdcccceb64e4723fd5b0a5325cf95 Mon Sep 17 00:00:00 2001 From: Zeno Rocha Date: Thu, 24 Sep 2015 15:09:34 -0700 Subject: [PATCH] Handles attributes with getters/setters and breaks code into two classes --- dist/clipboard.min.js | 2 +- src/clipboard-action.js | 123 ++++++++++++++++++++++++++++++++++++ src/clipboard.js | 137 ++++++---------------------------------- 3 files changed, 143 insertions(+), 119 deletions(-) create mode 100644 src/clipboard-action.js diff --git a/dist/clipboard.min.js b/dist/clipboard.min.js index 296dc73..03fd08a 100644 --- a/dist/clipboard.min.js +++ b/dist/clipboard.min.js @@ -1 +1 @@ -!function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g this.bind(trigger)); } bind(trigger) { - trigger.addEventListener('click', (e) => this.validate(e)); + trigger.addEventListener('click', (e) => this.initialize(e)); } - validate(e) { - let trigger = e.currentTarget; - let action = trigger.getAttribute('data-action') || 'copy'; - let target = trigger.getAttribute('data-target'); - let text = trigger.getAttribute('data-text'); - - if (action !== 'copy' && action !== 'cut') { - throw new Error('Invalid "data-action" value, use either "copy" or "cut"'); - } - - if (!target && !text) { - throw new Error('Missing required attributes, use either "data-target" or "data-text"'); - } - - if (target) { - target = document.getElementById(target); - if (!target) throw new Error('Invalid "data-target" selector, use a value that matches an ID'); - } - - new ClipboardAction(action, target, text, trigger); - } -} - -class ClipboardAction { - constructor(action, target, text, trigger) { - this.action = action; - this.target = target; - this.text = text; - this.trigger = trigger; - - this.selectedText = ''; - - if (this.text) { - this.selectValue(); - } - else if (this.target) { - this.selectTarget(); - } - } - - selectValue() { - let fake = document.createElement('input'); - - fake.style.position = 'absolute'; - fake.style.left = '-9999px'; - fake.value = this.text; - this.selectedText = this.text; - - document.body.appendChild(fake); - - fake.select(); - this.copyText(); - - document.body.removeChild(fake); - } - - selectTarget() { - if (this.target.nodeName === 'INPUT' || this.target.nodeName === 'TEXTAREA') { - this.target.select(); - this.selectedText = this.target.value; - } - else { - let range = document.createRange(); - let selection = window.getSelection(); - - range.selectNodeContents(this.target); - selection.addRange(range); - this.selectedText = selection.toString(); - } - - this.copyText(); - } - - copyText() { - let succeeded; - - try { - succeeded = document.execCommand(this.action); - } - catch (err) { - succeeded = false; - } - - this.handleResult(succeeded); - } - - handleResult(succeeded) { - if (succeeded) { - this.fireEvent('success', { - action: this.action, - text: this.selectedText - }); - - this.clearSelection(); - } - else { - this.fireEvent('error', `Cannot execute ${this.action} operation`); - } - } - - clearSelection() { - if (this.target) { - this.target.blur(); - } - - window.getSelection().removeAllRanges(); - } - - fireEvent(type, detail) { - let event = new CustomEvent(type, { - detail: detail + initialize(e) { + new ClipboardAction({ + action : e.currentTarget.getAttribute('data-action'), + target : e.currentTarget.getAttribute('data-target'), + text : e.currentTarget.getAttribute('data-text'), + trigger : e.currentTarget }); + } - this.trigger.dispatchEvent(event); + set triggers(triggers) { + if (!triggers.length) { + throw new Error('No matches were found for the provided selector'); + } + + this._triggers = triggers; + } + + get triggers() { + return this._triggers; } }