Projet

Général

Profil

Paste
Télécharger (5,99 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / js / views_bulk_operations.js @ b8087750

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
    $('input.vbo-table-select-all', form).click(function() {
42
      var table = $(this).closest('table:not(.sticky-header)')[0];
43
      $('.vbo-select: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
        var tagName = event.target.tagName.toLowerCase();
61
        if (tagName != 'input' && tagName != 'a' && tagName != 'label') {
62
          $('.vbo-select:not(:disabled)', this).each(function() {
63
            // Always return true for radios, you cannot de-select a radio by clicking on it,
64
            // it should be the same when clicking on a row.
65
            this.checked = $(this).is(':radio') ? true : !this.checked;
66
            $(this).trigger('change');
67
          });
68
        }
69
      });
70
    }
71
  }
72

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

    
86
  Drupal.vbo.initGenericBehaviors = function(form) {
87
    // Show the "select all" fieldset.
88
    $('.vbo-select-all-markup', form).show();
89

    
90
    $('.vbo-select-this-page', form).click(function() {
91
      $('.vbo-select', form).prop('checked', this.checked);
92
      Drupal.vbo.toggleButtonsState(form);
93
      $('.vbo-select-all-pages', form).prop('checked', false);
94

    
95
      // Toggle the "select all" checkbox in grouped tables (if any).
96
      $('.vbo-table-select-all', form).prop('checked', this.checked);
97
    });
98
    $('.vbo-select-all-pages', form).click(function() {
99
      $('.vbo-select', form).prop('checked', this.checked);
100
      Drupal.vbo.toggleButtonsState(form);
101
      $('.vbo-select-this-page', form).prop('checked', false);
102

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

    
106
      // Modify the value of the hidden form field.
107
      $('.select-all-rows', form).val(this.checked);
108
    });
109

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

    
116
    // Handle a "change" event originating either from a row click or an actual checkbox click.
117
    $('.vbo-select', form).change(function() {
118
      // If a checkbox was deselected, uncheck any "select all" checkboxes.
119
      if (!this.checked) {
120
        $('.vbo-select-this-page', form).prop('checked', false);
121
        $('.vbo-select-all-pages', form).prop('checked', false);
122
        // Modify the value of the hidden form field.
123
        $('.select-all-rows', form).val('0')
124

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

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

    
139
      Drupal.vbo.toggleButtonsState(form);
140
    });
141
  }
142

    
143
  Drupal.vbo.toggleButtonsState = function(form) {
144
    // If no rows are checked, disable any form submit actions.
145
    var selectbox = $('select[name="operation"]', form);
146
    var checkedCheckboxes = $('.vbo-select:checked', form);
147
    // The .vbo-prevent-toggle CSS class is added to buttons to prevent toggling
148
    // between disabled and enabled. For example the case of an 'add' button.
149
    var buttons = $('[id^="edit-select"] [type="submit"]:not(.vbo-prevent-toggle)', form);
150

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

    
160
})(jQuery);