category.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. var draggable = '[draggable="true"]',
  31. dropzone = '[dropzone="move"]';
  32. $('.drop-section').on('dragstart', draggable, function(e) {
  33. e.originalEvent.dataTransfer.effectAllowed = 'move';
  34. e.originalEvent.dataTransfer.setData('text/html', e.target.outerHTML);
  35. e.originalEvent.dataTransfer.setData('text', e.target.getAttribute('data-feed-id'));
  36. e.target.style.opacity = 0.3;
  37. dnd_successful = false;
  38. });
  39. $('.drop-section').on('dragend', draggable, function(e) {
  40. dragend_process(e.target);
  41. });
  42. $('.drop-section').on('dragenter', dropzone, function(e) {
  43. $(this).addClass('drag-hover');
  44. e.preventDefault();
  45. });
  46. $('.drop-section').on('dragleave', dropzone, function(e) {
  47. var pos_this = $(this).position(),
  48. scroll_top = $(document).scrollTop(),
  49. top = pos_this.top,
  50. left = pos_this.left,
  51. right = left + $(this).width(),
  52. bottom = top + $(this).height(),
  53. mouse_x = e.originalEvent.screenX,
  54. mouse_y = e.originalEvent.clientY + scroll_top;
  55. if (left <= mouse_x && mouse_x <= right &&
  56. top <= mouse_y && mouse_y <= bottom) {
  57. // HACK because dragleave is triggered when hovering children!
  58. return;
  59. }
  60. $(this).removeClass('drag-hover');
  61. });
  62. $('.drop-section').on('dragover', dropzone, function(e) {
  63. e.originalEvent.dataTransfer.dropEffect = "move";
  64. e.preventDefault();
  65. return false;
  66. });
  67. $('.drop-section').on('drop', dropzone, function(e) {
  68. var feed_id = e.originalEvent.dataTransfer.getData('text'),
  69. cat_id = e.target.parentNode.getAttribute('data-cat-id');
  70. loading = true;
  71. $.ajax({
  72. type: 'POST',
  73. url: './?c=feed&a=move',
  74. data : {
  75. f_id: feed_id,
  76. c_id: cat_id
  77. }
  78. }).done(function() {
  79. $(e.target).after(e.originalEvent.dataTransfer.getData('text/html'));
  80. if ($(e.target).hasClass('disabled')) {
  81. $(e.target).remove();
  82. }
  83. dnd_successful = true;
  84. }).always(function() {
  85. loading = false;
  86. });
  87. $(this).removeClass('drag-hover');
  88. e.preventDefault();
  89. });
  90. }
  91. if (document.readyState && document.readyState !== 'loading') {
  92. init_draggable();
  93. } else if (document.addEventListener) {
  94. document.addEventListener('DOMContentLoaded', function () {
  95. init_draggable();
  96. }, false);
  97. }