/
jquery.clickmenu.js
516 lines (495 loc) · 14.7 KB
/
jquery.clickmenu.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* clickMenu - v0.1.6
* Copyright (c) 2007 Roman Weich
* http://p.sohei.org
*
* Changelog:
* v 0.1.6 - 2007-09-06
* -fix: having a link in the top-level menu would not open the menu but call the link instead
* v 0.1.5 - 2007-07-07
* -change/fix: menu opening/closing now through simple show() and hide() calls - before fadeIn and fadeOut were used for which extra functions to stop a already running animation were created -> they were
* buggy (not working with the interface plugin in jquery1.1.2 and not working with jquery1.1.3 at all) and now removed
* -change: removed option: fadeTime
* -change: now using the bgiframe plugin for adding iframes in ie6 when available
* v 0.1.4 - 2007-03-20
* -fix: the default options were overwritten by the context related options
* -fix: hiding a submenu all hover- and click-events were unbound, even the ones not defined in this plugin - unbinding should work now
* v 0.1.3 - 2007-03-13
* -fix: some display problems ie had when no width was set on the submenu, so on ie the width for each submenu will be explicitely set
* -fix: the fix to the ie-width-problem is a fix to the "ie does not support css min-width stuff" problem too which displayed some submenus too narrow (it looked just not right)
* -fix: some bugs, when user the was too fast with the mouse
* v 0.1.2 - 2007-03-11
* -change: made a lot changes in the traversing routines to speed things up (having better memory usage now as well)
* -change: added $.fn.clickMenu.setDefaults() for setting global defaults
* -fix: hoverbug when a main menu item had no submenu
* -fix: some bugs i found while rewriting most of the stuff
* v 0.1.1 - 2007-03-04
* -change: the width of the submenus is no longer fixed, its set in the plugin now
* -change: the submenu-arrow is now an img, not the background-img of the list element - that allows better positioning, and background-changes on hover (you have to set the image through the arrowSrc option)
* -fix: clicking on a clickMenu while another was already open, didn't close the open one
* -change: clicking on the open main menu item will close it
* -fix: on an open menu moving the mouse to a main menu item and moving it fastly elsewere hid the whole menu
* v 0.1.0 - 2007-03-03
*/
//WERELATE: convert mouseover/mouseout to mouseenter/mouseleave
(function($)
{
var defaults = {
onClick: function(){
$(this).find('>a').each(function(){
if ( this.href )
{
window.location = this.href;
}
});
},
arrowSrc: '',
subDelay: 300,
mainDelay: 10
};
$.fn.clickMenu = function(options)
{
var shown = false;
var liOffset = ( ($.browser.msie) ? 4 : 2 );
var settings = $.extend({}, defaults, options);
var hideDIV = function(div, delay)
{
//a timer running to show the div?
if ( div.timer && !div.isVisible )
{
clearTimeout(div.timer);
}
else if (div.timer)
{
return; //hide-timer already running
}
if ( div.isVisible )
{
div.timer = setTimeout(function()
{
//remove events
$(getAllChilds(getOneChild(div, 'UL'), 'LI')).unbind('mouseenter', liHoverIn).unbind('mouseleave', liHoverOut).unbind('click', settings.onClick);
//hide it
$(div).hide();
div.isVisible = false;
div.timer = null;
}, delay);
}
};
var showDIV = function(div, delay)
{
if ( div.timer )
{
clearTimeout(div.timer);
}
if ( !div.isVisible )
{
div.timer = setTimeout(function()
{
//check if the mouse is still over the parent item - if not dont show the submenu
if ( !checkClass(div.parentNode, 'hover') )
{
return;
}
//assign events to all div>ul>li-elements
$(getAllChilds(getOneChild(div, 'UL'), 'LI')).mouseenter(liHoverIn).mouseleave(liHoverOut).click(settings.onClick);
//positioning
if ( !checkClass(div.parentNode, 'main') )
{
$(div).css('left', div.parentNode.offsetWidth - liOffset);
}
//show it
div.isVisible = true; //we use this over :visible to speed up traversing
$(div).show();
// WERELATE: I don't like browser-specific things; we can do this always
// if ( $.browser.msie ) //fixing a display-bug in ie6 and adding min-width
// {
var cW = $(getOneChild(div, 'UL')).width();
if ( cW < 80 )
{
cW = 80;
}
$(div).css('width', cW);
// }
div.timer = null;
}, delay);
}
};
//same as hover.handlehover in jquery - just can't use hover() directly - need the ability to unbind only the one hover event
var testHandleHover = function(e)
{
// Check if mouse(over|out) are still within the same parent element
var p = (e.type == "mouseenter" ? e.fromElement : e.toElement) || e.relatedTarget;
// Traverse up the tree
while ( p && p != this )
{
try
{
p = p.parentNode;
}
catch(e)
{
p = this;
}
}
// If we actually just moused on to a sub-element, ignore it
if ( p == this )
{
return false;
}
return true;
};
var mainHoverIn = function(e)
{
//no need to test e.target==this, as no child has the same event binded
//its possible, that a main menu item still has hover (if it has no submenu) - thus remove it
var lis = getAllChilds(this.parentNode, 'LI');
var pattern = new RegExp("(^|\\s)hover(\\s|$)");
for (var i = 0; i < lis.length; i++)
{
if ( pattern.test(lis[i].className) )
{
$(lis[i]).removeClass('hover');
}
}
$(this).addClass('hover');
if ( shown )
{
hoverIn(this, settings.mainDelay);
}
};
var liHoverIn = function(e)
{
if ( !testHandleHover(e) )
{
return false;
}
if ( e.target != this )
{
//look whether the target is a direct child of this (maybe an image)
if ( !isChild(this, e.target) )
{
return;
}
}
hoverIn(this, settings.subDelay);
};
var hoverIn = function(li, delay)
{
var innerDiv = getOneChild(li, 'DIV');
//stop running timers from the other menus on the same level - a little faster than $('>*>div', li.parentNode)
var n = li.parentNode.firstChild;
for ( ; n; n = n.nextSibling )
{
if ( n.nodeType == 1 && n.nodeName.toUpperCase() == 'LI' )
{
var div = getOneChild(n, 'DIV');
if ( div && div.timer && !div.isVisible ) //clear show-div timer
{
clearTimeout(div.timer);
div.timer = null;
}
}
}
//is there a timer running to hide one of the parent divs? stop it
var pNode = li.parentNode;
for ( ; pNode; pNode = pNode.parentNode )
{
if ( pNode.nodeType == 1 && pNode.nodeName.toUpperCase() == 'DIV' )
{
if (pNode.timer)
{
clearTimeout(pNode.timer);
pNode.timer = null;
$(pNode.parentNode).addClass('hover');
}
}
}
//highlight the current element
$(li).addClass('hover');
//is the submenu already visible?
if ( innerDiv && innerDiv.isVisible )
{
//hide-timer running?
if ( innerDiv.timer )
{
clearTimeout(innerDiv.timer);
innerDiv.timer = null;
}
else
{
return;
}
}
//hide all open menus on the same level and below and unhighlight the li item (but not the current submenu!)
$(li.parentNode.getElementsByTagName('DIV')).each(function(){
if ( this != innerDiv && this.isVisible )
{
hideDIV(this, delay);
$(this.parentNode).removeClass('hover');
}
});
//show the submenu, if there is one
if ( innerDiv )
{
showDIV(innerDiv, delay);
}
};
var liHoverOut = function(e)
{
if ( !testHandleHover(e) )
{
return false;
}
if ( e.target != this )
{
if ( !isChild(this, e.target) ) //return only if the target is no direct child of this
{
return;
}
}
//remove the hover from the submenu item, if the mouse is hovering out of the menu (this is only for the last open (levelwise) (sub-)menu)
var div = getOneChild(this, 'DIV');
if ( !div )
{
$(this).removeClass('hover');
}
else
{
if ( !div.isVisible )
{
$(this).removeClass('hover');
}
}
};
var mainHoverOut = function(e)
{
//no need to test e.target==this, as no child has the same event binded
//remove hover
var div = getOneChild(this, 'DIV');
var relTarget = e.relatedTarget || e.toElement; //this is undefined sometimes (e.g. when the mouse moves out of the window), so dont remove hover then
var p;
if ( !shown )
{
$(this).removeClass('hover');
}
else if ( !div && relTarget ) //menuitem has no submenu, so dont remove the hover if the mouse goes outside the menu
{
p = findParentWithClass(e.target, 'UL', 'clickMenu');
if ( p.contains(relTarget))
{
$(this).removeClass('hover');
}
}
else if ( relTarget )
{
//remove hover only when moving to anywhere inside the clickmenu
p = findParentWithClass(e.target, 'UL', 'clickMenu');
if ( !div.isVisible && (p.contains(relTarget)) )
{
$(this).removeClass('hover');
}
}
};
var mainClick = function()
{
var div = getOneChild(this, 'DIV');
if ( div && div.isVisible ) //clicked on an open main-menu-item
{
clean();
$(this).addClass('hover');
}
else
{
hoverIn(this, settings.mainDelay);
shown = true;
$(document).bind('mousedown', checkMouse);
}
return false;
};
var checkMouse = function(e)
{
//is the mouse inside a clickmenu? if yes, is it an open (the current) one?
var vis = false;
var cm = findParentWithClass(e.target, 'UL', 'clickMenu');
if ( cm )
{
$(cm.getElementsByTagName('DIV')).each(function(){
if ( this.isVisible )
{
vis = true;
}
});
}
if ( !vis )
{
clean();
}
};
var clean = function()
{
//remove timeout and hide the divs
$('ul.clickMenu div.outerbox').each(function(){
if ( this.timer )
{
clearTimeout(this.timer);
this.timer = null;
}
if ( this.isVisible )
{
$(this).hide();
this.isVisible = false;
}
});
$('ul.clickMenu li').removeClass('hover');
//remove events
$('ul.clickMenu>li li').unbind('mouseenter', liHoverIn).unbind('mouseleave', liHoverOut).unbind('click', settings.onClick);
$(document).unbind('mousedown', checkMouse);
shown = false;
};
var getOneChild = function(elem, name)
{
if ( !elem )
{
return null;
}
var n = elem.firstChild;
for ( ; n; n = n.nextSibling )
{
if ( n.nodeType == 1 && n.nodeName.toUpperCase() == name )
{
return n;
}
}
return null;
};
var getAllChilds = function(elem, name)
{
if ( !elem )
{
return [];
}
var r = [];
var n = elem.firstChild;
for ( ; n; n = n.nextSibling )
{
if ( n.nodeType == 1 && n.nodeName.toUpperCase() == name )
{
r[r.length] = n;
}
}
return r;
};
var findParentWithClass = function(elem, searchTag, searchClass)
{
var pNode = elem.parentNode;
var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
for ( ; pNode; pNode = pNode.parentNode )
{
if ( pNode.nodeType == 1 && pNode.nodeName.toUpperCase() == searchTag && pattern.test(pNode.className) )
{
return pNode;
}
}
return null;
};
var checkClass = function(elem, searchClass)
{
var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
if ( pattern.test(elem.className) )
{
return true;
}
return false;
};
var isChild = function(elem, childElem)
{
var n = elem.firstChild;
for ( ; n; n = n.nextSibling )
{
if ( n == childElem )
{
return true;
}
}
return false;
};
return this.each(function()
{
//add .contains() to mozilla - http://www.quirksmode.org/blog/archives/2006/01/contains_for_mo.html
if (window.Node && Node.prototype && !Node.prototype.contains)
{
Node.prototype.contains = function(arg)
{
return !!(this.compareDocumentPosition(arg) & 16);
};
}
//add class
if ( !checkClass(this, 'clickMenu') )
{
$(this).addClass('clickMenu');
}
//add shadows
$('ul', this).shadowBox();
//ie6? - add iframes
if ( $.browser.msie && (!$.browser.version || parseInt($.browser.version) <= 6) )
{
if ( $.fn.bgiframe )
{
$('div.outerbox', this).bgiframe();
}
else
{
/* thanks to Mark Gibson - http://www.nabble.com/forum/ViewPost.jtp?post=6504414&framed=y */
$('div.outerbox', this).append('<iframe style="display:block;position:absolute;top:0;left:0;z-index:-1;filter:mask();' +
'width:expression(this.parentNode.offsetWidth);height:expression(this.parentNode.offsetHeight)"/>');
}
}
//assign events
$(this).bind('closemenu', function(){clean();}); //assign closemenu-event, through wich the menu can be closed from outside the plugin
//add click event handling, if there are any elements inside the main menu
var liElems = getAllChilds(this, 'LI');
for ( var j = 0; j < liElems.length; j++ )
{
if ( getOneChild(getOneChild(getOneChild(liElems[j], 'DIV'), 'UL'), 'LI') ) // >div>ul>li
{
$(liElems[j]).click(mainClick);
}
}
//add hover event handling and assign classes
$(liElems).hover(mainHoverIn, mainHoverOut).addClass('main').find('>div').addClass('inner');
//add the little arrow before each submenu
if ( settings.arrowSrc )
{
$('div.inner div.outerbox', this).before('<img src="' + settings.arrowSrc + '" class="liArrow" />');
}
//the floating list elements are destroying the layout..so make it nice again..
$(this).wrap('<div class="cmDiv"></div>').after('<div style="clear: both; visibility: hidden;"></div>');
});
};
$.fn.clickMenu.setDefaults = function(o)
{
$.extend(defaults, o);
};
})(jQuery);
(function($)
{
$.fn.shadowBox = function() {
return this.each(function()
{
var outer = $('<div class="outerbox"></div>').get(0);
if ( $(this).css('position') == 'absolute' )
{
//if the child(this) is positioned abolute, we have to use relative positioning and shrink the outerbox accordingly to the innerbox
$(outer).css({position:'relative', width:this.offsetWidth, height:this.offsetHeight});
}
else
{
//shrink the outerbox
$(outer).css('position', 'absolute');
}
//add the boxes
$(this).addClass('innerBox').wrap(outer).
before('<div class="shadowbox1"></div><div class="shadowbox2"></div><div class="shadowbox3"></div>');
});
};
})(jQuery);