Projet

Général

Profil

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

root / drupal7 / sites / all / modules / module_filter / js / module_filter_tab.js @ 7d7b5830

1

    
2
(function ($) {
3

    
4
Drupal.ModuleFilter.tabs = {};
5
Drupal.ModuleFilter.enabling = {};
6
Drupal.ModuleFilter.disabling = {};
7

    
8
Drupal.ModuleFilter.jQueryIsNewer = function() {
9
  if (Drupal.ModuleFilter.jQueryNewer == undefined) {
10
    var v1parts = $.fn.jquery.split('.');
11
    var v2parts = new Array('1', '4', '4');
12

    
13
    for (var i = 0; i < v1parts.length; ++i) {
14
      if (v2parts.length == i) {
15
        Drupal.ModuleFilter.jQueryNewer = true;
16
        return Drupal.ModuleFilter.jQueryNewer;
17
      }
18

    
19
      if (v1parts[i] == v2parts[i]) {
20
        continue;
21
      }
22
      else if (v1parts[i] > v2parts[i]) {
23
        Drupal.ModuleFilter.jQueryNewer = true;
24
        return Drupal.ModuleFilter.jQueryNewer;
25
      }
26
      else {
27
        Drupal.ModuleFilter.jQueryNewer = false;
28
        return Drupal.ModuleFilter.jQueryNewer;
29
      }
30
    }
31

    
32
    if (v1parts.length != v2parts.length) {
33
      Drupal.ModuleFilter.jQueryNewer = false;
34
      return Drupal.ModuleFilter.jQueryNewer;
35
    }
36

    
37
    Drupal.ModuleFilter.jQueryNewer = false;
38
  }
39
  return Drupal.ModuleFilter.jQueryNewer;
40
};
41

    
42
Drupal.behaviors.moduleFilterTabs = {
43
  attach: function(context) {
44
    if (Drupal.settings.moduleFilter.tabs) {
45
      $('#module-filter-wrapper table:not(.sticky-header)', context).once('module-filter-tabs', function() {
46
        var $modules = $('#module-filter-modules');
47
        var moduleFilter = $('input[name="module_filter[name]"]').data('moduleFilter');
48
        var table = $(this);
49

    
50
        $('thead', table).show();
51

    
52
        // Remove package header rows.
53
        $('tr.admin-package-header', table).remove();
54

    
55
        var $tabsWrapper = $('<div id="module-filter-tabs"></div>');
56

    
57
        // Build tabs from package title rows.
58
        var tabs = '<ul>';
59
        for (var i in Drupal.settings.moduleFilter.packageIDs) {
60
          var id = Drupal.settings.moduleFilter.packageIDs[i];
61

    
62
          var name = id;
63
          var tabClass = 'project-tab';
64
          var title = null;
65
          var summary = (Drupal.settings.moduleFilter.countEnabled) ? '<span class="count">' + Drupal.ModuleFilter.countSummary(id) + '</span>' : '';
66

    
67
          switch (id) {
68
            case 'all':
69
              name = Drupal.t('All');
70
              break;
71
            case 'new':
72
              name = Drupal.t('New');
73
              title = Drupal.t('Modules installed within the last week.');
74
              if (Drupal.settings.moduleFilter.enabledCounts['new'].total == 0) {
75
                tabClass += ' disabled';
76
                summary += '<span>' + Drupal.t('No modules added within the last week.') + '</span>';
77
              }
78
              break;
79
            case 'recent':
80
              name = Drupal.t('Recent');
81
              title = Drupal.t('Modules enabled/disabled within the last week.');
82
              if (Drupal.settings.moduleFilter.enabledCounts['recent'].total == 0) {
83
                tabClass += ' disabled';
84
                summary += '<span>' + Drupal.t('No modules were enabled or disabled within the last week.') + '</span>';
85
              }
86
              break;
87
            default: 
88
              var $row = $('#' + id + '-package');
89
              name = $.trim($row.text());
90
              $row.remove();
91
              break;
92
          }
93

    
94
          tabs += '<li id="' + id + '-tab" class="' + tabClass + '"><a href="#' + id + '" class="overlay-exclude"' + (title ? ' title="' + title + '"' : '') + '><strong>' + name + '</strong><span class="summary">' + summary + '</span></a></li>';
95
        }
96
        tabs += '</ul>';
97
        $tabsWrapper.append(tabs);
98
        $modules.before($tabsWrapper);
99

    
100
        // Index tabs.
101
        $('#module-filter-tabs li').each(function() {
102
          var $tab = $(this);
103
          var id = $tab.attr('id');
104
          Drupal.ModuleFilter.tabs[id] = new Drupal.ModuleFilter.Tab($tab, id);
105
        });
106

    
107
        $('tbody td.checkbox input', $modules).change(function() {
108
          var $checkbox = $(this);
109
          var key = $checkbox.parents('tr').data('indexKey');
110

    
111
          moduleFilter.index[key].status = $checkbox.is(':checked');
112

    
113
          if (Drupal.settings.moduleFilter.visualAid) {
114
            var type = ($checkbox.is(':checked')) ? 'enable' : 'disable';
115
            Drupal.ModuleFilter.updateVisualAid(type, $checkbox.parents('tr'));
116
          }
117
        });
118

    
119
        // Sort rows.
120
        var rows = $('tbody tr.module', table).get();
121
        rows.sort(function(a, b) {
122
          var compA = $('td:nth(1)', a).text().toLowerCase();
123
          var compB = $('td:nth(1)', b).text().toLowerCase();
124
          return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
125
        });
126
        $.each(rows, function(idx, itm) { table.append(itm); });
127

    
128
        // Re-stripe rows.
129
        $('tr.module', table)
130
          .removeClass('odd even')
131
          .filter(':odd').addClass('even').end()
132
          .filter(':even').addClass('odd');
133

    
134
        moduleFilter.adjustHeight();
135

    
136
        moduleFilter.element.bind('moduleFilter:start', function() {
137
          moduleFilter.tabResults = {
138
            'all-tab': { items: {}, count: 0 },
139
            'recent-tab': { items: {}, count: 0 },
140
            'new-tab': { items: {}, count: 0 }
141
          };
142

    
143
          // Empty result info from tabs.
144
          for (var i in Drupal.ModuleFilter.tabs) {
145
            if (Drupal.ModuleFilter.tabs[i].resultInfo != undefined) {
146
              Drupal.ModuleFilter.tabs[i].resultInfo.empty();
147
            }
148
          }
149
        });
150

    
151
        moduleFilter.element.bind('moduleFilter:finish', function(e, data) {
152
          $.each(moduleFilter.index, function(key, item) {
153
            if (!item.element.hasClass('js-hide')) {
154
              var id = Drupal.ModuleFilter.getTabID(item.element);
155

    
156
              if (moduleFilter.tabResults[id] == undefined) {
157
                moduleFilter.tabResults[id] = { items: {}, count: 0 };
158
              }
159
              if (moduleFilter.tabResults[id].items[item.key] == undefined) {
160
                // All tab
161
                moduleFilter.tabResults['all-tab'].count++;
162

    
163
                // Recent tab
164
                if (item.element.hasClass('recent-module')) {
165
                  moduleFilter.tabResults['recent-tab'].count++;
166
                }
167

    
168
                // New tab
169
                if (item.element.hasClass('new-module')) {
170
                  moduleFilter.tabResults['new-tab'].count++;
171
                }
172

    
173
                moduleFilter.tabResults[id].items[item.key] = item;
174
                moduleFilter.tabResults[id].count++;
175
              }
176

    
177
              if (Drupal.ModuleFilter.activeTab != undefined && Drupal.ModuleFilter.activeTab.id != 'all-tab') {
178
                if ((Drupal.ModuleFilter.activeTab.id == 'recent-tab' && !item.element.hasClass('recent-module')) || (Drupal.ModuleFilter.activeTab.id == 'new-tab' && !item.element.hasClass('new-module')) || (Drupal.ModuleFilter.activeTab.id != 'recent-tab' && Drupal.ModuleFilter.activeTab.id != 'new-tab' && id != Drupal.ModuleFilter.activeTab.id)) {
179
                  // The item is not in the active tab, so hide it.
180
                  item.element.addClass('js-hide');
181
                }
182
              }
183
            }
184
          });
185

    
186
          if (Drupal.settings.moduleFilter.visualAid) {
187
            if (moduleFilter.text) {
188
              // Add result info to tabs.
189
              for (var id in moduleFilter.tabResults) {
190
                var tab = Drupal.ModuleFilter.tabs[id];
191

    
192
                if (tab.resultInfo == undefined) {
193
                  var resultInfo = '<span class="result-info"></span>'
194
                  $('a', tab.element).prepend(resultInfo);
195
                  tab.resultInfo = $('span.result-info', tab.element);
196
                }
197

    
198
                tab.resultInfo.append(moduleFilter.tabResults[id].count);
199
              }
200

    
201
              if (Drupal.settings.moduleFilter.hideEmptyTabs) {
202
                for (var id in Drupal.ModuleFilter.tabs) {
203
                  if (moduleFilter.tabResults[id] != undefined) {
204
                    Drupal.ModuleFilter.tabs[id].element.show();
205
                  }
206
                  else if (Drupal.ModuleFilter.activeTab == undefined || Drupal.ModuleFilter.activeTab.id != id) {
207
                    Drupal.ModuleFilter.tabs[id].element.hide();
208
                  }
209
                }
210
              }
211
            }
212
            else {
213
              // Make sure all tabs are visible.
214
              if (Drupal.settings.moduleFilter.hideEmptyTabs) {
215
                $('#module-filter-tabs li').show();
216
              }
217
            }
218
          }
219

    
220
          if ((Drupal.ModuleFilter.activeTab != undefined && (moduleFilter.tabResults[Drupal.ModuleFilter.activeTab.id] == undefined || moduleFilter.tabResults[Drupal.ModuleFilter.activeTab.id].count <= 0))) {
221
            // The current tab contains no results.
222
            moduleFilter.results = 0;
223
          }
224

    
225
          moduleFilter.adjustHeight();
226
        });
227

    
228
        if (Drupal.settings.moduleFilter.useURLFragment) {
229
          $(window).bind('hashchange.module-filter', $.proxy(Drupal.ModuleFilter, 'eventHandlerOperateByURLFragment')).triggerHandler('hashchange.module-filter');
230
        }
231
        else {
232
          Drupal.ModuleFilter.selectTab();
233
        }
234

    
235
        if (Drupal.settings.moduleFilter.useSwitch) {
236
          $('td.checkbox div.form-item').hide();
237
          $('td.checkbox').each(function(i) {
238
            var $cell = $(this);
239
            var $checkbox = $(':checkbox', $cell);
240
            var $switch = $('.toggle-enable', $cell);
241
            $switch.removeClass('js-hide').click(function() {
242
              if (!$(this).hasClass('disabled')) {
243
                if (Drupal.ModuleFilter.jQueryIsNewer()) {
244
                  $checkbox.click();
245
                }
246
                else {
247
                  $checkbox.click().change();
248
                }
249
              }
250
            });
251
            $checkbox.click(function() {
252
              if (!$switch.hasClass('disabled')) {
253
                $switch.toggleClass('off');
254
              }
255
            });
256
          });
257
        }
258

    
259
        var $tabs = $('#module-filter-tabs');
260

    
261
        function getParentTopOffset($obj, offset) {
262
          var $parent = $obj.offsetParent();
263
          if ($obj[0] != $parent[0]) {
264
            offset += $parent.position().top;
265
            return getParentTopOffset($parent, offset);
266
          }
267
          return offset;
268
        }
269

    
270
        var tabsTopOffset = null;
271
        function getParentsTopOffset() {
272
          if (tabsTopOffset === null) {
273
            tabsTopOffset = getParentTopOffset($tabs.parent(), 0);
274
          }
275
          return tabsTopOffset;
276
        }
277

    
278
        function viewportTop() {
279
          var top = $(window).scrollTop();
280
          return top;
281
        }
282

    
283
        function viewportBottom() {
284
          var top = $(window).scrollTop();
285
          var bottom = top + $(window).height();
286

    
287
          bottom -= $('#page-actions').height();
288

    
289
          return bottom;
290
        }
291

    
292
        function fixToTop(top) {
293
          if ($tabs.hasClass('bottom-fixed')) {
294
            $tabs.css({
295
              'position': 'absolute',
296
              'top': $tabs.position().top - getParentsTopOffset(),
297
              'bottom': 'auto'
298
            });
299
            $tabs.removeClass('bottom-fixed');
300
          }
301

    
302
          if (($tabs.css('position') == 'absolute' && $tabs.offset().top - top >= 0) || ($tabs.css('position') != 'absolute' && $tabs.offset().top - top <= 0)) {
303
            $tabs.addClass('top-fixed');
304
            $tabs.attr('style', '');
305
          }
306
        }
307

    
308
        function fixToBottom(bottom) {
309
          if ($tabs.hasClass('top-fixed')) {
310
            $tabs.css({
311
              'position': 'absolute',
312
              'top': $tabs.position().top - getParentsTopOffset(),
313
              'bottom': 'auto'
314
            });
315
            $tabs.removeClass('top-fixed');
316
          }
317

    
318
          if ($tabs.offset().top + $tabs.height() - bottom <= 0) {
319
            $tabs.addClass('bottom-fixed');
320
            var style = '';
321
            var pageActionsHeight = $('#page-actions').height();
322
            if (pageActionsHeight > 0) {
323
              style = 'bottom: ' + pageActionsHeight + 'px';
324
            }
325
            else if (Drupal.settings.moduleFilter.dynamicPosition) {
326
              // style = 'bottom: ' + $('#module-filter-submit', $tabs).height() + 'px';
327
            }
328
            $tabs.attr('style', style);
329
          }
330
        }
331

    
332
        var lastTop = 0;
333
        $(window).scroll(function() {
334
          var top = viewportTop();
335
          var bottom = viewportBottom();
336

    
337
          if ($modules.offset().top >= top) {
338
            $tabs.removeClass('top-fixed').attr('style', '');
339
          }
340
          else {
341
            if (top > lastTop) { // Downward scroll.
342
              if ($tabs.height() > bottom - top) {
343
                fixToBottom(bottom);
344
              }
345
              else {
346
                fixToTop(top);
347
              }
348
            }
349
            else { // Upward scroll.
350
              fixToTop(top);
351
            }
352
          }
353
          lastTop = top;
354
        });
355

    
356
        moduleFilter.adjustHeight();
357
      });
358
    }
359
  }
360
};
361

    
362
Drupal.ModuleFilter.Tab = function(element, id) {
363
  var self = this;
364

    
365
  this.id = id;
366
  this.hash = id.substring(0, id.length - 4);
367
  this.element = element;
368

    
369
  $('a', this.element).click(function() {
370
    if (!Drupal.settings.moduleFilter.useURLFragment) {
371
      var hash = (!self.element.hasClass('selected')) ? self.hash : 'all';
372
      Drupal.ModuleFilter.selectTab(hash);
373
      return false;
374
    }
375

    
376
    if (self.element.hasClass('selected')) {
377
      // Clear the active tab.
378
      window.location.hash = 'all';
379
      return false;
380
    }
381
  });
382

    
383
  $('tr.' + this.id, $('#system-modules')).hover(
384
    function() {
385
      self.element.addClass('suggest');
386
    },
387
    function() {
388
      self.element.removeClass('suggest');
389
    }
390
  );
391
};
392

    
393
Drupal.ModuleFilter.selectTab = function(hash) {
394
  if (!hash || Drupal.ModuleFilter.tabs[hash + '-tab'] == undefined || Drupal.settings.moduleFilter.enabledCounts[hash].total == 0) {
395
    if (Drupal.settings.moduleFilter.rememberActiveTab) {
396
      var activeTab = Drupal.ModuleFilter.getState('activeTab');
397
      if (activeTab && Drupal.ModuleFilter.tabs[activeTab + '-tab'] != undefined) {
398
        hash = activeTab;
399
      }
400
    }
401

    
402
    if (!hash) {
403
      hash = 'all';
404
    }
405
  }
406

    
407
  if (Drupal.ModuleFilter.activeTab != undefined) {
408
    Drupal.ModuleFilter.activeTab.element.removeClass('selected');
409
  }
410

    
411
  Drupal.ModuleFilter.activeTab = Drupal.ModuleFilter.tabs[hash + '-tab'];
412
  Drupal.ModuleFilter.activeTab.element.addClass('selected');
413

    
414
  var moduleFilter = $('input[name="module_filter[name]"]').data('moduleFilter');
415
  var filter = moduleFilter.applyFilter();
416

    
417
  if (!Drupal.ModuleFilter.modulesTop) {
418
    Drupal.ModuleFilter.modulesTop = $('#module-filter-modules').offset().top;
419
  }
420
  else {
421
    // Calculate header offset; this is important in case the site is using
422
    // admin_menu module which has fixed positioning and is on top of everything
423
    // else.
424
    var headerOffset = Drupal.settings.tableHeaderOffset ? eval(Drupal.settings.tableHeaderOffset + '()') : 0;
425
    // Scroll back to top of #module-filter-modules.
426
    $('html, body').animate({
427
      scrollTop: Drupal.ModuleFilter.modulesTop - headerOffset
428
    }, 500);
429
    // $('html, body').scrollTop(Drupal.ModuleFilter.modulesTop);
430
  }
431

    
432
  Drupal.ModuleFilter.setState('activeTab', hash);
433
};
434

    
435
Drupal.ModuleFilter.eventHandlerOperateByURLFragment = function(event) {
436
  var hash = $.param.fragment();
437
  Drupal.ModuleFilter.selectTab(hash);
438
};
439

    
440
Drupal.ModuleFilter.countSummary = function(id) {
441
  return Drupal.t('@enabled of @total', { '@enabled': Drupal.settings.moduleFilter.enabledCounts[id].enabled, '@total': Drupal.settings.moduleFilter.enabledCounts[id].total });
442
};
443

    
444
Drupal.ModuleFilter.Tab.prototype.updateEnabling = function(name, remove) {
445
  this.enabling = this.enabling || {};
446
  if (!remove) {
447
    this.enabling[name] = name;
448
  }
449
  else {
450
    delete this.enabling[name];
451
  }
452
};
453

    
454
Drupal.ModuleFilter.Tab.prototype.updateDisabling = function(name, remove) {
455
  this.disabling = this.disabling || {};
456
  if (!remove) {
457
    this.disabling[name] = name;
458
  }
459
  else {
460
    delete this.disabling[name];
461
  }
462
};
463

    
464
Drupal.ModuleFilter.Tab.prototype.updateVisualAid = function() {
465
  var visualAid = '';
466
  var enabling = new Array();
467
  var disabling = new Array();
468

    
469
  if (this.enabling != undefined) {
470
    for (var i in this.enabling) {
471
      enabling.push(this.enabling[i]);
472
    }
473
    if (enabling.length > 0) {
474
      enabling.sort();
475
      visualAid += '<span class="enabling">+' + enabling.join('</span>, <span class="enabling">') + '</span>';
476
    }
477
  }
478
  if (this.disabling != undefined) {
479
    for (var i in this.disabling) {
480
      disabling.push(this.disabling[i]);
481
    }
482
    if (disabling.length > 0) {
483
      disabling.sort();
484
      if (enabling.length > 0) {
485
        visualAid += '<br />';
486
      }
487
      visualAid += '<span class="disabling">-' + disabling.join('</span>, <span class="disabling">') + '</span>';
488
    }
489
  }
490

    
491
  if (this.visualAid == undefined) {
492
    $('a span.summary', this.element).append('<span class="visual-aid"></span>');
493
    this.visualAid = $('span.visual-aid', this.element);
494
  }
495

    
496
  this.visualAid.empty().append(visualAid);
497
};
498

    
499
Drupal.ModuleFilter.getTabID = function($row) {
500
  var id = $row.data('moduleFilterTabID');
501
  if (!id) {
502
    // Find the tab ID.
503
    var classes = $row.attr('class').split(' ');
504
    for (var i in classes) {
505
      if (Drupal.ModuleFilter.tabs[classes[i]] != undefined) {
506
        id = classes[i];
507
        break;
508
      }
509
    }
510
    $row.data('moduleFilterTabID', id);
511
  }
512
  return id;
513
};
514

    
515
Drupal.ModuleFilter.updateVisualAid = function(type, $row) {
516
  var id = Drupal.ModuleFilter.getTabID($row);
517

    
518
  if (!id) {
519
    return false;
520
  }
521

    
522
  var tab = Drupal.ModuleFilter.tabs[id];
523
  var name = $('td:nth(1) strong', $row).text();
524
  switch (type) {
525
    case 'enable':
526
      if (Drupal.ModuleFilter.disabling[id + name] != undefined) {
527
        delete Drupal.ModuleFilter.disabling[id + name];
528
        tab.updateDisabling(name, true);
529
        $row.removeClass('disabling');
530
      }
531
      else {
532
        Drupal.ModuleFilter.enabling[id + name] = name;
533
        tab.updateEnabling(name);
534
        $row.addClass('enabling');
535
      }
536
      break;
537
    case 'disable':
538
      if (Drupal.ModuleFilter.enabling[id + name] != undefined) {
539
        delete Drupal.ModuleFilter.enabling[id + name];
540
        tab.updateEnabling(name, true);
541
        $row.removeClass('enabling');
542
      }
543
      else {
544
        Drupal.ModuleFilter.disabling[id + name] = name;
545
        tab.updateDisabling(name);
546
        $row.addClass('disabling');
547
      }
548
      break;
549
  }
550

    
551
  tab.updateVisualAid();
552
};
553

    
554
Drupal.ModuleFilter.Filter.prototype.adjustHeight = function() {
555
  // Hack for adjusting the height of the modules section.
556
  var minHeight = $('#module-filter-tabs ul').height() + 10;
557
  minHeight += $('#module-filter-tabs #module-filter-submit').height();
558
  $('#module-filter-modules').css('min-height', minHeight);
559
  this.element.trigger('moduleFilter:adjustHeight');
560
}
561

    
562
})(jQuery);