category.js 2.9 KB

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