mirror of
https://github.com/zenorocha/clipboard.js.git
synced 2023-08-10 21:12:48 +03:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0a0de9fc01 | ||
|
|
db575bb4df | ||
|
|
bb6c4c0e7c | ||
|
|
03ee9829e0 | ||
|
|
24f6e1f541 | ||
|
|
5bdfff6375 | ||
|
|
4b73122f81 | ||
|
|
171f438f22 | ||
|
|
42a459402c | ||
|
|
b26cdb3b41 | ||
|
|
57c7fcf9a4 | ||
|
|
6b1f6b22a6 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
|
lib
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
bower_components
|
bower_components
|
||||||
node_modules
|
node_modules
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/.*/
|
/.*/
|
||||||
/example/
|
/demo/
|
||||||
/test/
|
/test/
|
||||||
/.*
|
/.*
|
||||||
/bower.json
|
/bower.json
|
||||||
/karma.conf.js
|
/karma.conf.js
|
||||||
|
/src
|
||||||
|
|||||||
@@ -1,16 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "clipboard",
|
"name": "clipboard",
|
||||||
"version": "1.4.3",
|
"version": "1.5.3",
|
||||||
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "dist/clipboard.js",
|
"main": "dist/clipboard.js",
|
||||||
"ignore": [
|
"ignore": [
|
||||||
"/.*/",
|
"/.*/",
|
||||||
"/example/",
|
"/demo/",
|
||||||
"/test/",
|
"/test/",
|
||||||
"/.*",
|
"/.*",
|
||||||
"/bower.json",
|
"/bower.json",
|
||||||
"/karma.conf.js"
|
"/karma.conf.js",
|
||||||
|
"/src",
|
||||||
|
"/lib"
|
||||||
],
|
],
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"clipboard",
|
"clipboard",
|
||||||
|
|||||||
28
demo/constructor-node.html
Normal file
28
demo/constructor-node.html
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>constructor-node</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<button id="btn" data-clipboard-text="1">Copy</button>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard by passing a HTML element -->
|
||||||
|
<script>
|
||||||
|
var btn = document.getElementById('btn');
|
||||||
|
var clipboard = new Clipboard(btn);
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
30
demo/constructor-nodelist.html
Normal file
30
demo/constructor-nodelist.html
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>constructor-nodelist</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<button data-clipboard-text="1">Copy</button>
|
||||||
|
<button data-clipboard-text="2">Copy</button>
|
||||||
|
<button data-clipboard-text="3">Copy</button>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard by passing a list of HTML elements -->
|
||||||
|
<script>
|
||||||
|
var btns = document.querySelectorAll('button');
|
||||||
|
var clipboard = new Clipboard(btns);
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
29
demo/constructor-selector.html
Normal file
29
demo/constructor-selector.html
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>constructor-selector</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<button class="btn" data-clipboard-text="1">Copy</button>
|
||||||
|
<button class="btn" data-clipboard-text="2">Copy</button>
|
||||||
|
<button class="btn" data-clipboard-text="3">Copy</button>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard by passing a string selector -->
|
||||||
|
<script>
|
||||||
|
var clipboard = new Clipboard('.btn');
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
32
demo/function-target.html
Normal file
32
demo/function-target.html
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>function-target</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<button class="btn">Copy</button>
|
||||||
|
<div>hello</div>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard -->
|
||||||
|
<script>
|
||||||
|
var clipboard = new Clipboard('.btn', {
|
||||||
|
target: function() {
|
||||||
|
return document.querySelector('div');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
31
demo/function-text.html
Normal file
31
demo/function-text.html
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>function-text</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<button class="btn">Copy</button>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard -->
|
||||||
|
<script>
|
||||||
|
var clipboard = new Clipboard('.btn', {
|
||||||
|
text: function() {
|
||||||
|
return 'to be or not to be';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
28
demo/target-div.html
Normal file
28
demo/target-div.html
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>target-div</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<div>hello</div>
|
||||||
|
<button class="btn" data-clipboard-action="copy" data-clipboard-target="div">Copy</button>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard -->
|
||||||
|
<script>
|
||||||
|
var clipboard = new Clipboard('.btn');
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
28
demo/target-input.html
Normal file
28
demo/target-input.html
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>target-input</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<input id="foo" type="text" value="hello">
|
||||||
|
<button class="btn" data-clipboard-action="copy" data-clipboard-target="#foo">Copy</button>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard -->
|
||||||
|
<script>
|
||||||
|
var clipboard = new Clipboard('.btn');
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
28
demo/target-textarea.html
Normal file
28
demo/target-textarea.html
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>target-textarea</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<!-- 1. Define some markup -->
|
||||||
|
<textarea id="bar">hello</textarea>
|
||||||
|
<button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">Cut</button>
|
||||||
|
|
||||||
|
<!-- 2. Include library -->
|
||||||
|
<script src="../dist/clipboard.min.js"></script>
|
||||||
|
|
||||||
|
<!-- 3. Instantiate clipboard -->
|
||||||
|
<script>
|
||||||
|
var clipboard = new Clipboard('.btn');
|
||||||
|
|
||||||
|
clipboard.on('success', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
|
||||||
|
clipboard.on('error', function(e) {
|
||||||
|
console.log(e);
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
269
dist/clipboard.js
vendored
269
dist/clipboard.js
vendored
@@ -1,5 +1,5 @@
|
|||||||
/*!
|
/*!
|
||||||
* clipboard.js v1.4.3
|
* clipboard.js v1.5.3
|
||||||
* https://zenorocha.github.io/clipboard.js
|
* https://zenorocha.github.io/clipboard.js
|
||||||
*
|
*
|
||||||
* Licensed MIT © Zeno Rocha
|
* Licensed MIT © Zeno Rocha
|
||||||
@@ -61,61 +61,225 @@ function match(el, selector) {
|
|||||||
var closest = require('closest');
|
var closest = require('closest');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegate event `type` to `selector`
|
* Delegates event to a selector.
|
||||||
* and invoke `fn(e)`. A callback function
|
|
||||||
* is returned which may be passed to `.unbind()`.
|
|
||||||
*
|
*
|
||||||
* @param {Element} el
|
* @param {Element} element
|
||||||
* @param {String} selector
|
* @param {String} selector
|
||||||
* @param {String} type
|
* @param {String} type
|
||||||
* @param {Function} fn
|
* @param {Function} callback
|
||||||
* @param {Boolean} capture
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
function delegate(element, selector, type, callback) {
|
||||||
|
var listenerFn = listener.apply(this, arguments);
|
||||||
|
|
||||||
|
element.addEventListener(type, listenerFn);
|
||||||
|
|
||||||
|
return {
|
||||||
|
destroy: function() {
|
||||||
|
element.removeEventListener(type, listenerFn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds closest match and invokes callback.
|
||||||
|
*
|
||||||
|
* @param {Element} element
|
||||||
|
* @param {String} selector
|
||||||
|
* @param {String} type
|
||||||
|
* @param {Function} callback
|
||||||
* @return {Function}
|
* @return {Function}
|
||||||
*/
|
*/
|
||||||
|
function listener(element, selector, type, callback) {
|
||||||
|
return function(e) {
|
||||||
|
var delegateTarget = closest(e.target, selector, true);
|
||||||
|
|
||||||
exports.bind = function(el, selector, type, fn, capture){
|
if (delegateTarget) {
|
||||||
return el.addEventListener(type, function(e){
|
Object.defineProperty(e, 'target', {
|
||||||
var target = e.target || e.srcElement;
|
value: delegateTarget
|
||||||
e.delegateTarget = closest(target, selector, true, el);
|
});
|
||||||
if (e.delegateTarget) fn.call(el, e);
|
|
||||||
}, capture);
|
callback.call(element, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = delegate;
|
||||||
|
|
||||||
|
},{"closest":1}],4:[function(require,module,exports){
|
||||||
|
/**
|
||||||
|
* Check if argument is a HTML element.
|
||||||
|
*
|
||||||
|
* @param {Object} value
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
exports.node = function(value) {
|
||||||
|
return value !== undefined
|
||||||
|
&& value instanceof HTMLElement
|
||||||
|
&& value.nodeType === 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unbind event `type`'s callback `fn`.
|
* Check if argument is a list of HTML elements.
|
||||||
*
|
*
|
||||||
* @param {Element} el
|
* @param {Object} value
|
||||||
* @param {String} type
|
* @return {Boolean}
|
||||||
* @param {Function} fn
|
|
||||||
* @param {Boolean} capture
|
|
||||||
*/
|
*/
|
||||||
|
exports.nodeList = function(value) {
|
||||||
|
var type = Object.prototype.toString.call(value);
|
||||||
|
|
||||||
exports.unbind = function(el, type, fn, capture){
|
return value !== undefined
|
||||||
el.removeEventListener(type, fn, capture);
|
&& (type === '[object NodeList]' || type === '[object HTMLCollection]')
|
||||||
|
&& ('length' in value)
|
||||||
|
&& (value.length === 0 || exports.node(value[0]));
|
||||||
};
|
};
|
||||||
|
|
||||||
},{"closest":1}],4:[function(require,module,exports){
|
/**
|
||||||
function select(element) {
|
* Check if argument is a string.
|
||||||
var selection = window.getSelection();
|
*
|
||||||
|
* @param {Object} value
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
exports.string = function(value) {
|
||||||
|
return typeof value === 'string'
|
||||||
|
|| value instanceof String;
|
||||||
|
};
|
||||||
|
|
||||||
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
|
/**
|
||||||
element.selectionStart = 0;
|
* Check if argument is a function.
|
||||||
element.selectionEnd = element.value.length;
|
*
|
||||||
|
* @param {Object} value
|
||||||
|
* @return {Boolean}
|
||||||
|
*/
|
||||||
|
exports.function = function(value) {
|
||||||
|
var type = Object.prototype.toString.call(value);
|
||||||
|
|
||||||
|
return type === '[object Function]';
|
||||||
|
};
|
||||||
|
|
||||||
|
},{}],5:[function(require,module,exports){
|
||||||
|
var is = require('./is');
|
||||||
|
var delegate = require('delegate');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates all params and calls the right
|
||||||
|
* listener function based on its target type.
|
||||||
|
*
|
||||||
|
* @param {String|HTMLElement|HTMLCollection|NodeList} target
|
||||||
|
* @param {String} type
|
||||||
|
* @param {Function} callback
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
function listen(target, type, callback) {
|
||||||
|
if (!target && !type && !callback) {
|
||||||
|
throw new Error('Missing required arguments');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is.string(type)) {
|
||||||
|
throw new TypeError('Second argument must be a String');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is.function(callback)) {
|
||||||
|
throw new TypeError('Third argument must be a Function');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is.node(target)) {
|
||||||
|
return listenNode(target, type, callback);
|
||||||
|
}
|
||||||
|
else if (is.nodeList(target)) {
|
||||||
|
return listenNodeList(target, type, callback);
|
||||||
|
}
|
||||||
|
else if (is.string(target)) {
|
||||||
|
return listenSelector(target, type, callback);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
throw new TypeError('First argument must be a String, HTMLElement, HTMLCollection, or NodeList');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an event listener to a HTML element
|
||||||
|
* and returns a remove listener function.
|
||||||
|
*
|
||||||
|
* @param {HTMLElement} node
|
||||||
|
* @param {String} type
|
||||||
|
* @param {Function} callback
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
function listenNode(node, type, callback) {
|
||||||
|
node.addEventListener(type, callback);
|
||||||
|
|
||||||
|
return {
|
||||||
|
destroy: function() {
|
||||||
|
node.removeEventListener(type, callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an event listener to a list of HTML elements
|
||||||
|
* and returns a remove listener function.
|
||||||
|
*
|
||||||
|
* @param {NodeList|HTMLCollection} nodeList
|
||||||
|
* @param {String} type
|
||||||
|
* @param {Function} callback
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
function listenNodeList(nodeList, type, callback) {
|
||||||
|
Array.prototype.forEach.call(nodeList, function(node) {
|
||||||
|
node.addEventListener(type, callback);
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
destroy: function() {
|
||||||
|
Array.prototype.forEach.call(nodeList, function(node) {
|
||||||
|
node.removeEventListener(type, callback);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an event listener to a selector
|
||||||
|
* and returns a remove listener function.
|
||||||
|
*
|
||||||
|
* @param {String} selector
|
||||||
|
* @param {String} type
|
||||||
|
* @param {Function} callback
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
function listenSelector(selector, type, callback) {
|
||||||
|
return delegate(document.body, selector, type, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = listen;
|
||||||
|
|
||||||
|
},{"./is":4,"delegate":3}],6:[function(require,module,exports){
|
||||||
|
function select(element) {
|
||||||
|
var selectedText;
|
||||||
|
|
||||||
|
if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
|
||||||
|
element.select();
|
||||||
|
|
||||||
|
selectedText = element.value;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var selection = window.getSelection();
|
||||||
var range = document.createRange();
|
var range = document.createRange();
|
||||||
|
|
||||||
range.selectNodeContents(element);
|
range.selectNodeContents(element);
|
||||||
selection.removeAllRanges();
|
selection.removeAllRanges();
|
||||||
selection.addRange(range);
|
selection.addRange(range);
|
||||||
|
|
||||||
|
selectedText = selection.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return selection.toString();
|
return selectedText;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = select;
|
module.exports = select;
|
||||||
|
|
||||||
},{}],5:[function(require,module,exports){
|
},{}],7:[function(require,module,exports){
|
||||||
function E () {
|
function E () {
|
||||||
// Keep this empty so it's easier to inherit from
|
// Keep this empty so it's easier to inherit from
|
||||||
// (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
|
// (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
|
||||||
@@ -183,7 +347,7 @@ E.prototype = {
|
|||||||
|
|
||||||
module.exports = E;
|
module.exports = E;
|
||||||
|
|
||||||
},{}],6:[function(require,module,exports){
|
},{}],8:[function(require,module,exports){
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
exports.__esModule = true;
|
exports.__esModule = true;
|
||||||
@@ -417,7 +581,7 @@ var ClipboardAction = (function () {
|
|||||||
exports['default'] = ClipboardAction;
|
exports['default'] = ClipboardAction;
|
||||||
module.exports = exports['default'];
|
module.exports = exports['default'];
|
||||||
|
|
||||||
},{"select":4}],7:[function(require,module,exports){
|
},{"select":6}],9:[function(require,module,exports){
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
exports.__esModule = true;
|
exports.__esModule = true;
|
||||||
@@ -432,16 +596,16 @@ var _clipboardAction = require('./clipboard-action');
|
|||||||
|
|
||||||
var _clipboardAction2 = _interopRequireDefault(_clipboardAction);
|
var _clipboardAction2 = _interopRequireDefault(_clipboardAction);
|
||||||
|
|
||||||
var _delegate = require('delegate');
|
|
||||||
|
|
||||||
var _delegate2 = _interopRequireDefault(_delegate);
|
|
||||||
|
|
||||||
var _tinyEmitter = require('tiny-emitter');
|
var _tinyEmitter = require('tiny-emitter');
|
||||||
|
|
||||||
var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
|
var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
|
||||||
|
|
||||||
|
var _goodListener = require('good-listener');
|
||||||
|
|
||||||
|
var _goodListener2 = _interopRequireDefault(_goodListener);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class which takes a selector, delegates a click event to it,
|
* Base class which takes one or more elements, adds event listeners to them,
|
||||||
* and instantiates a new `ClipboardAction` on each click.
|
* and instantiates a new `ClipboardAction` on each click.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -449,17 +613,17 @@ var Clipboard = (function (_Emitter) {
|
|||||||
_inherits(Clipboard, _Emitter);
|
_inherits(Clipboard, _Emitter);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {String} selector
|
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Clipboard(selector, options) {
|
function Clipboard(trigger, options) {
|
||||||
_classCallCheck(this, Clipboard);
|
_classCallCheck(this, Clipboard);
|
||||||
|
|
||||||
_Emitter.call(this);
|
_Emitter.call(this);
|
||||||
|
|
||||||
this.resolveOptions(options);
|
this.resolveOptions(options);
|
||||||
this.delegateClick(selector);
|
this.listenClick(trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -483,27 +647,18 @@ var Clipboard = (function (_Emitter) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegates a click event on the passed selector.
|
* Adds a click event listener to the passed trigger.
|
||||||
* @param {String} selector
|
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Clipboard.prototype.delegateClick = function delegateClick(selector) {
|
Clipboard.prototype.listenClick = function listenClick(trigger) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
|
||||||
this.binding = _delegate2['default'].bind(document.body, selector, 'click', function (e) {
|
this.listener = _goodListener2['default'](trigger, 'click', function (e) {
|
||||||
return _this.onClick(e);
|
return _this.onClick(e);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Undelegates a click event on body.
|
|
||||||
* @param {String} selector
|
|
||||||
*/
|
|
||||||
|
|
||||||
Clipboard.prototype.undelegateClick = function undelegateClick() {
|
|
||||||
_delegate2['default'].unbind(document.body, 'click', this.binding);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a new `ClipboardAction` on each click event.
|
* Defines a new `ClipboardAction` on each click event.
|
||||||
* @param {Event} e
|
* @param {Event} e
|
||||||
@@ -515,10 +670,10 @@ var Clipboard = (function (_Emitter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.clipboardAction = new _clipboardAction2['default']({
|
this.clipboardAction = new _clipboardAction2['default']({
|
||||||
action: this.action(e.delegateTarget),
|
action: this.action(e.target),
|
||||||
target: this.target(e.delegateTarget),
|
target: this.target(e.target),
|
||||||
text: this.text(e.delegateTarget),
|
text: this.text(e.target),
|
||||||
trigger: e.delegateTarget,
|
trigger: e.target,
|
||||||
emitter: this
|
emitter: this
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -559,7 +714,7 @@ var Clipboard = (function (_Emitter) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Clipboard.prototype.destroy = function destroy() {
|
Clipboard.prototype.destroy = function destroy() {
|
||||||
this.undelegateClick();
|
this.listener.destroy();
|
||||||
|
|
||||||
if (this.clipboardAction) {
|
if (this.clipboardAction) {
|
||||||
this.clipboardAction.destroy();
|
this.clipboardAction.destroy();
|
||||||
@@ -583,5 +738,5 @@ function getAttributeValue(suffix, element) {
|
|||||||
exports['default'] = Clipboard;
|
exports['default'] = Clipboard;
|
||||||
module.exports = exports['default'];
|
module.exports = exports['default'];
|
||||||
|
|
||||||
},{"./clipboard-action":6,"delegate":3,"tiny-emitter":5}]},{},[7])(7)
|
},{"./clipboard-action":8,"good-listener":5,"tiny-emitter":7}]},{},[9])(9)
|
||||||
});
|
});
|
||||||
4
dist/clipboard.min.js
vendored
4
dist/clipboard.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -3,7 +3,7 @@
|
|||||||
Package.describe({
|
Package.describe({
|
||||||
name: "zenorocha:clipboard",
|
name: "zenorocha:clipboard",
|
||||||
summary: "Modern copy to clipboard. No Flash. Just 2kb.",
|
summary: "Modern copy to clipboard. No Flash. Just 2kb.",
|
||||||
version: "1.4.2",
|
version: "1.5.1",
|
||||||
git: "https://github.com/zenorocha/clipboard.js"
|
git: "https://github.com/zenorocha/clipboard.js"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
33
package.json
33
package.json
@@ -1,34 +1,24 @@
|
|||||||
{
|
{
|
||||||
"name": "clipboard",
|
"name": "clipboard",
|
||||||
"version": "1.4.3",
|
"version": "1.5.3",
|
||||||
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
"description": "Modern copy to clipboard. No Flash. Just 2kb",
|
||||||
"repository": "zenorocha/clipboard.js",
|
"repository": "zenorocha/clipboard.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"main": "dist/clipboard.js",
|
"main": "lib/clipboard.js",
|
||||||
"browser": "src/clipboard.js",
|
|
||||||
"browserify": {
|
|
||||||
"transform": [
|
|
||||||
[
|
|
||||||
"babelify",
|
|
||||||
{
|
|
||||||
"loose": "all"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"clipboard",
|
"clipboard",
|
||||||
"copy",
|
"copy",
|
||||||
"cut"
|
"cut"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babelify": "^6.3.0",
|
"good-listener": "^1.1.2",
|
||||||
"browserify": "^11.2.0",
|
"select": "^1.0.4",
|
||||||
"delegate": "^1.0.0",
|
|
||||||
"select": "^1.0.0",
|
|
||||||
"tiny-emitter": "^1.0.0"
|
"tiny-emitter": "^1.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"babel": "^5.8.29",
|
||||||
|
"babelify": "^6.3.0",
|
||||||
|
"browserify": "^11.2.0",
|
||||||
"karma": "^0.13.10",
|
"karma": "^0.13.10",
|
||||||
"karma-browserify": "^4.4.0",
|
"karma-browserify": "^4.4.0",
|
||||||
"karma-chai": "^0.1.0",
|
"karma-chai": "^0.1.0",
|
||||||
@@ -43,11 +33,10 @@
|
|||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "npm run build-debug && npm run build-min",
|
"build": "npm run build-debug && npm run build-min",
|
||||||
"build-debug": "browserify src/clipboard.js -s Clipboard -p [bannerify --file .banner ] -o dist/clipboard.js",
|
"build-debug": "browserify src/clipboard.js -s Clipboard -t [babelify --loose all] -p [bannerify --file .banner ] -o dist/clipboard.js",
|
||||||
"build-min": "uglifyjs dist/clipboard.js --comments '/!/' -m screw_ie8=true -c screw_ie8=true,unused=false -o dist/clipboard.min.js",
|
"build-min": "uglifyjs dist/clipboard.js --comments '/!/' -m screw_ie8=true -c screw_ie8=true,unused=false -o dist/clipboard.min.js",
|
||||||
"build-watch": "watchify src/clipboard.js -s Clipboard -o dist/clipboard.js -v",
|
"build-watch": "watchify src/clipboard.js -s Clipboard -t [babelify --loose all] -o dist/clipboard.js -v",
|
||||||
"test": "npm run test-browser && npm run test-server",
|
"test": "karma start --single-run",
|
||||||
"test-browser": "karma start --single-run",
|
"prepublish": "babel src --out-dir lib --loose all"
|
||||||
"test-server": "mocha test/module-systems.js"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
10
readme.md
10
readme.md
@@ -31,19 +31,13 @@ If you're not into package management, just [download a ZIP](https://github.com/
|
|||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
First, include the script located on the `dist` folder.
|
First, include the script located on the `dist` folder or load it from [a third-party CDN provider](https://github.com/zenorocha/clipboard.js/wiki/CDN-Providers).
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<script src="dist/clipboard.min.js"></script>
|
<script src="dist/clipboard.min.js"></script>
|
||||||
```
|
```
|
||||||
|
|
||||||
Or load it from a CDN.
|
Now, you need to instantiate it by [passing a DOM selector](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-selector.html#L18), [HTML element](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-node.html#L16-L17), or [list of HTML elements](https://github.com/zenorocha/clipboard.js/blob/master/demo/constructor-nodelist.html#L18-L19).
|
||||||
|
|
||||||
```html
|
|
||||||
<script src="https://cdn.rawgit.com/zenorocha/clipboard.js/master/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
|
```js
|
||||||
new Clipboard('.btn');
|
new Clipboard('.btn');
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
import ClipboardAction from './clipboard-action';
|
import ClipboardAction from './clipboard-action';
|
||||||
import Delegate from 'delegate';
|
|
||||||
import Emitter from 'tiny-emitter';
|
import Emitter from 'tiny-emitter';
|
||||||
|
import listen from 'good-listener';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class which takes a selector, delegates a click event to it,
|
* Base class which takes one or more elements, adds event listeners to them,
|
||||||
* and instantiates a new `ClipboardAction` on each click.
|
* and instantiates a new `ClipboardAction` on each click.
|
||||||
*/
|
*/
|
||||||
class Clipboard extends Emitter {
|
class Clipboard extends Emitter {
|
||||||
/**
|
/**
|
||||||
* @param {String} selector
|
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
||||||
* @param {Object} options
|
* @param {Object} options
|
||||||
*/
|
*/
|
||||||
constructor(selector, options) {
|
constructor(trigger, options) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.resolveOptions(options);
|
this.resolveOptions(options);
|
||||||
this.delegateClick(selector);
|
this.listenClick(trigger);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,19 +30,11 @@ class Clipboard extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegates a click event on the passed selector.
|
* Adds a click event listener to the passed trigger.
|
||||||
* @param {String} selector
|
* @param {String|HTMLElement|HTMLCollection|NodeList} trigger
|
||||||
*/
|
*/
|
||||||
delegateClick(selector) {
|
listenClick(trigger) {
|
||||||
this.binding = Delegate.bind(document.body, selector, 'click', (e) => this.onClick(e));
|
this.listener = listen(trigger, 'click', (e) => this.onClick(e));
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Undelegates a click event on body.
|
|
||||||
* @param {String} selector
|
|
||||||
*/
|
|
||||||
undelegateClick() {
|
|
||||||
Delegate.unbind(document.body, 'click', this.binding);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -55,10 +47,10 @@ class Clipboard extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.clipboardAction = new ClipboardAction({
|
this.clipboardAction = new ClipboardAction({
|
||||||
action : this.action(e.delegateTarget),
|
action : this.action(e.target),
|
||||||
target : this.target(e.delegateTarget),
|
target : this.target(e.target),
|
||||||
text : this.text(e.delegateTarget),
|
text : this.text(e.target),
|
||||||
trigger : e.delegateTarget,
|
trigger : e.target,
|
||||||
emitter : this
|
emitter : this
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -95,7 +87,7 @@ class Clipboard extends Emitter {
|
|||||||
* Destroy lifecycle.
|
* Destroy lifecycle.
|
||||||
*/
|
*/
|
||||||
destroy() {
|
destroy() {
|
||||||
this.undelegateClick();
|
this.listener.destroy();
|
||||||
|
|
||||||
if (this.clipboardAction) {
|
if (this.clipboardAction) {
|
||||||
this.clipboardAction.destroy();
|
this.clipboardAction.destroy();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import Clipboard from '../src/clipboard';
|
import Clipboard from '../src/clipboard';
|
||||||
import ClipboardAction from '../src/clipboard-action';
|
import ClipboardAction from '../src/clipboard-action';
|
||||||
import Delegate from 'delegate';
|
import listen from 'good-listener';
|
||||||
|
|
||||||
describe('Clipboard', () => {
|
describe('Clipboard', () => {
|
||||||
before(() => {
|
before(() => {
|
||||||
@@ -10,7 +10,7 @@ describe('Clipboard', () => {
|
|||||||
document.body.appendChild(global.button);
|
document.body.appendChild(global.button);
|
||||||
|
|
||||||
global.event = {
|
global.event = {
|
||||||
delegateTarget: global.button
|
target: global.button
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -48,45 +48,10 @@ describe('Clipboard', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#delegateClick', () => {
|
describe('#listenClick', () => {
|
||||||
before(() => {
|
it('should add a click event listener to the passed selector', () => {
|
||||||
global.spy = sinon.spy(Delegate, 'bind');
|
|
||||||
});
|
|
||||||
|
|
||||||
after(() => {
|
|
||||||
global.spy.restore();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should delegate a click event to the passed selector', () => {
|
|
||||||
let element = document.body;
|
|
||||||
let selector = '.btn';
|
|
||||||
let event = 'click';
|
|
||||||
|
|
||||||
let clipboard = new Clipboard(selector);
|
|
||||||
|
|
||||||
assert.ok(global.spy.calledOnce);
|
|
||||||
assert.ok(global.spy.calledWith(element, selector, event));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#undelegateClick', () => {
|
|
||||||
before(() => {
|
|
||||||
global.spy = sinon.spy(Delegate, 'unbind');
|
|
||||||
});
|
|
||||||
|
|
||||||
after(() => {
|
|
||||||
global.spy.restore();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should undelegate a click event', () => {
|
|
||||||
let element = document.body;
|
|
||||||
let event = 'click';
|
|
||||||
|
|
||||||
let clipboard = new Clipboard('.btn');
|
let clipboard = new Clipboard('.btn');
|
||||||
clipboard.undelegateClick();
|
assert.isObject(clipboard.listener);
|
||||||
|
|
||||||
assert.ok(global.spy.calledOnce);
|
|
||||||
assert.ok(global.spy.calledWith(element, event));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -105,6 +70,7 @@ describe('Clipboard', () => {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
clipboard.onClick(global.event);
|
clipboard.onClick(global.event);
|
||||||
}
|
}
|
||||||
catch(e) {
|
catch(e) {
|
||||||
|
|||||||
@@ -1,15 +0,0 @@
|
|||||||
var assert = require('chai').assert;
|
|
||||||
var browserify = require('browserify');
|
|
||||||
|
|
||||||
describe('CommonJS', function() {
|
|
||||||
it('should import the lib in a commonjs env without babel', function(done) {
|
|
||||||
browserify('./dist/clipboard.js').bundle(function(err) {
|
|
||||||
assert.equal(err, null);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('AMD', function() {
|
|
||||||
// TODO: Write test case
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user