Projet

Général

Profil

Paste
Télécharger (6,03 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / js / views_bulk_operations.js @ 7547bb19

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

    
9
  Drupal.behaviors.vbo = {
10
    attach: function(context) {
11
      $('.vbo-views-form', context).each(function() {
12
        Drupal.vbo.initTableBehaviors(this);
13
        Drupal.vbo.initGenericBehaviors(this);
14
        Drupal.vbo.toggleButtonsState(this);
15
      });
16
    }
17
  }
18

    
19
  Drupal.vbo = Drupal.vbo || {};
20
  Drupal.vbo.initTableBehaviors = function(form) {
21
    // If the table is not grouped, "Select all on this page / all pages"
22
    // markup gets inserted below the table header.
23
    var selectAllMarkup = $('.vbo-table-select-all-markup', form);
24
    if (selectAllMarkup.length) {
25
      $('.views-table > tbody', form).prepend('<tr class="views-table-row-select-all even">></tr>');
26
      var colspan = $('table th', form).length;
27
      $('.views-table-row-select-all', form).html('<td colspan="' + colspan + '">' + selectAllMarkup.html() + '</td>');
28

    
29
      $('.vbo-table-select-all-pages', form).click(function() {
30
        Drupal.vbo.tableSelectAllPages(form);
31
        return false;
32
      });
33
      $('.vbo-table-select-this-page', form).click(function() {
34
        Drupal.vbo.tableSelectThisPage(form);
35
        return false;
36
      });
37
    }
38

    
39
    $('.vbo-table-select-all', form).show();
40
    // This is the "select all" checkbox in (each) table header.
41
    $('.vbo-table-select-all', form).click(function() {
42
      var table = $(this).closest('table')[0];
43
      $('input[id^="edit-views-bulk-operations"]:not(:disabled)', table).prop('checked', this.checked);
44
      Drupal.vbo.toggleButtonsState(form);
45

    
46
      // Toggle the visibility of the "select all" row (if any).
47
      if (this.checked) {
48
        $('.views-table-row-select-all', table).show();
49
      }
50
      else {
51
        $('.views-table-row-select-all', table).hide();
52
        // Disable "select all across pages".
53
        Drupal.vbo.tableSelectThisPage(form);
54
      }
55
    });
56

    
57
    // Set up the ability to click anywhere on the row to select it.
58
    if (Drupal.settings.vbo.row_clickable) {
59
      $('.views-table tbody tr', form).click(function(event) {
60
        if (event.target.tagName.toLowerCase() != 'input' && event.target.tagName.toLowerCase() != 'a') {
61
          $('input[id^="edit-views-bulk-operations"]:not(:disabled)', this).each(function() {
62
            var checked = this.checked;
63
            // trigger() toggles the checkmark *after* the event is set,
64
            // whereas manually clicking the checkbox toggles it *beforehand*.
65
            // that's why we manually set the checkmark first, then trigger the
66
            // event (so that listeners get notified), then re-set the checkmark
67
            // which the trigger will have toggled. yuck!
68
            this.checked = !checked;
69
            $(this).trigger('click');
70
            this.checked = !checked;
71
          });
72
        }
73
      });
74
    }
75
  }
76

    
77
  Drupal.vbo.tableSelectAllPages = function(form) {
78
    $('.vbo-table-this-page', form).hide();
79
    $('.vbo-table-all-pages', form).show();
80
    // Modify the value of the hidden form field.
81
    $('.select-all-rows', form).val('1');
82
  }
83
  Drupal.vbo.tableSelectThisPage = function(form) {
84
    $('.vbo-table-all-pages', form).hide();
85
    $('.vbo-table-this-page', form).show();
86
    // Modify the value of the hidden form field.
87
    $('.select-all-rows', form).val('0');
88
  }
89

    
90
  Drupal.vbo.initGenericBehaviors = function(form) {
91
    // Show the "select all" fieldset.
92
    $('.vbo-select-all-markup', form).show();
93

    
94
    $('.vbo-select-this-page', form).click(function() {
95
      $('input[id^="edit-views-bulk-operations"]', form).prop('checked', this.checked);
96
      Drupal.vbo.toggleButtonsState(form);
97
      $('.vbo-select-all-pages', form).prop('checked', false);
98

    
99
      // Toggle the "select all" checkbox in grouped tables (if any).
100
      $('.vbo-table-select-all', form).prop('checked', this.checked);
101
    });
102
    $('.vbo-select-all-pages', form).click(function() {
103
      $('input[id^="edit-views-bulk-operations"]', form).prop('checked', this.checked);
104
      Drupal.vbo.toggleButtonsState(form);
105
      $('.vbo-select-this-page', form).prop('checked', false);
106

    
107
      // Toggle the "select all" checkbox in grouped tables (if any).
108
      $('.vbo-table-select-all', form).prop('checked', this.checked);
109

    
110
      // Modify the value of the hidden form field.
111
      $('.select-all-rows', form).val(this.checked);
112
    });
113

    
114
    // Toggle submit buttons' "disabled" states with the state of the operation
115
    // selectbox.
116
    $('select[name="operation"]', form).change(function () {
117
      Drupal.vbo.toggleButtonsState(form);
118
    });
119

    
120
    $('.vbo-select', form).click(function() {
121
      // If a checkbox was deselected, uncheck any "select all" checkboxes.
122
      if (!this.checked) {
123
        $('.vbo-select-this-page', form).prop('checked', false);
124
        $('.vbo-select-all-pages', form).prop('checked', false);
125
        // Modify the value of the hidden form field.
126
        $('.select-all-rows', form).val('0')
127

    
128
        var table = $(this).closest('table')[0];
129
        if (table) {
130
          // Uncheck the "select all" checkbox in the table header.
131
          $('.vbo-table-select-all', table).prop('checked', false);
132

    
133
          // If there's a "select all" row, hide it.
134
          if ($('.vbo-table-select-this-page', table).length) {
135
            $('.views-table-row-select-all', table).hide();
136
            // Disable "select all across pages".
137
            Drupal.vbo.tableSelectThisPage(form);
138
          }
139
        }
140
      }
141

    
142
      Drupal.vbo.toggleButtonsState(form);
143
    });
144
  }
145

    
146
  Drupal.vbo.toggleButtonsState = function(form) {
147
    // If no rows are checked, disable any form submit actions.
148
    var selectbox = $('select[name="operation"]', form);
149
    var checkedCheckboxes = $('.vbo-select:checked', form);
150
    var buttons = $('[id^="edit-select"] input[type="submit"]', form);
151

    
152
    if (selectbox.length) {
153
      var has_selection = checkedCheckboxes.length && selectbox.val() !== '0';
154
      buttons.prop('disabled', !has_selection);
155
    }
156
    else {
157
      buttons.prop('disabled', !checkedCheckboxes.length);
158
    }
159
  };
160

    
161
})(jQuery);