views_bulk_operations.js 4.9 KB

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