4
0

category.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. e.originalEvent.dataTransfer.effectAllowed = 'move';
  36. e.originalEvent.dataTransfer.setData('text/html', e.target.outerHTML);
  37. e.originalEvent.dataTransfer.setData('text', e.target.getAttribute('data-feed-id'));
  38. e.target.style.opacity = 0.3;
  39. dnd_successful = false;
  40. });
  41. $('.drop-section').on('dragend', draggable, function(e) {
  42. dragend_process(e.target);
  43. });
  44. $('.drop-section').on('dragenter', dropzone, function(e) {
  45. $(this).addClass('drag-hover');
  46. e.preventDefault();
  47. });
  48. $('.drop-section').on('dragleave', dropzone, function(e) {
  49. var pos_this = $(this).position(),
  50. scroll_top = $(document).scrollTop(),
  51. top = pos_this.top,
  52. left = pos_this.left,
  53. right = left + $(this).width(),
  54. bottom = top + $(this).height(),
  55. mouse_x = e.originalEvent.screenX,
  56. mouse_y = e.originalEvent.clientY + scroll_top;
  57. if (left <= mouse_x && mouse_x <= right &&
  58. top <= mouse_y && mouse_y <= bottom) {
  59. // HACK because dragleave is triggered when hovering children!
  60. return;
  61. }
  62. $(this).removeClass('drag-hover');
  63. });
  64. $('.drop-section').on('dragover', dropzone, function(e) {
  65. e.originalEvent.dataTransfer.dropEffect = "move";
  66. e.preventDefault();
  67. return false;
  68. });
  69. $('.drop-section').on('drop', dropzone, function(e) {
  70. var feed_id = e.originalEvent.dataTransfer.getData('text'),
  71. cat_id = e.target.parentNode.getAttribute('data-cat-id');
  72. loading = true;
  73. $.ajax({
  74. type: 'POST',
  75. url: './?c=feed&a=move',
  76. data : {
  77. f_id: feed_id,
  78. c_id: cat_id
  79. }
  80. }).done(function() {
  81. $(e.target).after(e.originalEvent.dataTransfer.getData('text/html'));
  82. if ($(e.target).hasClass('disabled')) {
  83. $(e.target).remove();
  84. }
  85. dnd_successful = true;
  86. }).always(function() {
  87. loading = false;
  88. });
  89. $(this).removeClass('drag-hover');
  90. e.preventDefault();
  91. });
  92. }
  93. if (document.readyState && document.readyState !== 'loading') {
  94. init_draggable();
  95. } else if (document.addEventListener) {
  96. document.addEventListener('DOMContentLoaded', function () {
  97. init_draggable();
  98. }, false);
  99. }