category.js 2.9 KB

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