category.js 2.8 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. $.event.props.push('dataTransfer');
  33. var draggable = '[draggable="true"]',
  34. dropzone = '[dropzone="move"]';
  35. $('.drop-section').on('dragstart', draggable, function(e) {
  36. e.dataTransfer.effectAllowed = 'move';
  37. e.dataTransfer.setData('text/html', e.target.outerHTML);
  38. e.dataTransfer.setData('text', e.target.getAttribute('data-feed-id'));
  39. e.target.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.dataTransfer.dropEffect = "move";
  67. e.preventDefault();
  68. return false;
  69. });
  70. $('.drop-section').on('drop', dropzone, function(e) {
  71. var feed_id = e.dataTransfer.getData('text'),
  72. cat_id = e.target.parentNode.getAttribute('data-cat-id');
  73. loading = true;
  74. $.ajax({
  75. type: 'POST',
  76. url: './?c=feed&a=move',
  77. data : {
  78. f_id: feed_id,
  79. c_id: cat_id
  80. }
  81. }).success(function() {
  82. $(e.target).after(e.dataTransfer.getData('text/html'));
  83. if ($(e.target).hasClass('disabled')) {
  84. $(e.target).remove();
  85. }
  86. dnd_successful = true;
  87. }).complete(function() {
  88. loading = false;
  89. });
  90. $(this).removeClass('drag-hover');
  91. e.preventDefault();
  92. });
  93. }
  94. if (document.readyState && document.readyState !== 'loading') {
  95. init_draggable();
  96. } else if (document.addEventListener) {
  97. document.addEventListener('DOMContentLoaded', function () {
  98. init_draggable();
  99. }, false);
  100. }