Skip to content

Commit

Permalink
Release v1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zenorocha committed Sep 30, 2015
1 parent 3c414a6 commit 1539bba
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 43 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clipboard",
"version": "1.3.0",
"version": "1.3.1",
"description": "Modern copy to clipboard. No Flash. Just 2kb",
"license": "MIT",
"main": "src/clipboard.js",
Expand Down
117 changes: 77 additions & 40 deletions dist/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ module.exports = E;

},{}],6:[function(require,module,exports){
/**
* Inner class which performs selection and copy operations.
* Inner class which performs selection from either `text` or `target`
* properties and then executes copy or cut operations.
*/
'use strict';

Expand All @@ -219,21 +220,39 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons

var ClipboardAction = (function () {
/**
* Initializes selection from either `text` or `target` property.
* @param {Object} options
*/

function ClipboardAction(options) {
_classCallCheck(this, ClipboardAction);

this.resolveOptions(options);
this.initSelection();
}

/**
* Defines base properties passed from constructor.
* @param {Object} options
*/

ClipboardAction.prototype.resolveOptions = function resolveOptions() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

this.action = options.action;
this.emitter = options.emitter;
this.target = options.target;
this.text = options.text;
this.trigger = options.trigger;

this.selectedText = '';
};

/**
* Decides which selection strategy is going to be applied based
* on the existence of `text` and `target` properties.
*/

ClipboardAction.prototype.initSelection = function initSelection() {
if (this.text && this.target) {
throw new Error('Multiple attributes declared, use either "target" or "text"');
} else if (this.text) {
Expand All @@ -243,7 +262,7 @@ var ClipboardAction = (function () {
} else {
throw new Error('Missing required attributes, use either "target" or "text"');
}
}
};

/**
* Creates a fake input element, sets its value from `text` property,
Expand Down Expand Up @@ -366,8 +385,10 @@ var ClipboardAction = (function () {

_createClass(ClipboardAction, [{
key: 'action',
set: function set(action) {
this._action = action || 'copy';
set: function set() {
var action = arguments.length <= 0 || arguments[0] === undefined ? 'copy' : arguments[0];

this._action = action;

if (this._action !== 'copy' && this._action !== 'cut') {
throw new Error('Invalid "action" value, use either "copy" or "cut"');
Expand All @@ -383,8 +404,8 @@ var ClipboardAction = (function () {
}

/**
* Sets the `target` property using an element that will be have its content
* copied.
* Sets the `target` property using an element
* that will be have its content copied.
* @param {Element} target
*/
}, {
Expand Down Expand Up @@ -448,45 +469,75 @@ 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);
});
this.delegateClick(selector);
}

/**
* Defines if attributes would be resolved using an internal setter function
* or a custom function that was passed in the constructor.
* Defines if attributes would be resolved using internal setter functions
* or custom functions that were passed in the constructor.
* @param {Object} options
*/

Clipboard.prototype.resolveOptions = function resolveOptions(options) {
options = options || {};
Clipboard.prototype.resolveOptions = function resolveOptions() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];

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;
};

/**
* Delegates a click event on the passed selector.
* @param {String} selector
*/

Clipboard.prototype.delegateClick = function delegateClick(selector) {
var _this = this;

_delegateEvents2['default'].bind(document.body, selector, 'click', function (e) {
return _this.initialize(e);
});
};

/**
* 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
});
};

/**
* Sets the `action` lookup function.
* @param {Element} trigger
*/

Clipboard.prototype.setAction = function setAction(trigger) {
if (!trigger.hasAttribute(prefix + 'action')) {
return;
}

return trigger.getAttribute(prefix + 'action');
};

Expand All @@ -496,11 +547,12 @@ var Clipboard = (function (_Emitter) {
*/

Clipboard.prototype.setTarget = function setTarget(trigger) {
var target = trigger.getAttribute(prefix + 'target');

if (target) {
return document.querySelector(target);
if (!trigger.hasAttribute(prefix + 'target')) {
return;
}

var target = trigger.getAttribute(prefix + 'target');
return document.querySelector(target);
};

/**
Expand All @@ -509,26 +561,11 @@ var Clipboard = (function (_Emitter) {
*/

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;
if (!trigger.hasAttribute(prefix + 'text')) {
return;
}

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 trigger.getAttribute(prefix + 'text');
};

return Clipboard;
Expand Down
Loading

0 comments on commit 1539bba

Please sign in to comment.