Projet

Général

Profil

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

root / drupal7 / sites / all / modules / module_filter / js / module_filter_tab.js @ 18596a08

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
                  $switch.toggleClass('off');
246
                }
247
                else {
248
                  $checkbox.click().change();
249
                  $switch.toggleClass('off');
250
                }
251
              }
252
            });
253
          });
254
        }
255

    
256
        var $tabs = $('#module-filter-tabs');
257

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

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

    
275
        function viewportTop() {
276
          var top = $(window).scrollTop();
277
          return top;
278
        }
279

    
280
        function viewportBottom() {
281
          var top = $(window).scrollTop();
282
          var bottom = top + $(window).height();
283

    
284
          bottom -= $('#page-actions').height();
285

    
286
          return bottom;
287
        }
288

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

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

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

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

    
329
        var lastTop = 0;
330
        $(window).scroll(function() {
331
          var top = viewportTop();
332
          var bottom = viewportBottom();
333

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

    
353
        moduleFilter.adjustHeight();
354
      });
355
    }
356
  }
357
};
358

    
359
Drupal.ModuleFilter.Tab = function(element, id) {
360
  var self = this;
361

    
362
  this.id = id;
363
  this.hash = id.substring(0, id.length - 4);
364
  this.element = element;
365

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

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

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

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

    
399
    if (!hash) {
400
      hash = 'all';
401
    }
402
  }
403

    
404
  if (Drupal.ModuleFilter.activeTab != undefined) {
405
    Drupal.ModuleFilter.activeTab.element.removeClass('selected');
406
  }
407

    
408
  Drupal.ModuleFilter.activeTab = Drupal.ModuleFilter.tabs[hash + '-tab'];
409
  Drupal.ModuleFilter.activeTab.element.addClass('selected');
410

    
411
  var moduleFilter = $('input[name="module_filter[name]"]').data('moduleFilter');
412
  var filter = moduleFilter.applyFilter();
413

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

    
429
  Drupal.ModuleFilter.setState('activeTab', hash);
430
};
431

    
432
Drupal.ModuleFilter.eventHandlerOperateByURLFragment = function(event) {
433
  var hash = $.param.fragment();
434
  Drupal.ModuleFilter.selectTab(hash);
435
};
436

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

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

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

    
461
Drupal.ModuleFilter.Tab.prototype.updateVisualAid = function() {
462
  var visualAid = '';
463
  var enabling = new Array();
464
  var disabling = new Array();
465

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

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

    
493
  this.visualAid.empty().append(visualAid);
494
};
495

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

    
512
Drupal.ModuleFilter.updateVisualAid = function(type, $row) {
513
  var id = Drupal.ModuleFilter.getTabID($row);
514

    
515
  if (!id) {
516
    return false;
517
  }
518

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

    
548
  tab.updateVisualAid();
549
};
550

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

    
559
})(jQuery);