category.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. "use strict";
  2. /* globals context */
  3. /* jshint esversion:6, strict:global */
  4. var loading = false,
  5. dnd_successful = false;
  6. function dragend_process(t) {
  7. t.setAttribute('draggable', 'false');
  8. if (loading) {
  9. setTimeout(function() {
  10. dragend_process(t);
  11. }, 50);
  12. return;
  13. }
  14. if (!dnd_successful) {
  15. t.style.display = '';
  16. t.style.opacity = '';
  17. t.setAttribute('draggable', 'true');
  18. } else {
  19. const p = t.parentElement;
  20. t.remove();
  21. if (p.childElementCount <= 0) {
  22. p.insertAdjacentHTML('beforeend', '<li class="item disabled" dropzone="move">' + context.i18n.category_empty + '</li>');
  23. }
  24. }
  25. }
  26. var dragFeedId = '',
  27. dragHtml = '';
  28. function init_draggable() {
  29. if (!window.context) {
  30. if (window.console) {
  31. console.log('FreshRSS category waiting for JS…');
  32. }
  33. setTimeout(init_draggable, 50);
  34. return;
  35. }
  36. const draggable = '[draggable="true"]',
  37. dropzone = '[dropzone="move"]',
  38. dropSection = document.querySelector('.drop-section');
  39. dropSection.ondragstart = function(ev) {
  40. const li = ev.target.closest ? ev.target.closest(draggable) : null;
  41. if (li) {
  42. const drag = ev.target.closest('[draggable]');
  43. ev.dataTransfer.effectAllowed = 'move';
  44. dragHtml = drag.outerHTML;
  45. dragFeedId = drag.getAttribute('data-feed-id');
  46. ev.dataTransfer.setData('text', dragFeedId);
  47. drag.style.opacity = 0.3;
  48. dnd_successful = false;
  49. }
  50. };
  51. dropSection.ondragend = function(ev) {
  52. const li = ev.target.closest ? ev.target.closest(draggable) : null;
  53. if (li) {
  54. dragend_process(li);
  55. }
  56. };
  57. dropSection.ondragenter = function(ev) {
  58. const li = ev.target.closest ? ev.target.closest(dropzone) : null;
  59. if (li) {
  60. li.classList.add('drag-hover');
  61. return false;
  62. }
  63. };
  64. dropSection.onddragleave = function(ev) {
  65. const li = ev.target.closest ? ev.target.closest(dropzone) : null;
  66. if (li) {
  67. const scroll_top = document.documentElement.scrollTop,
  68. top = li.offsetTop,
  69. left = li.offsetLeft,
  70. right = left + li.clientWidth,
  71. bottom = top + li.clientHeight,
  72. mouse_x = ev.screenX,
  73. mouse_y = ev.clientY + scroll_top;
  74. if (left <= mouse_x && mouse_x <= right &&
  75. top <= mouse_y && mouse_y <= bottom) {
  76. // HACK because dragleave is triggered when hovering children!
  77. return;
  78. }
  79. li.classList.remove('drag-hover');
  80. }
  81. };
  82. dropSection.ondragover = function(ev) {
  83. const li = ev.target.closest ? ev.target.closest(dropzone) : null;
  84. if (li) {
  85. ev.dataTransfer.dropEffect = "move";
  86. return false;
  87. }
  88. };
  89. dropSection.ondrop = function(ev) {
  90. const li = ev.target.closest ? ev.target.closest(dropzone) : null;
  91. if (li) {
  92. loading = true;
  93. const req = new XMLHttpRequest();
  94. req.open('POST', './?c=feed&a=move', true);
  95. req.responseType = 'json';
  96. req.onload = function (e) {
  97. if (this.status == 200) {
  98. li.insertAdjacentHTML('afterend', dragHtml);
  99. if (li.classList.contains('disabled')) {
  100. li.remove();
  101. }
  102. dnd_successful = true;
  103. }
  104. };
  105. req.onloadend = function (e) {
  106. loading = false;
  107. dragFeedId = '';
  108. dragHtml = '';
  109. };
  110. req.setRequestHeader('Content-Type', 'application/json');
  111. req.send(JSON.stringify({
  112. f_id: dragFeedId,
  113. c_id: li.parentElement.getAttribute('data-cat-id'),
  114. _csrf: context.csrf,
  115. }));
  116. li.classList.remove('drag-hover');
  117. return false;
  118. }
  119. };
  120. }
  121. if (document.readyState && document.readyState !== 'loading') {
  122. init_draggable();
  123. } else if (document.addEventListener) {
  124. document.addEventListener('DOMContentLoaded', function () {
  125. init_draggable();
  126. }, false);
  127. }