category.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. /* globals context */
  4. let loading = false;
  5. let 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 <= 1) {
  22. p.insertAdjacentHTML('afterbegin',
  23. '<li class="item feed disabled" dropzone="move"><div class="alert-warn">' + context.i18n.category_empty + '</div></li>');
  24. }
  25. }
  26. }
  27. let dragFeedId = '';
  28. let 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. const dropzone = '[dropzone="move"]';
  39. const 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. const top = li.offsetTop;
  70. const left = li.offsetLeft;
  71. const right = left + li.clientWidth;
  72. const bottom = top + li.clientHeight;
  73. const mouse_x = ev.screenX;
  74. const 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. if (!e.target.checked) element.style.visibility = 'visible'; // Help for Edge 44
  129. });
  130. }
  131. });
  132. slider.addEventListener('click', function (e) {
  133. if (e.target.closest('button[type=reset]')) {
  134. const archiving = document.getElementById('use_default_purge_options');
  135. if (archiving) {
  136. slider.querySelectorAll('.archiving').forEach(function (element) {
  137. element.hidden = archiving.getAttribute('data-leave-validation') == 1;
  138. });
  139. }
  140. }
  141. });
  142. }
  143. if (document.readyState && document.readyState !== 'loading') {
  144. init_draggable();
  145. archiving();
  146. } else if (document.addEventListener) {
  147. document.addEventListener('DOMContentLoaded', function () {
  148. init_draggable();
  149. archiving();
  150. }, false);
  151. }
  152. // @license-end