1 /**
2 * dropDownMenu v0.5 sw edition
3 * An easy to implement dropDown Menu for Websites, that may be based on styled list tags
4 *
5 * Works for IE 5.5+ PC, Mozilla 1+ all Plattforms, Opera 7+
6 *
7 * Copyright (c) 2004 Knallgrau New Medias Solutions GmbH, Vienna - Austria
8 *
9 * Original written by Matthias Platzer at http://knallgrau.at
10 *
11 * Modified by Sven Wappler http://www.wappler.eu
12 *
13 * Use it as you need it
14 * It is distributed under a BSD style license
15 */
18 /**
19 * Container Class (Prototype) for the dropDownMenu
20 *
21 * @param idOrElement String|HTMLElement root Node of the menu (ul)
22 * @param name String name of the variable that stores the result
23 * of this constructor function
24 * @param customConfigFunction Function optional config function to override the default settings
25 * for an example see Menu.prototype.config
26 */
27 var Menu = Class.create();
28 Menu.prototype = {
30 initialize: function(idOrElement, name, customConfigFunction) {
32 this.name = name;
33 this.type = "menu";
34 this.closeDelayTimer = null;
35 this.closingMenuItem = null;
37 this.config();
38 if (typeof customConfigFunction == "function") {
39 this.customConfig = customConfigFunction;
40 this.customConfig();
41 }
42 this.rootContainer = new MenuContainer(idOrElement, this);
43 },
45 config: function() {
46 this.collapseBorders = true;
47 this.quickCollapse = true;
48 this.closeDelayTime = 500;
49 }
51 }
53 var MenuContainer = Class.create();
54 MenuContainer.prototype = {
55 initialize: function(idOrElement, parent) {
56 this.type = "menuContainer";
57 this.menuItems = [];
58 this.init(idOrElement, parent);
59 },
61 init: function(idOrElement, parent) {
62 this.element = $(idOrElement);
63 this.parent = parent;
64 this.parentMenu = (this.type == "menuContainer") ? ((parent) ? parent.parent : null) : parent;
65 this.root = parent instanceof Menu ? parent : parent.root;
66 this.id = this.element.id;
68 if (this.type == "menuContainer") {
69 if (this.element.hasClassName("level1")) this.menuType = "horizontal";
70 else if (this.element.hasClassName("level2")) this.menuType = "dropdown";
71 else this.menuType = "flyout";
73 if (this.menuType == "flyout" || this.menuType == "dropdown") {
74 this.isOpen = false;
75 Element.setStyle(this.element,{
76 position: "absolute",
77 top: "0px",
78 left: "0px",
79 visibility: "hidden"});
80 } else {
81 this.isOpen = true;
82 }
83 } else {
84 this.isOpen = this.parentMenu.isOpen;
85 }
87 var childNodes = this.element.childNodes;
88 if (childNodes == null) return;
90 for (var i = 0; i < childNodes.length; i++) {
91 var node = childNodes[i];
92 if (node.nodeType == 1) {
93 if (this.type == "menuContainer") {
94 if (node.tagName.toLowerCase() == "li") {
95 this.menuItems.push(new MenuItem(node, this));
96 }
97 } else {
98 if (node.tagName.toLowerCase() == "ul") {
99 this.subMenu = new MenuContainer(node, this);
100 }
101 }
102 }
103 }
104 },
106 getBorders: function(element) {
107 var ltrb = ["Left","Top","Right","Bottom"];
108 var result = {};
109 for (var i = 0; i < ltrb.length; ++i) {
110 if (this.element.currentStyle)
111 var value = parseInt(this.element.currentStyle["border"+ltrb[i]+"Width"]);
112 else if (window.getComputedStyle)
113 var value = parseInt(window.getComputedStyle(this.element, "").getPropertyValue("border-"+ltrb[i].toLowerCase()+"-width"));
114 else
115 var value = parseInt(this.element.style["border"+ltrb[i]]);
116 result[ltrb[i].toLowerCase()] = isNaN(value) ? 0 : value;
117 }
118 return result;
119 },
121 open: function() {
122 if (this.root.closeDelayTimer) window.clearTimeout(this.root.closeDelayTimer);
123 this.parentMenu.closeAll(this);
124 this.isOpen = true;
125 if (this.menuType == "dropdown") {
126 Element.setStyle(this.element,{
127 left: (Position.positionedOffset(this.parent.element)[0]) + "px",
128 top: (Position.positionedOffset(this.parent.element)[1] + Element.getHeight(this.parent.element)) + "px"
129 });
131 } else if (this.menuType == "flyout") {
132 var parentMenuBorders = this.parentMenu ? this.parentMenu.getBorders() : new Object();
133 var thisBorders = this.getBorders();
134 if (
135 (Position.positionedOffset(this.parentMenu.element)[0] + this.parentMenu.element.offsetWidth + this.element.offsetWidth + 20) >
136 (window.innerWidth ? window.innerWidth : document.body.offsetWidth)
137 ) {
138 Element.setStyle(this.element,{
139 left: (- this.element.offsetWidth - (this.root.collapseBorders ? 0 : parentMenuBorders["left"])) + "px"
140 });
141 } else {
142 Element.setStyle(this.element,{
143 left: (this.parentMenu.element.offsetWidth - parentMenuBorders["left"] - (this.root.collapseBorders ? Math.min(parentMenuBorders["right"], thisBorders["left"]) : 0)) + "px"
144 });
145 }
146 Element.setStyle(this.element,{
147 top: (this.parent.element.offsetTop - parentMenuBorders["top"] - this.menuItems[0].element.offsetTop) + "px"
148 });
149 }
150 Element.setStyle(this.element,{visibility: "visible"});
151 },
153 close: function() {
154 Element.setStyle(this.element,{visibility: "hidden"});
155 this.isOpen = false;
156 this.closeAll();
157 },
159 closeAll: function(trigger) {
160 for (var i = 0; i < this.menuItems.length; ++i) {
161 this.menuItems[i].closeItem(trigger);
162 }
163 }
165 }
168 var MenuItem = Class.create();
170 Object.extend(Object.extend(MenuItem.prototype, MenuContainer.prototype), {
171 initialize: function(idOrElement, parent) {
172 var menuItem = this;
173 this.type = "menuItem";
174 this.subMenu;
175 this.init(idOrElement, parent);
176 if (this.subMenu) {
177 this.element.onmouseover = function() {
178 menuItem.subMenu.open();
179 }
180 } else {
181 if (this.root.quickCollapse) {
182 this.element.onmouseover = function() {
183 menuItem.parentMenu.closeAll();
184 }
185 }
186 }
187 var linkTag = this.element.getElementsByTagName("A")[0];
188 if (linkTag) {
189 linkTag.onfocus = this.element.onmouseover;
190 this.link = linkTag;
191 this.text = linkTag.text;
192 }
193 if (this.subMenu) {
194 this.element.onmouseout = function() {
195 if (menuItem.root.openDelayTimer) window.clearTimeout(menuItem.root.openDelayTimer);
196 if (menuItem.root.closeDelayTimer) window.clearTimeout(menuItem.root.closeDelayTimer);
197 eval(menuItem.root.name + ".closingMenuItem = menuItem");
198 menuItem.root.closeDelayTimer = window.setTimeout(menuItem.root.name + ".closingMenuItem.subMenu.close()", menuItem.root.closeDelayTime);
199 }
200 }
201 },
203 openItem: function() {
204 this.isOpen = true;
205 if (this.subMenu) { this.subMenu.open(); }
206 },
208 closeItem: function(trigger) {
209 this.isOpen = false;
210 if (this.subMenu) {
211 if (this.subMenu != trigger) this.subMenu.close();
212 }
213 }
214 });
217 var menu;
220 function configMenu() {
221 this.closeDelayTime = 300;
222 }
224 function initMenu() {
225 menu = new Menu('root', 'menu', configMenu);
226 }
229 Event.observe(window, 'load', initMenu, false);