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 if (!this.element) return;
64 this.parent = parent;
65 this.parentMenu = (this.type == "menuContainer") ? ((parent) ? parent.parent : null) : parent;
66 this.root = parent instanceof Menu ? parent : parent.root;
67 this.id = this.element.id;
69 if (this.type == "menuContainer") {
70 if (this.element.hasClassName("level1")) this.menuType = "horizontal";
71 else if (this.element.hasClassName("level2")) this.menuType = "dropdown";
72 else this.menuType = "flyout";
74 if (this.menuType == "flyout" || this.menuType == "dropdown") {
75 this.isOpen = false;
76 Element.setStyle(this.element,{
77 position: "absolute",
78 top: "0px",
79 left: "0px",
80 visibility: "hidden"});
81 } else {
82 this.isOpen = true;
83 }
84 } else {
85 this.isOpen = this.parentMenu.isOpen;
86 }
88 var childNodes = this.element.childNodes;
89 if (childNodes == null) return;
91 for (var i = 0; i < childNodes.length; i++) {
92 var node = childNodes[i];
93 if (node.nodeType == 1) {
94 if (this.type == "menuContainer") {
95 if (node.tagName.toLowerCase() == "li") {
96 this.menuItems.push(new MenuItem(node, this));
97 }
98 } else {
99 if (node.tagName.toLowerCase() == "ul") {
100 this.subMenu = new MenuContainer(node, this);
101 }
102 }
103 }
104 }
105 },
107 getBorders: function(element) {
108 var ltrb = ["Left","Top","Right","Bottom"];
109 var result = {};
110 for (var i = 0; i < ltrb.length; ++i) {
111 if (this.element.currentStyle)
112 var value = parseInt(this.element.currentStyle["border"+ltrb[i]+"Width"]);
113 else if (window.getComputedStyle)
114 var value = parseInt(window.getComputedStyle(this.element, "").getPropertyValue("border-"+ltrb[i].toLowerCase()+"-width"));
115 else
116 var value = parseInt(this.element.style["border"+ltrb[i]]);
117 result[ltrb[i].toLowerCase()] = isNaN(value) ? 0 : value;
118 }
119 return result;
120 },
122 open: function() {
123 if (this.root.closeDelayTimer) window.clearTimeout(this.root.closeDelayTimer);
124 this.parentMenu.closeAll(this);
125 this.isOpen = true;
126 if (this.menuType == "dropdown") {
127 Element.setStyle(this.element,{
128 left: (Position.positionedOffset(this.parent.element)[0]) + "px",
129 top: (Position.positionedOffset(this.parent.element)[1] + Element.getHeight(this.parent.element)) + "px"
130 });
132 } else if (this.menuType == "flyout") {
133 var parentMenuBorders = this.parentMenu ? this.parentMenu.getBorders() : new Object();
134 var thisBorders = this.getBorders();
135 if (
136 (Position.positionedOffset(this.parentMenu.element)[0] + this.parentMenu.element.offsetWidth + this.element.offsetWidth + 20) >
137 (window.innerWidth ? window.innerWidth : document.body.offsetWidth)
138 ) {
139 Element.setStyle(this.element,{
140 left: (- this.element.offsetWidth - (this.root.collapseBorders ? 0 : parentMenuBorders["left"])) + "px"
141 });
142 } else {
143 Element.setStyle(this.element,{
144 left: (this.parentMenu.element.offsetWidth - parentMenuBorders["left"] - (this.root.collapseBorders ? Math.min(parentMenuBorders["right"], thisBorders["left"]) : 0)) + "px"
145 });
146 }
147 Element.setStyle(this.element,{
148 top: (this.parent.element.offsetTop - parentMenuBorders["top"] - this.menuItems[0].element.offsetTop) + "px"
149 });
150 }
151 Element.setStyle(this.element,{visibility: "visible"});
152 },
154 close: function() {
155 Element.setStyle(this.element,{visibility: "hidden"});
156 this.isOpen = false;
157 this.closeAll();
158 },
160 closeAll: function(trigger) {
161 for (var i = 0; i < this.menuItems.length; ++i) {
162 this.menuItems[i].closeItem(trigger);
163 }
164 }
166 }
169 var MenuItem = Class.create();
171 Object.extend(Object.extend(MenuItem.prototype, MenuContainer.prototype), {
172 initialize: function(idOrElement, parent) {
173 var menuItem = this;
174 this.type = "menuItem";
175 this.subMenu;
176 this.init(idOrElement, parent);
177 if (this.subMenu) {
178 this.element.onmouseover = function() {
179 menuItem.subMenu.open();
180 }
181 } else {
182 if (this.root.quickCollapse) {
183 this.element.onmouseover = function() {
184 menuItem.parentMenu.closeAll();
185 }
186 }
187 }
188 var linkTag = this.element.getElementsByTagName("A")[0];
189 if (linkTag) {
190 linkTag.onfocus = this.element.onmouseover;
191 this.link = linkTag;
192 this.text = linkTag.text;
193 }
194 if (this.subMenu) {
195 this.element.onmouseout = function() {
196 if (menuItem.root.openDelayTimer) window.clearTimeout(menuItem.root.openDelayTimer);
197 if (menuItem.root.closeDelayTimer) window.clearTimeout(menuItem.root.closeDelayTimer);
198 eval(menuItem.root.name + ".closingMenuItem = menuItem");
199 menuItem.root.closeDelayTimer = window.setTimeout(menuItem.root.name + ".closingMenuItem.subMenu.close()", menuItem.root.closeDelayTime);
200 }
201 }
202 },
204 openItem: function() {
205 this.isOpen = true;
206 if (this.subMenu) { this.subMenu.open(); }
207 },
209 closeItem: function(trigger) {
210 this.isOpen = false;
211 if (this.subMenu) {
212 if (this.subMenu != trigger) this.subMenu.close();
213 }
214 }
215 });
218 var menu;
221 function configMenu() {
222 this.closeDelayTime = 300;
223 }
225 function initMenu() {
226 menu = new Menu('root', 'menu', configMenu);
227 }
230 Event.observe(window, 'load', initMenu, false);