Projet

Général

Profil

Paste
Télécharger (9,62 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / module_filter / js / module_filter_tab.js @ dc45a079

1

    
2
(function ($) {
3
  Drupal.ModuleFilter = Drupal.ModuleFilter || {};
4
  Drupal.ModuleFilter.textFilter = '';
5
  Drupal.ModuleFilter.timeout;
6
  Drupal.ModuleFilter.tabs = {};
7
  Drupal.ModuleFilter.enabling = {};
8
  Drupal.ModuleFilter.disabling = {};
9

    
10
  Drupal.behaviors.moduleFilter = {
11
    attach: function() {
12
      // Set the focus on the module filter textfield.
13
      $('input[name="module_filter[name]"]').focus();
14

    
15
      $('#module-filter-squeeze').css('min-height', $('#module-filter-tabs').height());
16

    
17
      $('#module-filter-left a.project-tab').each(function(i) {
18
        Drupal.ModuleFilter.tabs[$(this).attr('id')] = new Drupal.ModuleFilter.Tab(this);
19
      });
20

    
21
      // Move anchors to top of tabs.
22
      $('a.anchor', $('#module-filter-left')).remove().prependTo('#module-filter-tabs');
23

    
24
      $('input[name="module_filter[name]"]').keyup(function(e) {
25
        switch (e.which) {
26
          case 13:
27
            if (Drupal.ModuleFilter.timeout) {
28
              clearTimeout(Drupal.ModuleFilter.timeout);
29
            }
30

    
31
            Drupal.ModuleFilter.filter(Drupal.ModuleFilter.textFilter);
32
            break;
33
          default:
34
            if (Drupal.ModuleFilter.textFilter != $(this).val()) {
35
              Drupal.ModuleFilter.textFilter = this.value;
36
              if (Drupal.ModuleFilter.timeout) {
37
                clearTimeout(Drupal.ModuleFilter.timeout);
38
              }
39
              Drupal.ModuleFilter.timeout = setTimeout('Drupal.ModuleFilter.filter("' + Drupal.ModuleFilter.textFilter + '")', 500);
40
            }
41
            break;
42
        }
43
      });
44
      $('input[name="module_filter[name]"]').keypress(function(e) {
45
        if (e.which == 13) e.preventDefault();
46
      });
47

    
48
      Drupal.ModuleFilter.showEnabled = $('#edit-module-filter-show-enabled').is(':checked');
49
      $('#edit-module-filter-show-enabled').change(function() {
50
        Drupal.ModuleFilter.showEnabled = $(this).is(':checked');
51
        Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
52
      });
53
      Drupal.ModuleFilter.showDisabled = $('#edit-module-filter-show-disabled').is(':checked');
54
      $('#edit-module-filter-show-disabled').change(function() {
55
        Drupal.ModuleFilter.showDisabled = $(this).is(':checked');
56
        Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
57
      });
58
      Drupal.ModuleFilter.showRequired = $('#edit-module-filter-show-required').is(':checked');
59
      $('#edit-module-filter-show-required').change(function() {
60
        Drupal.ModuleFilter.showRequired = $(this).is(':checked');
61
        Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
62
      });
63
      Drupal.ModuleFilter.showUnavailable = $('#edit-module-filter-show-unavailable').is(':checked');
64
      $('#edit-module-filter-show-unavailable').change(function() {
65
        Drupal.ModuleFilter.showUnavailable = $(this).is(':checked');
66
        Drupal.ModuleFilter.filter($('input[name="module_filter[name]"]').val());
67
      });
68

    
69
      if (Drupal.settings.moduleFilter.visualAid == 1) {
70
        $('table.package tbody td.checkbox input').change(function() {
71
          if ($(this).is(':checked')) {
72
            Drupal.ModuleFilter.updateVisualAid('enable', $(this).parents('tr'));
73
          }
74
          else {
75
            Drupal.ModuleFilter.updateVisualAid('disable', $(this).parents('tr'));
76
          }
77
        });
78
      }
79

    
80
      // Check for anchor.
81
      var url = document.location.toString();
82
      if (url.match('#')) {
83
        // Make tab active based on anchor.
84
        var anchor = '#' + url.split('#')[1];
85
        $('a[href="' + anchor + '"]').click();
86
      }
87
      // Else if no active tab is defined, set it to the all tab.
88
      else if (Drupal.ModuleFilter.activeTab == undefined) {
89
        Drupal.ModuleFilter.activeTab = Drupal.ModuleFilter.tabs['all-tab'];
90
      }
91
    }
92
  }
93

    
94
  Drupal.ModuleFilter.visible = function(checkbox) {
95
    if (checkbox.length > 0) {
96
      if (Drupal.ModuleFilter.showEnabled) {
97
        if ($(checkbox).is(':checked') && !$(checkbox).is(':disabled')) {
98
          return true;
99
        }
100
      }
101
      if (Drupal.ModuleFilter.showDisabled) {
102
        if (!$(checkbox).is(':checked') && !$(checkbox).is(':disabled')) {
103
          return true;
104
        }
105
      }
106
      if (Drupal.ModuleFilter.showRequired) {
107
        if ($(checkbox).is(':checked') && $(checkbox).is(':disabled')) {
108
          return true;
109
        }
110
      }
111
    }
112
    if (Drupal.ModuleFilter.showUnavailable) {
113
      if (checkbox.length == 0 || (!$(checkbox).is(':checked') && $(checkbox).is(':disabled'))) {
114
        return true;
115
      }
116
    }
117
    return false;
118
  }
119

    
120
  Drupal.ModuleFilter.filter = function(string) {
121
    var stringLowerCase = string.toLowerCase();
122
    var flip = 'odd';
123

    
124
    if (Drupal.ModuleFilter.activeTab.id == 'all-tab') {
125
      var selector = 'table.package tbody tr td label > strong';
126
    }
127
    else {
128
      var selector = 'table.package tbody tr.' + Drupal.ModuleFilter.activeTab.id + '-content td label strong';
129
    }
130

    
131
    $(selector).each(function(i) {
132
      var $row = $(this).parents('tr');
133
      var module = $(this).text();
134
      var moduleLowerCase = module.toLowerCase();
135

    
136
      if (moduleLowerCase.match(stringLowerCase)) {
137
        if (Drupal.ModuleFilter.visible($('td.checkbox :checkbox', $row))) {
138
          $row.removeClass('odd even');
139
          $row.addClass(flip);
140
          $row.show();
141
          flip = (flip == 'odd') ? 'even' : 'odd';
142
        }
143
        else {
144
          $row.hide();
145
        }
146
      }
147
      else {
148
        $row.hide();
149
      }
150
    });
151
  }
152

    
153
  Drupal.ModuleFilter.Tab = function(element) {
154
    this.id = $(element).attr('id');
155
    this.element = element;
156

    
157
    $(this.element).click(function() {
158
      Drupal.ModuleFilter.tabs[$(this).attr('id')].setActive();
159
    });
160
  }
161

    
162
  Drupal.ModuleFilter.Tab.prototype.setActive = function() {
163
    if (Drupal.ModuleFilter.activeTab) {
164
      $(Drupal.ModuleFilter.activeTab.element).parent().removeClass('active');
165
    }
166
    // Assume the default active tab is #all-tab. Remove its active class.
167
    else {
168
      $('#all-tab').parent().removeClass('active');
169
    }
170

    
171
    Drupal.ModuleFilter.activeTab = this;
172
    $(Drupal.ModuleFilter.activeTab.element).parent().addClass('active');
173
    Drupal.ModuleFilter.activeTab.displayRows();
174

    
175
    // Clear filter textfield and refocus on it.
176
    $('input[name="module_filter[name]"]').val('');
177
    $('input[name="module_filter[name]"]').focus();
178
  }
179

    
180
  Drupal.ModuleFilter.Tab.prototype.displayRows = function() {
181
    var flip = 'odd';
182
    var selector = (Drupal.ModuleFilter.activeTab.id == 'all-tab') ? 'table.package tbody tr' : 'table.package tbody tr.' + this.id + '-content';
183
    $('table.package tbody tr').hide();
184
    $('table.package tbody tr').removeClass('odd even');
185
    $(selector).each(function(i) {
186
      if (Drupal.ModuleFilter.visible($('td.checkbox input', $(this)))) {
187
        $(this).addClass(flip);
188
        flip = (flip == 'odd') ? 'even' : 'odd';
189
        $(this).show();
190
      }
191
    });
192
  }
193

    
194
  Drupal.ModuleFilter.Tab.prototype.updateEnabling = function(amount) {
195
    this.enabling = this.enabling || 0;
196
    this.enabling += amount;
197
    if (this.enabling == 0) {
198
      delete(this.enabling);
199
    }
200
  }
201

    
202
  Drupal.ModuleFilter.Tab.prototype.updateDisabling = function(amount) {
203
    this.disabling = this.disabling || 0;
204
    this.disabling += amount;
205
    if (this.disabling == 0) {
206
      delete(this.disabling);
207
    }
208
  }
209

    
210
  Drupal.ModuleFilter.Tab.prototype.updateVisualAid = function() {
211
    var visualAid = '';
212
    if (this.enabling != undefined) {
213
      visualAid += '<span class="enabling">' + Drupal.t('+@count', { '@count': this.enabling }) + '</span>';
214
    }
215
    if (this.disabling != undefined) {
216
      visualAid += '<span class="disabling">' + Drupal.t('-@count', { '@count': this.disabling }) + '</span>';
217
    }
218

    
219
    if (!$('span.visual-aid', $(this.element)).size() && visualAid != '') {
220
      $(this.element).prepend('<span class="visual-aid"></span>');
221
    }
222

    
223
    $('span.visual-aid', $(this.element)).empty().append(visualAid);
224
  }
225

    
226
  Drupal.ModuleFilter.updateVisualAid = function(type, row) {
227
    // Find row class.
228
    var classes = row.attr('class').split(' ');
229
    for (var i in classes) {
230
      // Remove '-content' so we can use as id.
231
      var id = classes[i].substring(0, classes[i].length - 8);
232
      if (Drupal.ModuleFilter.tabs[id] != undefined) {
233
        break;
234
      }
235
    }
236

    
237
    if (Drupal.ModuleFilter.activeTab.id == 'all-tab') {
238
      var allTab = Drupal.ModuleFilter.activeTab;
239
      var projectTab = Drupal.ModuleFilter.tabs[id];
240
    }
241
    else {
242
      var allTab = Drupal.ModuleFilter.tabs['all-tab'];
243
      var projectTab = Drupal.ModuleFilter.activeTab;
244
    }
245

    
246
    var name = $('td label strong', row).text();
247
    switch (type) {
248
      case 'enable':
249
        if (Drupal.ModuleFilter.disabling[id + name] != undefined) {
250
          delete(Drupal.ModuleFilter.disabling[id + name]);
251
          allTab.updateDisabling(-1);
252
          projectTab.updateDisabling(-1);
253
          row.removeClass('disabling');
254
        }
255
        else {
256
          Drupal.ModuleFilter.enabling[id + name] = name;
257
          allTab.updateEnabling(1);
258
          projectTab.updateEnabling(1);
259
          row.addClass('enabling');
260
        }
261
        break;
262
      case 'disable':
263
        if (Drupal.ModuleFilter.enabling[id + name] != undefined) {
264
          delete(Drupal.ModuleFilter.enabling[id + name]);
265
          allTab.updateEnabling(-1);
266
          projectTab.updateEnabling(-1);
267
          row.removeClass('enabling');
268
        }
269
        else {
270
          Drupal.ModuleFilter.disabling[id + name] = name;
271
          allTab.updateDisabling(1);
272
          projectTab.updateDisabling(1);
273
          row.addClass('disabling');
274
        }
275
        break;
276
    }
277

    
278
    allTab.updateVisualAid();
279
    projectTab.updateVisualAid();
280
  }
281
})(jQuery);