This repository was archived by the owner on May 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdropdown.js
More file actions
155 lines (124 loc) · 5.12 KB
/
dropdown.js
File metadata and controls
155 lines (124 loc) · 5.12 KB
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
151
152
153
154
155
/**
Dropdown
---------------------------------------------------------------------
Author: Lane Olson
Sources: jQuery Plugin Patterns:
http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
Version: 1.1
Date: April 27, 2012
Description: Displays a list of links as a dropdown menu
---------------------------------------------------------------------
**/
;(function ($) {
// initialize namespace if it doesn't exist
if (!$.responsive) {
$.responsive = {};
};
$.responsive.dropdown = function ( el, options ) {
// To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions.
var base = this;
// Access to jQuery and DOM versions of element
base.$el = $(el);
base.el = el;
// Link to show/hide links
var linkEl, subNav;
// to keep track of plugin state
var isCreated = false;
// Add a reverse reference to the DOM object
base.$el.data( "responsive.dropdown" , base );
base.init = function () {
base.options = $.extend({}, $.responsive.dropdown.defaultOptions, options);
base.$el.bind("createdropdown", function() {
base.create();
});
base.$el.bind("destroydropdown", function() {
base.destroy();
});
base.create();
};
base.create = function() {
if(!isCreated)
{
// Wrap existing list in another list
base.$el.find('ul:first').wrap('<ul><li></li></ul>');
// Add the show/hide link
base.$el.find('ul li:first').prepend(
'<a class="toggle-nav" href="#">'+base.options.linkText+'</a>'
);
// Assign the subnav
linkEl = $('.toggle-nav', base.el);
subNav = linkEl.siblings('ul');
// Add the arrow to the link
linkEl.append('<strong>'+ base.options.htmlArrowMore +'</strong>');
// Hide dropdown if not visible by default
if(!base.options.showLinksByDefault)
subNav.hide();
if(base.options.bindOn == 'hover')
{
linkEl.parent().bind('mouseenter', function(event) {
base.showDropdown();
return false;
});
linkEl.parent().bind('mouseleave', function() {
base.hideDropdown();
return false;
});
}
if(base.options.bindOn == 'click')
{
// hide dropdown on click outside of menu
$('html').click(function() {
base.hideDropdown();
});
linkEl.bind('click', function(event) {
// do not propagate event
event.stopPropagation();
if(subNav.is(':hidden')) {
base.showDropdown();
} else {
base.hideDropdown();
}
return false;
});
}
isCreated = true;
}
};
base.showDropdown = function() {
subNav.slideDown('fast');
$('strong', linkEl).html(base.options.htmlArrowLess);
};
base.hideDropdown = function() {
subNav.slideUp('fast');
$('strong', linkEl).html(base.options.htmlArrowMore);
};
base.destroy = function() {
if(isCreated) {
// remove from wrapper
base.$el.html(subNav);
// remove any inline styling
$('ul', base.el).removeAttr('style');
// unbind events
linkEl.parent().unbind('mouseenter mouseleave');
linkEl.unbind('click');
linkEl = null;
subNav = null;
isCreated = false;
}
};
base.init();
};
$.responsive.dropdown.defaultOptions = {
linkText: 'Navigation', // Wording for collapsed menu
htmlArrowMore: '▼', // html to represent more arrow
htmlArrowLess: '▲', // html to represent less arrow
bindOn: 'hover', // what event to trigger the subnav ('click' or 'hover')
showLinksByDefault: false // display navigation links by default
};
$.fn.dropdown = function( options ) {
return this.each(function () {
(new $.responsive.dropdown(this, options));
});
};
})( jQuery );