/
ap-drilldown-menu.js
314 lines (246 loc) · 15.3 KB
/
ap-drilldown-menu.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*! ap-drilldown-menu v0.1.1 | (c) 2013, 2015 Aptuitiv, Inc. */
;(function($, window, undefined) {
// Modernizr 2.6.2 (Custom Build) | MIT & BSD - Code to test support for media queries here only.
window.Modernizr=function(a,b,c){function v(a){i.cssText=a}function w(a,b){return v(prefixes.join(a+";")+(b||""))}function x(a,b){return typeof a===b}function y(a,b){return!!~(""+a).indexOf(b)}function z(a,b,d){for(var e in a){var f=b[a[e]];if(f!==c)return d===!1?a[e]:x(f,"function")?f.bind(d||b):f}return!1}var d="2.6.2",e={},f=b.documentElement,g="modernizr",h=b.createElement(g),i=h.style,j,k={}.toString,l={},m={},n={},o=[],p=o.slice,q,r=function(a,c,d,e){var h,i,j,k,l=b.createElement("div"),m=b.body,n=m||b.createElement("body");if(parseInt(d,10))while(d--)j=b.createElement("div"),j.id=e?e[d]:g+(d+1),l.appendChild(j);return h=["­",'<style id="s',g,'">',a,"</style>"].join(""),l.id=g,(m?l:n).innerHTML+=h,n.appendChild(l),m||(n.style.background="",n.style.overflow="hidden",k=f.style.overflow,f.style.overflow="hidden",f.appendChild(n)),i=c(l,a),m?l.parentNode.removeChild(l):(n.parentNode.removeChild(n),f.style.overflow=k),!!i},s=function(b){var c=a.matchMedia||a.msMatchMedia;if(c)return c(b).matches;var d;return r("@media "+b+" { #"+g+" { position: absolute; } }",function(b){d=(a.getComputedStyle?getComputedStyle(b,null):b.currentStyle)["position"]=="absolute"}),d},t={}.hasOwnProperty,u;!x(t,"undefined")&&!x(t.call,"undefined")?u=function(a,b){return t.call(a,b)}:u=function(a,b){return b in a&&x(a.constructor.prototype[b],"undefined")},Function.prototype.bind||(Function.prototype.bind=function(b){var c=this;if(typeof c!="function")throw new TypeError;var d=p.call(arguments,1),e=function(){if(this instanceof e){var a=function(){};a.prototype=c.prototype;var f=new a,g=c.apply(f,d.concat(p.call(arguments)));return Object(g)===g?g:f}return c.apply(b,d.concat(p.call(arguments)))};return e});for(var A in l)u(l,A)&&(q=A.toLowerCase(),e[q]=l[A](),o.push((e[q]?"":"no-")+q));return e.addTest=function(a,b){if(typeof a=="object")for(var d in a)u(a,d)&&e.addTest(d,a[d]);else{a=a.toLowerCase();if(e[a]!==c)return e;b=typeof b=="function"?b():b,typeof enableClasses!="undefined"&&enableClasses&&(f.className+=" "+(b?"":"no-")+a),e[a]=b}return e},v(""),h=j=null,e._version=d,e.mq=s,e.testStyles=r,e}(this,this.document);
var mq = Modernizr.mq('only all');
$.fn.apDrillDownMenu = function(options) {
var opts = $.extend({}, $.fn.apDrillDownMenu.defaults, options);
opts.showSpeed = parseInt(opts.showSpeed);
var maxHeight = parseInt(opts.height);
var css = {
current: 'ap-ddmenu-current',
icon: 'ap-ddmenu-link-icon',
menuTop: 'ap-ddmenu-top',
menuWrapper: 'ap-ddmenu',
scroll: 'ap-ddmenu-scroll'
};
// Showing and hiding the toggle switch via JS for IE8.
if (!mq) {
var p = $(this).parents().eq(0);
$(window).on('load resize', function(){
if ($(window).width() > opts.maxWindowWidth){
$('html').removeClass(opts.ieMobileClass);
$(opts.toggleSwitch).hide();
} else {
$('html').addClass(opts.ieMobileClass);
$(opts.toggleSwitch).show();
}
});
}
css.menuTopTest = '.' + css.menuTop;
return this.each(function() {
if (this.tagName.toLowerCase() == 'ul') {
var menu = $(this);
var p = menu.parent(), container = menu.parent();
var ancestor;
// Handling the sliding/hiding of the navigation via the toggle.
$(opts.toggleSwitch).on('click touchstart', function (e) {
e.preventDefault();
// Fixing the menu widths when the toggle is clicked.
fixWidths();
if (!mq) {
p.toggle();
} else {
p.stop().slideToggle({'duration' : opts.toggleSpeed});
}
checkHeight($('.' + css.current));
});
// Checks the height of the element against the max height
var checkHeight = function(el) {
if ( $(window).width() < opts.maxWindowWidth ) {
if (maxHeight > 0) {
if (parseInt(el.css('height')) > maxHeight) el.addClass(css.scroll).css('height', maxHeight);
} else {
container.css('height', el.outerHeight() + $(opts.headerSelector).outerHeight());
}
}
};
var setup = menu.data('apDrillDownMenuSetup');
if (setup != 'yes') {
menu.data('apDrillDownMenuSetup', 'yes');
menu.addClass(css.menuTop).addClass(css.current);
menu.wrap('<div class="' + css.menuWrapper + '"></div>');
container = menu.parent();
container.css({width: opts.width, height: opts.height});
// Getting the parent of the new container.
ancestor = container.parent();
// Creating the header if it doesn't exist.
if ( !$(opts.headerSelector).length ) {
container.prepend('<div class="' + opts.headerSelector.split('.')[1] + '"></div>' );
}
var ddmenuHeader = $(opts.headerSelector);
// Setup the current text placeholder if necessary
var currentTextHolder = false;
if (opts.currentText) {
// If the current text holder is needed but doesn't exist, create it now.
if ( !$(opts.currentTextSelector).length ) {
ddmenuHeader.prepend('<div class="' + opts.currentTextSelector.split('.')[1] + '"></div>');
}
currentTextHolder = $(opts.currentTextSelector);
}
// Create the back link if it doesn't exist.
if ( !(opts.backLink === true && opts.backLinkSelector !== undefined && $(opts.backLinkSelector).length > 0) ) {
ddmenuHeader.prepend('<a href="#" class="' + opts.backLinkSelector.split('.')[1] + '">' + opts.backLinkText + '</a>');
}
var backLink = $(opts.backLinkSelector);
backLink.hide();
backLink.click(function(e) {
e.preventDefault();
if (!menu.is(':animated')) {
var b = $(this);
var prevLeftVal = parseFloat(menu.css('left')) + container.width();
menu.animate({ left: prevLeftVal }, opts.showSpeed, function(){
var current = $('ul.' + css.current, menu);
current.hide();
var currentParent = current.parents('ul:first');
reset(current);
setCurrent(currentParent);
if (currentTextHolder !== false && currentParent.is(css.menuTopTest) === false) {
currentTextHolder.text(getCurrentText(currentParent.prev()));
}
checkHeight($('.' + css.current));
});
}
});
// Set internal functions. Set here so that local variables are available
// Resets the old current ul
var reset = function (el) {
el.removeClass(css.scroll).removeClass(css.current);
};
// Handles setting up the current ul
var setCurrent = function (el) {
var isTop = el.is(css.menuTopTest);
if (!isTop) {
el.css('left', container.width());
}
el.show().addClass(css.current);
checkHeight(el);
if (backLink !== false) {
(!isTop) ? backLink.show() : backLink.hide();
}
if (currentTextHolder !== false) {
(!isTop) ? currentTextHolder.show() : currentTextHolder.hide();
}
};
// Setting up a timeout function for the resize event.
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
// Fixes the widths and positions when the device is rotated or the window is resized
var fixWidths = function() {
var current = $('.' + css.current, menu);
var numParents = current.parentsUntil('.' + css.menuWrapper, 'ul').length;
// var width = parseInt(container.css('width'));
// Getting the window width to set the widths.
// .css() can't be called on a hidden element.
var width = $(window).width();
if (numParents > 0) {
menu.css('left', (numParents * width * -1));
current.parentsUntil('.' + css.menuTop, 'ul').css('left', width);
current.css('left', width);
}
};
// Get the current text of the link / parent link.
var getCurrentText = function(el) {
return el.clone().children().remove().end().text();
};
/**
* Add listeners to fix the widths when the device is rotated or the window is resized
*/
// IE8- doesn't support addEventListener - use attachEvent instead.
if (!window.addEventListener) {
window.attachEvent('onresize', fixWidths);
} else {
window.addEventListener("resize", fixWidths, false);
window.addEventListener("orientationchange", fixWidths, false);
}
// Set the initial height of the first level menu
checkHeight(menu);
// Handle the clicks for each menu item
menu.find('a').each(function() {
var link = $(this);
// If the link has a child menu
if (link.siblings('ul').length > 0) {
if (opts.prependCurrentOnChild) {
var clone = link.clone(true);
var li = $('<li class="' + opts.cloneClass + '"></li>');
li.append(clone);
link.siblings('ul').prepend(li);
if (typeof opts.prependCurrentOnChildCallback == 'function') {
opts.prependCurrentOnChildCallback.apply(clone);
}
}
link.append('<span class="' + css.icon + '">' + opts.parentIconText + '</span>');
link.click(function(e) {
// Checking window width - need to know whether to make the click event scroll down the menus or not.
if ( $(window).width() <= opts.maxWindowWidth) {
// Cancel the page load if it's a parent link.
e.preventDefault();
var nextList = $(this).siblings('ul');
var parentList = link.parents('ul:first');
var isFirstLevel = parentList.is(css.menuTopTest);
var parentLeft = (isFirstLevel) ? 0 : parseFloat(menu.css('left'));
var containerWidth = parseFloat(container.width());
var nextLeft = Math.round(parentLeft - containerWidth);
reset(parentList);
setCurrent(nextList);
menu.animate({left: nextLeft}, opts.showSpeed);
if (currentTextHolder !== false) {
// Set the current text without getting the text within the icon span tag.
currentTextHolder.text(getCurrentText(link));
}
}
});
}
});
$(window).on('resize', function(){
showOrHide();
fixWidths();
});
} else {
// This element is being set up again. Make sure that the menu dimensions are correct
checkHeight($('ul.' + css.current, container));
}
var showOrHide = function() {
if ( $(window).width() > opts.maxWindowWidth ){
container.attr('style', '');
$(css.menuTopTest).find('ul').attr('style', '');
if (! $('.' + css.current).hasClass(css.menuTop) && $('.' + css.current).parent().hasClass(css.menuWrapper) ) {
$('.' + css.current).hide().parent().closest('.sub-menu').hide();
}
ancestor.attr('style', '');
} else {
container.css('height', $('.' + css.current).css('height'));
if ( ancestor.hasClass('expanded')) ancestor.show();
$('.' + css.current).show().parent().closest('.sub-menu').show();
// Check the heights after the resize - just in case.
checkHeight( $('.' + css.current, container) );
}
};
}
});
};
$.fn.apDrillDownMenu.defaults = {
maxWindowWidth: 875, // Width that the navigation becomes "active"
ieMobileClass: 'mobile',
width: '100%',
height: 'auto',
showSpeed: 200,
backLink: true,
backLinkText: 'Back',
backLinkSelector: '.ap-ddmenu-back',
headerSelector: '.ap-ddmenu-header',
currentText: true,
currentTextSelector: '.ap-ddmenu-current-text',
cloneClass: 'clone',
prependCurrentOnChild: true,
prependCurrentOnChildCallback: function() {},
parentIconText: '',
toggleSwitch: '.ap-ddmenu-toggle',
toggleSpeed: 200
};
})(jQuery, window);