mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Adds documentation for each block
This commit is contained in:
parent
1febe4eecc
commit
540038e2ad
2
dist/clipboard.min.js
vendored
2
dist/clipboard.min.js
vendored
@ -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){"use strict";function d(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}c.__esModule=!0;var e=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),f=a("custom-event"),g=function(){function a(b){d(this,a),this.action=b.action,this.target=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.position="absolute",a.style.left="-9999px",a.value=this.text,this.selectedText=this.text,document.body.appendChild(a),a.select(),this.copyText(),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.selectNodeContents(this.target),b.addRange(a),this.selectedText=b.toString()}this.copyText()},a.prototype.copyText=function(){var a=void 0;try{a=document.execCommand(this.action)}catch(b){a=!1}this.handleResult(a)},a.prototype.handleResult=function(a){a?this.fireEvent("success",{action:this.action,text:this.selectedText,clearSelection:this.clearSelection.bind(this)}):this.fireEvent("error",{action:this.action,clearSelection:this.clearSelection.bind(this)})},a.prototype.clearSelection=function(){this.target&&this.target.blur(),window.getSelection().removeAllRanges()},a.prototype.fireEvent=function(a,b){var c=new f(a,{detail:b});this.trigger.dispatchEvent(c)},e(a,[{key:"action",set:function(a){if(this._action=a||"copy","copy"!==this._action&&"cut"!==this._action)throw new Error('Invalid "data-action" value, use either "copy" or "cut"')},get:function(){return this._action}},{key:"target",set:function(a){if(a&&(this._target=document.getElementById(a),!this._target))throw new Error('Invalid "data-target" selector, use a value that matches an ID')},get:function(){return this._target}}]),a}();c["default"]=g,b.exports=c["default"]},{"custom-event":1}],3:[function(a,b,c){(function(d){"use strict";function e(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}c.__esModule=!0;var f=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),g=a("./clipboard-action"),h=function(){function a(b){var c=this;e(this,a),this.triggers=document.querySelectorAll(b),[].forEach.call(this.triggers,function(a){return c.bind(a)})}return a.prototype.bind=function(a){var b=this;a.addEventListener("click",function(a){return b.initialize(a)})},a.prototype.initialize=function(a){new g({action:a.currentTarget.getAttribute("data-action"),target:a.currentTarget.getAttribute("data-target"),text:a.currentTarget.getAttribute("data-text"),trigger:a.currentTarget})},f(a,[{key:"triggers",set:function(a){if(!a.length)throw new Error("No matches were found for the provided selector");this._triggers=a},get:function(){return this._triggers}}]),a}();c["default"]=h,d.Clipboard=h,b.exports=c["default"]}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./clipboard-action":2}]},{},[3]);
|
!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){"use strict";function d(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}c.__esModule=!0;var e=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),f=a("custom-event"),g=function(){function a(b){d(this,a),this.action=b.action,this.target=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.position="absolute",a.style.left="-9999px",a.value=this.text,this.selectedText=this.text,document.body.appendChild(a),a.select(),this.copyText(),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.selectNodeContents(this.target),b.addRange(a),this.selectedText=b.toString()}this.copyText()},a.prototype.copyText=function(){var a=void 0;try{a=document.execCommand(this.action)}catch(b){a=!1}this.handleResult(a)},a.prototype.handleResult=function(a){a?this.fireEvent("success",{action:this.action,text:this.selectedText,clearSelection:this.clearSelection.bind(this)}):this.fireEvent("error",{action:this.action,clearSelection:this.clearSelection.bind(this)})},a.prototype.fireEvent=function(a,b){var c=new f(a,{detail:b});this.trigger.dispatchEvent(c)},a.prototype.clearSelection=function(){this.target&&this.target.blur(),window.getSelection().removeAllRanges()},e(a,[{key:"action",set:function(a){if(this._action=a||"copy","copy"!==this._action&&"cut"!==this._action)throw new Error('Invalid "data-action" value, use either "copy" or "cut"')},get:function(){return this._action}},{key:"target",set:function(a){if(a&&(this._target=document.getElementById(a),!this._target))throw new Error('Invalid "data-target" selector, use a value that matches an ID')},get:function(){return this._target}}]),a}();c["default"]=g,b.exports=c["default"]},{"custom-event":1}],3:[function(a,b,c){(function(d){"use strict";function e(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}c.__esModule=!0;var f=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),g=a("./clipboard-action"),h=function(){function a(b){var c=this;e(this,a),this.triggers=document.querySelectorAll(b),[].forEach.call(this.triggers,function(a){return c.bind(a)})}return a.prototype.bind=function(a){var b=this;a.addEventListener("click",function(a){return b.initialize(a)})},a.prototype.initialize=function(a){new g({action:a.currentTarget.getAttribute("data-action"),target:a.currentTarget.getAttribute("data-target"),text:a.currentTarget.getAttribute("data-text"),trigger:a.currentTarget})},f(a,[{key:"triggers",set:function(a){if(!a.length)throw new Error("No matches were found for the provided selector");this._triggers=a},get:function(){return this._triggers}}]),a}();c["default"]=h,d.Clipboard=h,b.exports=c["default"]}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./clipboard-action":2}]},{},[3]);
|
@ -1,6 +1,13 @@
|
|||||||
var CustomEvent = require('custom-event');
|
var CustomEvent = require('custom-event');
|
||||||
|
|
||||||
export default class ClipboardAction {
|
/**
|
||||||
|
* Inner class which performs selection and copy operations.
|
||||||
|
*/
|
||||||
|
class ClipboardAction {
|
||||||
|
/**
|
||||||
|
* Initializes selection from either `text` or `target` property.
|
||||||
|
* @param {Object} options
|
||||||
|
*/
|
||||||
constructor(options) {
|
constructor(options) {
|
||||||
this.action = options.action;
|
this.action = options.action;
|
||||||
this.target = options.target;
|
this.target = options.target;
|
||||||
@ -17,6 +24,9 @@ export default class ClipboardAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selects the content from value passed on `text` property.
|
||||||
|
*/
|
||||||
selectValue() {
|
selectValue() {
|
||||||
let fake = document.createElement('input');
|
let fake = document.createElement('input');
|
||||||
|
|
||||||
@ -33,6 +43,9 @@ export default class ClipboardAction {
|
|||||||
document.body.removeChild(fake);
|
document.body.removeChild(fake);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selects the content from element passed on `target` property.
|
||||||
|
*/
|
||||||
selectTarget() {
|
selectTarget() {
|
||||||
if (this.target.nodeName === 'INPUT' || this.target.nodeName === 'TEXTAREA') {
|
if (this.target.nodeName === 'INPUT' || this.target.nodeName === 'TEXTAREA') {
|
||||||
this.target.select();
|
this.target.select();
|
||||||
@ -50,6 +63,9 @@ export default class ClipboardAction {
|
|||||||
this.copyText();
|
this.copyText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the copy operation based on the current selection.
|
||||||
|
*/
|
||||||
copyText() {
|
copyText() {
|
||||||
let succeeded;
|
let succeeded;
|
||||||
|
|
||||||
@ -63,6 +79,10 @@ export default class ClipboardAction {
|
|||||||
this.handleResult(succeeded);
|
this.handleResult(succeeded);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fires an event based on the copy operation result.
|
||||||
|
* @param {Boolean} succeeded
|
||||||
|
*/
|
||||||
handleResult(succeeded) {
|
handleResult(succeeded) {
|
||||||
if (succeeded) {
|
if (succeeded) {
|
||||||
this.fireEvent('success', {
|
this.fireEvent('success', {
|
||||||
@ -79,14 +99,11 @@ export default class ClipboardAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clearSelection() {
|
/**
|
||||||
if (this.target) {
|
* Emits a custom event into the `trigger` element.
|
||||||
this.target.blur();
|
* @param {String} type
|
||||||
}
|
* @param {*} detail
|
||||||
|
*/
|
||||||
window.getSelection().removeAllRanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
fireEvent(type, detail) {
|
fireEvent(type, detail) {
|
||||||
let event = new CustomEvent(type, {
|
let event = new CustomEvent(type, {
|
||||||
detail: detail
|
detail: detail
|
||||||
@ -95,6 +112,21 @@ export default class ClipboardAction {
|
|||||||
this.trigger.dispatchEvent(event);
|
this.trigger.dispatchEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes current selection and focus from `target` element.
|
||||||
|
*/
|
||||||
|
clearSelection() {
|
||||||
|
if (this.target) {
|
||||||
|
this.target.blur();
|
||||||
|
}
|
||||||
|
|
||||||
|
window.getSelection().removeAllRanges();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the `action` to be performed which can be either 'copy' or 'cut'.
|
||||||
|
* @param {String} action
|
||||||
|
*/
|
||||||
set action(action) {
|
set action(action) {
|
||||||
this._action = action || 'copy';
|
this._action = action || 'copy';
|
||||||
|
|
||||||
@ -103,10 +135,19 @@ export default class ClipboardAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the `action` property.
|
||||||
|
* @return {String}
|
||||||
|
*/
|
||||||
get action() {
|
get action() {
|
||||||
return this._action;
|
return this._action;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the `target` property using the ID of an element
|
||||||
|
* that will be have its content copied.
|
||||||
|
* @param {String} target
|
||||||
|
*/
|
||||||
set target(target) {
|
set target(target) {
|
||||||
if (target) {
|
if (target) {
|
||||||
this._target = document.getElementById(target);
|
this._target = document.getElementById(target);
|
||||||
@ -117,7 +158,13 @@ export default class ClipboardAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the `target` property.
|
||||||
|
* @return {String|HTMLElement}
|
||||||
|
*/
|
||||||
get target() {
|
get target() {
|
||||||
return this._target;
|
return this._target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default ClipboardAction;
|
||||||
|
@ -1,16 +1,32 @@
|
|||||||
var ClipboardAction = require('./clipboard-action');
|
var ClipboardAction = require('./clipboard-action');
|
||||||
|
|
||||||
export default class Clipboard {
|
/**
|
||||||
|
* Base class which takes a selector, binds a click event for
|
||||||
|
* each element found, and instantiates a new `ClipboardAction`.
|
||||||
|
*/
|
||||||
|
class Clipboard {
|
||||||
|
/**
|
||||||
|
* Fetches all elements from a selector and
|
||||||
|
* calls `bind` method for each element found.
|
||||||
|
* @param {String} triggers
|
||||||
|
*/
|
||||||
constructor(triggers) {
|
constructor(triggers) {
|
||||||
this.triggers = document.querySelectorAll(triggers);
|
this.triggers = document.querySelectorAll(triggers);
|
||||||
|
|
||||||
[].forEach.call(this.triggers, (trigger) => this.bind(trigger));
|
[].forEach.call(this.triggers, (trigger) => this.bind(trigger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a click event listener for each element.
|
||||||
|
*/
|
||||||
bind(trigger) {
|
bind(trigger) {
|
||||||
trigger.addEventListener('click', (e) => this.initialize(e));
|
trigger.addEventListener('click', (e) => this.initialize(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines a new `ClipboardAction` on each click event.
|
||||||
|
* @param {Event} e
|
||||||
|
*/
|
||||||
initialize(e) {
|
initialize(e) {
|
||||||
new ClipboardAction({
|
new ClipboardAction({
|
||||||
action : e.currentTarget.getAttribute('data-action'),
|
action : e.currentTarget.getAttribute('data-action'),
|
||||||
@ -20,6 +36,11 @@ export default class Clipboard {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the `triggers` property if there's at least
|
||||||
|
* one match for the provided selector.
|
||||||
|
* @param {NodeList} triggers
|
||||||
|
*/
|
||||||
set triggers(triggers) {
|
set triggers(triggers) {
|
||||||
if (!triggers.length) {
|
if (!triggers.length) {
|
||||||
throw new Error('No matches were found for the provided selector');
|
throw new Error('No matches were found for the provided selector');
|
||||||
@ -28,9 +49,15 @@ export default class Clipboard {
|
|||||||
this._triggers = triggers;
|
this._triggers = triggers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the `triggers` property.
|
||||||
|
* @return {NodeList}
|
||||||
|
*/
|
||||||
get triggers() {
|
get triggers() {
|
||||||
return this._triggers;
|
return this._triggers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default Clipboard;
|
||||||
|
|
||||||
global.Clipboard = Clipboard;
|
global.Clipboard = Clipboard;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user