-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjquery.ui.navigation.js
More file actions
296 lines (244 loc) · 10.1 KB
/
jquery.ui.navigation.js
File metadata and controls
296 lines (244 loc) · 10.1 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
Dropdown
---------------------------------------------------------------------
Author: Lane Olson
Version: 1.1
Date: May 2, 2012
Description: Displays a list of links as a dropdown menu
---------------------------------------------------------------------
**/
;(function ( $, window, document, undefined ) {
$.widget( "navigation.dropdown" , {
//Options to be used as defaults
options: {
linkText: 'Navigation', // Wording for collapsed menu
htmlArrowMore: '▼', // html to represent more arrow
htmlArrowLess: '▲', // html to represent less arrow
bindOn: 'click', // what event to trigger the subnav ('click' or 'hover')
showLinksByDefault: false // display navigation links by default
},
//Setup widget (eg. element creation, apply theming
// , bind events etc.)
_create: function () {
var self = this;
// List containing the links
this.list = this.element.children(); //sub nav
// Wrap existing list in another list
this.listWrapper = this.list.wrap('<ul><li></li></ul>');
this.isAnimating = false;
// Add the show/hide link (linkEl)
this.toggleLink = $('<a class="'+this.widgetBaseClass+'-toggle" href="#">'+this.options.linkText+'</a>')
.insertBefore(this.list)
.append('<strong>'+ this.options.htmlArrowMore +'</strong>');
// Hide dropdown if not visible by default
if(!this.options.showLinksByDefault)
this.list.hide();
if(this.options.bindOn == 'click')
{
$('html').click(function() { self.hide(); }); // hide dropdown on click outside of menu
this.list.parent().bind('click.dropdown', $.proxy(this, "_click"));
} else {
this.list.parent().bind('mouseenter.dropdown mouseleave.dropdown', $.proxy(this, "_hover"));
}
},
_hover: function(e) {
(e.type == "mouseenter" ? this.show() : this.hide());
this._trigger("hover", e, {
hovered: $(e.target)
});
},
_click: function(e) {
e.stopPropagation();
(this.list.is(":hidden") ? this.show() : this.hide());
this._trigger("click", e, {
clicked: $(e.target)
});
},
hide: function() {
this.element.addClass(this.widgetBaseClass+"-closed")
.removeClass(this.widgetBaseClass+"-open");
this.list.slideUp("fast");
$('strong', this.toggleLink).html(this.options.htmlArrowMore);
},
show: function() {
this.element.addClass(this.widgetBaseClass+"-open")
.removeClass(this.widgetBaseClass+"-closed");
this.list.slideDown("fast");
$('strong', this.toggleLink).html(this.options.htmlArrowLess);
},
destroy: function () {
// remove from wrapper
this.element.html(this.list);
// remove any inline styling
$('ul', this.element).removeAttr('style');
// unbind events
this.list.parent().unbind();
$.Widget.prototype.destroy.call(this);
}
});
$.widget( "navigation.slidernav" , {
//Options to be used as defaults
options: {
transitionTime: 200,
backWording: "Back to",
moreArrow: "▶",
prevArrow: "◀"
},
//Setup widget (eg. element creation, apply theming
// , bind events etc.)
_create: function () {
var self = this;
this.element.css('overflow', 'hidden');
this.topLevel = this.element.find("ul:first");
this.currentNav = this.topLevel;
this.currentNav.addClass(this.widgetBaseClass+'-current');
$('ul', this.element).css({
'position': 'absolute',
'width': '100%',
'top': '0px'
});
this.element.css('height', this.topLevel.height());
$("li", this.topLevel).each(function() {
if($(this).children("ul").length > 0 || $(this).children('div').children('ul').length > 0)
{
$(this).children("ul","div").addClass(self.widgetBaseClass+'-right');
$(this).children("a").append('<strong>'+self.options.moreArrow+'</strong>').addClass(self.widgetBaseClass+'-more');
}
});
this.element.on("click", "a."+self.widgetBaseClass+'-more', function() {
self.slideTo($(this));
return false;
});
this.element.on("click", "a."+self.widgetBaseClass+'-back', function () {
self.slideBack();
return false;
});
},
slideTo: function(item) {
var currUl = this.currentNav;
var nextUl = item.siblings("ul:first");
var divContainer = false;
if(!this.isAnimating)
{
this.isAnimating = true;
if(nextUl.length < 1)
{
divContainer = item.siblings("div");
nextUl = $("ul:first", divContainer);
}
nextUl.prepend('<li><a class="'+this.widgetBaseClass+'-back'+'" href="#">'+this.options.backWording+' '+item.html()+'</a></li>');
$('a.'+this.widgetBaseClass+'-back'+' strong', nextUl).html(this.options.prevArrow);
if(!this.transitionSupport())
{
nextUl.css("left", nextUl.position().left+"px");
nextUl.animate({ left: "0" }, this.options.transitionTime, "linear", function()
{
this.switchClassesNext(currUl, nextUl);
this.isAnimating = false;
});
} else {
this.switchClassesNext(currUl, nextUl);
this.isAnimating = false;
}
this.currentNav = nextUl;
}
},
slideBack: function() {
var currUl = this.currentNav;
var prevUl = currUl.parents("ul:first"); // get first parent <ul>
var moveLeft; // value to animate left to
if(!this.isAnimating) {
this.isAnimating = true;
if(!this.transitionSupport()) {
if(prevUl.position().left < 0)
moveLeft = "0";
else
moveLeft = "100%";
prevUl.css("left", prevUl.position().left+"px");
prevUl.animate({ left: moveLeft }, this.options.transitionTime, "linear", function()
{
this.switchClassesPrevious(currUl, prevUl);
this.isAnimating = false;
});
} else {
this.switchClassesPrevious(currUl, prevUl);
this.isAnimating = false;
}
this.currentNav = prevUl;
}
},
switchClassesNext: function(current, next) {
current.removeClass(this.widgetBaseClass+'-current');
current.addClass(this.widgetBaseClass+'-left');
next.addClass(this.widgetBaseClass+'-current');
next.removeClass(this.widgetBaseClass+'-right');
this.element.height(next.height());
next.css("left", "");
},
switchClassesPrevious: function(current, previous) {
current.removeClass(this.widgetBaseClass+'-current');
current.addClass(this.widgetBaseClass+'-right');
previous.addClass(this.widgetBaseClass+'-current').css("left", "");
previous.removeClass(this.widgetBaseClass+'-left').css("left", "");
current.find("li:first").remove();
this.element.height(previous.height());
},
transitionSupport: function() {
var d = document.createElement("detect"),
CSSprefix = "Webkit,Moz,O,ms,Khtml".split(","),
All = ("transition " + CSSprefix.join("Transition,") + "Transition").split(",");
for (var n = 0, np = All.length; n < np; n++) {
if (d.style[All[n]] === "") {
return true;
}
}
return false;
},
destroy: function () {
this.element.removeAttr('style');
this.element.off("click", "**");
$('.'+this.widgetBaseClass+'-current', this.element).removeClass(this.widgetBaseClass+'-current');
$('.'+this.widgetBaseClass+'-right', this.element).removeClass(this.widgetBaseClass+'-right');
$('.'+this.widgetBaseClass+'-left', this.element).removeClass(this.widgetBaseClass+'-left');
$('.'+this.widgetBaseClass+'-more'+' strong', this.element).remove();
$('.'+this.widgetBaseClass+'-more', this.element).removeClass(this.widgetBaseClass+'-more');
$('ul', this.element).removeAttr('style');
$('.'+this.widgetBaseClass+'-back', this.element).parent().remove();
$.Widget.prototype.destroy.call(this);
}
});
$.widget( "navigation.multilevel" , {
//Options to be used as defaults
options: {
},
//Setup widget (eg. element creation, apply theming
// , bind events etc.)
_create: function () {
var self = this;
// hide the sub navigation
this.hideDropdown();
$('ul:first > li', this.element).bind("mouseenter.multilevel mouseleave.multilevel", $.proxy(this, "_hover"));
},
_hover: function(e) {
(e.type == "mouseleave" ? this.hideDropdown() : this.showDropdown($(e.target)));
this._trigger("hover", e, {
hovered: $(e.target)
});
},
showDropdown: function(item) {
item.siblings().show();
},
hideDropdown: function() {
if($('ul > li > div', this.element).length > 0) {
$('ul > li > div', this.element).hide();
} else {
$('ul > li > ul', this.element).hide();
}
},
destroy: function () {
$('div', base.el).removeAttr('style');
$('ul', base.el).removeAttr('style');
$.Widget.prototype.destroy.call(this);
}
});
})( jQuery, window, document );