Improves error handling

This commit is contained in:
Zeno Rocha 2015-09-20 13:47:35 -07:00
parent ec20389775
commit 52b444609e
2 changed files with 29 additions and 29 deletions

View File

@ -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<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){(function(a){function c(){try{var a=new d("cat",{detail:{foo:"bar"}});return"cat"===a.type&&"bar"===a.detail.foo}catch(b){}return!1}var d=a.CustomEvent;b.exports=c()?d:"function"==typeof document.createEvent?function(a,b){var c=document.createEvent("CustomEvent");return b?c.initCustomEvent(a,b.bubbles,b.cancelable,b.detail):c.initCustomEvent(a,!1,!1,void 0),c}:function(a,b){var c=document.createEventObject();return c.type=a,b?(c.bubbles=Boolean(b.bubbles),c.cancelable=Boolean(b.cancelable),c.detail=b.detail):(c.bubbles=!1,c.cancelable=!1,c.detail=void 0),c}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}],2:[function(a,b,c){(function(b){"use strict";function c(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var d=a("custom-event"),e=function(){function a(b){c(this,a),this.triggers=document.querySelectorAll(b),this.init()}return a.prototype.init=function(){var a=this;if(0===this.triggers.length)throw new Error("The provided selector is empty");[].forEach.call(this.triggers,function(b){return a.bind(b)})},a.prototype.bind=function(a){a.addEventListener("click",function(a){var b=a.currentTarget.getAttribute("data-action"),c=a.currentTarget.getAttribute("data-target"),d=a.currentTarget.getAttribute("data-text");if("copy"===!b||"cut"===!b)throw new Error('Invalid "data-action" attribute');if(!c&&!d)throw new Error('Missing "data-target" or "data-text" attribute');new f({action:b,target:c,text:d,trigger:a.currentTarget})})},a}(),f=function(){function a(b){c(this,a),this.action=b.action||"copy",this.target=document.getElementById(b.target),this.text=b.text,this.trigger=b.trigger,this.selectedText="",this.text?this.selectValue():this.target&&this.selectTarget()}return a.prototype.selectValue=function(){var a=document.createElement("input");a.style.opacity=0,a.style.zIndex=-1,a.value=this.text,this.selectedText=this.text,document.body.appendChild(a),a.select(),this.copy(),document.body.removeChild(a)},a.prototype.selectTarget=function(){if("INPUT"===this.target.nodeName||"TEXTAREA"===this.target.nodeName)this.target.select(),this.selectedText=this.target.value;else{var a=document.createRange(),b=window.getSelection();a.selectNode(this.target),b.addRange(a),this.selectedText=b.toString()}this.copy()},a.prototype.copy=function(){var a=document.queryCommandSupported(this.action);try{document.execCommand(this.action),this.fireEventDetails(),window.getSelection().removeAllRanges()}catch(b){a=!1}a||this.fireNoSupport()},a.prototype.fireEventDetails=function(){var a=new d(this.action,{detail:this.selectedText});this.trigger.dispatchEvent(a)},a.prototype.fireNoSupport=function(){var a=new d("no-support");this.trigger.dispatchEvent(a)},a}();b.Clipboard=e}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"custom-event":1}]},{},[2]);
!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<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){(function(a){function c(){try{var a=new d("cat",{detail:{foo:"bar"}});return"cat"===a.type&&"bar"===a.detail.foo}catch(b){}return!1}var d=a.CustomEvent;b.exports=c()?d:"function"==typeof document.createEvent?function(a,b){var c=document.createEvent("CustomEvent");return b?c.initCustomEvent(a,b.bubbles,b.cancelable,b.detail):c.initCustomEvent(a,!1,!1,void 0),c}:function(a,b){var c=document.createEventObject();return c.type=a,b?(c.bubbles=Boolean(b.bubbles),c.cancelable=Boolean(b.cancelable),c.detail=b.detail):(c.bubbles=!1,c.cancelable=!1,c.detail=void 0),c}}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}],2:[function(a,b,c){(function(b){"use strict";function c(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}var d=a("custom-event"),e=function(){function a(b){var d=this;if(c(this,a),this.triggers=document.querySelectorAll(b),!this.triggers.length)throw new Error("No matches were found for the provided selector");[].forEach.call(this.triggers,function(a){return d.bind(a)})}return a.prototype.bind=function(a){var b=this;a.addEventListener("click",function(a){return b.validate(a)})},a.prototype.validate=function(a){var b=a.currentTarget,c=b.getAttribute("data-action")||"copy",d=b.getAttribute("data-target"),e=b.getAttribute("data-text");if("copy"!==c&&"cut"!==c)throw new Error('Invalid "data-action" value, use either "copy" or "cut"');if(!d&&!e)throw new Error('Missing required attributes, use either "data-target" or "data-text"');if(d&&(d=document.getElementById(d),!d))throw new Error('Invalid "data-target" selector, use a value that matches an ID');new f(c,d,e,b)},a}(),f=function(){function a(b,d,e,f){c(this,a),this.action=b,this.target=d,this.text=e,this.trigger=f,this.selectedText="",this.text?this.selectValue():this.target&&this.selectTarget()}return a.prototype.selectValue=function(){var a=document.createElement("input");a.style.opacity=0,a.style.zIndex=-1,a.value=this.text,this.selectedText=this.text,document.body.appendChild(a),a.select(),this.copy(),document.body.removeChild(a)},a.prototype.selectTarget=function(){if("INPUT"===this.target.nodeName||"TEXTAREA"===this.target.nodeName)this.target.select(),this.selectedText=this.target.value;else{var a=document.createRange(),b=window.getSelection();a.selectNode(this.target),b.addRange(a),this.selectedText=b.toString()}this.copy()},a.prototype.copy=function(){var a=document.queryCommandSupported(this.action);try{document.execCommand(this.action),this.fireEventDetails(),window.getSelection().removeAllRanges()}catch(b){a=!1}a||this.fireNoSupport()},a.prototype.fireEventDetails=function(){var a=new d(this.action,{detail:this.selectedText});this.trigger.dispatchEvent(a)},a.prototype.fireNoSupport=function(){var a=new d("no-support");this.trigger.dispatchEvent(a)},a}();b.Clipboard=e}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"custom-event":1}]},{},[2]);

View File

@ -1,49 +1,49 @@
var CustomEvent = require('custom-event');
class Clipboard {
constructor(triggers) {
this.triggers = document.querySelectorAll(triggers);
this.init();
}
init() {
if (this.triggers.length === 0) {
throw new Error('The provided selector is empty');
if (!this.triggers.length) {
throw new Error('No matches were found for the provided selector');
}
[].forEach.call(this.triggers, (trigger) => this.bind(trigger));
}
bind(trigger) {
trigger.addEventListener('click', function(e) {
let action = e.currentTarget.getAttribute('data-action');
let target = e.currentTarget.getAttribute('data-target');
let text = e.currentTarget.getAttribute('data-text');
trigger.addEventListener('click', (e) => this.validate(e));
}
if (!action === 'copy' || !action === 'cut') {
throw new Error('Invalid "data-action" attribute');
}
else if (!target && !text) {
throw new Error('Missing "data-target" or "data-text" attribute');
}
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');
new ClipboardAction({
action : action,
target : target,
text : text,
trigger : e.currentTarget
});
});
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(options) {
this.action = options.action || 'copy';
this.target = document.getElementById(options.target);
this.text = options.text;
this.trigger = options.trigger;
constructor(action, target, text, trigger) {
this.action = action;
this.target = target;
this.text = text;
this.trigger = trigger;
this.selectedText = '';