category.js 4.4 KB

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