clipboard.min.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. !function (a) {
  2. if ("object" == typeof exports && "undefined" != typeof module)module.exports = a(); else if ("function" == typeof define && define.amd)define([], a); else {
  3. var b;
  4. b = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : this, b.Clipboard = a()
  5. }
  6. }(function () {
  7. return function a(b, c, d) {
  8. function e(g, h) {
  9. if (!c[g]) {
  10. if (!b[g]) {
  11. var i = "function" == typeof require && require;
  12. if (!h && i)return i(g, !0);
  13. if (f)return f(g, !0);
  14. var j = new Error("Cannot find module '" + g + "'");
  15. throw j.code = "MODULE_NOT_FOUND", j
  16. }
  17. var k = c[g] = {exports: {}};
  18. b[g][0].call(k.exports, function (a) {
  19. var c = b[g][1][a];
  20. return e(c ? c : a)
  21. }, k, k.exports, a, b, c, d)
  22. }
  23. return c[g].exports
  24. }
  25. for (var f = "function" == typeof require && require, g = 0; g < d.length; g++)e(d[g]);
  26. return e
  27. }({
  28. 1: [function (a, b, c) {
  29. var d = a("closest"), e = a("component-event"), f = ["focus", "blur"];
  30. c.bind = function (a, b, c, g, h) {
  31. return -1 !== f.indexOf(c) && (h = !0), e.bind(a, c, function (c) {
  32. var e = c.target || c.srcElement;
  33. c.delegateTarget = d(e, b, !0, a), c.delegateTarget && g.call(a, c)
  34. }, h)
  35. }, c.unbind = function (a, b, c, d) {
  36. -1 !== f.indexOf(b) && (d = !0), e.unbind(a, b, c, d)
  37. }
  38. }, {closest: 2, "component-event": 4}], 2: [function (a, b, c) {
  39. var d = a("matches-selector");
  40. b.exports = function (a, b, c) {
  41. for (var e = c ? a : a.parentNode; e && e !== document;) {
  42. if (d(e, b))return e;
  43. e = e.parentNode
  44. }
  45. }
  46. }, {"matches-selector": 3}], 3: [function (a, b, c) {
  47. function d(a, b) {
  48. if (f)return f.call(a, b);
  49. for (var c = a.parentNode.querySelectorAll(b), d = 0; d < c.length; ++d)if (c[d] == a)return !0;
  50. return !1
  51. }
  52. var e = Element.prototype, f = e.matchesSelector || e.webkitMatchesSelector || e.mozMatchesSelector || e.msMatchesSelector || e.oMatchesSelector;
  53. b.exports = d
  54. }, {}], 4: [function (a, b, c) {
  55. var d = window.addEventListener ? "addEventListener" : "attachEvent", e = window.removeEventListener ? "removeEventListener" : "detachEvent", f = "addEventListener" !== d ? "on" : "";
  56. c.bind = function (a, b, c, e) {
  57. return a[d](f + b, c, e || !1), c
  58. }, c.unbind = function (a, b, c, d) {
  59. return a[e](f + b, c, d || !1), c
  60. }
  61. }, {}], 5: [function (a, b, c) {
  62. function d() {
  63. }
  64. d.prototype = {
  65. on: function (a, b, c) {
  66. var d = this.e || (this.e = {});
  67. return (d[a] || (d[a] = [])).push({fn: b, ctx: c}), this
  68. }, once: function (a, b, c) {
  69. var d = this, e = function () {
  70. d.off(a, e), b.apply(c, arguments)
  71. };
  72. return this.on(a, e, c)
  73. }, emit: function (a) {
  74. var b = [].slice.call(arguments, 1), c = ((this.e || (this.e = {}))[a] || []).slice(), d = 0, e = c.length;
  75. for (d; e > d; d++)c[d].fn.apply(c[d].ctx, b);
  76. return this
  77. }, off: function (a, b) {
  78. var c = this.e || (this.e = {}), d = c[a], e = [];
  79. if (d && b)for (var f = 0, g = d.length; g > f; f++)d[f].fn !== b && e.push(d[f]);
  80. return e.length ? c[a] = e : delete c[a], this
  81. }
  82. }, b.exports = d
  83. }, {}], 6: [function (a, b, c) {
  84. "use strict";
  85. function d(a, b) {
  86. if (!(a instanceof b))throw new TypeError("Cannot call a class as a function")
  87. }
  88. c.__esModule = !0;
  89. var e = function () {
  90. function a(a, b) {
  91. for (var c = 0; c < b.length; c++) {
  92. var d = b[c];
  93. d.enumerable = d.enumerable || !1, d.configurable = !0, "value" in d && (d.writable = !0), Object.defineProperty(a, d.key, d)
  94. }
  95. }
  96. return function (b, c, d) {
  97. return c && a(b.prototype, c), d && a(b, d), b
  98. }
  99. }(), f = function () {
  100. function a(b) {
  101. d(this, a), this.resolveOptions(b), this.initSelection()
  102. }
  103. return a.prototype.resolveOptions = function () {
  104. var a = arguments.length <= 0 || void 0 === arguments[0] ? {} : arguments[0];
  105. this.action = a.action, this.emitter = a.emitter, this.target = a.target, this.text = a.text, this.trigger = a.trigger, this.selectedText = ""
  106. }, a.prototype.initSelection = function () {
  107. if (this.text && this.target)throw new Error('Multiple attributes declared, use either "target" or "text"');
  108. if (this.text)this.selectFake(); else {
  109. if (!this.target)throw new Error('Missing required attributes, use either "target" or "text"');
  110. this.selectTarget()
  111. }
  112. }, a.prototype.selectFake = function () {
  113. var a = this;
  114. this.removeFake(), this.fakeHandler = document.body.addEventListener("click", function () {
  115. return a.removeFake()
  116. }), this.fakeElem = document.createElement("input"), this.fakeElem.style.position = "absolute", this.fakeElem.style.left = "-9999px", this.fakeElem.setAttribute("readonly", ""), this.fakeElem.value = this.text, this.selectedText = this.text, document.body.appendChild(this.fakeElem), this.fakeElem.select(), this.copyText()
  117. }, a.prototype.removeFake = function () {
  118. this.fakeHandler && (document.body.removeEventListener("click"), this.fakeHandler = null), this.fakeElem && (document.body.removeChild(this.fakeElem), this.fakeElem = null)
  119. }, a.prototype.selectTarget = function () {
  120. if ("INPUT" === this.target.nodeName || "TEXTAREA" === this.target.nodeName)this.target.select(), this.selectedText = this.target.value; else {
  121. var a = document.createRange(), b = window.getSelection();
  122. a.selectNodeContents(this.target), b.addRange(a), this.selectedText = b.toString()
  123. }
  124. this.copyText()
  125. }, a.prototype.copyText = function () {
  126. var a = void 0;
  127. try {
  128. a = document.execCommand(this.action)
  129. } catch (b) {
  130. a = !1
  131. }
  132. this.handleResult(a)
  133. }, a.prototype.handleResult = function (a) {
  134. a ? this.emitter.emit("success", {
  135. action: this.action,
  136. text: this.selectedText,
  137. trigger: this.trigger,
  138. clearSelection: this.clearSelection.bind(this)
  139. }) : this.emitter.emit("error", {
  140. action: this.action,
  141. trigger: this.trigger,
  142. clearSelection: this.clearSelection.bind(this)
  143. })
  144. }, a.prototype.clearSelection = function () {
  145. this.target && this.target.blur(), window.getSelection().removeAllRanges()
  146. }, e(a, [{
  147. key: "action", set: function () {
  148. var a = arguments.length <= 0 || void 0 === arguments[0] ? "copy" : arguments[0];
  149. if (this._action = a, "copy" !== this._action && "cut" !== this._action)throw new Error('Invalid "action" value, use either "copy" or "cut"')
  150. }, get: function () {
  151. return this._action
  152. }
  153. }, {
  154. key: "target", set: function (a) {
  155. if (void 0 !== a) {
  156. if (!a || "object" != typeof a || 1 !== a.nodeType)throw new Error('Invalid "target" value, use a valid Element');
  157. this._target = a
  158. }
  159. }, get: function () {
  160. return this._target
  161. }
  162. }]), a
  163. }();
  164. c["default"] = f, b.exports = c["default"]
  165. }, {}], 7: [function (a, b, c) {
  166. "use strict";
  167. function d(a) {
  168. return a && a.__esModule ? a : {"default": a}
  169. }
  170. function e(a, b) {
  171. if (!(a instanceof b))throw new TypeError("Cannot call a class as a function")
  172. }
  173. function f(a, b) {
  174. if ("function" != typeof b && null !== b)throw new TypeError("Super expression must either be null or a function, not " + typeof b);
  175. a.prototype = Object.create(b && b.prototype, {
  176. constructor: {
  177. value: a,
  178. enumerable: !1,
  179. writable: !0,
  180. configurable: !0
  181. }
  182. }), b && (Object.setPrototypeOf ? Object.setPrototypeOf(a, b) : a.__proto__ = b)
  183. }
  184. c.__esModule = !0;
  185. var g = a("./clipboard-action"), h = d(g), i = a("delegate-events"), j = d(i), k = a("tiny-emitter"), l = d(k), m = "data-clipboard-", n = function (a) {
  186. function b(c, d) {
  187. e(this, b), a.call(this), this.resolveOptions(d), this.delegateClick(c)
  188. }
  189. return f(b, a), b.prototype.resolveOptions = function () {
  190. var a = arguments.length <= 0 || void 0 === arguments[0] ? {} : arguments[0];
  191. this.action = "function" == typeof a.action ? a.action : this.setAction, this.target = "function" == typeof a.target ? a.target : this.setTarget, this.text = "function" == typeof a.text ? a.text : this.setText
  192. }, b.prototype.delegateClick = function (a) {
  193. var b = this;
  194. j["default"].bind(document.body, a, "click", function (a) {
  195. return b.initialize(a)
  196. })
  197. }, b.prototype.initialize = function (a) {
  198. this.clipboardAction && (this.clipboardAction = null), this.clipboardAction = new h["default"]({
  199. action: this.action(a.delegateTarget),
  200. target: this.target(a.delegateTarget),
  201. text: this.text(a.delegateTarget),
  202. trigger: a.delegateTarget,
  203. emitter: this
  204. })
  205. }, b.prototype.setAction = function (a) {
  206. return a.hasAttribute(m + "action") ? a.getAttribute(m + "action") : void 0
  207. }, b.prototype.setTarget = function (a) {
  208. if (a.hasAttribute(m + "target")) {
  209. var b = a.getAttribute(m + "target");
  210. return document.querySelector(b)
  211. }
  212. }, b.prototype.setText = function (a) {
  213. return a.hasAttribute(m + "text") ? a.getAttribute(m + "text") : void 0
  214. }, b
  215. }(l["default"]);
  216. c["default"] = n, b.exports = c["default"]
  217. }, {"./clipboard-action": 6, "delegate-events": 1, "tiny-emitter": 5}]
  218. }, {}, [7])(7)
  219. });