Projet

Général

Profil

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

root / drupal7 / sites / all / themes / responsive / js / jquery.mobilemenu.js @ cc7b6b59

1
(function($){
2

    
3
  //variable for storing the menu count when no ID is present
4
  var menuCount = 0;
5
  
6
  //plugin code
7
  $.fn.mobileMenu = function(options){
8
    
9
    //plugin's default options
10
    var settings = {
11
      switchWidth: 750,
12
      topOptionText: 'Select menu item',
13
      indentString: '   '
14
    };
15
    
16
    
17
    //function to check if selector matches a list
18
    function isList($this){
19
      return $this.is('ul, ol');
20
    }
21
  
22
  
23
    //function to decide if mobile or not
24
    function isMobile(){
25
      return ($(window).width() < settings.switchWidth);
26
    }
27
    
28
    
29
    //check if dropdown exists for the current element
30
    function menuExists($this){
31
      
32
      //if the list has an ID, use it to give the menu an ID
33
      if($this.attr('id')){
34
        return ($('#mobileMenu_'+$this.attr('id')).length > 0);
35
      } 
36
      
37
      //otherwise, give the list and select elements a generated ID
38
      else {
39
        menuCount++;
40
        $this.attr('id', 'mm'+menuCount);
41
        return ($('#mobileMenu_mm'+menuCount).length > 0);
42
      }
43
    }
44
    
45
    
46
    //change page on mobile menu selection
47
    function goToPage($this){
48
      if($this.val() !== null){document.location.href = $this.val()}
49
    }
50
    
51
    
52
    //show the mobile menu
53
    function showMenu($this){
54
      $this.css('display', 'none');
55
      $('#mobileMenu_'+$this.attr('id')).show();
56
    }
57
    
58
    
59
    //hide the mobile menu
60
    function hideMenu($this){
61
      $this.css('display', '');
62
      $('#mobileMenu_'+$this.attr('id')).hide();
63
    }
64
    
65
    
66
    //create the mobile menu
67
    function createMenu($this){
68
      if(isList($this)){
69
                
70
        //generate select element as a string to append via jQuery
71
        var selectString = '<select id="mobileMenu_'+$this.attr('id')+'" class="mobileMenu">';
72
        
73
        //create first option (no value)
74
        selectString += '<option value="">'+settings.topOptionText+'</option>';
75
        
76
        //loop through list items
77
        $this.find('li').each(function(){
78
          
79
          //when sub-item, indent
80
          var levelStr = '';
81
          var len = $(this).parents('ul, ol').length;
82
          for(i=1;i<len;i++){levelStr += settings.indentString;}
83
          
84
          //get url and text for option
85
          var link = $(this).find('a:first-child').attr('href');
86
          var text = levelStr + $(this).clone().children('ul, ol').remove().end().text();
87
          
88
          //add option
89
          selectString += '<option value="'+link+'">'+text+'</option>';
90
        });
91
        
92
        selectString += '</select>';
93
        
94
        //append select element to ul/ol's container
95
        $this.parent().append(selectString);
96
        
97
        //add change event handler for mobile menu
98
        $('#mobileMenu_'+$this.attr('id')).change(function(){
99
          goToPage($(this));
100
        });
101
        
102
        //hide current menu, show mobile menu
103
        showMenu($this);
104
      } else {
105
        alert('mobileMenu will only work with UL or OL elements!');
106
      }
107
    }
108
    
109
    
110
    //plugin functionality
111
    function run($this){
112
      
113
      //menu doesn't exist
114
      if(isMobile() && !menuExists($this)){
115
        createMenu($this);
116
      }
117
      
118
      //menu already exists
119
      else if(isMobile() && menuExists($this)){
120
        showMenu($this);
121
      }
122
      
123
      //not mobile browser
124
      else if(!isMobile() && menuExists($this)){
125
        hideMenu($this);
126
      }
127

    
128
    }
129
    
130
    //run plugin on each matched ul/ol
131
    //maintain chainability by returning "this"
132
    return this.each(function() {
133
      
134
      //override the default settings if user provides some
135
      if(options){$.extend(settings, options);}
136
      
137
      //cache "this"
138
      var $this = $(this);
139
    
140
      //bind event to browser resize
141
      $(window).resize(function(){run($this);});
142

    
143
      //run plugin
144
      run($this);
145

    
146
    });
147
    
148
  };
149
  
150
})(jQuery);