Projet

Général

Profil

Paste
Télécharger (3,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / libraries / Superfish-for-Drupal-1.x / supersubs.js @ 13755f8d

1
/*
2
 * Supersubs v0.2b - jQuery plugin
3
 * Copyright (c) 2008 Joel Birch
4
 *
5
 * Dual licensed under the MIT and GPL licenses:
6
 *  http://www.opensource.org/licenses/mit-license.php
7
 *  http://www.gnu.org/licenses/gpl.html
8
 *
9
 * This plugin automatically adjusts submenu widths of suckerfish-style menus to that of
10
 * their longest list item children. If you use this, please expect bugs and report them
11
 * to the jQuery Google Group with the word 'Superfish' in the subject line.
12
 *
13
 */
14
/*
15
 * This is not the original jQuery Supersubs plugin.
16
 * Please refer to the README for more information.
17
 */
18

    
19
(function($){ // $ will refer to jQuery within this closure
20
  $.fn.supersubs = function(options){
21
    var opts = $.extend({}, $.fn.supersubs.defaults, options);
22
    // return original object to support chaining
23
    return this.each(function() {
24
      // cache selections
25
      var $$ = $(this);
26
      // support metadata
27
      var o = $.meta ? $.extend({}, opts, $$.data()) : opts;
28
      // get the font size of menu.
29
      // .css('fontSize') returns various results cross-browser, so measure an em dash instead
30
      var fontsize = $('<li id="menu-fontsize">&#8212;</li>').css({
31
        'padding' : 0,
32
        'position' : 'absolute',
33
        'top' : '-99999em',
34
        'width' : 'auto'
35
      }).appendTo($$).width(); //clientWidth is faster, but was incorrect here
36
      // remove em dash
37
      $('#menu-fontsize').remove();
38

    
39
      // Jump on level if it's a "NavBar"
40
      if ($$.hasClass('sf-navbar')) {
41
        $$ = $('li > ul', $$);
42
      }
43
      // cache all ul elements 
44
      $ULs = $$.find('ul:not(.sf-megamenu)');
45
      // loop through each ul in menu
46
      $ULs.each(function(i) {
47
        // cache this ul
48
        var $ul = $ULs.eq(i);
49
        // get all (li) children of this ul
50
        var $LIs = $ul.children();
51
        // get all anchor grand-children
52
        var $As = $LIs.children('a');
53
        // force content to one line and save current float property
54
        var liFloat = $LIs.css('white-space','nowrap').css('float');
55
        // remove width restrictions and floats so elements remain vertically stacked
56
        var emWidth = $ul.add($LIs).add($As).css({
57
          'float' : 'none',
58
          'width'  : 'auto'
59
        })
60
        // this ul will now be shrink-wrapped to longest li due to position:absolute
61
        // so save its width as ems. Clientwidth is 2 times faster than .width() - thanks Dan Switzer
62
        .end().end()[0].clientWidth / fontsize;
63
        // add more width to ensure lines don't turn over at certain sizes in various browsers
64
        emWidth += o.extraWidth;
65
        // restrict to at least minWidth and at most maxWidth
66
        if (emWidth > o.maxWidth)    { emWidth = o.maxWidth; }
67
        else if (emWidth < o.minWidth)  { emWidth = o.minWidth; }
68
        emWidth += 'em';
69
        // set ul to width in ems
70
        $ul.css('width',emWidth);
71
        // restore li floats to avoid IE bugs
72
        // set li width to full width of this ul
73
        // revert white-space to normal
74
        $LIs.css({
75
          'float' : liFloat,
76
          'width' : '100%',
77
          'white-space' : 'normal'
78
        })
79
        // update offset position of descendant ul to reflect new width of parent
80
        .each(function(){
81
          var $childUl = $('>ul',this);
82
          var offsetDirection = $childUl.css('left')!==undefined ? 'left' : 'right';
83
          $childUl.css(offsetDirection,emWidth);
84
        });
85
      });
86
    });
87
  };
88
  // expose defaults
89
  $.fn.supersubs.defaults = {
90
    minWidth: 9, // requires em unit.
91
    maxWidth: 25, // requires em unit.
92
    extraWidth: 0 // extra width can ensure lines don't sometimes turn over due to slight browser differences in how they round-off values
93
  };
94
})(jQuery); // plugin code ends