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