nodequeue_dragdrop.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. (function ($) {
  2. Drupal.behaviors.nodequeueDrag = {
  3. attach: function(context) {
  4. $('.nodequeue-dragdrop').each(function() {
  5. var table_id = $(this).attr('id');
  6. var tableDrag = Drupal.tableDrag[table_id];
  7. tableDrag.onDrop = function() {
  8. $('#' + table_id + ' td.position').each(function(i) {
  9. $(this).html(i + 1);
  10. });
  11. nodequeueUpdateNodePositions(table_id);
  12. }
  13. });
  14. }
  15. };
  16. Drupal.behaviors.nodequeueReverse = {
  17. attach: function(context) {
  18. $('#edit-actions-reverse').click(function() {
  19. var $table = $(this).parent().parent().find('.nodequeue-dragdrop:first');
  20. var table_id = $table.attr('id');
  21. // Reverse table rows.
  22. $table.find('tr.draggable').each(function(i) {
  23. $table.find('tbody').prepend(this);
  24. });
  25. nodequeueUpdateNodePositions(table_id);
  26. nodequeueInsertChangedWarning(table_id);
  27. nodequeueRestripeTable(table_id);
  28. return false;
  29. });
  30. }
  31. };
  32. Drupal.behaviors.nodequeueShuffle = {
  33. attach: function(context) {
  34. $('#edit-actions-shuffle').click(function() {
  35. var $table = $(this).parent().parent().find('.nodequeue-dragdrop:first');
  36. var table_id = $table.attr('id');
  37. // Randomize table rows.
  38. var rows = $('#' + table_id + ' tbody tr:not(:hidden)').get();
  39. rows.sort(function(){return (Math.round(Math.random())-0.5);});
  40. $.each(rows, function(i, row) {
  41. $('#' + table_id + ' tbody').prepend(this);
  42. });
  43. nodequeueUpdateNodePositions(table_id);
  44. nodequeueInsertChangedWarning(table_id);
  45. nodequeueRestripeTable(table_id);
  46. return false;
  47. });
  48. }
  49. };
  50. Drupal.behaviors.nodequeueClear = {
  51. attach: function(context) {
  52. $('#edit-actions-clear').click(function() {
  53. var $table = $(this).parent().parent().find('.nodequeue-dragdrop:first');
  54. var table_id = $table.attr('id');
  55. // Mark nodes for removal.
  56. $('#' + table_id + ' .node-position').each(function(i) {
  57. $(this).val('r');
  58. });
  59. // Remove table rows.
  60. rows = $('#' + table_id + ' tbody tr:not(:hidden)').hide();
  61. nodequeuePrependEmptyMessage(table_id);
  62. nodequeueInsertChangedWarning(table_id);
  63. return false;
  64. });
  65. }
  66. };
  67. Drupal.behaviors.nodequeueRemoveNode = {
  68. attach: function(context) {
  69. $('a.nodequeue-remove').css('display', 'block');
  70. $('a.nodequeue-remove').click(function() {
  71. a = $(this).attr('id');
  72. a = '#' + a.replace('nodequeue-remove-', 'edit-') + '-position';
  73. $(a).val('r');
  74. // Hide the current row.
  75. $(this).parent().parent().fadeOut('fast', function() {
  76. var $table = $(this).parent().parent();
  77. var table_id = $table.attr('id');
  78. if ($('#' + table_id + ' tbody tr:not(:hidden)').size() == 0) {
  79. nodequeuePrependEmptyMessage(table_id);
  80. }
  81. else {
  82. nodequeueRestripeTable(table_id)
  83. nodequeueInsertChangedWarning(table_id);
  84. }
  85. });
  86. return false;
  87. });
  88. }
  89. }
  90. Drupal.behaviors.nodequeueClearTitle = {
  91. attach: function(context) {
  92. $('.subqueue-add-nid').focus(function() {
  93. if (this.value == this.defaultValue) {
  94. this.value = '';
  95. $(this).css('color', '#000');
  96. }
  97. }).blur(function() {
  98. if (!this.value.length) {
  99. this.value = this.defaultValue;
  100. $(this).css('color', '#999');
  101. }
  102. });
  103. }
  104. }
  105. /**
  106. * Updates node positions after nodequeue has been rearranged.
  107. * It cares about the reverse order and populates nodes the other way round.
  108. */
  109. function nodequeueUpdateNodePositions(table_id) {
  110. // Check if reverse option is set.
  111. var reverse = Drupal.settings.nodequeue.reverse[table_id.replace(/-/g, '_')];
  112. var size = reverse ? $('#' + table_id + ' .node-position').size() : 1;
  113. $('#' + table_id + ' tr').filter(":visible").find('.node-position').each(function(i) {
  114. $(this).val(size);
  115. reverse ? size-- : size++;
  116. });
  117. }
  118. /**
  119. * Restripe the nodequeue table after removing an element or changing the
  120. * order of the elements.
  121. */
  122. function nodequeueRestripeTable(table_id) {
  123. $('#' + table_id + ' tbody tr:not(:hidden)')
  124. .filter(':odd')
  125. .removeClass('odd').addClass('even')
  126. .end()
  127. .filter(':even')
  128. .removeClass('even').addClass('odd')
  129. .end();
  130. $('#' + table_id + ' tr:visible td.position').each(function(i) {
  131. $(this).html(i + 1);
  132. });
  133. }
  134. /**
  135. * Add a row to the nodequeue table explaining that the queue is empty.
  136. */
  137. function nodequeuePrependEmptyMessage(table_id) {
  138. $('#' + table_id + ' tbody').prepend('<tr class="odd"><td colspan="6">'+Drupal.t('No nodes in this queue.')+'</td></tr>');
  139. }
  140. /**
  141. * Display a warning reminding the user to save the nodequeue.
  142. */
  143. function nodequeueInsertChangedWarning(table_id) {
  144. if (Drupal.tableDrag[table_id].changed == false) {
  145. $(Drupal.theme('tableDragChangedWarning')).insertAfter('#' + table_id).hide().fadeIn('slow');
  146. Drupal.tableDrag[table_id].changed = true;
  147. }
  148. }
  149. })(jQuery);