Release v1.4.0

This commit is contained in:
Zeno Rocha
2015-10-03 19:17:42 -07:00
parent 8e56ee61c4
commit 019b021624
4 changed files with 119 additions and 98 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "clipboard", "name": "clipboard",
"version": "1.3.1", "version": "1.4.0",
"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",

211
dist/clipboard.js vendored
View File

@@ -51,57 +51,57 @@ exports.unbind = function(el, type, fn, capture){
}; };
},{"closest":2,"component-event":4}],2:[function(require,module,exports){ },{"closest":2,"component-event":4}],2:[function(require,module,exports){
var matches = require('matches-selector') var matches = require('matches-selector')
module.exports = function (element, selector, checkYoSelf) { module.exports = function (element, selector, checkYoSelf) {
var parent = checkYoSelf ? element : element.parentNode var parent = checkYoSelf ? element : element.parentNode
while (parent && parent !== document) { while (parent && parent !== document) {
if (matches(parent, selector)) return parent; if (matches(parent, selector)) return parent;
parent = parent.parentNode parent = parent.parentNode
} }
} }
},{"matches-selector":3}],3:[function(require,module,exports){ },{"matches-selector":3}],3:[function(require,module,exports){
/** /**
* Element prototype. * Element prototype.
*/ */
var proto = Element.prototype; var proto = Element.prototype;
/** /**
* Vendor function. * Vendor function.
*/ */
var vendor = proto.matchesSelector var vendor = proto.matchesSelector
|| proto.webkitMatchesSelector || proto.webkitMatchesSelector
|| proto.mozMatchesSelector || proto.mozMatchesSelector
|| proto.msMatchesSelector || proto.msMatchesSelector
|| proto.oMatchesSelector; || proto.oMatchesSelector;
/** /**
* Expose `match()`. * Expose `match()`.
*/ */
module.exports = match; module.exports = match;
/** /**
* Match `el` to `selector`. * Match `el` to `selector`.
* *
* @param {Element} el * @param {Element} el
* @param {String} selector * @param {String} selector
* @return {Boolean} * @return {Boolean}
* @api public * @api public
*/ */
function match(el, selector) { function match(el, selector) {
if (vendor) return vendor.call(el, selector); if (vendor) return vendor.call(el, selector);
var nodes = el.parentNode.querySelectorAll(selector); var nodes = el.parentNode.querySelectorAll(selector);
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
if (nodes[i] == el) return true; if (nodes[i] == el) return true;
} }
return false; return false;
} }
},{}],4:[function(require,module,exports){ },{}],4:[function(require,module,exports){
var bind = window.addEventListener ? 'addEventListener' : 'attachEvent', var bind = window.addEventListener ? 'addEventListener' : 'attachEvent',
@@ -148,12 +148,12 @@ function E () {
E.prototype = { E.prototype = {
on: function (name, callback, ctx) { on: function (name, callback, ctx) {
var e = this.e || (this.e = {}); var e = this.e || (this.e = {});
(e[name] || (e[name] = [])).push({ (e[name] || (e[name] = [])).push({
fn: callback, fn: callback,
ctx: ctx ctx: ctx
}); });
return this; return this;
}, },
@@ -163,7 +163,7 @@ E.prototype = {
self.off(name, fn); self.off(name, fn);
callback.apply(ctx, arguments); callback.apply(ctx, arguments);
}; };
return this.on(name, fn, ctx); return this.on(name, fn, ctx);
}, },
@@ -172,11 +172,11 @@ E.prototype = {
var evtArr = ((this.e || (this.e = {}))[name] || []).slice(); var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
var i = 0; var i = 0;
var len = evtArr.length; var len = evtArr.length;
for (i; i < len; i++) { for (i; i < len; i++) {
evtArr[i].fn.apply(evtArr[i].ctx, data); evtArr[i].fn.apply(evtArr[i].ctx, data);
} }
return this; return this;
}, },
@@ -184,21 +184,21 @@ E.prototype = {
var e = this.e || (this.e = {}); var e = this.e || (this.e = {});
var evts = e[name]; var evts = e[name];
var liveEvents = []; var liveEvents = [];
if (evts && callback) { if (evts && callback) {
for (var i = 0, len = evts.length; i < len; i++) { for (var i = 0, len = evts.length; i < len; i++) {
if (evts[i].fn !== callback) liveEvents.push(evts[i]); if (evts[i].fn !== callback) liveEvents.push(evts[i]);
} }
} }
// Remove event from queue to prevent memory leak // Remove event from queue to prevent memory leak
// Suggested by https://github.com/lazd // Suggested by https://github.com/lazd
// Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910
(liveEvents.length) (liveEvents.length)
? e[name] = liveEvents ? e[name] = liveEvents
: delete e[name]; : delete e[name];
return this; return this;
} }
}; };
@@ -281,6 +281,7 @@ var ClipboardAction = (function () {
this.fakeElem = document.createElement('textarea'); this.fakeElem = document.createElement('textarea');
this.fakeElem.style.position = 'absolute'; this.fakeElem.style.position = 'absolute';
this.fakeElem.style.left = '-9999px'; this.fakeElem.style.left = '-9999px';
this.fakeElem.style.top = document.body.scrollTop + 'px';
this.fakeElem.setAttribute('readonly', ''); this.fakeElem.setAttribute('readonly', '');
this.fakeElem.value = this.text; this.fakeElem.value = this.text;
this.selectedText = this.text; this.selectedText = this.text;
@@ -383,6 +384,14 @@ var ClipboardAction = (function () {
* @param {String} action * @param {String} action
*/ */
/**
* Destroy lifecycle.
*/
ClipboardAction.prototype.destroy = function destroy() {
this.removeFake();
};
_createClass(ClipboardAction, [{ _createClass(ClipboardAction, [{
key: 'action', key: 'action',
set: function set() { set: function set() {
@@ -458,8 +467,6 @@ var _tinyEmitter = require('tiny-emitter');
var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter); var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
var prefix = 'data-clipboard-';
/** /**
* Base class which takes a selector, delegates a click event to it, * Base class which takes a selector, delegates a click event to it,
* and instantiates a new `ClipboardAction` on each click. * and instantiates a new `ClipboardAction` on each click.
@@ -477,21 +484,17 @@ var Clipboard = (function (_Emitter) {
_classCallCheck(this, Clipboard); _classCallCheck(this, Clipboard);
_Emitter.call(this); _Emitter.call(this);
this.initialize = this.initialize.bind(this); // pre-bind for consistent reference on remove
this.resolveOptions(options); this.resolveOptions(options);
this.delegateClick(selector); this.delegateClick(selector);
} }
/** /**
* @param {String} selector * Helper function to retrieve attribute value.
* @param {Object} options * @param {String} suffix
* @param {Element} element
*/ */
Clipboard.prototype.destroy = function destroy() {
this.undelegateClick();
};
/** /**
* Defines if attributes would be resolved using internal setter functions * Defines if attributes would be resolved using internal setter functions
* or custom functions that were passed in the constructor. * or custom functions that were passed in the constructor.
@@ -501,9 +504,9 @@ var Clipboard = (function (_Emitter) {
Clipboard.prototype.resolveOptions = function resolveOptions() { Clipboard.prototype.resolveOptions = function resolveOptions() {
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
this.action = typeof options.action === 'function' ? options.action : this.setAction; this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
this.target = typeof options.target === 'function' ? options.target : this.setTarget; this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
this.text = typeof options.text === 'function' ? options.text : this.setText; this.text = typeof options.text === 'function' ? options.text : this.defaultText;
}; };
/** /**
@@ -512,16 +515,20 @@ var Clipboard = (function (_Emitter) {
*/ */
Clipboard.prototype.delegateClick = function delegateClick(selector) { Clipboard.prototype.delegateClick = function delegateClick(selector) {
_delegateEvents2['default'].bind(document.body, selector, 'click', this.initialize); var _this = this;
this.binding = _delegateEvents2['default'].bind(document.body, selector, 'click', function (e) {
return _this.onClick(e);
});
}; };
/** /**
* Undelegates a click event on the passed selector. * Undelegates a click event on body.
* @param {String} selector * @param {String} selector
*/ */
Clipboard.prototype.undelegateClick = function undelegateClick() { Clipboard.prototype.undelegateClick = function undelegateClick() {
_delegateEvents2['default'].unbind(document.body, 'click', this.initialize); _delegateEvents2['default'].unbind(document.body, 'click', this.binding);
}; };
/** /**
@@ -529,7 +536,7 @@ var Clipboard = (function (_Emitter) {
* @param {Event} e * @param {Event} e
*/ */
Clipboard.prototype.initialize = function initialize(e) { Clipboard.prototype.onClick = function onClick(e) {
if (this.clipboardAction) { if (this.clipboardAction) {
this.clipboardAction = null; this.clipboardAction = null;
} }
@@ -544,50 +551,64 @@ var Clipboard = (function (_Emitter) {
}; };
/** /**
* Sets the `action` lookup function. * Default `action` lookup function.
* @param {Element} trigger * @param {Element} trigger
*/ */
Clipboard.prototype.setAction = function setAction(trigger) { Clipboard.prototype.defaultAction = function defaultAction(trigger) {
if (!trigger.hasAttribute(prefix + 'action')) { return getAttributeValue('action', trigger);
return;
}
return trigger.getAttribute(prefix + 'action');
}; };
/** /**
* Sets the `target` lookup function. * Default `target` lookup function.
* @param {Element} trigger * @param {Element} trigger
*/ */
Clipboard.prototype.setTarget = function setTarget(trigger) { Clipboard.prototype.defaultTarget = function defaultTarget(trigger) {
if (!trigger.hasAttribute(prefix + 'target')) { var selector = getAttributeValue('target', trigger);
return;
}
var target = trigger.getAttribute(prefix + 'target'); if (selector) {
return document.querySelector(target); return document.querySelector(selector);
}
}; };
/** /**
* Sets the `text` lookup function. * Default `text` lookup function.
* @param {Element} trigger * @param {Element} trigger
*/ */
Clipboard.prototype.setText = function setText(trigger) { Clipboard.prototype.defaultText = function defaultText(trigger) {
if (!trigger.hasAttribute(prefix + 'text')) { return getAttributeValue('text', trigger);
return; };
}
return trigger.getAttribute(prefix + 'text'); /**
* Destroy lifecycle.
*/
Clipboard.prototype.destroy = function destroy() {
this.undelegateClick();
if (this.clipboardAction) {
this.clipboardAction.destroy();
this.clipboardAction = null;
}
}; };
return Clipboard; return Clipboard;
})(_tinyEmitter2['default']); })(_tinyEmitter2['default']);
function getAttributeValue(suffix, element) {
var attribute = 'data-clipboard-' + suffix;
if (!element.hasAttribute(attribute)) {
return;
}
return element.getAttribute(attribute);
}
exports['default'] = Clipboard; exports['default'] = Clipboard;
module.exports = exports['default']; module.exports = exports['default'];
},{"./clipboard-action":6,"delegate-events":1,"tiny-emitter":5}]},{},[7])(7) },{"./clipboard-action":6,"delegate-events":1,"tiny-emitter":5}]},{},[7])(7)
}); });

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,6 @@
{ {
"name": "clipboard", "name": "clipboard",
"version": "1.3.1", "version": "1.4.0",
"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",