3 * This script was created by Erik Arvidsson (erik@eae.net)
4 * for WebFX (http://webfx.eae.net)
7 * For usage see license at http://webfx.eae.net/license.html
10 * Updates: 2001-11-20 Added hover mode support and removed Opera focus hacks
11 * 2001-12-20 Added auto positioning and some properties to support this
12 * 2002-08-13 toString used ' for attributes. Changed to " to allow in args
16 var ua = navigator.userAgent;
17 var opera = /opera [56789]|opera\/[56789]/i.test(ua);
18 var ie = !opera && /MSIE/.test(ua);
19 var ie50 = ie && /MSIE 5\.[01234]/.test(ua);
20 var ie6 = ie && /MSIE [6789]/.test(ua);
21 var ieBox = ie && (document.compatMode == null || document.compatMode != "CSS1Compat");
22 var moz = !opera && /gecko/i.test(ua);
23 var nn6 = !opera && /netscape.*6\./i.test(ua);
24 var khtml = /KHTML/i.test(ua);
26 // define the default values
28 webfxMenuDefaultWidth = 154;
30 webfxMenuDefaultBorderLeft = 1;
31 webfxMenuDefaultBorderRight = 1;
32 webfxMenuDefaultBorderTop = 1;
33 webfxMenuDefaultBorderBottom = 1;
35 webfxMenuDefaultPaddingLeft = 1;
36 webfxMenuDefaultPaddingRight = 1;
37 webfxMenuDefaultPaddingTop = 1;
38 webfxMenuDefaultPaddingBottom = 1;
40 webfxMenuDefaultShadowLeft = 0;
41 webfxMenuDefaultShadowRight = ie && !ie50 && /win32/i.test(navigator.platform) ? 4 :0;
42 webfxMenuDefaultShadowTop = 0;
43 webfxMenuDefaultShadowBottom = ie && !ie50 && /win32/i.test(navigator.platform) ? 4 : 0;
46 webfxMenuItemDefaultHeight = 18;
47 webfxMenuItemDefaultText = "Untitled";
48 webfxMenuItemDefaultHref = "javascript:void(0)";
50 webfxMenuSeparatorDefaultHeight = 6;
52 webfxMenuDefaultEmptyText = "Empty";
54 webfxMenuDefaultUseAutoPosition = nn6 ? false : true;
58 // other global constants
60 webfxMenuImagePath = "";
62 webfxMenuUseHover = opera ? true : false;
63 webfxMenuHideTime = 500;
64 webfxMenuShowTime = 200;
68 var webFXMenuHandler = {
70 idPrefix : "webfx-menu-object-",
72 getId : function () { return this.idPrefix + this.idCounter++; },
73 overMenuItem : function (oItem) {
74 if (this.showTimeout != null)
75 window.clearTimeout(this.showTimeout);
76 if (this.hideTimeout != null)
77 window.clearTimeout(this.hideTimeout);
78 var jsItem = this.all[oItem.id];
79 if (webfxMenuShowTime <= 0)
82 //this.showTimeout = window.setTimeout(function () { webFXMenuHandler._over(jsItem) ; }, webfxMenuShowTime);
83 // I hate IE5.0 because the piece of shit crashes when using setTimeout with a function object
84 this.showTimeout = window.setTimeout("webFXMenuHandler._over(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuShowTime);
86 outMenuItem : function (oItem) {
87 if (this.showTimeout != null)
88 window.clearTimeout(this.showTimeout);
89 if (this.hideTimeout != null)
90 window.clearTimeout(this.hideTimeout);
91 var jsItem = this.all[oItem.id];
92 if (webfxMenuHideTime <= 0)
95 //this.hideTimeout = window.setTimeout(function () { webFXMenuHandler._out(jsItem) ; }, webfxMenuHideTime);
96 this.hideTimeout = window.setTimeout("webFXMenuHandler._out(webFXMenuHandler.all['" + jsItem.id + "'])", webfxMenuHideTime);
98 blurMenu : function (oMenuItem) {
99 window.setTimeout("webFXMenuHandler.all[\"" + oMenuItem.id + "\"].subMenu.hide();", webfxMenuHideTime);
101 _over : function (jsItem) {
102 if (jsItem.subMenu) {
103 jsItem.parentMenu.hideAllSubs();
104 jsItem.subMenu.show();
107 jsItem.parentMenu.hideAllSubs();
109 _out : function (jsItem) {
110 // find top most menu
113 if (root instanceof WebFXMenuButton)
116 m = jsItem.parentMenu;
117 while (m.parentMenu != null && !(m.parentMenu instanceof WebFXMenuBar))
123 hideMenu : function (menu) {
124 if (this.showTimeout != null)
125 window.clearTimeout(this.showTimeout);
126 if (this.hideTimeout != null)
127 window.clearTimeout(this.hideTimeout);
129 this.hideTimeout = window.setTimeout("webFXMenuHandler.all['" + menu.id + "'].hide()", webfxMenuHideTime);
131 showMenu : function (menu, src, dir) {
132 if (this.showTimeout != null)
133 window.clearTimeout(this.showTimeout);
134 if (this.hideTimeout != null)
135 window.clearTimeout(this.hideTimeout);
137 if (arguments.length < 3)
144 function WebFXMenu() {
145 this._menuItems = [];
147 this.id = webFXMenuHandler.getId();
151 this.parentMenu = null;
152 webFXMenuHandler.all[this.id] = this;
155 WebFXMenu.prototype.width = webfxMenuDefaultWidth;
156 WebFXMenu.prototype.emptyText = webfxMenuDefaultEmptyText;
157 WebFXMenu.prototype.useAutoPosition = webfxMenuDefaultUseAutoPosition;
159 WebFXMenu.prototype.borderLeft = webfxMenuDefaultBorderLeft;
160 WebFXMenu.prototype.borderRight = webfxMenuDefaultBorderRight;
161 WebFXMenu.prototype.borderTop = webfxMenuDefaultBorderTop;
162 WebFXMenu.prototype.borderBottom = webfxMenuDefaultBorderBottom;
164 WebFXMenu.prototype.paddingLeft = webfxMenuDefaultPaddingLeft;
165 WebFXMenu.prototype.paddingRight = webfxMenuDefaultPaddingRight;
166 WebFXMenu.prototype.paddingTop = webfxMenuDefaultPaddingTop;
167 WebFXMenu.prototype.paddingBottom = webfxMenuDefaultPaddingBottom;
169 WebFXMenu.prototype.shadowLeft = webfxMenuDefaultShadowLeft;
170 WebFXMenu.prototype.shadowRight = webfxMenuDefaultShadowRight;
171 WebFXMenu.prototype.shadowTop = webfxMenuDefaultShadowTop;
172 WebFXMenu.prototype.shadowBottom = webfxMenuDefaultShadowBottom;
176 WebFXMenu.prototype.add = function (menuItem) {
177 this._menuItems[this._menuItems.length] = menuItem;
178 if (menuItem.subMenu) {
179 this._subMenus[this._subMenus.length] = menuItem.subMenu;
180 menuItem.subMenu.parentMenu = this;
183 menuItem.parentMenu = this;
186 WebFXMenu.prototype.show = function (relObj, sDir) {
187 if (this.useAutoPosition)
188 this.position(relObj, sDir);
190 var divElement = document.getElementById(this.id);
193 divElement.style.left = opera ? this.left : this.left + "px";
194 divElement.style.top = opera ? this.top : this.top + "px";
195 divElement.style.visibility = "visible";
198 var shimElement = document.getElementById(this.id + "Shim");
200 shimElement.style.width = divElement.offsetWidth;
201 shimElement.style.height = divElement.offsetHeight;
202 shimElement.style.top = divElement.style.top;
203 shimElement.style.left = divElement.style.left;
204 /*shimElement.style.zIndex = divElement.style.zIndex - 1; */
205 shimElement.style.display = "block";
206 shimElement.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
215 this.parentMenu.show();
218 WebFXMenu.prototype.hide = function () {
220 var divElement = document.getElementById(this.id);
222 divElement.style.visibility = "hidden";
224 var shimElement = document.getElementById(this.id + "Shim");
226 shimElement.style.display = "none";
234 WebFXMenu.prototype.hideAllSubs = function () {
235 for (var i = 0; i < this._subMenus.length; i++) {
236 if (this._subMenus[i].shown)
237 this._subMenus[i].hide();
241 WebFXMenu.prototype.toString = function () {
242 var top = this.top + this.borderTop + this.paddingTop;
243 var str = "<div id='" + this.id + "' class='webfx-menu' style='" +
245 this.width - this.borderLeft - this.paddingLeft - this.borderRight - this.paddingRight :
246 this.width) + "px;" +
247 (this.useAutoPosition ?
248 "left:" + this.left + "px;" + "top:" + this.top + "px;" :
250 (ie50 ? "filter: none;" : "") +
253 if (this._menuItems.length == 0) {
254 str += "<span class='webfx-menu-empty'>" + this.emptyText + "</span>";
257 str += '<span class="webfx-menu-title" onmouseover="webFXMenuHandler.overMenuItem(this)"' +
258 (webfxMenuUseHover ? " onmouseout='webFXMenuHandler.outMenuItem(this)'" : "") +
259 '>' + this.emptyText + '</span>';
260 // str += '<div id="' + this.id + '-title">' + this.emptyText + '</div>';
261 // loop through all menuItems
262 for (var i = 0; i < this._menuItems.length; i++) {
263 var mi = this._menuItems[i];
265 if (!this.useAutoPosition) {
266 if (mi.subMenu && !mi.subMenu.useAutoPosition)
267 mi.subMenu.top = top - mi.subMenu.borderTop - mi.subMenu.paddingTop;
277 str += "<iframe id='" + this.id + "Shim' src='javascript:false;' scrolling='no' frameBorder='0' style='position:absolute; top:0px; left: 0px; display:none;'></iframe>";
280 for (var i = 0; i < this._subMenus.length; i++) {
281 this._subMenus[i].left = this.left + this.width - this._subMenus[i].borderLeft;
282 str += this._subMenus[i];
287 // WebFXMenu.prototype.position defined later
289 function WebFXMenuItem(sText, sHref, sToolTip, oSubMenu) {
290 this.text = sText || webfxMenuItemDefaultText;
291 this.href = (sHref == null || sHref == "") ? webfxMenuItemDefaultHref : sHref;
292 this.subMenu = oSubMenu;
294 oSubMenu.parentMenuItem = this;
295 this.toolTip = sToolTip;
296 this.id = webFXMenuHandler.getId();
297 webFXMenuHandler.all[this.id] = this;
299 WebFXMenuItem.prototype.height = webfxMenuItemDefaultHeight;
300 WebFXMenuItem.prototype.toString = function () {
302 " id='" + this.id + "'" +
303 " href=\"" + this.href + "\"" +
304 (this.toolTip ? " title=\"" + this.toolTip + "\"" : "") +
305 " onmouseover='webFXMenuHandler.overMenuItem(this)'" +
306 (webfxMenuUseHover ? " onmouseout='webFXMenuHandler.outMenuItem(this)'" : "") +
307 (this.subMenu ? " unselectable='on' tabindex='-1'" : "") +
309 (this.subMenu ? "<img class='arrow' src=\"" + webfxMenuImagePath + "arrow.right.black.png\">" : "") +
315 function WebFXMenuSeparator() {
316 this.id = webFXMenuHandler.getId();
317 webFXMenuHandler.all[this.id] = this;
319 WebFXMenuSeparator.prototype.height = webfxMenuSeparatorDefaultHeight;
320 WebFXMenuSeparator.prototype.toString = function () {
322 " id='" + this.id + "'" +
324 " onmouseover='webFXMenuHandler.overMenuItem(this)'" +
325 " onmouseout='webFXMenuHandler.outMenuItem(this)'"
331 function WebFXMenuBar() {
332 this._parentConstructor = WebFXMenu;
333 this._parentConstructor();
335 WebFXMenuBar.prototype = new WebFXMenu;
336 WebFXMenuBar.prototype.toString = function () {
337 var str = "<div id='" + this.id + "' class='webfx-menu-bar'>";
339 // loop through all menuButtons
340 for (var i = 0; i < this._menuItems.length; i++)
341 str += this._menuItems[i];
345 for (var i = 0; i < this._subMenus.length; i++)
346 str += this._subMenus[i];
351 function WebFXMenuButton(sText, sHref, sToolTip, oSubMenu) {
352 this._parentConstructor = WebFXMenuItem;
353 this._parentConstructor(sText, sHref, sToolTip, oSubMenu);
355 WebFXMenuButton.prototype = new WebFXMenuItem;
356 WebFXMenuButton.prototype.toString = function () {
358 " id='" + this.id + "'" +
359 " href='" + this.href + "'" +
360 (this.toolTip ? " title='" + this.toolTip + "'" : "") +
362 (" onmouseover='webFXMenuHandler.overMenuItem(this)'" +
363 " onmouseout='webFXMenuHandler.outMenuItem(this)'") :
365 " onfocus='webFXMenuHandler.overMenuItem(this)'" +
367 " onblur='webFXMenuHandler.blurMenu(this)'" :
372 (this.subMenu ? "<img class='arrow' src='" + webfxMenuImagePath + "arrow.right.png'>" : "") +
381 /* Position functions */
384 function getInnerLeft(el) {
386 if (el == null) return 0;
388 if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
390 return parseInt( getLeft(el) + parseInt(getBorderLeft(el)) );
396 function getLeft(el, debug) {
398 if (el == null) return 0;
401 // alert ( el.offsetLeft + ' - ' + getInnerLeft(el.offsetParent) );
403 return parseInt( el.offsetLeft + parseInt(getInnerLeft(el.offsetParent)) );
409 function getInnerTop(el) {
411 if (el == null) return 0;
413 if (ieBox && el == document.body || !ieBox && el == document.documentElement) return 0;
415 return parseInt( getTop(el) + parseInt(getBorderTop(el)) );
421 function getTop(el) {
423 if (el == null) return 0;
425 return parseInt( el.offsetTop + parseInt(getInnerTop(el.offsetParent)) );
431 function getBorderLeft(el) {
438 ? parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue("border-left-width"))
439 : parseInt(window.getComputedStyle(el, null).getPropertyValue("border-left-width"))
446 function getBorderTop(el) {
453 ? parseInt(document.defaultView.getComputedStyle(el, null).getPropertyValue("border-left-width"))
454 : parseInt(window.getComputedStyle(el, null).getPropertyValue("border-top-width"))
461 function opera_getLeft(el) {
463 if (el == null) return 0;
465 return el.offsetLeft + opera_getLeft(el.offsetParent);
471 function opera_getTop(el) {
473 if (el == null) return 0;
475 return el.offsetTop + opera_getTop(el.offsetParent);
481 function getOuterRect(el, debug) {
485 left: (opera ? opera_getLeft(el) : getLeft(el, debug)),
487 top: (opera ? opera_getTop(el) : getTop(el)),
489 width: el.offsetWidth,
491 height: el.offsetHeight
499 // mozilla bug! scrollbars not included in innerWidth/height
501 function getDocumentRect(el) {
511 (ieBox ? document.body.clientWidth : document.documentElement.clientWidth) :
519 (ieBox ? document.body.clientHeight : document.documentElement.clientHeight) :
531 function getScrollPos(el) {
537 (ieBox ? document.body.scrollLeft : document.documentElement.scrollLeft) :
545 (ieBox ? document.body.scrollTop : document.documentElement.scrollTop) :
556 /* end position functions */
558 WebFXMenu.prototype.position = function (relEl, sDir) {
560 // find parent item rectangle, piRect
563 var pi = this.parentMenuItem;
564 if (!this.parentMenuItem)
567 relEl = document.getElementById(pi.id);
569 dir = pi instanceof WebFXMenuButton ? "vertical" : "horizontal";
570 //alert('created RelEl from parent: ' + pi.id);
571 piRect = getOuterRect(relEl, 1);
573 else if (relEl.left != null && relEl.top != null && relEl.width != null && relEl.height != null) { // got a rect
574 //alert('passed a Rect as RelEl: ' + typeof(relEl));
579 //alert('passed an element as RelEl: ' + typeof(relEl));
580 piRect = getOuterRect(relEl);
583 var menuEl = document.getElementById(this.id);
584 var menuRect = getOuterRect(menuEl);
585 var docRect = getDocumentRect();
586 var scrollPos = getScrollPos();
587 var pMenu = this.parentMenu;
589 if (dir == "vertical") {
590 if (piRect.left + menuRect.width - scrollPos.left <= docRect.width) {
591 //alert('piRect.left: ' + piRect.left);
592 this.left = piRect.left;
594 this.left = this.left + 138;
595 } else if (docRect.width >= menuRect.width) {
596 //konq (not safari though) winds up here by accident and positions the menus all weird
597 //alert('docRect.width + scrollPos.left - menuRect.width');
599 this.left = docRect.width + scrollPos.left - menuRect.width;
601 //alert('scrollPos.left: ' + scrollPos.left);
602 this.left = scrollPos.left;
605 if (piRect.top + piRect.height + menuRect.height <= docRect.height + scrollPos.top)
607 this.top = piRect.top + piRect.height;
609 else if (piRect.top - menuRect.height >= scrollPos.top)
611 this.top = piRect.top - menuRect.height;
613 else if (docRect.height >= menuRect.height)
615 this.top = docRect.height + scrollPos.top - menuRect.height;
619 this.top = scrollPos.top;
622 if (piRect.top + menuRect.height - this.borderTop - this.paddingTop <= docRect.height + scrollPos.top)
624 this.top = piRect.top - this.borderTop - this.paddingTop;
626 else if (piRect.top + piRect.height - menuRect.height + this.borderTop + this.paddingTop >= 0)
628 this.top = piRect.top + piRect.height - menuRect.height + this.borderBottom + this.paddingBottom + this.shadowBottom;
630 else if (docRect.height >= menuRect.height)
632 this.top = docRect.height + scrollPos.top - menuRect.height;
636 this.top = scrollPos.top;
640 var pMenuPaddingLeft = pMenu ? pMenu.paddingLeft : 0;
642 var pMenuBorderLeft = pMenu ? pMenu.borderLeft : 0;
644 var pMenuPaddingRight = pMenu ? pMenu.paddingRight : 0;
646 var pMenuBorderRight = pMenu ? pMenu.borderRight : 0;
650 if (piRect.left + piRect.width + menuRect.width + pMenuPaddingRight +
652 pMenuBorderRight - this.borderLeft + this.shadowRight <= docRect.width + scrollPos.left)
654 this.left = piRect.left + piRect.width + pMenuPaddingRight + pMenuBorderRight - this.borderLeft;
656 else if (piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight >= 0)
658 this.left = piRect.left - menuRect.width - pMenuPaddingLeft - pMenuBorderLeft + this.borderRight + this.shadowRight;
660 else if (docRect.width >= menuRect.width)
662 this.left = docRect.width + scrollPos.left - menuRect.width;
666 this.left = scrollPos.left;