Projet

Général

Profil

Paste
Télécharger (4,86 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views_bulk_operations / js / views_bulk_operations.js @ 13755f8d

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

    
11
  Drupal.vbo = Drupal.vbo || {};
12
  Drupal.vbo.initTableBehaviors = function(form) {
13
    // If the table is not grouped, "Select all on this page / all pages"
14
    // markup gets inserted below the table header.
15
    var selectAllMarkup = $('.vbo-table-select-all-markup', form);
16
    if (selectAllMarkup.length) {
17
      $('.views-table > tbody', form).prepend('<tr class="views-table-row-select-all even">></tr>');
18
      var colspan = $('table th', form).length;
19
      $('.views-table-row-select-all', form).html('<td colspan="' + colspan + '">' + selectAllMarkup.html() + '</td>');
20

    
21
      $('.vbo-table-select-all-pages', form).click(function() {
22
        Drupal.vbo.tableSelectAllPages(form);
23
        return false;
24
      });
25
      $('.vbo-table-select-this-page', form).click(function() {
26
        Drupal.vbo.tableSelectThisPage(form);
27
        return false;
28
      });
29
    }
30

    
31
    $('.vbo-table-select-all', form).show();
32
    // This is the "select all" checkbox in (each) table header.
33
    $('.vbo-table-select-all', form).click(function() {
34
      var table = $(this).closest('table')[0];
35
      $('input[id^="edit-views-bulk-operations"]:not(:disabled)', table).attr('checked', this.checked);
36

    
37
      // Toggle the visibility of the "select all" row (if any).
38
      if (this.checked) {
39
        $('.views-table-row-select-all', table).show();
40
      }
41
      else {
42
        $('.views-table-row-select-all', table).hide();
43
        // Disable "select all across pages".
44
        Drupal.vbo.tableSelectThisPage(form);
45
      }
46
    });
47

    
48
    // Set up the ability to click anywhere on the row to select it.
49
    $('.views-table tbody tr', form).click(function(event) {
50
      if (event.target.tagName.toLowerCase() != 'input' && event.target.tagName.toLowerCase() != 'a') {
51
        $('input[id^="edit-views-bulk-operations"]:not(:disabled)', this).each(function() {
52
          var checked = this.checked;
53
          // trigger() toggles the checkmark *after* the event is set,
54
          // whereas manually clicking the checkbox toggles it *beforehand*.
55
          // that's why we manually set the checkmark first, then trigger the
56
          // event (so that listeners get notified), then re-set the checkmark
57
          // which the trigger will have toggled. yuck!
58
          this.checked = !checked;
59
          $(this).trigger('click');
60
          this.checked = !checked;
61
        });
62
      }
63
    });
64
  }
65

    
66
  Drupal.vbo.tableSelectAllPages = function(form) {
67
    $('.vbo-table-this-page', form).hide();
68
    $('.vbo-table-all-pages', form).show();
69
    // Modify the value of the hidden form field.
70
    $('.select-all-rows', form).val('1');
71
  }
72
  Drupal.vbo.tableSelectThisPage = function(form) {
73
    $('.vbo-table-all-pages', form).hide();
74
    $('.vbo-table-this-page', form).show();
75
    // Modify the value of the hidden form field.
76
    $('.select-all-rows', form).val('0');
77
  }
78

    
79
  Drupal.vbo.initGenericBehaviors = function(form) {
80
    // Show the "select all" fieldset.
81
    $('.vbo-select-all-markup', form).show();
82

    
83
    $('.vbo-select-this-page', form).click(function() {
84
      $('input[id^="edit-views-bulk-operations"]', form).attr('checked', this.checked);
85
      $('.vbo-select-all-pages', form).attr('checked', false);
86

    
87
      // Toggle the "select all" checkbox in grouped tables (if any).
88
      $('.vbo-table-select-all', form).attr('checked', this.checked);
89
    });
90
    $('.vbo-select-all-pages', form).click(function() {
91
      $('input[id^="edit-views-bulk-operations"]', form).attr('checked', this.checked);
92
      $('.vbo-select-this-page', form).attr('checked', false);
93

    
94
      // Toggle the "select all" checkbox in grouped tables (if any).
95
      $('.vbo-table-select-all', form).attr('checked', this.checked);
96

    
97
      // Modify the value of the hidden form field.
98
      $('.select-all-rows', form).val(this.checked);
99
    });
100

    
101
    $('.vbo-select', form).click(function() {
102
      // If a checkbox was deselected, uncheck any "select all" checkboxes.
103
      if (!this.checked) {
104
        $('.vbo-select-this-page', form).attr('checked', false);
105
        $('.vbo-select-all-pages', form).attr('checked', false);
106
        // Modify the value of the hidden form field.
107
        $('.select-all-rows', form).val('0')
108

    
109
        var table = $(this).closest('table')[0];
110
        if (table) {
111
          // Uncheck the "select all" checkbox in the table header.
112
          $('.vbo-table-select-all', table).attr('checked', false);
113

    
114
          // If there's a "select all" row, hide it.
115
          if ($('.vbo-table-select-this-page', table).length) {
116
            $('.views-table-row-select-all', table).hide();
117
            // Disable "select all across pages".
118
            Drupal.vbo.tableSelectThisPage(form);
119
          }
120
        }
121
      }
122
    });
123
  }
124

    
125
})(jQuery);