vendor/assets/javascripts/clipboard.js in clipboard-rails-1.3.1.1 vs vendor/assets/javascripts/clipboard.js in clipboard-rails-1.4.0
- old
+ new
@@ -49,61 +49,61 @@
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
- }
-}
+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;
+
+/**
+ * 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' : '';
@@ -139,16 +139,16 @@
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
+ // 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) {
+ on: function (name, callback, ctx) {
var e = this.e || (this.e = {});
(e[name] || (e[name] = [])).push({
fn: callback,
ctx: ctx
@@ -263,11 +263,11 @@
throw new Error('Missing required attributes, use either "target" or "text"');
}
};
/**
- * Creates a fake input element, sets its value from `text` property,
+ * Creates a fake textarea element, sets its value from `text` property,
* and makes a selection on it.
*/
ClipboardAction.prototype.selectFake = function selectFake() {
var _this = this;
@@ -276,13 +276,14 @@
this.fakeHandler = document.body.addEventListener('click', function () {
return _this.removeFake();
});
- this.fakeElem = document.createElement('input');
+ this.fakeElem = document.createElement('textarea');
this.fakeElem.style.position = 'absolute';
this.fakeElem.style.left = '-9999px';
+ this.fakeElem.style.top = document.body.scrollTop + 'px';
this.fakeElem.setAttribute('readonly', '');
this.fakeElem.value = this.text;
this.selectedText = this.text;
document.body.appendChild(this.fakeElem);
@@ -381,10 +382,18 @@
/**
* Sets the `action` to be performed which can be either 'copy' or 'cut'.
* @param {String} action
*/
+ /**
+ * Destroy lifecycle.
+ */
+
+ ClipboardAction.prototype.destroy = function destroy() {
+ this.removeFake();
+ };
+
_createClass(ClipboardAction, [{
key: 'action',
set: function set() {
var action = arguments.length <= 0 || arguments[0] === undefined ? 'copy' : arguments[0];
@@ -456,12 +465,10 @@
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.
*/
@@ -481,42 +488,57 @@
this.resolveOptions(options);
this.delegateClick(selector);
}
/**
+ * Helper function to retrieve attribute value.
+ * @param {String} suffix
+ * @param {Element} element
+ */
+
+ /**
* 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() {
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;
+ this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
+ this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
+ this.text = typeof options.text === 'function' ? options.text : this.defaultText;
};
/**
* 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);
+ this.binding = _delegateEvents2['default'].bind(document.body, selector, 'click', function (e) {
+ return _this.onClick(e);
});
};
/**
+ * Undelegates a click event on body.
+ * @param {String} selector
+ */
+
+ Clipboard.prototype.undelegateClick = function undelegateClick() {
+ _delegateEvents2['default'].unbind(document.body, 'click', this.binding);
+ };
+
+ /**
* Defines a new `ClipboardAction` on each click event.
* @param {Event} e
*/
- Clipboard.prototype.initialize = function initialize(e) {
+ Clipboard.prototype.onClick = function onClick(e) {
if (this.clipboardAction) {
this.clipboardAction = null;
}
this.clipboardAction = new _clipboardAction2['default']({
@@ -527,50 +549,64 @@
emitter: this
});
};
/**
- * Sets the `action` lookup function.
+ * Default `action` lookup function.
* @param {Element} trigger
*/
- Clipboard.prototype.setAction = function setAction(trigger) {
- if (!trigger.hasAttribute(prefix + 'action')) {
- return;
- }
-
- return trigger.getAttribute(prefix + 'action');
+ Clipboard.prototype.defaultAction = function defaultAction(trigger) {
+ return getAttributeValue('action', trigger);
};
/**
- * Sets the `target` lookup function.
+ * Default `target` lookup function.
* @param {Element} trigger
*/
- Clipboard.prototype.setTarget = function setTarget(trigger) {
- if (!trigger.hasAttribute(prefix + 'target')) {
- return;
- }
+ Clipboard.prototype.defaultTarget = function defaultTarget(trigger) {
+ var selector = getAttributeValue('target', trigger);
- var target = trigger.getAttribute(prefix + 'target');
- return document.querySelector(target);
+ if (selector) {
+ return document.querySelector(selector);
+ }
};
/**
- * Sets the `text` lookup function.
+ * Default `text` lookup function.
* @param {Element} trigger
*/
- Clipboard.prototype.setText = function setText(trigger) {
- if (!trigger.hasAttribute(prefix + 'text')) {
- return;
- }
+ Clipboard.prototype.defaultText = function defaultText(trigger) {
+ return getAttributeValue('text', trigger);
+ };
- 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;
})(_tinyEmitter2['default']);
+
+function getAttributeValue(suffix, element) {
+ var attribute = 'data-clipboard-' + suffix;
+
+ if (!element.hasAttribute(attribute)) {
+ return;
+ }
+
+ return element.getAttribute(attribute);
+}
exports['default'] = Clipboard;
module.exports = exports['default'];
},{"./clipboard-action":6,"delegate-events":1,"tiny-emitter":5}]},{},[7])(7)
\ No newline at end of file