clipboard.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*!
  2. * clipboard.js v1.5.12
  3. * https://zenorocha.github.io/clipboard.js
  4. *
  5. * Licensed MIT © Zeno Rocha
  6. */
  7. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Clipboard = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  8. var matches = require('matches-selector')
  9. module.exports = function (element, selector, checkYoSelf) {
  10. var parent = checkYoSelf ? element : element.parentNode
  11. while (parent && parent !== document) {
  12. if (matches(parent, selector)) return parent;
  13. parent = parent.parentNode
  14. }
  15. }
  16. },{"matches-selector":5}],2:[function(require,module,exports){
  17. var closest = require('closest');
  18. /**
  19. * Delegates event to a selector.
  20. *
  21. * @param {Element} element
  22. * @param {String} selector
  23. * @param {String} type
  24. * @param {Function} callback
  25. * @param {Boolean} useCapture
  26. * @return {Object}
  27. */
  28. function delegate(element, selector, type, callback, useCapture) {
  29. var listenerFn = listener.apply(this, arguments);
  30. element.addEventListener(type, listenerFn, useCapture);
  31. return {
  32. destroy: function() {
  33. element.removeEventListener(type, listenerFn, useCapture);
  34. }
  35. }
  36. }
  37. /**
  38. * Finds closest match and invokes callback.
  39. *
  40. * @param {Element} element
  41. * @param {String} selector
  42. * @param {String} type
  43. * @param {Function} callback
  44. * @return {Function}
  45. */
  46. function listener(element, selector, type, callback) {
  47. return function(e) {
  48. e.delegateTarget = closest(e.target, selector, true);
  49. if (e.delegateTarget) {
  50. callback.call(element, e);
  51. }
  52. }
  53. }
  54. module.exports = delegate;
  55. },{"closest":1}],3:[function(require,module,exports){
  56. /**
  57. * Check if argument is a HTML element.
  58. *
  59. * @param {Object} value
  60. * @return {Boolean}
  61. */
  62. exports.node = function(value) {
  63. return value !== undefined
  64. && value instanceof HTMLElement
  65. && value.nodeType === 1;
  66. };
  67. /**
  68. * Check if argument is a list of HTML elements.
  69. *
  70. * @param {Object} value
  71. * @return {Boolean}
  72. */
  73. exports.nodeList = function(value) {
  74. var type = Object.prototype.toString.call(value);
  75. return value !== undefined
  76. && (type === '[object NodeList]' || type === '[object HTMLCollection]')
  77. && ('length' in value)
  78. && (value.length === 0 || exports.node(value[0]));
  79. };
  80. /**
  81. * Check if argument is a string.
  82. *
  83. * @param {Object} value
  84. * @return {Boolean}
  85. */
  86. exports.string = function(value) {
  87. return typeof value === 'string'
  88. || value instanceof String;
  89. };
  90. /**
  91. * Check if argument is a function.
  92. *
  93. * @param {Object} value
  94. * @return {Boolean}
  95. */
  96. exports.fn = function(value) {
  97. var type = Object.prototype.toString.call(value);
  98. return type === '[object Function]';
  99. };
  100. },{}],4:[function(require,module,exports){
  101. var is = require('./is');
  102. var delegate = require('delegate');
  103. /**
  104. * Validates all params and calls the right
  105. * listener function based on its target type.
  106. *
  107. * @param {String|HTMLElement|HTMLCollection|NodeList} target
  108. * @param {String} type
  109. * @param {Function} callback
  110. * @return {Object}
  111. */
  112. function listen(target, type, callback) {
  113. if (!target && !type && !callback) {
  114. throw new Error('Missing required arguments');
  115. }
  116. if (!is.string(type)) {
  117. throw new TypeError('Second argument must be a String');
  118. }
  119. if (!is.fn(callback)) {
  120. throw new TypeError('Third argument must be a Function');
  121. }
  122. if (is.node(target)) {
  123. return listenNode(target, type, callback);
  124. }
  125. else if (is.nodeList(target)) {
  126. return listenNodeList(target, type, callback);
  127. }
  128. else if (is.string(target)) {
  129. return listenSelector(target, type, callback);
  130. }
  131. else {
  132. throw new TypeError('First argument must be a String, HTMLElement, HTMLCollection, or NodeList');
  133. }
  134. }
  135. /**
  136. * Adds an event listener to a HTML element
  137. * and returns a remove listener function.
  138. *
  139. * @param {HTMLElement} node
  140. * @param {String} type
  141. * @param {Function} callback
  142. * @return {Object}
  143. */
  144. function listenNode(node, type, callback) {
  145. node.addEventListener(type, callback);
  146. return {
  147. destroy: function() {
  148. node.removeEventListener(type, callback);
  149. }
  150. }
  151. }
  152. /**
  153. * Add an event listener to a list of HTML elements
  154. * and returns a remove listener function.
  155. *
  156. * @param {NodeList|HTMLCollection} nodeList
  157. * @param {String} type
  158. * @param {Function} callback
  159. * @return {Object}
  160. */
  161. function listenNodeList(nodeList, type, callback) {
  162. Array.prototype.forEach.call(nodeList, function(node) {
  163. node.addEventListener(type, callback);
  164. });
  165. return {
  166. destroy: function() {
  167. Array.prototype.forEach.call(nodeList, function(node) {
  168. node.removeEventListener(type, callback);
  169. });
  170. }
  171. }
  172. }
  173. /**
  174. * Add an event listener to a selector
  175. * and returns a remove listener function.
  176. *
  177. * @param {String} selector
  178. * @param {String} type
  179. * @param {Function} callback
  180. * @return {Object}
  181. */
  182. function listenSelector(selector, type, callback) {
  183. return delegate(document.body, selector, type, callback);
  184. }
  185. module.exports = listen;
  186. },{"./is":3,"delegate":2}],5:[function(require,module,exports){
  187. /**
  188. * Element prototype.
  189. */
  190. var proto = Element.prototype;
  191. /**
  192. * Vendor function.
  193. */
  194. var vendor = proto.matchesSelector
  195. || proto.webkitMatchesSelector
  196. || proto.mozMatchesSelector
  197. || proto.msMatchesSelector
  198. || proto.oMatchesSelector;
  199. /**
  200. * Expose `match()`.
  201. */
  202. module.exports = match;
  203. /**
  204. * Match `el` to `selector`.
  205. *
  206. * @param {Element} el
  207. * @param {String} selector
  208. * @return {Boolean}
  209. * @api public
  210. */
  211. function match(el, selector) {
  212. if (vendor) return vendor.call(el, selector);
  213. var nodes = el.parentNode.querySelectorAll(selector);
  214. for (var i = 0; i < nodes.length; ++i) {
  215. if (nodes[i] == el) return true;
  216. }
  217. return false;
  218. }
  219. },{}],6:[function(require,module,exports){
  220. function select(element) {
  221. var selectedText;
  222. if (element.nodeName === 'INPUT' || element.nodeName === 'TEXTAREA') {
  223. element.focus();
  224. element.setSelectionRange(0, element.value.length);
  225. selectedText = element.value;
  226. }
  227. else {
  228. if (element.hasAttribute('contenteditable')) {
  229. element.focus();
  230. }
  231. var selection = window.getSelection();
  232. var range = document.createRange();
  233. range.selectNodeContents(element);
  234. selection.removeAllRanges();
  235. selection.addRange(range);
  236. selectedText = selection.toString();
  237. }
  238. return selectedText;
  239. }
  240. module.exports = select;
  241. },{}],7:[function(require,module,exports){
  242. function E () {
  243. // Keep this empty so it's easier to inherit from
  244. // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3)
  245. }
  246. E.prototype = {
  247. on: function (name, callback, ctx) {
  248. var e = this.e || (this.e = {});
  249. (e[name] || (e[name] = [])).push({
  250. fn: callback,
  251. ctx: ctx
  252. });
  253. return this;
  254. },
  255. once: function (name, callback, ctx) {
  256. var self = this;
  257. function listener () {
  258. self.off(name, listener);
  259. callback.apply(ctx, arguments);
  260. };
  261. listener._ = callback
  262. return this.on(name, listener, ctx);
  263. },
  264. emit: function (name) {
  265. var data = [].slice.call(arguments, 1);
  266. var evtArr = ((this.e || (this.e = {}))[name] || []).slice();
  267. var i = 0;
  268. var len = evtArr.length;
  269. for (i; i < len; i++) {
  270. evtArr[i].fn.apply(evtArr[i].ctx, data);
  271. }
  272. return this;
  273. },
  274. off: function (name, callback) {
  275. var e = this.e || (this.e = {});
  276. var evts = e[name];
  277. var liveEvents = [];
  278. if (evts && callback) {
  279. for (var i = 0, len = evts.length; i < len; i++) {
  280. if (evts[i].fn !== callback && evts[i].fn._ !== callback)
  281. liveEvents.push(evts[i]);
  282. }
  283. }
  284. // Remove event from queue to prevent memory leak
  285. // Suggested by https://github.com/lazd
  286. // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910
  287. (liveEvents.length)
  288. ? e[name] = liveEvents
  289. : delete e[name];
  290. return this;
  291. }
  292. };
  293. module.exports = E;
  294. },{}],8:[function(require,module,exports){
  295. (function (global, factory) {
  296. if (typeof define === "function" && define.amd) {
  297. define(['module', 'select'], factory);
  298. } else if (typeof exports !== "undefined") {
  299. factory(module, require('select'));
  300. } else {
  301. var mod = {
  302. exports: {}
  303. };
  304. factory(mod, global.select);
  305. global.clipboardAction = mod.exports;
  306. }
  307. })(this, function (module, _select) {
  308. 'use strict';
  309. var _select2 = _interopRequireDefault(_select);
  310. function _interopRequireDefault(obj) {
  311. return obj && obj.__esModule ? obj : {
  312. default: obj
  313. };
  314. }
  315. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  316. return typeof obj;
  317. } : function (obj) {
  318. return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj;
  319. };
  320. function _classCallCheck(instance, Constructor) {
  321. if (!(instance instanceof Constructor)) {
  322. throw new TypeError("Cannot call a class as a function");
  323. }
  324. }
  325. var _createClass = function () {
  326. function defineProperties(target, props) {
  327. for (var i = 0; i < props.length; i++) {
  328. var descriptor = props[i];
  329. descriptor.enumerable = descriptor.enumerable || false;
  330. descriptor.configurable = true;
  331. if ("value" in descriptor) descriptor.writable = true;
  332. Object.defineProperty(target, descriptor.key, descriptor);
  333. }
  334. }
  335. return function (Constructor, protoProps, staticProps) {
  336. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  337. if (staticProps) defineProperties(Constructor, staticProps);
  338. return Constructor;
  339. };
  340. }();
  341. var ClipboardAction = function () {
  342. /**
  343. * @param {Object} options
  344. */
  345. function ClipboardAction(options) {
  346. _classCallCheck(this, ClipboardAction);
  347. this.resolveOptions(options);
  348. this.initSelection();
  349. }
  350. /**
  351. * Defines base properties passed from constructor.
  352. * @param {Object} options
  353. */
  354. ClipboardAction.prototype.resolveOptions = function resolveOptions() {
  355. var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  356. this.action = options.action;
  357. this.emitter = options.emitter;
  358. this.target = options.target;
  359. this.text = options.text;
  360. this.trigger = options.trigger;
  361. this.selectedText = '';
  362. };
  363. ClipboardAction.prototype.initSelection = function initSelection() {
  364. if (this.text) {
  365. this.selectFake();
  366. } else if (this.target) {
  367. this.selectTarget();
  368. }
  369. };
  370. ClipboardAction.prototype.selectFake = function selectFake() {
  371. var _this = this;
  372. var isRTL = document.documentElement.getAttribute('dir') == 'rtl';
  373. this.removeFake();
  374. this.fakeHandlerCallback = function () {
  375. return _this.removeFake();
  376. };
  377. this.fakeHandler = document.body.addEventListener('click', this.fakeHandlerCallback) || true;
  378. this.fakeElem = document.createElement('textarea');
  379. // Prevent zooming on iOS
  380. this.fakeElem.style.fontSize = '12pt';
  381. // Reset box model
  382. this.fakeElem.style.border = '0';
  383. this.fakeElem.style.padding = '0';
  384. this.fakeElem.style.margin = '0';
  385. // Move element out of screen horizontally
  386. this.fakeElem.style.position = 'absolute';
  387. this.fakeElem.style[isRTL ? 'right' : 'left'] = '-9999px';
  388. // Move element to the same position vertically
  389. this.fakeElem.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
  390. this.fakeElem.setAttribute('readonly', '');
  391. this.fakeElem.value = this.text;
  392. document.body.appendChild(this.fakeElem);
  393. this.selectedText = (0, _select2.default)(this.fakeElem);
  394. this.copyText();
  395. };
  396. ClipboardAction.prototype.removeFake = function removeFake() {
  397. if (this.fakeHandler) {
  398. document.body.removeEventListener('click', this.fakeHandlerCallback);
  399. this.fakeHandler = null;
  400. this.fakeHandlerCallback = null;
  401. }
  402. if (this.fakeElem) {
  403. document.body.removeChild(this.fakeElem);
  404. this.fakeElem = null;
  405. }
  406. };
  407. ClipboardAction.prototype.selectTarget = function selectTarget() {
  408. this.selectedText = (0, _select2.default)(this.target);
  409. this.copyText();
  410. };
  411. ClipboardAction.prototype.copyText = function copyText() {
  412. var succeeded = undefined;
  413. try {
  414. succeeded = document.execCommand(this.action);
  415. } catch (err) {
  416. succeeded = false;
  417. }
  418. this.handleResult(succeeded);
  419. };
  420. ClipboardAction.prototype.handleResult = function handleResult(succeeded) {
  421. if (succeeded) {
  422. this.emitter.emit('success', {
  423. action: this.action,
  424. text: this.selectedText,
  425. trigger: this.trigger,
  426. clearSelection: this.clearSelection.bind(this)
  427. });
  428. } else {
  429. this.emitter.emit('error', {
  430. action: this.action,
  431. trigger: this.trigger,
  432. clearSelection: this.clearSelection.bind(this)
  433. });
  434. }
  435. };
  436. ClipboardAction.prototype.clearSelection = function clearSelection() {
  437. if (this.target) {
  438. this.target.blur();
  439. }
  440. window.getSelection().removeAllRanges();
  441. };
  442. ClipboardAction.prototype.destroy = function destroy() {
  443. this.removeFake();
  444. };
  445. _createClass(ClipboardAction, [{
  446. key: 'action',
  447. set: function set() {
  448. var action = arguments.length <= 0 || arguments[0] === undefined ? 'copy' : arguments[0];
  449. this._action = action;
  450. if (this._action !== 'copy' && this._action !== 'cut') {
  451. throw new Error('Invalid "action" value, use either "copy" or "cut"');
  452. }
  453. },
  454. get: function get() {
  455. return this._action;
  456. }
  457. }, {
  458. key: 'target',
  459. set: function set(target) {
  460. if (target !== undefined) {
  461. if (target && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object' && target.nodeType === 1) {
  462. if (this.action === 'copy' && target.hasAttribute('disabled')) {
  463. throw new Error('Invalid "target" attribute. Please use "readonly" instead of "disabled" attribute');
  464. }
  465. if (this.action === 'cut' && (target.hasAttribute('readonly') || target.hasAttribute('disabled'))) {
  466. throw new Error('Invalid "target" attribute. You can\'t cut text from elements with "readonly" or "disabled" attributes');
  467. }
  468. this._target = target;
  469. } else {
  470. throw new Error('Invalid "target" value, use a valid Element');
  471. }
  472. }
  473. },
  474. get: function get() {
  475. return this._target;
  476. }
  477. }]);
  478. return ClipboardAction;
  479. }();
  480. module.exports = ClipboardAction;
  481. });
  482. },{"select":6}],9:[function(require,module,exports){
  483. (function (global, factory) {
  484. if (typeof define === "function" && define.amd) {
  485. define(['module', './clipboard-action', 'tiny-emitter', 'good-listener'], factory);
  486. } else if (typeof exports !== "undefined") {
  487. factory(module, require('./clipboard-action'), require('tiny-emitter'), require('good-listener'));
  488. } else {
  489. var mod = {
  490. exports: {}
  491. };
  492. factory(mod, global.clipboardAction, global.tinyEmitter, global.goodListener);
  493. global.clipboard = mod.exports;
  494. }
  495. })(this, function (module, _clipboardAction, _tinyEmitter, _goodListener) {
  496. 'use strict';
  497. var _clipboardAction2 = _interopRequireDefault(_clipboardAction);
  498. var _tinyEmitter2 = _interopRequireDefault(_tinyEmitter);
  499. var _goodListener2 = _interopRequireDefault(_goodListener);
  500. function _interopRequireDefault(obj) {
  501. return obj && obj.__esModule ? obj : {
  502. default: obj
  503. };
  504. }
  505. function _classCallCheck(instance, Constructor) {
  506. if (!(instance instanceof Constructor)) {
  507. throw new TypeError("Cannot call a class as a function");
  508. }
  509. }
  510. function _possibleConstructorReturn(self, call) {
  511. if (!self) {
  512. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  513. }
  514. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  515. }
  516. function _inherits(subClass, superClass) {
  517. if (typeof superClass !== "function" && superClass !== null) {
  518. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  519. }
  520. subClass.prototype = Object.create(superClass && superClass.prototype, {
  521. constructor: {
  522. value: subClass,
  523. enumerable: false,
  524. writable: true,
  525. configurable: true
  526. }
  527. });
  528. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  529. }
  530. var Clipboard = function (_Emitter) {
  531. _inherits(Clipboard, _Emitter);
  532. /**
  533. * @param {String|HTMLElement|HTMLCollection|NodeList} trigger
  534. * @param {Object} options
  535. */
  536. function Clipboard(trigger, options) {
  537. _classCallCheck(this, Clipboard);
  538. var _this = _possibleConstructorReturn(this, _Emitter.call(this));
  539. _this.resolveOptions(options);
  540. _this.listenClick(trigger);
  541. return _this;
  542. }
  543. /**
  544. * Defines if attributes would be resolved using internal setter functions
  545. * or custom functions that were passed in the constructor.
  546. * @param {Object} options
  547. */
  548. Clipboard.prototype.resolveOptions = function resolveOptions() {
  549. var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  550. this.action = typeof options.action === 'function' ? options.action : this.defaultAction;
  551. this.target = typeof options.target === 'function' ? options.target : this.defaultTarget;
  552. this.text = typeof options.text === 'function' ? options.text : this.defaultText;
  553. };
  554. Clipboard.prototype.listenClick = function listenClick(trigger) {
  555. var _this2 = this;
  556. this.listener = (0, _goodListener2.default)(trigger, 'click', function (e) {
  557. return _this2.onClick(e);
  558. });
  559. };
  560. Clipboard.prototype.onClick = function onClick(e) {
  561. var trigger = e.delegateTarget || e.currentTarget;
  562. if (this.clipboardAction) {
  563. this.clipboardAction = null;
  564. }
  565. this.clipboardAction = new _clipboardAction2.default({
  566. action: this.action(trigger),
  567. target: this.target(trigger),
  568. text: this.text(trigger),
  569. trigger: trigger,
  570. emitter: this
  571. });
  572. };
  573. Clipboard.prototype.defaultAction = function defaultAction(trigger) {
  574. return getAttributeValue('action', trigger);
  575. };
  576. Clipboard.prototype.defaultTarget = function defaultTarget(trigger) {
  577. var selector = getAttributeValue('target', trigger);
  578. if (selector) {
  579. return document.querySelector(selector);
  580. }
  581. };
  582. Clipboard.prototype.defaultText = function defaultText(trigger) {
  583. return getAttributeValue('text', trigger);
  584. };
  585. Clipboard.prototype.destroy = function destroy() {
  586. this.listener.destroy();
  587. if (this.clipboardAction) {
  588. this.clipboardAction.destroy();
  589. this.clipboardAction = null;
  590. }
  591. };
  592. return Clipboard;
  593. }(_tinyEmitter2.default);
  594. /**
  595. * Helper function to retrieve attribute value.
  596. * @param {String} suffix
  597. * @param {Element} element
  598. */
  599. function getAttributeValue(suffix, element) {
  600. var attribute = 'data-clipboard-' + suffix;
  601. if (!element.hasAttribute(attribute)) {
  602. return;
  603. }
  604. return element.getAttribute(attribute);
  605. }
  606. module.exports = Clipboard;
  607. });
  608. },{"./clipboard-action":8,"good-listener":4,"tiny-emitter":7}]},{},[9])(9)
  609. });