mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Compare commits
63 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5bc00f2e4 | ||
|
|
ffb2b3fcd9 | ||
|
|
a4a68d8774 | ||
|
|
05a807e2fb | ||
|
|
0102dd6453 | ||
|
|
84d1949718 | ||
|
|
b80f9f8aae | ||
|
|
2aff9ab55a | ||
|
|
194bf6aeb3 | ||
|
|
fe6c408e48 | ||
|
|
1d74794565 | ||
|
|
1f61e16eb5 | ||
|
|
775e4b898d | ||
|
|
56bac2ce09 | ||
|
|
f34bf8eabe | ||
|
|
b842987292 | ||
|
|
beab7bc087 | ||
|
|
1ce64f39a2 | ||
|
|
40e6ac9674 | ||
|
|
157b0fb5a2 | ||
|
|
d5a4ba1ff0 | ||
|
|
a9c50a74fa | ||
|
|
b0e118f750 | ||
|
|
467684333f | ||
|
|
1acd23049e | ||
|
|
abeee82bdc | ||
|
|
ce7b9652c7 | ||
|
|
bb60a866b2 | ||
|
|
e3f69de585 | ||
|
|
aa6cc8e4df | ||
|
|
8d2fb2c08b | ||
|
|
1ac258dea5 | ||
|
|
34c798851d | ||
|
|
076e3b8a64 | ||
|
|
3610bfa08c | ||
|
|
ced945f11a | ||
|
|
540038e2ad | ||
|
|
1febe4eecc | ||
|
|
56dd1aac22 | ||
|
|
e72ce02c87 | ||
|
|
dedfbffe05 | ||
|
|
ba417cf53d | ||
|
|
fbb2a316bf | ||
|
|
b4a748f89f | ||
|
|
e2b9ba69a6 | ||
|
|
706d55504b | ||
|
|
90878d90c4 | ||
|
|
c92c4e545a | ||
|
|
23b20d6006 | ||
|
|
f3c042a364 | ||
|
|
aebcbdf292 | ||
|
|
8050bda877 | ||
|
|
cd7c8bfc27 | ||
|
|
52b444609e | ||
|
|
ec20389775 | ||
|
|
b1d0ac9520 | ||
|
|
bea448d6c5 | ||
|
|
66c18fbcb4 | ||
|
|
24f4bc77ed | ||
|
|
960d1a9dd9 | ||
|
|
7a92a67487 | ||
|
|
76ab07a186 | ||
|
|
718c1b33f0 |
3
.travis.yml
Normal file
3
.travis.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- 0.12
|
||||
150
README.md
150
README.md
@@ -1,55 +1,169 @@
|
||||
# clipboard.js
|
||||
|
||||
> A modern approach to copy & cut to the clipboard. No Flash. No dependencies. Just 1kb.
|
||||
[](https://travis-ci.org/zenorocha/clipboard.js)
|
||||
|
||||
> Modern copy to clipboard. No Flash. Just 2kb
|
||||
|
||||
<a href="http://zenorocha.github.io/clipboard.js/"><img width="728" src="https://cloud.githubusercontent.com/assets/398893/9983535/5ab0a950-5fb4-11e5-9602-e73c0b661883.jpg" alt="Demo"></a>
|
||||
|
||||
## Why
|
||||
|
||||
Copy text to the clipboard shouldn't be hard. It shouldn't require dozens of steps to configure or hundreds of KBs to load. But most of all, it shouldn't depend on Flash or any bloated framework.
|
||||
|
||||
That's why clipboard.js exists.
|
||||
|
||||
## Install
|
||||
|
||||
You can get it using bower:
|
||||
You can get it on npm.
|
||||
|
||||
```
|
||||
npm install clipboard --save
|
||||
```
|
||||
|
||||
Or bower, too.
|
||||
|
||||
```
|
||||
bower install clipboard --save
|
||||
```
|
||||
|
||||
Or [download as ZIP](https://github.com/zenorocha/clipboard.js/archive/master.zip).
|
||||
If you're not into package management, just [download a ZIP](https://github.com/zenorocha/clipboard.js/archive/master.zip) file.
|
||||
|
||||
## Usage
|
||||
## Setup
|
||||
|
||||
First, you need to instantiate it using a selector. This selector corresponds to the trigger element, usually a `<button>`.
|
||||
First, include the script located on the `dist` folder
|
||||
|
||||
```html
|
||||
<script src="dist/clipboard.min.js"></script>
|
||||
```
|
||||
|
||||
Now, you need to instantiate it using a DOM selector. This selector corresponds to the trigger element(s), for example `<button class="btn">`.
|
||||
|
||||
```js
|
||||
new Clipboard('.btn');
|
||||
```
|
||||
|
||||
The easiest way to copy some content to the clipboard, is to include a `data-text` attribute in your trigger element.
|
||||
Internally, we need to fetch all elements that matches with your selector and attach event listeners for each one. But guess what? If you have hundreds of matches, this operation can consume a lot of memory.
|
||||
|
||||
For this reason we use [event delegation](http://stackoverflow.com/questions/1687296/what-is-dom-event-delegation) which replaces multiple event listeners with just a single listener. After all, [#perfmatters](https://twitter.com/hashtag/perfmatters).
|
||||
|
||||
# Usage
|
||||
|
||||
We're living a _declarative renaissance_, that's why we decided to take advantage of [HTML5 data attributes](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes) for better usability.
|
||||
|
||||
### Copy text from another element
|
||||
|
||||
A pretty common use case is to copy content from another element. You can do that by adding a `data-clipboard-target` attribute in your trigger element.
|
||||
|
||||
The value you include on this attribute needs to match another's element selector.
|
||||
|
||||
<a href="http://zenorocha.github.io/clipboard.js/#example-target"><img width="473" alt="example-2" src="https://cloud.githubusercontent.com/assets/398893/9983467/a4946aaa-5fb1-11e5-9780-f09fcd7ca6c8.png"></a>
|
||||
|
||||
```html
|
||||
<button class="btn" data-text="Lorem ipsum">Copy</button>
|
||||
<!-- Target -->
|
||||
<input id="foo" value="https://github.com/zenorocha/clipboard.js.git">
|
||||
|
||||
<!-- Trigger -->
|
||||
<button class="btn" data-clipboard-target="#foo">
|
||||
<img src="assets/clippy.svg" alt="Copy to clipboard">
|
||||
</button>
|
||||
```
|
||||
|
||||
Another way of doing it, is to copy the content from an another element. You can do that by adding a `data-target` attribute in your trigger element. The value you include on this attribute needs to match another's element `id` attribute.
|
||||
### Cut text from another element
|
||||
|
||||
Additionally, you can define a `data-clipboard-action` attribute to specify if you want to either `copy` or `cut` content.
|
||||
|
||||
If you omit this attribute, `copy` will be used by default.
|
||||
|
||||
<a href="http://zenorocha.github.io/clipboard.js/#example-action"><img width="473" alt="example-3" src="https://cloud.githubusercontent.com/assets/398893/10000358/7df57b9c-6050-11e5-9cd1-fbc51d2fd0a7.png"></a>
|
||||
|
||||
```html
|
||||
<p id="foo">Lorem ipsum</p>
|
||||
<button class="btn" data-target="foo">Copy</button>
|
||||
```
|
||||
<!-- Target -->
|
||||
<textarea id="bar">Mussum ipsum cacilds...</textarea>
|
||||
|
||||
Additionally, you can define a `data-action` attribute to specify if you want to either `copy` or `cut` content. If you omit this attribute, `copy` will be used.
|
||||
|
||||
```html
|
||||
<input id="foo" value="Lorem ipsum"></inpu>
|
||||
<button class="btn" data-action="cut" data-target="foo">Copy</button>
|
||||
<!-- Trigger -->
|
||||
<button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">
|
||||
Cut to clipboard
|
||||
</button>
|
||||
```
|
||||
|
||||
As you may expect, the `cut` action only works on `<input>` or `<textarea>` elements.
|
||||
|
||||
### Copy text from attribute
|
||||
|
||||
Truth is, you don't even need another element to copy its content from. You can just include a `data-clipboard-text` attribute in your trigger element.
|
||||
|
||||
<a href="http://zenorocha.github.io/clipboard.js/#example-text"><img width="147" alt="example-1" src="https://cloud.githubusercontent.com/assets/398893/10000347/6e16cf8c-6050-11e5-9883-1c5681f9ec45.png"></a>
|
||||
|
||||
```html
|
||||
<!-- Trigger -->
|
||||
<button class="btn" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
|
||||
Copy to clipboard
|
||||
</button>
|
||||
```
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
If you don't want to modify your HTML, there's a pretty handy imperative API for you to use. All you need to do is declare a function, do your thing, and return a value.
|
||||
|
||||
For instance, if you want to dynamically set a `target`, you'll need to return a Node.
|
||||
|
||||
```js
|
||||
new Clipboard('.btn', {
|
||||
target: function(trigger) {
|
||||
return trigger.nextElementSibling;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
If you want to dynamically set a `text`, you'll return a String.
|
||||
|
||||
```js
|
||||
new Clipboard('.btn', {
|
||||
text: function(trigger) {
|
||||
return trigger.getAttribute('aria-label');
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
## Events
|
||||
|
||||
There are cases where you'd like to show some user feedback or capture what has been selected after a copy/cut operation.
|
||||
|
||||
That's why we fire custom events such as `success` and `error` for you to listen and implement your custom logic.
|
||||
|
||||
```js
|
||||
var clipboard = new Clipboard('.btn');
|
||||
|
||||
clipboard.on('success', function(e) {
|
||||
console.info('Action:', e.action);
|
||||
console.info('Text:', e.text);
|
||||
console.info('Trigger:', e.trigger);
|
||||
|
||||
e.clearSelection();
|
||||
});
|
||||
|
||||
clipboard.on('error', function(e) {
|
||||
console.error('Action:', e.action);
|
||||
console.error('Trigger:', e.trigger);
|
||||
});
|
||||
```
|
||||
|
||||
For a live demonstration, open this [site](http://zenorocha.github.io/clipboard.js/) and just your console :)
|
||||
|
||||
## Browser Support
|
||||
|
||||
This project relies on both [Select API](https://developer.mozilla.org/en-US/docs/Web/API/Selection) and [execCommand API](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand). When combined, they're supported in the following browsers.
|
||||
This library relies on both [Selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection) and [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) APIs. The second one is supported in the following browsers.
|
||||
|
||||
| <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/chrome/chrome_64x64.png" width="48px" height="48px" alt="Chrome logo"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/firefox/firefox_64x64.png" width="48px" height="48px" alt="Firefox logo"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/internet-explorer/internet-explorer_64x64.png" width="48px" height="48px" alt="Internet Explorer logo"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/opera/opera_64x64.png" width="48px" height="48px" alt="Opera logo"> | <img src="https://raw.githubusercontent.com/alrra/browser-logos/master/safari/safari_64x64.png" width="48px" height="48px" alt="Safari logo"> |
|
||||
| <img src="http://zenorocha.github.io/clipboard.js/assets/images/chrome.png" width="48px" height="48px" alt="Chrome logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/firefox.png" width="48px" height="48px" alt="Firefox logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/ie.png" width="48px" height="48px" alt="Internet Explorer logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/opera.png" width="48px" height="48px" alt="Opera logo"> | <img src="http://zenorocha.github.io/clipboard.js/assets/images/safari.png" width="48px" height="48px" alt="Safari logo"> |
|
||||
|:---:|:---:|:---:|:---:|:---:|
|
||||
| 42+ ✔ | 41+ ✔ | 9+ ✔ | 29+ ✔ | Nope ✘ |
|
||||
|
||||
Although copy/cut operations with [execCommand](https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand) aren't supported on Safari yet (including mobile), it gracefully degrades because [Selection](https://developer.mozilla.org/en-US/docs/Web/API/Selection) is supported.
|
||||
|
||||
That means you can show a tooltip saying `Copied!` when `success` event is called and `Press Ctrl+C to copy` when `error` event is called because the text is already selected.
|
||||
|
||||
For a live demonstration, open this [site](http://zenorocha.github.io/clipboard.js/) on Safari.
|
||||
|
||||
## License
|
||||
|
||||
[MIT License](http://zenorocha.mit-license.org/) © Zeno Rocha
|
||||
|
||||
11
bower.json
11
bower.json
@@ -1,17 +1,12 @@
|
||||
{
|
||||
"name": "clipboard",
|
||||
"version": "0.0.0",
|
||||
"description": "A modern approach to copy & cut to the clipboard",
|
||||
"version": "1.3.0",
|
||||
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
||||
"license": "MIT",
|
||||
"main": "src/clipboard.js",
|
||||
"keywords": [
|
||||
"clipboard",
|
||||
"copy",
|
||||
"cut"
|
||||
],
|
||||
"devDependencies": {
|
||||
"highlightjs": "~8.8.0",
|
||||
"octicons": "~3.1.0",
|
||||
"primer-css": "~2.3.3"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
232
demo.css
232
demo.css
@@ -1,232 +0,0 @@
|
||||
body {
|
||||
font-family: 'Lato', sans-serif;
|
||||
}
|
||||
|
||||
.gradient {
|
||||
background: #4cd964;
|
||||
background: -moz-linear-gradient(45deg, #4cd964 0%, #5ac8fa 100%);
|
||||
background: -webkit-gradient(left bottom, right top, color-stop(0%, #4cd964), color-stop(100%, #5ac8fa));
|
||||
background: -webkit-linear-gradient(45deg, #4cd964 0%, #5ac8fa 100%);
|
||||
background: -o-linear-gradient(45deg, #4cd964 0%, #5ac8fa 100%);
|
||||
background: -ms-linear-gradient(45deg, #4cd964 0%, #5ac8fa 100%);
|
||||
background: linear-gradient(45deg, #4cd964 0%, #5ac8fa 100%);
|
||||
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4cd964', endColorstr='#5ac8fa', GradientType=1 );
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Header
|
||||
========================================================================== */
|
||||
|
||||
.header {
|
||||
padding-top: 92px;
|
||||
}
|
||||
|
||||
.title {
|
||||
color: white;
|
||||
font-size: 64px;
|
||||
font-weight: 900;
|
||||
letter-spacing: -1px;
|
||||
margin: 0 0 20px;
|
||||
}
|
||||
|
||||
.subtitle {
|
||||
color: #16a085;
|
||||
font-size: 27px;
|
||||
font-weight: 400;
|
||||
margin: 0 0 20px;
|
||||
}
|
||||
|
||||
.subtitle + .subtitle {
|
||||
color: white;
|
||||
}
|
||||
|
||||
.gh-btns {
|
||||
margin: 92px 0 0;
|
||||
background: rgba(0, 0, 0, .1);
|
||||
padding: 20px 0 10px;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Main
|
||||
========================================================================== */
|
||||
|
||||
.wrap {
|
||||
margin: 0 auto 90px;
|
||||
width: 500px;
|
||||
}
|
||||
|
||||
p {
|
||||
color: #333;
|
||||
font-size: 18px;
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #1BC1A1;
|
||||
border-bottom: 1px dotted #1BC1A1;
|
||||
-webkit-transition: opacity .3s ease-in-out;
|
||||
transition: opacity .3s ease-in-out;
|
||||
}
|
||||
|
||||
a:hover,
|
||||
a:focus {
|
||||
text-decoration: none;
|
||||
opacity: .7;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin-top: 80px;
|
||||
}
|
||||
|
||||
h3 {
|
||||
color: #333;
|
||||
margin: 40px 0;
|
||||
font-size: 18px;
|
||||
font-weight: 300;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
/* Code
|
||||
========================================================================== */
|
||||
|
||||
pre code {
|
||||
font-size: 14px;
|
||||
line-height: 20px;
|
||||
}
|
||||
|
||||
code {
|
||||
background-color: rgba(0, 0, 0, 0.04);
|
||||
border-radius: 3px;
|
||||
font-size: 85%;
|
||||
margin: 0;
|
||||
padding: 0.2em;
|
||||
}
|
||||
|
||||
.hljs-keyword {
|
||||
color: #008080;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
/* Example
|
||||
========================================================================== */
|
||||
|
||||
.example {
|
||||
position: relative;
|
||||
margin: 15px 0 0;
|
||||
padding: 39px 19px 14px;
|
||||
background-color: #fff;
|
||||
border-radius: 4px 4px 0 0;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.example p {
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.example:after {
|
||||
content: "Example";
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
padding: 2px 8px;
|
||||
font-size: 12px;
|
||||
font-weight: bold;
|
||||
background-color: #f5f5f5;
|
||||
color: #9da0a4;
|
||||
border-radius: 4px 0 4px 0;
|
||||
}
|
||||
|
||||
.example + pre {
|
||||
background: #f8f8f8;
|
||||
border-radius: 4px;
|
||||
border: 1px solid #ddd;
|
||||
clear: both;
|
||||
margin-top: -20px;
|
||||
padding: 20px 5px 0;
|
||||
}
|
||||
|
||||
/* Live example
|
||||
========================================================================== */
|
||||
|
||||
.form-actions {
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
.form-actions .btn {
|
||||
float: left;
|
||||
}
|
||||
|
||||
textarea {
|
||||
-webkit-box-sizing: border-box;
|
||||
-moz-box-sizing: border-box;
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Support
|
||||
========================================================================== */
|
||||
|
||||
.support {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.support li {
|
||||
display: inline-block;
|
||||
text-align: center;
|
||||
margin: 5px 8px 0;
|
||||
}
|
||||
|
||||
.support p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
Footer
|
||||
========================================================================== */
|
||||
|
||||
.footer {
|
||||
padding: 36px 0;
|
||||
}
|
||||
|
||||
.credits {
|
||||
font-weight: 400;
|
||||
font-family: 'Lato', sans-serif;
|
||||
font-size: 20px;
|
||||
color: #16a085;
|
||||
}
|
||||
|
||||
.credits-link {
|
||||
color: white;
|
||||
border-color: white;
|
||||
}
|
||||
|
||||
.credits-link:hover,
|
||||
.credits-link:focus {
|
||||
text-decoration: none;
|
||||
border-color: white;
|
||||
}
|
||||
|
||||
.love {
|
||||
display: inline-block;
|
||||
position: relative;
|
||||
top: .2em;
|
||||
font-size: 1.4em;
|
||||
-webkit-transform: scale(.9);
|
||||
-moz-transform: scale(.9);
|
||||
transform: scale(.9);
|
||||
-webkit-animation: love .5s infinite linear alternate-reverse;
|
||||
-moz-animation: love .5s infinite linear alternate-reverse;
|
||||
animation: love .5s infinite linear alternate-reverse;
|
||||
}
|
||||
|
||||
@-webkit-keyframes love {
|
||||
to {-webkit-transform: scale(1.2);}
|
||||
}
|
||||
|
||||
@-moz-keyframes love {
|
||||
to {-moz-transform: scale(1.2);}
|
||||
}
|
||||
@keyframes love {
|
||||
to {transform: scale(1.2);}
|
||||
}
|
||||
541
dist/clipboard.js
vendored
Normal file
541
dist/clipboard.js
vendored
Normal file
@@ -0,0 +1,541 @@
|
||||
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Clipboard = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var closest = require('closest')
|
||||
, event = require('component-event');
|
||||
|
||||
/**
|
||||
* Delegate event `type` to `selector`
|
||||
* and invoke `fn(e)`. A callback function
|
||||
* is returned which may be passed to `.unbind()`.
|
||||
*
|
||||
* @param {Element} el
|
||||
* @param {String} selector
|
||||
* @param {String} type
|
||||
* @param {Function} fn
|
||||
* @param {Boolean} capture
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
// Some events don't bubble, so we want to bind to the capture phase instead
|
||||
// when delegating.
|
||||
var forceCaptureEvents = ['focus', 'blur'];
|
||||
|
||||
exports.bind = function(el, selector, type, fn, capture){
|
||||
if (forceCaptureEvents.indexOf(type) !== -1) capture = true;
|
||||
|
||||
return event.bind(el, type, function(e){
|
||||
var target = e.target || e.srcElement;
|
||||
e.delegateTarget = closest(target, selector, true, el);
|
||||
if (e.delegateTarget) fn.call(el, e);
|
||||
}, capture);
|
||||
};
|
||||
|
||||
/**
|
||||
* Unbind event `type`'s callback `fn`.
|
||||
*
|
||||
* @param {Element} el
|
||||
* @param {String} type
|
||||
* @param {Function} fn
|
||||
* @param {Boolean} capture
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.unbind = function(el, type, fn, capture){
|
||||
if (forceCaptureEvents.indexOf(type) !== -1) capture = true;
|
||||
|
||||
event.unbind(el, type, fn, capture);
|
||||
};
|
||||
|
||||
},{"closest":2,"component-event":4}],2:[function(require,module,exports){
|
||||
var matches = require('matches-selector')
|
||||
|
||||
module.exports = function (element, selector, checkYoSelf) {
|
||||
var parent = checkYoSelf ? element : element.parentNode
|
||||
|
||||
while (parent && parent !== document) {
|
||||
if (matches(parent, selector)) return parent;
|
||||
parent = parent.parentNode
|
||||
}
|
||||
}
|
||||
|
||||
},{"matches-selector":3}],3:[function(require,module,exports){
|
||||
|
||||
/**
|
||||
* Element prototype.
|
||||
*/
|
||||
|
||||
var proto = Element.prototype;
|
||||
|
||||
/**
|
||||
* Vendor function.
|
||||
*/
|
||||
|
||||
var vendor = proto.matchesSelector
|
||||
|| proto.webkitMatchesSelector
|
||||
|| proto.mozMatchesSelector
|
||||
|| proto.msMatchesSelector
|
||||
|| proto.oMatchesSelector;
|
||||
|
||||
/**
|
||||
* Expose `match()`.
|
||||
*/
|
||||
|
||||
module.exports = match;
|
||||
|
||||
/**
|
||||
* Match `el` to `selector`.
|
||||
*
|
||||
* @param {Element} el
|
||||
* @param {String} selector
|
||||
* @return {Boolean}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function match(el, selector) {
|
||||
if (vendor) return vendor.call(el, selector);
|
||||
var nodes = el.parentNode.querySelectorAll(selector);
|
||||
for (var i = 0; i < nodes.length; ++i) {
|
||||
if (nodes[i] == el) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
},{}],4:[function(require,module,exports){
|
||||
var bind = window.addEventListener ? 'addEventListener' : 'attachEvent',
|
||||
unbind = window.removeEventListener ? 'removeEventListener' : 'detachEvent',
|
||||
prefix = bind !== 'addEventListener' ? 'on' : '';
|
||||
|
||||
/**
|
||||
* Bind `el` event `type` to `fn`.
|
||||
*
|
||||
* @param {Element} el
|
||||
* @param {String} type
|
||||
* @param {Function} fn
|
||||
* @param {Boolean} capture
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.bind = function(el, type, fn, capture){
|
||||
el[bind](prefix + type, fn, capture || false);
|
||||
return fn;
|
||||
};
|
||||
|
||||
/**
|
||||
* Unbind `el` event `type`'s callback `fn`.
|
||||
*
|
||||
* @param {Element} el
|
||||
* @param {String} type
|
||||
* @param {Function} fn
|
||||
* @param {Boolean} capture
|
||||
* @return {Function}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
exports.unbind = function(el, type, fn, capture){
|
||||
el[unbind](prefix + type, fn, capture || false);
|
||||
return fn;
|
||||
};
|
||||
},{}],5:[function(require,module,exports){
|
||||
function E () {
|
||||
// Keep this empty so it's easier to inherit from
|
||||
// (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
|
||||
}
|
||||
|
||||
E.prototype = {
|
||||
on: function (name, callback, ctx) {
|
||||
var e = this.e || (this.e = {});
|
||||
|
||||
(e[name] || (e[name] = [])).push({
|
||||
fn: callback,
|
||||
ctx: ctx
|
||||
});
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
once: function (name, callback, ctx) {
|
||||
var self = this;
|
||||
var fn = function () {
|
||||
self.off(name, fn);
|
||||
callback.apply(ctx, arguments);
|
||||
};
|
||||
|
||||
return this.on(name, fn, ctx);
|
||||
},
|
||||
|
||||
emit: function (name) {
|
||||
var data = [].slice.call(arguments, 1);
|
||||
var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
|
||||
var i = 0;
|
||||
var len = evtArr.length;
|
||||
|
||||
for (i; i < len; i++) {
|
||||
evtArr[i].fn.apply(evtArr[i].ctx, data);
|
||||
}
|
||||
|
||||
return this;
|
||||
},
|
||||
|
||||
off: function (name, callback) {
|
||||
var e = this.e || (this.e = {});
|
||||
var evts = e[name];
|
||||
var liveEvents = [];
|
||||
|
||||
if (evts && callback) {
|
||||
for (var i = 0, len = evts.length; i < len; i++) {
|
||||
if (evts[i].fn !== callback) liveEvents.push(evts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove event from queue to prevent memory leak
|
||||
// Suggested by https://github.com/lazd
|
||||
// Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910
|
||||
|
||||
(liveEvents.length)
|
||||
? e[name] = liveEvents
|
||||
: delete e[name];
|
||||
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = E;
|
||||
|
||||
},{}],6:[function(require,module,exports){
|
||||
/**
|
||||
* Inner class which performs selection and copy operations.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
var ClipboardAction = (function () {
|
||||
/**
|
||||
* Initializes selection from either `text` or `target` property.
|
||||
* @param {Object} options
|
||||
*/
|
||||
|
||||
function ClipboardAction(options) {
|
||||
_classCallCheck(this, ClipboardAction);
|
||||
|
||||
this.action = options.action;
|
||||
this.emitter = options.emitter;
|
||||
this.target = options.target;
|
||||
this.text = options.text;
|
||||
this.trigger = options.trigger;
|
||||
|
||||
this.selectedText = '';
|
||||
|
||||
if (this.text && this.target) {
|
||||
throw new Error('Multiple attributes declared, use either "target" or "text"');
|
||||
} else if (this.text) {
|
||||
this.selectFake();
|
||||
} else if (this.target) {
|
||||
this.selectTarget();
|
||||
} else {
|
||||
throw new Error('Missing required attributes, use either "target" or "text"');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a fake input element, sets its value from `text` property,
|
||||
* and makes a selection on it.
|
||||
*/
|
||||
|
||||
ClipboardAction.prototype.selectFake = function selectFake() {
|
||||
var _this = this;
|
||||
|
||||
this.removeFake();
|
||||
|
||||
this.fakeHandler = document.body.addEventListener('click', function () {
|
||||
return _this.removeFake();
|
||||
});
|
||||
|
||||
this.fakeElem = document.createElement('input');
|
||||
this.fakeElem.style.position = 'absolute';
|
||||
this.fakeElem.style.left = '-9999px';
|
||||
this.fakeElem.setAttribute('readonly', '');
|
||||
this.fakeElem.value = this.text;
|
||||
this.selectedText = this.text;
|
||||
|
||||
document.body.appendChild(this.fakeElem);
|
||||
|
||||
this.fakeElem.select();
|
||||
this.copyText();
|
||||
};
|
||||
|
||||
/**
|
||||
* Only removes the fake element after another click event, that way
|
||||
* an user can hit `Ctrl+C` to copy because selection still exists.
|
||||
*/
|
||||
|
||||
ClipboardAction.prototype.removeFake = function removeFake() {
|
||||
if (this.fakeHandler) {
|
||||
document.body.removeEventListener('click');
|
||||
this.fakeHandler = null;
|
||||
}
|
||||
|
||||
if (this.fakeElem) {
|
||||
document.body.removeChild(this.fakeElem);
|
||||
this.fakeElem = null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Selects the content from element passed on `target` property.
|
||||
*/
|
||||
|
||||
ClipboardAction.prototype.selectTarget = function selectTarget() {
|
||||
if (this.target.nodeName === 'INPUT' || this.target.nodeName === 'TEXTAREA') {
|
||||
this.target.select();
|
||||
this.selectedText = this.target.value;
|
||||
} else {
|
||||
var range = document.createRange();
|
||||
var selection = window.getSelection();
|
||||
|
||||
range.selectNodeContents(this.target);
|
||||
selection.addRange(range);
|
||||
this.selectedText = selection.toString();
|
||||
}
|
||||
|
||||
this.copyText();
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes the copy operation based on the current selection.
|
||||
*/
|
||||
|
||||
ClipboardAction.prototype.copyText = function copyText() {
|
||||
var succeeded = undefined;
|
||||
|
||||
try {
|
||||
succeeded = document.execCommand(this.action);
|
||||
} catch (err) {
|
||||
succeeded = false;
|
||||
}
|
||||
|
||||
this.handleResult(succeeded);
|
||||
};
|
||||
|
||||
/**
|
||||
* Fires an event based on the copy operation result.
|
||||
* @param {Boolean} succeeded
|
||||
*/
|
||||
|
||||
ClipboardAction.prototype.handleResult = function handleResult(succeeded) {
|
||||
if (succeeded) {
|
||||
this.emitter.emit('success', {
|
||||
action: this.action,
|
||||
text: this.selectedText,
|
||||
trigger: this.trigger,
|
||||
clearSelection: this.clearSelection.bind(this)
|
||||
});
|
||||
} else {
|
||||
this.emitter.emit('error', {
|
||||
action: this.action,
|
||||
trigger: this.trigger,
|
||||
clearSelection: this.clearSelection.bind(this)
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Removes current selection and focus from `target` element.
|
||||
*/
|
||||
|
||||
ClipboardAction.prototype.clearSelection = function 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
|
||||
*/
|
||||
|
||||
_createClass(ClipboardAction, [{
|
||||
key: 'action',
|
||||
set: function set(action) {
|
||||
this._action = action || 'copy';
|
||||
|
||||
if (this._action !== 'copy' && this._action !== 'cut') {
|
||||
throw new Error('Invalid "action" value, use either "copy" or "cut"');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the `action` property.
|
||||
* @return {String}
|
||||
*/
|
||||
get: function get() {
|
||||
return this._action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `target` property using an element that will be have its content
|
||||
* copied.
|
||||
* @param {Element} target
|
||||
*/
|
||||
}, {
|
||||
key: 'target',
|
||||
set: function set(target) {
|
||||
if (target !== undefined) {
|
||||
if (target && typeof target === 'object' && target.nodeType === 1) {
|
||||
this._target = target;
|
||||
} else {
|
||||
throw new Error('Invalid "target" value, use a valid Element');
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the `target` property.
|
||||
* @return {String|HTMLElement}
|
||||
*/
|
||||
get: function get() {
|
||||
return this._target;
|
||||
}
|
||||
}]);
|
||||
|
||||
return ClipboardAction;
|
||||
})();
|
||||
|
||||
exports['default'] = ClipboardAction;
|
||||
module.exports = exports['default'];
|
||||
|
||||
},{}],7:[function(require,module,exports){
|
||||
'use strict';
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
||||
|
||||
var _clipboardAction = require('./clipboard-action');
|
||||
|
||||
var _clipboardAction2 = _interopRequireDefault(_clipboardAction);
|
||||
|
||||
var _delegateEvents = require('delegate-events');
|
||||
|
||||
var _delegateEvents2 = _interopRequireDefault(_delegateEvents);
|
||||
|
||||
var _tinyEmitter = require('tiny-emitter');
|
||||
|
||||
var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
|
||||
|
||||
var prefix = 'data-clipboard-';
|
||||
|
||||
/**
|
||||
* Base class which takes a selector, delegates a click event to it,
|
||||
* and instantiates a new `ClipboardAction` on each click.
|
||||
*/
|
||||
|
||||
var Clipboard = (function (_Emitter) {
|
||||
_inherits(Clipboard, _Emitter);
|
||||
|
||||
/**
|
||||
* Delegates a click event on the passed selector.
|
||||
* @param {String} selector
|
||||
* @param {Object} options
|
||||
*/
|
||||
|
||||
function Clipboard(selector, options) {
|
||||
var _this = this;
|
||||
|
||||
_classCallCheck(this, Clipboard);
|
||||
|
||||
_Emitter.call(this);
|
||||
|
||||
this.resolveOptions(options);
|
||||
|
||||
_delegateEvents2['default'].bind(document.body, selector, 'click', function (e) {
|
||||
return _this.initialize(e);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines if attributes would be resolved using an internal setter function
|
||||
* or a custom function that was passed in the constructor.
|
||||
* @param {Object} options
|
||||
*/
|
||||
|
||||
Clipboard.prototype.resolveOptions = function resolveOptions(options) {
|
||||
options = 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;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the `action` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
|
||||
Clipboard.prototype.setAction = function setAction(trigger) {
|
||||
return trigger.getAttribute(prefix + 'action');
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the `target` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
|
||||
Clipboard.prototype.setTarget = function setTarget(trigger) {
|
||||
var target = trigger.getAttribute(prefix + 'target');
|
||||
|
||||
if (target) {
|
||||
return document.querySelector(target);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets the `text` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
|
||||
Clipboard.prototype.setText = function setText(trigger) {
|
||||
return trigger.getAttribute(prefix + 'text');
|
||||
};
|
||||
|
||||
/**
|
||||
* Defines a new `ClipboardAction` on each click event.
|
||||
* @param {Event} e
|
||||
*/
|
||||
|
||||
Clipboard.prototype.initialize = function initialize(e) {
|
||||
if (this.clipboardAction) {
|
||||
this.clipboardAction = null;
|
||||
}
|
||||
|
||||
this.clipboardAction = new _clipboardAction2['default']({
|
||||
action: this.action(e.delegateTarget),
|
||||
target: this.target(e.delegateTarget),
|
||||
text: this.text(e.delegateTarget),
|
||||
trigger: e.delegateTarget,
|
||||
emitter: this
|
||||
});
|
||||
};
|
||||
|
||||
return Clipboard;
|
||||
})(_tinyEmitter2['default']);
|
||||
|
||||
exports['default'] = Clipboard;
|
||||
module.exports = exports['default'];
|
||||
|
||||
},{"./clipboard-action":6,"delegate-events":1,"tiny-emitter":5}]},{},[7])(7)
|
||||
});
|
||||
2
dist/clipboard.min.js
vendored
2
dist/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
168
index.html
168
index.html
@@ -1,168 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>clipboard.js</title>
|
||||
|
||||
<link rel="stylesheet" href="bower_components/primer-css/css/primer.css">
|
||||
<link rel="stylesheet" href="bower_components/octicons/octicons/octicons.css">
|
||||
<link rel="stylesheet" href="bower_components/highlightjs/styles/github.css">
|
||||
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:300,400,700,900">
|
||||
<link rel="stylesheet" href="demo.css">
|
||||
</head>
|
||||
<body>
|
||||
<header class="header gradient text-center">
|
||||
<h1 class="title">clipboard.js</h1>
|
||||
<h2 class="subtitle">A modern approach to copy & cut to the clipboard</h2>
|
||||
<h2 class="subtitle">No Flash. No dependencies. Just 2kb</h2>
|
||||
<p class="gh-btns">
|
||||
<iframe src="http://ghbtns.com/github-btn.html?user=zenorocha&repo=clipboard.js&type=watch&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="152" height="30"></iframe>
|
||||
<iframe src="http://ghbtns.com/github-btn.html?user=zenorocha&repo=clipboard.js&type=fork&count=true&size=large"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" width="156" height="30"></iframe>
|
||||
</p>
|
||||
</header>
|
||||
|
||||
<main class="wrap">
|
||||
<h1>Install</h1>
|
||||
|
||||
<p>You can get it on npm.</p>
|
||||
|
||||
<pre><code class="js">npm install clipboard --save</code></pre>
|
||||
|
||||
<p>Or bower, too.</p>
|
||||
|
||||
<pre><code class="js">bower install clipboard --save</code></pre>
|
||||
|
||||
<p>If you're not into package management, just <a href="https://github.com/zenorocha/clipboard.js/archive/master.zip">download a ZIP</a> file.</p>
|
||||
|
||||
<h1>Setup</h1>
|
||||
|
||||
<p>First, include the script located on the <code>dist</code> folder</p>
|
||||
|
||||
<pre><code class="html"><script src="dist/clipboard.min.js"></script></code></pre>
|
||||
|
||||
<p>Now, you need to instantiate it using a DOM selector. This selector corresponds to the trigger element, i.e. <code><button></code>.</p>
|
||||
|
||||
<pre><code class="html"><script> new Clipboard('.btn'); </script></code></pre>
|
||||
|
||||
<h1>Usage</h1>
|
||||
|
||||
<p>We're living a <em>declarative renaissance</em>, that's why we decided to take advantage of <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes">HTML5 data attributes</a> for better usability.</p>
|
||||
|
||||
<h3>Copy text from attribute</h3>
|
||||
|
||||
<p>The easiest way to copy some content to the clipboard, is to include a <code>data-text</code> attribute in your trigger element.</p>
|
||||
|
||||
<div class="example">
|
||||
<button class="btn" data-action="copy" data-text="Just because you can doesn't mean you should — clipboard.js">Copy to the clipboard</button>
|
||||
</div>
|
||||
|
||||
<pre><code class="html"><!-- Trigger -->
|
||||
<button class="btn" data-text="Heya!">Copy</button></code></pre>
|
||||
|
||||
<h3>Copy text from another element</h3>
|
||||
|
||||
<p>Alternatively, you can copy content from another element by adding a <code>data-target</code> attribute in your trigger element.</p>
|
||||
<p>The value you include on this attribute needs to match another's element <code>id</code> attribute.</p>
|
||||
|
||||
<div class="example">
|
||||
<div class="input-group">
|
||||
<input id="foo" type="text" value="https://github.com/zenorocha/clipboard.js.git">
|
||||
<span class="input-group-button">
|
||||
<button class="btn" type="button" data-action="copy" data-target="foo">
|
||||
<span class="octicon octicon-clippy"></span>
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<pre><code class="html"><!-- Target -->
|
||||
<input id="foo" value="https://git.io/vn3cM">
|
||||
|
||||
<!-- Trigger -->
|
||||
<button class="btn" data-target="foo">Copy</button></code></pre>
|
||||
|
||||
<h3>Cut text from another element</h3>
|
||||
|
||||
<p>Additionally, you can define a <code>data-action</code> attribute to specify if you want to either <code>copy</code> or <code>cut</code> content.</p>
|
||||
<p>If you omit this attribute, <code>copy</code> will be used by default.</p>
|
||||
|
||||
<div class="example">
|
||||
<div class="input-group">
|
||||
<textarea id="bar" cols="62" rows="4" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">Mussum ipsum cacilds, vidis litro abertis. Consetis adipiscings elitis. Pra lá , depois divoltis porris, paradis. Paisis, filhis, espiritis santis. Mé faiz elementum girarzis, nisi eros vermeio, in elementis mé pra quem é amistosis quis leo. Manduma pindureta quium dia nois paga.</textarea>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button class="btn" type="button" data-action="cut" data-target="bar">
|
||||
Cut to the clipboard
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<pre><code class="html"><!-- Target -->
|
||||
<textarea id="bar">clipboard.js rocks!</textarea>
|
||||
|
||||
<!-- Trigger -->
|
||||
<button class="btn" data-action="cut" data-target="bar">
|
||||
Copy
|
||||
</button></code></pre>
|
||||
|
||||
<p>As you may expect, the <code>cut</code> action only works on <code><input></code> or <code><textarea></code> elements.</p>
|
||||
|
||||
<h1>Browser Support</h1>
|
||||
|
||||
<p>This library relies on both <a href="https://developer.mozilla.org/en-US/docs/Web/API/Selection">Selection</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand">execCommand</a> APIs. When combined, they're supported in the following browsers.</p>
|
||||
|
||||
<ul class="support">
|
||||
<li>
|
||||
<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/chrome/chrome_128x128.png" width="64" height="64" alt="Chrome logo">
|
||||
<p>Chrome 42+</p>
|
||||
<li>
|
||||
<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/firefox/firefox_128x128.png" width="64" height="64" alt="Firefox logo">
|
||||
<p>Firefox 41+</p>
|
||||
</li>
|
||||
<li>
|
||||
<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/internet-explorer/internet-explorer_128x128.png" width="64" height="64" alt="Internet Explorer logo">
|
||||
<p>IE 9+</p>
|
||||
</li>
|
||||
<li>
|
||||
<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/opera/opera_128x128.png" width="64" height="64" alt="Opera logo">
|
||||
<p>Opera 29+</p>
|
||||
</li>
|
||||
<li>
|
||||
<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/safari/safari_128x128.png" width="64" height="64" alt="Safari logo">
|
||||
<p>Safari ✘</p>
|
||||
</li>
|
||||
</ul>
|
||||
</main>
|
||||
|
||||
<footer class="footer gradient text-center">
|
||||
<p class="credits">
|
||||
Made with <span class="love">♥</span> by <a class="credits-link" href="http://zenorocha.com/">Zeno Rocha</a> under <a class="credits-link" href="http://zenorocha.mit-license.org/">MIT license</a>
|
||||
</p>
|
||||
</footer>
|
||||
|
||||
<!-- Clipboard.js -->
|
||||
<script src="dist/clipboard.min.js"></script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
new Clipboard('.btn');
|
||||
});
|
||||
</script>
|
||||
|
||||
<!-- Highlight.js -->
|
||||
<script src="bower_components/highlightjs/highlight.pack.min.js"></script>
|
||||
<script>hljs.initHighlightingOnLoad();</script>
|
||||
|
||||
<!-- Google Analytics -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
|
||||
ga('create', 'UA-4114546-44', 'auto');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
25
karma.conf.js
Normal file
25
karma.conf.js
Normal file
@@ -0,0 +1,25 @@
|
||||
module.exports = function(karma) {
|
||||
karma.set({
|
||||
plugins: ['karma-browserify', 'karma-chai', 'karma-sinon', 'karma-mocha', 'karma-phantomjs-launcher'],
|
||||
|
||||
frameworks: ['browserify', 'chai', 'sinon', 'mocha'],
|
||||
|
||||
files: [
|
||||
'src/**/*.js',
|
||||
'test/**/*.js',
|
||||
'./node_modules/phantomjs-polyfill/bind-polyfill.js'
|
||||
],
|
||||
|
||||
preprocessors: {
|
||||
'src/**/*.js' : ['browserify'],
|
||||
'test/**/*.js': ['browserify']
|
||||
},
|
||||
|
||||
browserify: {
|
||||
debug: true,
|
||||
transform: ['babelify']
|
||||
},
|
||||
|
||||
browsers: ['PhantomJS']
|
||||
});
|
||||
}
|
||||
33
package.json
33
package.json
@@ -1,16 +1,35 @@
|
||||
{
|
||||
"name": "clipboard",
|
||||
"version": "0.0.0",
|
||||
"description": "A modern approach to copy to the clipboard",
|
||||
"version": "1.3.0",
|
||||
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
||||
"repository": "zenorocha/clipboard.js",
|
||||
"main": "src/clipboard.js",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"clipboard",
|
||||
"copy",
|
||||
"cut"
|
||||
],
|
||||
"dependencies": {
|
||||
"babel": "^5.8.23",
|
||||
"delegate-events": "^1.1.1",
|
||||
"tiny-emitter": "^1.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babelify": "^6.3.0",
|
||||
"browserify": "^11.1.0",
|
||||
"karma": "^0.13.10",
|
||||
"karma-browserify": "^4.3.0",
|
||||
"karma-chai": "^0.1.0",
|
||||
"karma-mocha": "^0.2.0",
|
||||
"karma-phantomjs-launcher": "^0.2.1",
|
||||
"karma-sinon": "^1.0.4",
|
||||
"phantomjs-polyfill": "0.0.1",
|
||||
"uglify": "^0.1.5"
|
||||
},
|
||||
"scripts": {
|
||||
"start" : "npm run build && npm run minify",
|
||||
"build" : "babel src/clipboard.js --out-file dist/clipboard.min.js",
|
||||
"watch" : "babel src/clipboard.js --out-file dist/clipboard.min.js --watch",
|
||||
"minify": "uglify -s dist/clipboard.min.js -o dist/clipboard.min.js"
|
||||
"publish": "npm run build && npm run minify",
|
||||
"build": "browserify src/clipboard.js -s Clipboard -t [babelify --loose all] -o dist/clipboard.js",
|
||||
"minify": "uglify -s dist/clipboard.js -o dist/clipboard.min.js",
|
||||
"test": "karma start --single-run"
|
||||
}
|
||||
}
|
||||
|
||||
184
src/clipboard-action.js
Normal file
184
src/clipboard-action.js
Normal file
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* Inner class which performs selection and copy operations.
|
||||
*/
|
||||
class ClipboardAction {
|
||||
/**
|
||||
* Initializes selection from either `text` or `target` property.
|
||||
* @param {Object} options
|
||||
*/
|
||||
constructor(options) {
|
||||
this.action = options.action;
|
||||
this.emitter = options.emitter;
|
||||
this.target = options.target;
|
||||
this.text = options.text;
|
||||
this.trigger = options.trigger;
|
||||
|
||||
this.selectedText = '';
|
||||
|
||||
if (this.text && this.target) {
|
||||
throw new Error('Multiple attributes declared, use either "target" or "text"');
|
||||
}
|
||||
else if (this.text) {
|
||||
this.selectFake();
|
||||
}
|
||||
else if (this.target) {
|
||||
this.selectTarget();
|
||||
}
|
||||
else {
|
||||
throw new Error('Missing required attributes, use either "target" or "text"');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a fake input element, sets its value from `text` property,
|
||||
* and makes a selection on it.
|
||||
*/
|
||||
selectFake() {
|
||||
this.removeFake();
|
||||
|
||||
this.fakeHandler = document.body.addEventListener('click', () => this.removeFake());
|
||||
|
||||
this.fakeElem = document.createElement('input');
|
||||
this.fakeElem.style.position = 'absolute';
|
||||
this.fakeElem.style.left = '-9999px';
|
||||
this.fakeElem.setAttribute('readonly', '');
|
||||
this.fakeElem.value = this.text;
|
||||
this.selectedText = this.text;
|
||||
|
||||
document.body.appendChild(this.fakeElem);
|
||||
|
||||
this.fakeElem.select();
|
||||
this.copyText();
|
||||
}
|
||||
|
||||
/**
|
||||
* Only removes the fake element after another click event, that way
|
||||
* an user can hit `Ctrl+C` to copy because selection still exists.
|
||||
*/
|
||||
removeFake() {
|
||||
if (this.fakeHandler) {
|
||||
document.body.removeEventListener('click');
|
||||
this.fakeHandler = null;
|
||||
}
|
||||
|
||||
if (this.fakeElem) {
|
||||
document.body.removeChild(this.fakeElem);
|
||||
this.fakeElem = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects the content from element passed on `target` property.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the copy operation based on the current selection.
|
||||
*/
|
||||
copyText() {
|
||||
let succeeded;
|
||||
|
||||
try {
|
||||
succeeded = document.execCommand(this.action);
|
||||
}
|
||||
catch (err) {
|
||||
succeeded = false;
|
||||
}
|
||||
|
||||
this.handleResult(succeeded);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fires an event based on the copy operation result.
|
||||
* @param {Boolean} succeeded
|
||||
*/
|
||||
handleResult(succeeded) {
|
||||
if (succeeded) {
|
||||
this.emitter.emit('success', {
|
||||
action: this.action,
|
||||
text: this.selectedText,
|
||||
trigger: this.trigger,
|
||||
clearSelection: this.clearSelection.bind(this)
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.emitter.emit('error', {
|
||||
action: this.action,
|
||||
trigger: this.trigger,
|
||||
clearSelection: this.clearSelection.bind(this)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
this._action = action || 'copy';
|
||||
|
||||
if (this._action !== 'copy' && this._action !== 'cut') {
|
||||
throw new Error('Invalid "action" value, use either "copy" or "cut"');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `action` property.
|
||||
* @return {String}
|
||||
*/
|
||||
get action() {
|
||||
return this._action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the `target` property using an element that will be have its content
|
||||
* copied.
|
||||
* @param {Element} target
|
||||
*/
|
||||
set target(target) {
|
||||
if (target !== undefined) {
|
||||
if (target && typeof target === 'object' && target.nodeType === 1) {
|
||||
this._target = target;
|
||||
}
|
||||
else {
|
||||
throw new Error('Invalid "target" value, use a valid Element');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the `target` property.
|
||||
* @return {String|HTMLElement}
|
||||
*/
|
||||
get target() {
|
||||
return this._target;
|
||||
}
|
||||
}
|
||||
|
||||
export default ClipboardAction;
|
||||
141
src/clipboard.js
141
src/clipboard.js
@@ -1,94 +1,85 @@
|
||||
class Clipboard {
|
||||
import ClipboardAction from './clipboard-action';
|
||||
import Delegate from 'delegate-events';
|
||||
import Emitter from 'tiny-emitter';
|
||||
|
||||
// Constructor
|
||||
const prefix = 'data-clipboard-';
|
||||
|
||||
constructor(triggers) {
|
||||
this._triggers = triggers;
|
||||
this.init();
|
||||
/**
|
||||
* Base class which takes a selector, delegates a click event to it,
|
||||
* and instantiates a new `ClipboardAction` on each click.
|
||||
*/
|
||||
class Clipboard extends Emitter {
|
||||
/**
|
||||
* Delegates a click event on the passed selector.
|
||||
* @param {String} selector
|
||||
* @param {Object} options
|
||||
*/
|
||||
constructor(selector, options) {
|
||||
super();
|
||||
|
||||
this.resolveOptions(options);
|
||||
|
||||
Delegate.bind(document.body, selector, 'click', (e) => this.initialize(e));
|
||||
}
|
||||
|
||||
// Getters & Setters
|
||||
/**
|
||||
* Defines if attributes would be resolved using an internal setter function
|
||||
* or a custom function that was passed in the constructor.
|
||||
* @param {Object} options
|
||||
*/
|
||||
resolveOptions(options) {
|
||||
options = options || {};
|
||||
|
||||
get triggers() {
|
||||
return document.querySelectorAll(this._triggers);
|
||||
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;
|
||||
}
|
||||
|
||||
set triggers(val) {
|
||||
return this._triggers = val;
|
||||
/**
|
||||
* Sets the `action` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
setAction(trigger) {
|
||||
return trigger.getAttribute(prefix + 'action');
|
||||
}
|
||||
|
||||
// Methods
|
||||
/**
|
||||
* Sets the `target` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
setTarget(trigger) {
|
||||
let target = trigger.getAttribute(prefix + 'target');
|
||||
|
||||
init() {
|
||||
if (this.triggers.length > 0) {
|
||||
[].forEach.call(this.triggers, (trigger) => this.bind(trigger));
|
||||
}
|
||||
else {
|
||||
throw new Error('The provided selector is empty');
|
||||
if (target) {
|
||||
return document.querySelector(target);
|
||||
}
|
||||
}
|
||||
|
||||
bind(trigger) {
|
||||
trigger.addEventListener('click', (e) => this.select(e));
|
||||
/**
|
||||
* Sets the `text` lookup function.
|
||||
* @param {Element} trigger
|
||||
*/
|
||||
setText(trigger) {
|
||||
return trigger.getAttribute(prefix + 'text');
|
||||
}
|
||||
|
||||
select(e) {
|
||||
let actionAttr = e.currentTarget.getAttribute('data-action') || 'copy';
|
||||
let targetAttr = e.currentTarget.getAttribute('data-target');
|
||||
let textAttr = e.currentTarget.getAttribute('data-text');
|
||||
|
||||
if (textAttr) {
|
||||
this.selectValue(textAttr, actionAttr);
|
||||
}
|
||||
else if (targetAttr) {
|
||||
this.selectTarget(targetAttr, actionAttr);
|
||||
}
|
||||
else {
|
||||
throw new Error('Missing "data-target" or "data-text" attribute');
|
||||
/**
|
||||
* Defines a new `ClipboardAction` on each click event.
|
||||
* @param {Event} e
|
||||
*/
|
||||
initialize(e) {
|
||||
if (this.clipboardAction) {
|
||||
this.clipboardAction = null;
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
}
|
||||
|
||||
selectValue(textAttr, actionAttr) {
|
||||
let fake = document.createElement('input');
|
||||
|
||||
fake.value = textAttr;
|
||||
fake.style.opacity = 0;
|
||||
fake.style.zIndex = -1;
|
||||
|
||||
document.body.appendChild(fake);
|
||||
|
||||
fake.select();
|
||||
this.copy(actionAttr);
|
||||
|
||||
document.body.removeChild(fake);
|
||||
}
|
||||
|
||||
selectTarget(targetAttr, actionAttr) {
|
||||
let target = document.getElementById(targetAttr);
|
||||
|
||||
if (target.nodeName === 'INPUT' || target.nodeName === 'TEXTAREA') {
|
||||
target.select();
|
||||
}
|
||||
else {
|
||||
let range = document.createRange();
|
||||
range.selectNode(target);
|
||||
window.getSelection().addRange(range);
|
||||
}
|
||||
|
||||
this.copy(actionAttr);
|
||||
}
|
||||
|
||||
copy(actionAttr) {
|
||||
try {
|
||||
let successful = document.execCommand(actionAttr);
|
||||
if (!successful) throw 'Invalid "data-action" attribute';
|
||||
|
||||
window.getSelection().removeAllRanges();
|
||||
}
|
||||
catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
this.clipboardAction = new ClipboardAction({
|
||||
action : this.action(e.delegateTarget),
|
||||
target : this.target(e.delegateTarget),
|
||||
text : this.text(e.delegateTarget),
|
||||
trigger : e.delegateTarget,
|
||||
emitter : this
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default Clipboard;
|
||||
|
||||
215
test/clipboard-action.js
Normal file
215
test/clipboard-action.js
Normal file
@@ -0,0 +1,215 @@
|
||||
import Clipboard from '../src/clipboard-action';
|
||||
import ClipboardAction from '../src/clipboard-action';
|
||||
import Emitter from 'tiny-emitter';
|
||||
|
||||
describe('ClipboardAction', () => {
|
||||
before(() => {
|
||||
global.input = document.createElement('input');
|
||||
global.input.setAttribute('id', 'input');
|
||||
global.input.setAttribute('value', 'abc');
|
||||
document.body.appendChild(global.input);
|
||||
|
||||
global.paragraph = document.createElement('p');
|
||||
global.paragraph.setAttribute('id', 'paragraph');
|
||||
global.paragraph.textContent = 'abc';
|
||||
document.body.appendChild(global.paragraph);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
describe('#constructor', () => {
|
||||
it('should throw an error since both "text" and "target" were passed', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
text: 'foo',
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Multiple attributes declared, use either "target" or "text"');
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error since neither "text" nor "target" were passed', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
action: ''
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Missing required attributes, use either "target" or "text"');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#set action', () => {
|
||||
it('should throw an error since "action" is invalid', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
text: 'foo',
|
||||
action: 'paste'
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Invalid "action" value, use either "copy" or "cut"');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#set target', () => {
|
||||
it('should throw an error since "target" do not match any element', done => {
|
||||
try {
|
||||
new ClipboardAction({
|
||||
target: document.querySelector('#foo')
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Invalid "target" value, use a valid Element');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#selectText', () => {
|
||||
it('should create a fake element and select its value', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
text: 'blah'
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.fakeElem.value);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#removeFake', () => {
|
||||
it('should remove a temporary fake element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
text: 'blah'
|
||||
});
|
||||
|
||||
clip.removeFake();
|
||||
|
||||
assert.equal(clip.fakeElem, null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#selectTarget', () => {
|
||||
it('should select text from editable element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.target.value);
|
||||
});
|
||||
|
||||
it('should select text from non-editable element', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: document.querySelector('#paragraph')
|
||||
});
|
||||
|
||||
assert.equal(clip.selectedText, clip.target.textContent);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#copyText', () => {
|
||||
before(() => {
|
||||
global.stub = sinon.stub(document, 'execCommand');
|
||||
});
|
||||
|
||||
after(() => {
|
||||
global.stub.restore();
|
||||
});
|
||||
|
||||
it('should fire a success event on browsers that support copy command', done => {
|
||||
global.stub.returns(true);
|
||||
|
||||
let emitter = new Emitter()
|
||||
|
||||
emitter.on('success', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
let clip = new ClipboardAction({
|
||||
emitter: emitter,
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
});
|
||||
|
||||
it('should fire an error event on browsers that support copy command', done => {
|
||||
global.stub.returns(false);
|
||||
|
||||
let emitter = new Emitter()
|
||||
|
||||
emitter.on('error', () => {
|
||||
done();
|
||||
});
|
||||
|
||||
let clip = new ClipboardAction({
|
||||
emitter: emitter,
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#handleResult', () => {
|
||||
it('should fire a success event with certain properties', done => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
clip.emitter.on('success', (e) => {
|
||||
assert.property(e, 'action');
|
||||
assert.property(e, 'text');
|
||||
assert.property(e, 'trigger');
|
||||
assert.property(e, 'clearSelection');
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
clip.handleResult(true);
|
||||
});
|
||||
|
||||
it('should fire a error event with certain properties', done => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
clip.emitter.on('error', (e) => {
|
||||
assert.property(e, 'action');
|
||||
assert.property(e, 'trigger');
|
||||
assert.property(e, 'clearSelection');
|
||||
|
||||
done();
|
||||
});
|
||||
|
||||
clip.handleResult(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#clearSelection', () => {
|
||||
it('should remove focus from target and text selection', () => {
|
||||
let clip = new ClipboardAction({
|
||||
emitter: new Emitter(),
|
||||
target: document.querySelector('#input')
|
||||
});
|
||||
|
||||
clip.clearSelection();
|
||||
|
||||
let selectedElem = document.activeElement;
|
||||
let selectedText = window.getSelection().toString();
|
||||
|
||||
assert.equal(selectedElem, document.body);
|
||||
assert.equal(selectedText, '');
|
||||
});
|
||||
});
|
||||
});
|
||||
69
test/clipboard.js
Normal file
69
test/clipboard.js
Normal file
@@ -0,0 +1,69 @@
|
||||
import Clipboard from '../src/clipboard';
|
||||
import ClipboardAction from '../src/clipboard-action';
|
||||
|
||||
describe('Clipboard', () => {
|
||||
before(() => {
|
||||
global.button = document.createElement('button');
|
||||
global.button.setAttribute('class', 'btn');
|
||||
global.button.setAttribute('data-clipboard-text', 'foo');
|
||||
document.body.appendChild(global.button);
|
||||
|
||||
global.event = {
|
||||
delegateTarget: global.button
|
||||
};
|
||||
});
|
||||
|
||||
after(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
describe('#resolveOptions', function() {
|
||||
it('should set action as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
action: fn
|
||||
});
|
||||
assert.equal(fn, clipboard.action);
|
||||
});
|
||||
|
||||
it('should set target as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
target: fn
|
||||
});
|
||||
assert.equal(fn, clipboard.target);
|
||||
});
|
||||
|
||||
it('should set text as a function', () => {
|
||||
var fn = function() {};
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
text: fn
|
||||
});
|
||||
assert.equal(fn, clipboard.text);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#initialize', () => {
|
||||
it('should create a new instance of ClipboardAction', () => {
|
||||
let clipboard = new Clipboard('.btn');
|
||||
|
||||
clipboard.initialize(global.event);
|
||||
assert.instanceOf(clipboard.clipboardAction, ClipboardAction);
|
||||
});
|
||||
|
||||
it('should throws exception target', done => {
|
||||
try {
|
||||
var clipboard = new Clipboard('.btn', {
|
||||
target: function() {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
clipboard.initialize(global.event);
|
||||
}
|
||||
catch(e) {
|
||||
assert.equal(e.message, 'Invalid "target" value, use a valid Element');
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user