vue-clipboard.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var Clipboard = require('clipboard/dist/clipboard.min.js') // FIXME: workaround for browserify
  2. var VueClipboardConfig = {
  3. autoSetContainer: false,
  4. appendToBody: true // This fixes IE, see #50
  5. }
  6. var VueClipboard = {
  7. install: function (Vue) {
  8. Vue.prototype.$clipboardConfig = VueClipboardConfig
  9. Vue.prototype.$copyText = function (text, container) {
  10. return new Promise(function (resolve, reject) {
  11. var fakeElement = document.createElement('button')
  12. var clipboard = new Clipboard(fakeElement, {
  13. text: function () { return text },
  14. action: function () { return 'copy' },
  15. container: typeof container === 'object' ? container : document.body
  16. })
  17. clipboard.on('success', function (e) {
  18. clipboard.destroy()
  19. resolve(e)
  20. })
  21. clipboard.on('error', function (e) {
  22. clipboard.destroy()
  23. reject(e)
  24. })
  25. if (VueClipboardConfig.appendToBody) document.body.appendChild(fakeElement)
  26. fakeElement.click()
  27. if (VueClipboardConfig.appendToBody) document.body.removeChild(fakeElement)
  28. })
  29. }
  30. Vue.directive('clipboard', {
  31. bind: function (el, binding, vnode) {
  32. if (binding.arg === 'success') {
  33. el._vClipboard_success = binding.value
  34. } else if (binding.arg === 'error') {
  35. el._vClipboard_error = binding.value
  36. } else {
  37. var clipboard = new Clipboard(el, {
  38. text: function () { return binding.value },
  39. action: function () { return binding.arg === 'cut' ? 'cut' : 'copy' },
  40. container: VueClipboardConfig.autoSetContainer ? el : undefined
  41. })
  42. clipboard.on('success', function (e) {
  43. var callback = el._vClipboard_success
  44. callback && callback(e)
  45. })
  46. clipboard.on('error', function (e) {
  47. var callback = el._vClipboard_error
  48. callback && callback(e)
  49. })
  50. el._vClipboard = clipboard
  51. }
  52. },
  53. update: function (el, binding) {
  54. if (binding.arg === 'success') {
  55. el._vClipboard_success = binding.value
  56. } else if (binding.arg === 'error') {
  57. el._vClipboard_error = binding.value
  58. } else {
  59. el._vClipboard.text = function () { return binding.value }
  60. el._vClipboard.action = function () { return binding.arg === 'cut' ? 'cut' : 'copy' }
  61. }
  62. },
  63. unbind: function (el, binding) {
  64. if (binding.arg === 'success') {
  65. delete el._vClipboard_success
  66. } else if (binding.arg === 'error') {
  67. delete el._vClipboard_error
  68. } else {
  69. el._vClipboard.destroy()
  70. delete el._vClipboard
  71. }
  72. }
  73. })
  74. },
  75. config: VueClipboardConfig
  76. }
  77. if (typeof exports === 'object') {
  78. module.exports = VueClipboard
  79. } else if (typeof define === 'function' && define.amd) {
  80. define([], function () {
  81. return VueClipboard
  82. })
  83. }