category.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. var loading = false,
  3. dnd_successful = false;
  4. function dragend_process(t) {
  5. t.style.display = 'none';
  6. if (loading) {
  7. window.setTimeout(function() {
  8. dragend_process(t);
  9. }, 50);
  10. }
  11. if (!dnd_successful) {
  12. t.style.display = 'block';
  13. t.style.opacity = 1.0;
  14. } else {
  15. var parent = $(t.parentNode);
  16. $(t).remove();
  17. if (parent.children().length <= 0) {
  18. parent.append('<li class="item disabled" dropzone="move">' + i18n['category_empty'] + '</li>');
  19. }
  20. }
  21. }
  22. function init_draggable() {
  23. if (!(window.$ && window.i18n)) {
  24. if (window.console) {
  25. console.log('FreshRSS waiting for JS…');
  26. }
  27. window.setTimeout(init_draggable, 50);
  28. return;
  29. }
  30. $.event.props.push('dataTransfer');
  31. var draggable = '[draggable="true"]',
  32. dropzone = '[dropzone="move"]';
  33. $('.drop-section').on('dragstart', draggable, function(e) {
  34. e.dataTransfer.effectAllowed = 'move';
  35. e.dataTransfer.setData('text/html', e.target.outerHTML);
  36. e.dataTransfer.setData('text', e.target.getAttribute('data-feed-id'));
  37. e.target.style.opacity = 0.3;
  38. dnd_successful = false;
  39. });
  40. $('.drop-section').on('dragend', draggable, function(e) {
  41. dragend_process(e.target);
  42. });
  43. $('.drop-section').on('dragenter', dropzone, function(e) {
  44. $(this).addClass('drag-hover');
  45. e.preventDefault();
  46. });
  47. $('.drop-section').on('dragleave', dropzone, function(e) {
  48. var pos_this = $(this).position(),
  49. scroll_top = $(document).scrollTop(),
  50. top = pos_this.top,
  51. left = pos_this.left,
  52. right = left + $(this).width(),
  53. bottom = top + $(this).height(),
  54. mouse_x = e.originalEvent.screenX,
  55. mouse_y = e.originalEvent.clientY + scroll_top;
  56. if (left <= mouse_x && mouse_x <= right &&
  57. top <= mouse_y && mouse_y <= bottom) {
  58. // HACK because dragleave is triggered when hovering children!
  59. return;
  60. }
  61. $(this).removeClass('drag-hover');
  62. });
  63. $('.drop-section').on('dragover', dropzone, function(e) {
  64. e.dataTransfer.dropEffect = "move";
  65. e.preventDefault();
  66. return false;
  67. });
  68. $('.drop-section').on('drop', dropzone, function(e) {
  69. var feed_id = e.dataTransfer.getData('text'),
  70. cat_id = e.target.parentNode.getAttribute('data-cat-id');
  71. loading = true;
  72. $.ajax({
  73. type: 'POST',
  74. url: './?c=feed&a=move',
  75. data : {
  76. f_id: feed_id,
  77. c_id: cat_id
  78. }
  79. }).success(function() {
  80. $(e.target).after(e.dataTransfer.getData('text/html'));
  81. if ($(e.target).hasClass('disabled')) {
  82. $(e.target).remove();
  83. }
  84. dnd_successful = true;
  85. }).complete(function() {
  86. loading = false;
  87. });
  88. $(this).removeClass('drag-hover');
  89. e.preventDefault();
  90. });
  91. }
  92. if (document.readyState && document.readyState !== 'loading') {
  93. init_draggable();
  94. } else if (document.addEventListener) {
  95. document.addEventListener('DOMContentLoaded', function () {
  96. init_draggable();
  97. }, false);
  98. }