views_bulk_operations.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. (function ($) {
  2. // Polyfill for jQuery less than 1.6.
  3. if (typeof $.fn.prop != 'function') {
  4. jQuery.fn.extend({
  5. prop: jQuery.fn.attr
  6. });
  7. }
  8. Drupal.behaviors.vbo = {
  9. attach: function(context) {
  10. $('.vbo-views-form', context).each(function() {
  11. Drupal.vbo.initTableBehaviors(this);
  12. Drupal.vbo.initGenericBehaviors(this);
  13. Drupal.vbo.toggleButtonsState(this);
  14. });
  15. }
  16. }
  17. Drupal.vbo = Drupal.vbo || {};
  18. Drupal.vbo.initTableBehaviors = function(form) {
  19. // If the table is not grouped, "Select all on this page / all pages"
  20. // markup gets inserted below the table header.
  21. var selectAllMarkup = $('.vbo-table-select-all-markup', form);
  22. if (selectAllMarkup.length) {
  23. $('.views-table > tbody', form).prepend('<tr class="views-table-row-select-all even">></tr>');
  24. var colspan = $('table th', form).length;
  25. $('.views-table-row-select-all', form).html('<td colspan="' + colspan + '">' + selectAllMarkup.html() + '</td>');
  26. $('.vbo-table-select-all-pages', form).click(function() {
  27. Drupal.vbo.tableSelectAllPages(form);
  28. return false;
  29. });
  30. $('.vbo-table-select-this-page', form).click(function() {
  31. Drupal.vbo.tableSelectThisPage(form);
  32. return false;
  33. });
  34. }
  35. $('.vbo-table-select-all', form).show();
  36. // This is the "select all" checkbox in (each) table header.
  37. $('.vbo-table-select-all', form).click(function() {
  38. var table = $(this).closest('table')[0];
  39. $('input[id^="edit-views-bulk-operations"]:not(:disabled)', table).prop('checked', this.checked);
  40. Drupal.vbo.toggleButtonsState(form);
  41. // Toggle the visibility of the "select all" row (if any).
  42. if (this.checked) {
  43. $('.views-table-row-select-all', table).show();
  44. }
  45. else {
  46. $('.views-table-row-select-all', table).hide();
  47. // Disable "select all across pages".
  48. Drupal.vbo.tableSelectThisPage(form);
  49. }
  50. });
  51. // Set up the ability to click anywhere on the row to select it.
  52. if (Drupal.settings.vbo.row_clickable) {
  53. $('.views-table tbody tr', form).click(function(event) {
  54. if (event.target.tagName.toLowerCase() != 'input' && event.target.tagName.toLowerCase() != 'a') {
  55. $('input[id^="edit-views-bulk-operations"]:not(:disabled)', this).each(function() {
  56. var checked = this.checked;
  57. // trigger() toggles the checkmark *after* the event is set,
  58. // whereas manually clicking the checkbox toggles it *beforehand*.
  59. // that's why we manually set the checkmark first, then trigger the
  60. // event (so that listeners get notified), then re-set the checkmark
  61. // which the trigger will have toggled. yuck!
  62. this.checked = !checked;
  63. $(this).trigger('click');
  64. this.checked = !checked;
  65. });
  66. }
  67. });
  68. }
  69. }
  70. Drupal.vbo.tableSelectAllPages = function(form) {
  71. $('.vbo-table-this-page', form).hide();
  72. $('.vbo-table-all-pages', form).show();
  73. // Modify the value of the hidden form field.
  74. $('.select-all-rows', form).val('1');
  75. }
  76. Drupal.vbo.tableSelectThisPage = function(form) {
  77. $('.vbo-table-all-pages', form).hide();
  78. $('.vbo-table-this-page', form).show();
  79. // Modify the value of the hidden form field.
  80. $('.select-all-rows', form).val('0');
  81. }
  82. Drupal.vbo.initGenericBehaviors = function(form) {
  83. // Show the "select all" fieldset.
  84. $('.vbo-select-all-markup', form).show();
  85. $('.vbo-select-this-page', form).click(function() {
  86. $('input[id^="edit-views-bulk-operations"]', form).prop('checked', this.checked);
  87. Drupal.vbo.toggleButtonsState(form);
  88. $('.vbo-select-all-pages', form).prop('checked', false);
  89. // Toggle the "select all" checkbox in grouped tables (if any).
  90. $('.vbo-table-select-all', form).prop('checked', this.checked);
  91. });
  92. $('.vbo-select-all-pages', form).click(function() {
  93. $('input[id^="edit-views-bulk-operations"]', form).prop('checked', this.checked);
  94. Drupal.vbo.toggleButtonsState(form);
  95. $('.vbo-select-this-page', form).prop('checked', false);
  96. // Toggle the "select all" checkbox in grouped tables (if any).
  97. $('.vbo-table-select-all', form).prop('checked', this.checked);
  98. // Modify the value of the hidden form field.
  99. $('.select-all-rows', form).val(this.checked);
  100. });
  101. // Toggle submit buttons' "disabled" states with the state of the operation
  102. // selectbox.
  103. $('select[name="operation"]', form).change(function () {
  104. Drupal.vbo.toggleButtonsState(form);
  105. });
  106. $('.vbo-select', form).click(function() {
  107. // If a checkbox was deselected, uncheck any "select all" checkboxes.
  108. if (!this.checked) {
  109. $('.vbo-select-this-page', form).prop('checked', false);
  110. $('.vbo-select-all-pages', form).prop('checked', false);
  111. // Modify the value of the hidden form field.
  112. $('.select-all-rows', form).val('0')
  113. var table = $(this).closest('table')[0];
  114. if (table) {
  115. // Uncheck the "select all" checkbox in the table header.
  116. $('.vbo-table-select-all', table).prop('checked', false);
  117. // If there's a "select all" row, hide it.
  118. if ($('.vbo-table-select-this-page', table).length) {
  119. $('.views-table-row-select-all', table).hide();
  120. // Disable "select all across pages".
  121. Drupal.vbo.tableSelectThisPage(form);
  122. }
  123. }
  124. }
  125. Drupal.vbo.toggleButtonsState(form);
  126. });
  127. }
  128. Drupal.vbo.toggleButtonsState = function(form) {
  129. // If no rows are checked, disable any form submit actions.
  130. var selectbox = $('select[name="operation"]', form);
  131. var checkedCheckboxes = $('.vbo-select:checked', form);
  132. var buttons = $('[id^="edit-select"] input[type="submit"]', form);
  133. if (selectbox.length) {
  134. var has_selection = checkedCheckboxes.length && selectbox.val() !== '0';
  135. buttons.prop('disabled', !has_selection);
  136. }
  137. else {
  138. buttons.prop('disabled', !checkedCheckboxes.length);
  139. }
  140. };
  141. })(jQuery);