clipBoard.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. * clipboard.js
  3. * width clipboard.js, you can copy cut and paste clipboard data
  4. * the main methods ard execCommand for modern browser and clipboardData for ie
  5. * @author ganzw@gmail.com
  6. * @url https://github.com/baixuexiyang/clipBoard.js
  7. */
  8. ;(function(name, fun) {
  9. if (typeof module !== 'undefined' && module.exports) {
  10. module.exports = fun();
  11. } else if (typeof define === 'function' && define.amd) {
  12. define(fun);
  13. } else {
  14. this[name] = fun();
  15. }
  16. })('clipBoard', function() {
  17. "use strict";
  18. /**
  19. * tar is the value
  20. * @date 2016-04-25
  21. * @param {[type]} tar [description]
  22. * @param {[type]} options [description]
  23. * @return {[type]} [description]
  24. */
  25. function clipBoard(tar, options) {
  26. this.options = options || {};
  27. this.tar = tar[0] || tar;
  28. // if options contain copy, copy will be applied soon
  29. if (this.options.copy) {
  30. this.copyd();
  31. }
  32. if(this.options.cut) {
  33. this.cut();
  34. }
  35. if(this.options.paste) {
  36. this.paste();
  37. }
  38. }
  39. /**
  40. * coping is to set the value to clipboard
  41. * you can set the value through copy function, and the function be called autoly
  42. * Also you can set the paramer and it will be set the clipboard
  43. */
  44. clipBoard.prototype.copyd = function(value) {
  45. // before the copy it will be called, you can check the value or modify the value
  46. if (this.options.beforeCopy) {
  47. this.options.beforeCopy();
  48. }
  49. // if the options set copy function, the value will be set. then get the paramer value.
  50. // above all, if the value is null, then will be set the tar of value
  51. value = value || this.tar.value || this.tar.innerText;
  52. if (this.options.copy) {
  53. value = this.options.copy();
  54. }
  55. // for modern browser
  56. if (document.execCommand) {
  57. var element = document.createElement('SPAN');
  58. element.textContent = value;
  59. document.body.appendChild(element);
  60. if (document.selection) {
  61. var range = document.body.createTextRange();
  62. range.moveToElementText(element);
  63. range.select();
  64. } else if (window.getSelection) {
  65. var range = document.createRange();
  66. range.selectNode(element);
  67. window.getSelection().removeAllRanges();
  68. window.getSelection().addRange(range);
  69. }
  70. document.execCommand('copy');
  71. element.remove ? element.remove() : element.removeNode(true);
  72. }
  73. // for ie
  74. if (window.clipboardData) {
  75. window.clipboardData.setData('text', value);
  76. }
  77. // after copy
  78. if (this.options.afterCopy) {
  79. this.options.afterCopy();
  80. }
  81. };
  82. /**
  83. * cut the value of input or textarea
  84. * @date 2016-04-25
  85. * @return {[type]} [description]
  86. */
  87. clipBoard.prototype.cut = function() {
  88. if (this.tar.type !== 'text' && this.tar.type !== 'textarea') {
  89. return;
  90. }
  91. if (this.options.beforeCut) {
  92. this.options.beforeCut();
  93. }
  94. if (document.execCommand) {
  95. var element = this.tar;
  96. if (document.selection) {
  97. var range = document.body.createTextRange();
  98. range.moveToElementText(element);
  99. range.select();
  100. } else if (window.getSelection) {
  101. element.select();
  102. }
  103. document.execCommand('cut');
  104. }
  105. // for ie
  106. if (window.clipboardData) {
  107. window.clipboardData.setData('text', this.tar.value);
  108. this.tar.value = '';
  109. }
  110. // after copy
  111. if (this.options.afterCut) {
  112. this.options.afterCut();
  113. }
  114. };
  115. /**
  116. * paste the clipboard value to input or textarea
  117. * @date 2016-04-25
  118. * @return {[type]} [description]
  119. */
  120. clipBoard.prototype.paste = function() {
  121. if (this.tar.type !== 'text' && this.tar.type !== 'textarea') {
  122. return;
  123. }
  124. if (this.options.beforePaste) {
  125. this.options.beforePaste();
  126. }
  127. if (document.execCommand) {
  128. var element = this.tar;
  129. if(element.setSelectionRange) {
  130. element.focus();
  131. element.setSelectionRange(element.value.length, element.value.length);
  132. } else if (element.createTextRange) {
  133. var range = element.createTextRange();
  134. range.collapse(true);
  135. range.moveEnd('character', element.value.length);
  136. range.moveStart('character', element.value.length);
  137. range.select();
  138. }
  139. document.execCommand('paste');
  140. }
  141. // for ie
  142. if (!document.execCommand && window.clipboardData) {
  143. this.tar.value += window.clipboardData.getData('text');
  144. }
  145. // after Paste
  146. if (this.options.afterPaste) {
  147. this.options.afterPaste();
  148. }
  149. };
  150. return clipBoard;
  151. });