index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Created by harrylang on 16/10/24.
  3. */
  4. $(function () {
  5. var isHold = false,
  6. duration = 100; // 长按时长
  7. var bangBox = $('.bang-box'),
  8. bangInner = $('.bang-inner'),
  9. bangCopy = $('.bang-copy'),
  10. bangClose = $('.bang-close');
  11. var container = $('.container');
  12. // 消息操作
  13. $('.msg').on('mousedown', function () {
  14. isHold = true;
  15. setTimeout((function (elem) {
  16. return function () {
  17. if (isHold) {
  18. startBang(elem);
  19. }
  20. };
  21. }(this)), duration);
  22. }).on('mouseup', function () {
  23. isHold = false;
  24. }).on('mouseleave', function () {
  25. isHold = false;
  26. });
  27. // 开始爆炸
  28. function startBang(elem) {
  29. elem = $(elem);
  30. var text = elem.text();
  31. if (!$.trim(text)) {
  32. return;
  33. }
  34. $.ajax({
  35. type: "POST",
  36. url: '/bang',
  37. data: JSON.stringify({ text: text }),
  38. contentType: "application/json",
  39. success: function (data) {
  40. var html = '';
  41. for (var i = 0, l = data.length; i < l; i++) {
  42. html += '<div class="bang-label" data-index="' + (i + 1) + '">' + data[i] + '</div>';
  43. }
  44. bangInner.html(html);
  45. container.addClass('bang-scroll');
  46. bangBox.show();
  47. }
  48. });
  49. }
  50. // 爆炸操作
  51. // 关闭
  52. bangClose.on('click', function () {
  53. bangBox.hide();
  54. container.removeClass('bang-scroll');
  55. });
  56. // 分词选取操作
  57. bangInner.on('click', '.bang-label', function () {
  58. var self = $(this);
  59. self[self.hasClass('bang-active') ? 'removeClass' : 'addClass']('bang-active');
  60. });
  61. // 复制
  62. ZeroClipboard.config({
  63. moviePath: '/static/js/zeroclipboard-2.2.0/ZeroClipboard.swf'
  64. });
  65. var clip = new ZeroClipboard(bangCopy);
  66. clip.on('beforecopy', function () {
  67. var text = '';
  68. var actves = $('.bang-active');
  69. for (var i = 0, len = actves.length; i < len; i++) {
  70. text += actves.eq(i).text() + ' ';
  71. }
  72. clip.setText(text);
  73. }).on('aftercopy', function () {
  74. bangClose.trigger('click');
  75. alert('复制成功!');
  76. });
  77. bangCopy.on("click", function () {
  78. var text = '';
  79. var actves = $('.bang-active');
  80. for (var i = 0, len = actves.length; i < len; i++) {
  81. text += actves.eq(i).text() + ' ';
  82. }
  83. const input = document.createElement('input');
  84. document.body.appendChild(input);
  85. input.setAttribute('value', text);
  86. input.select();
  87. if (document.execCommand('copy')) {
  88. document.execCommand('copy');
  89. }
  90. document.body.removeChild(input);
  91. bangClose.trigger('click');
  92. alert('复制成功!');
  93. });
  94. });