Compare commits

..

1 Commits

Author SHA1 Message Date
Zeno Rocha
7758d2fcfe Release v1.1.0 2015-09-28 10:30:56 -07:00
8 changed files with 75 additions and 159 deletions

View File

@@ -54,7 +54,7 @@ We're living a _declarative renaissance_, that's why we decided to take advantag
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.
The value you include on this attribute needs to match another's element `id` attribute.
<a href="http://zenorocha.github.io/clipboard.js/#demo-target"><img width="473" alt="example-2" src="https://cloud.githubusercontent.com/assets/398893/9983467/a4946aaa-5fb1-11e5-9780-f09fcd7ca6c8.png"></a>
@@ -63,7 +63,7 @@ The value you include on this attribute needs to match another's element selecto
<input id="foo" value="https://github.com/zenorocha/clipboard.js.git">
<!-- Trigger -->
<button class="btn" data-clipboard-target="#foo">
<button class="btn" data-clipboard-target="foo">
<img src="assets/clippy.svg" alt="Copy to clipboard">
</button>
```
@@ -81,7 +81,7 @@ If you omit this attribute, `copy` will be used by default.
<textarea id="bar">Mussum ipsum cacilds...</textarea>
<!-- Trigger -->
<button class="btn" data-clipboard-action="cut" data-clipboard-target="#bar">
<button class="btn" data-clipboard-action="cut" data-clipboard-target="bar">
Cut to clipboard
</button>
```
@@ -101,30 +101,6 @@ Truth is, you don't even need another element to copy its content from. You can
</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.

View File

@@ -1,6 +1,6 @@
{
"name": "clipboard",
"version": "1.2.0",
"version": "1.1.0",
"description": "Modern copy to clipboard. No Flash. Just 2kb",
"license": "MIT",
"main": "src/clipboard.js",

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
{
"name": "clipboard",
"version": "1.2.0",
"version": "1.1.0",
"description": "Modern copy to clipboard. No Flash. Just 2kb",
"repository": "zenorocha/clipboard.js",
"main": "src/clipboard.js",

View File

@@ -16,7 +16,7 @@ class ClipboardAction {
this.selectedText = '';
if (this.text && this.target) {
throw new Error('Multiple attributes declared, use either "target" or "text"');
throw new Error('Multiple attributes declared, use either "data-clipboard-target" or "data-clipboard-text"');
}
else if (this.text) {
this.selectFake();
@@ -25,7 +25,7 @@ class ClipboardAction {
this.selectTarget();
}
else {
throw new Error('Missing required attributes, use either "target" or "text"');
throw new Error('Missing required attributes, use either "data-clipboard-target" or "data-clipboard-text"');
}
}
@@ -144,7 +144,7 @@ class ClipboardAction {
this._action = action || 'copy';
if (this._action !== 'copy' && this._action !== 'cut') {
throw new Error('Invalid "action" value, use either "copy" or "cut"');
throw new Error('Invalid "data-clipboard-action" value, use either "copy" or "cut"');
}
}
@@ -157,17 +157,16 @@ class ClipboardAction {
}
/**
* Sets the `target` property using an element that will be have its content
* copied.
* @param {Element} target
* Sets the `target` property using the ID of an element
* that will be have its content copied.
* @param {String} 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');
if (target) {
this._target = document.getElementById(target);
if (!this._target) {
throw new Error('Invalid "data-clipboard-target" selector, use a value that matches an ID');
}
}
}

View File

@@ -2,8 +2,6 @@ import ClipboardAction from './clipboard-action';
import Delegate from 'delegate-events';
import Emitter from 'tiny-emitter';
const prefix = 'data-clipboard-';
/**
* Base class which takes a selector, delegates a click event to it,
* and instantiates a new `ClipboardAction` on each click.
@@ -12,57 +10,17 @@ class Clipboard extends Emitter {
/**
* Delegates a click event on the passed selector.
* @param {String} selector
* @param {Object} options
*/
constructor(selector, options) {
constructor(selector) {
super();
this.resolveOptions(options);
if (!document.querySelectorAll(selector).length) {
throw new Error('No matches were found for the provided selector');
}
Delegate.bind(document.body, selector, 'click', (e) => 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
*/
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
*/
setAction(trigger) {
return trigger.getAttribute(prefix + 'action');
}
/**
* Sets the `target` lookup function.
* @param {Element} trigger
*/
setTarget(trigger) {
let target = trigger.getAttribute(prefix + 'target');
if (target) {
return document.querySelector(target);
}
}
/**
* Sets the `text` lookup function.
* @param {Element} trigger
*/
setText(trigger) {
return trigger.getAttribute(prefix + 'text');
}
/**
* Defines a new `ClipboardAction` on each click event.
* @param {Event} e
@@ -72,10 +30,12 @@ class Clipboard extends Emitter {
this.clipboardAction = null;
}
const prefix = 'data-clipboard-';
this.clipboardAction = new ClipboardAction({
action : this.action(e.delegateTarget),
target : this.target(e.delegateTarget),
text : this.text(e.delegateTarget),
action : e.delegateTarget.getAttribute(prefix + 'action'),
target : e.delegateTarget.getAttribute(prefix + 'target'),
text : e.delegateTarget.getAttribute(prefix + 'text'),
trigger : e.delegateTarget,
emitter : this
});

View File

@@ -20,34 +20,34 @@ describe('ClipboardAction', () => {
});
describe('#constructor', () => {
it('should throw an error since both "text" and "target" were passed', done => {
it('should throw an error since both "data-clipboard-text" and "data-clipboard-target" were passed', done => {
try {
new ClipboardAction({
text: 'foo',
target: document.querySelector('#input')
target: 'input'
});
}
catch(e) {
assert.equal(e.message, 'Multiple attributes declared, use either "target" or "text"');
assert.equal(e.message, 'Multiple attributes declared, use either "data-clipboard-target" or "data-clipboard-text"');
done();
}
});
it('should throw an error since neither "text" nor "target" were passed', done => {
it('should throw an error since neither "data-clipboard-text" nor "data-clipboard-target" were passed', done => {
try {
new ClipboardAction({
action: ''
});
}
catch(e) {
assert.equal(e.message, 'Missing required attributes, use either "target" or "text"');
assert.equal(e.message, 'Missing required attributes, use either "data-clipboard-target" or "data-clipboard-text"');
done();
}
});
});
describe('#set action', () => {
it('should throw an error since "action" is invalid', done => {
it('should throw an error since "data-clipboard-action" is invalid', done => {
try {
new ClipboardAction({
text: 'foo',
@@ -55,21 +55,21 @@ describe('ClipboardAction', () => {
});
}
catch(e) {
assert.equal(e.message, 'Invalid "action" value, use either "copy" or "cut"');
assert.equal(e.message, 'Invalid "data-clipboard-action" value, use either "copy" or "cut"');
done();
}
});
});
describe('#set target', () => {
it('should throw an error since "target" do not match any element', done => {
it('should throw an error since "data-clipboard-target" do not match any element', done => {
try {
new ClipboardAction({
target: document.querySelector('#foo')
target: 'foo'
});
}
catch(e) {
assert.equal(e.message, 'Invalid "target" value, use a valid Element');
assert.equal(e.message, 'Invalid "data-clipboard-target" selector, use a value that matches an ID');
done();
}
});
@@ -103,7 +103,7 @@ describe('ClipboardAction', () => {
it('should select text from editable element', () => {
let clip = new ClipboardAction({
emitter: new Emitter(),
target: document.querySelector('#input')
target: 'input'
});
assert.equal(clip.selectedText, clip.target.value);
@@ -112,7 +112,7 @@ describe('ClipboardAction', () => {
it('should select text from non-editable element', () => {
let clip = new ClipboardAction({
emitter: new Emitter(),
target: document.querySelector('#paragraph')
target: 'paragraph'
});
assert.equal(clip.selectedText, clip.target.textContent);
@@ -139,7 +139,7 @@ describe('ClipboardAction', () => {
let clip = new ClipboardAction({
emitter: emitter,
target: document.querySelector('#input')
target: 'input'
});
});
@@ -154,7 +154,7 @@ describe('ClipboardAction', () => {
let clip = new ClipboardAction({
emitter: emitter,
target: document.querySelector('#input')
target: 'input'
});
});
});
@@ -163,7 +163,7 @@ describe('ClipboardAction', () => {
it('should fire a success event with certain properties', done => {
let clip = new ClipboardAction({
emitter: new Emitter(),
target: document.querySelector('#input')
target: 'input'
});
clip.emitter.on('success', (e) => {
@@ -181,7 +181,7 @@ describe('ClipboardAction', () => {
it('should fire a error event with certain properties', done => {
let clip = new ClipboardAction({
emitter: new Emitter(),
target: document.querySelector('#input')
target: 'input'
});
clip.emitter.on('error', (e) => {
@@ -200,7 +200,7 @@ describe('ClipboardAction', () => {
it('should remove focus from target and text selection', () => {
let clip = new ClipboardAction({
emitter: new Emitter(),
target: document.querySelector('#input')
target: 'input'
});
clip.clearSelection();

View File

@@ -2,68 +2,49 @@ 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);
describe('#constructor', () => {
it('should throw an error since there was no arguments passed', done => {
try {
new Clipboard();
}
catch(e) {
assert.equal(e.message, 'No matches were found for the provided selector');
done();
}
});
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);
it('should throw an error since an empty selector has been passed', done => {
try {
new Clipboard('#abc');
}
catch(e) {
assert.equal(e.message, 'No matches were found for the provided selector');
done();
}
});
});
describe('#initialize', () => {
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 = '';
});
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();
}
});
});
});