Revision 5ec56823
Von Thomas Heck vor etwa 12 Jahren hinzugefügt
js/dhtmlsuite/menu-for-applications.js | ||
---|---|---|
1 |
/************************************************************************************************************ |
|
2 |
@fileoverview |
|
3 |
DHTML Suite for Applications. |
|
4 |
Copyright (C) 2006 Alf Magne Kalleland(post@dhtmlgoodies.com)<br> |
|
5 |
<br> |
|
6 |
This library is free software; you can redistribute it and/or<br> |
|
7 |
modify it under the terms of the GNU Lesser General Public<br> |
|
8 |
License as published by the Free Software Foundation; either<br> |
|
9 |
version 2.1 of the License, or (at your option) any later version.<br> |
|
10 |
<br> |
|
11 |
This library is distributed in the hope that it will be useful,<br> |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of<br> |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br> |
|
14 |
Lesser General Public License for more details.<br> |
|
15 |
<br> |
|
16 |
You should have received a copy of the GNU Lesser General Public<br> |
|
17 |
License along with this library; if not, write to the Free Software<br> |
|
18 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA<br> |
|
19 |
<br> |
|
20 |
<br> |
|
21 |
www.dhtmlgoodies.com<br> |
|
22 |
Alf Magne Kalleland<br> |
|
23 |
|
|
24 |
************************************************************************************************************/ |
|
25 |
|
|
26 |
|
|
27 |
/** |
|
28 |
* |
|
29 |
* @package DHTMLSuite for applications |
|
30 |
* @copyright Copyright © 2006, www.dhtmlgoodies.com |
|
31 |
* @author Alf Magne Kalleland <post@dhtmlgoodies.com> |
|
32 |
*/ |
|
33 |
|
|
34 |
|
|
35 |
/************************************************************************************************************ |
|
36 |
* |
|
37 |
* Global variables |
|
38 |
* |
|
39 |
************************************************************************************************************/ |
|
40 |
|
|
41 |
|
|
42 |
// {{{ DHTMLSuite.createStandardObjects() |
|
43 |
/** |
|
44 |
* Create objects used by all scripts |
|
45 |
* |
|
46 |
* @public |
|
47 |
*/ |
|
48 |
|
|
49 |
|
|
50 |
var DHTMLSuite = new Object(); |
|
51 |
|
|
52 |
var standardObjectsCreated = false; // The classes below will check this variable, if it is false, default help objects will be created |
|
53 |
DHTMLSuite.eventElements = new Array(); // Array of elements that has been assigned to an event handler. |
|
54 |
|
|
55 |
DHTMLSuite.createStandardObjects = function() |
|
56 |
{ |
|
57 |
DHTMLSuite.clientInfoObj = new DHTMLSuite.clientInfo(); // Create browser info object |
|
58 |
DHTMLSuite.clientInfoObj.init(); |
|
59 |
if(!DHTMLSuite.configObj){ // If this object isn't allready created, create it. |
|
60 |
DHTMLSuite.configObj = new DHTMLSuite.config(); // Create configuration object. |
|
61 |
DHTMLSuite.configObj.init(); |
|
62 |
} |
|
63 |
DHTMLSuite.commonObj = new DHTMLSuite.common(); // Create configuration object. |
|
64 |
DHTMLSuite.variableStorage = new DHTMLSuite.globalVariableStorage();; // Create configuration object. |
|
65 |
DHTMLSuite.commonObj.init(); |
|
66 |
window.onunload = function() { DHTMLSuite.commonObj.__clearGarbage(); } |
|
67 |
|
|
68 |
standardObjectsCreated = true; |
|
69 |
|
|
70 |
|
|
71 |
} |
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
/************************************************************************************************************ |
|
77 |
* Configuration class used by most of the scripts |
|
78 |
* |
|
79 |
* Created: August, 19th, 2006 |
|
80 |
* Update log: |
|
81 |
* |
|
82 |
************************************************************************************************************/ |
|
83 |
|
|
84 |
|
|
85 |
/** |
|
86 |
* @constructor |
|
87 |
* @class Store global variables/configurations used by the classes below. Example: If you want to |
|
88 |
* change the path to the images used by the scripts, change it here. An object of this |
|
89 |
* class will always be available to the other classes. The name of this object is |
|
90 |
* "DHTMLSuite.configObj". <br><br> |
|
91 |
* |
|
92 |
* If you want to create an object of this class manually, remember to name it "DHTMLSuite.configObj" |
|
93 |
* This object should then be created before any other objects. This is nescessary if you want |
|
94 |
* the other objects to use the values you have put into the object. <br> |
|
95 |
* @version 1.0 |
|
96 |
* @version 1.0 |
|
97 |
* @author Alf Magne Kalleland(www.dhtmlgoodies.com) |
|
98 |
**/ |
|
99 |
DHTMLSuite.config = function() |
|
100 |
{ |
|
101 |
var imagePath; // Path to images used by the classes. |
|
102 |
var cssPath; // Path to CSS files used by the DHTML suite. |
|
103 |
} |
|
104 |
|
|
105 |
|
|
106 |
DHTMLSuite.config.prototype = { |
|
107 |
// {{{ init() |
|
108 |
/** |
|
109 |
* |
|
110 |
* @public |
|
111 |
*/ |
|
112 |
init : function() |
|
113 |
{ |
|
114 |
this.imagePath = '../images_dhtmlsuite/'; // Path to images |
|
115 |
this.cssPath = '../css_dhtmlsuite/'; // Path to images |
|
116 |
} |
|
117 |
// }}} |
|
118 |
, |
|
119 |
// {{{ setCssPath() |
|
120 |
/** |
|
121 |
* This method will save a new CSS path, i.e. where the css files of the dhtml suite are located. |
|
122 |
* |
|
123 |
* @param string newCssPath = New path to css files |
|
124 |
* @public |
|
125 |
*/ |
|
126 |
|
|
127 |
setCssPath : function(newCssPath) |
|
128 |
{ |
|
129 |
this.cssPath = newCssPath; |
|
130 |
} |
|
131 |
// }}} |
|
132 |
, |
|
133 |
// {{{ setImagePath() |
|
134 |
/** |
|
135 |
* This method will save a new image file path, i.e. where the image files used by the dhtml suite ar located |
|
136 |
* |
|
137 |
* @param string newImagePath = New path to image files |
|
138 |
* @public |
|
139 |
*/ |
|
140 |
setImagePath : function(newImagePath) |
|
141 |
{ |
|
142 |
this.imagePath = newImagePath; |
|
143 |
} |
|
144 |
// }}} |
|
145 |
} |
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
DHTMLSuite.globalVariableStorage = function() |
|
150 |
{ |
|
151 |
var menuBar_highlightedItems; // Array of highlighted menu bar items |
|
152 |
this.menuBar_highlightedItems = new Array(); |
|
153 |
|
|
154 |
var arrayOfDhtmlSuiteObjects; // Array of objects of class menuItem. |
|
155 |
this.arrayOfDhtmlSuiteObjects = new Array(); |
|
156 |
} |
|
157 |
|
|
158 |
DHTMLSuite.globalVariableStorage.prototype = { |
|
159 |
|
|
160 |
} |
|
161 |
|
|
162 |
|
|
163 |
/************************************************************************************************************ |
|
164 |
* A class with general methods used by most of the scripts |
|
165 |
* |
|
166 |
* Created: August, 19th, 2006 |
|
167 |
* Purpose of class: A class containing common method used by one or more of the gui classes below, |
|
168 |
* example: loadCSS. |
|
169 |
* An object("DHTMLSuite.commonObj") of this class will always be available to the other classes. |
|
170 |
* Update log: |
|
171 |
* |
|
172 |
************************************************************************************************************/ |
|
173 |
|
|
174 |
|
|
175 |
/** |
|
176 |
* @constructor |
|
177 |
* @class A class containing common method used by one or more of the gui classes below, example: loadCSS. An object("DHTMLSuite.commonObj") of this class will always be available to the other classes. |
|
178 |
* @version 1.0 |
|
179 |
* @author Alf Magne Kalleland(www.dhtmlgoodies.com) |
|
180 |
**/ |
|
181 |
|
|
182 |
DHTMLSuite.common = function() |
|
183 |
{ |
|
184 |
var loadedCSSFiles; // Array of loaded CSS files. Prevent same CSS file from being loaded twice. |
|
185 |
var cssCacheStatus; // Css cache status |
|
186 |
var eventElements; |
|
187 |
|
|
188 |
this.cssCacheStatus = true; // Caching of css files = on(Default) |
|
189 |
this.eventElements = new Array(); |
|
190 |
} |
|
191 |
|
|
192 |
DHTMLSuite.common.prototype = { |
|
193 |
|
|
194 |
// {{{ init() |
|
195 |
/** |
|
196 |
* This method initializes the DHTMLSuite_common object. |
|
197 |
* |
|
198 |
* @public |
|
199 |
*/ |
|
200 |
|
|
201 |
init : function() |
|
202 |
{ |
|
203 |
this.loadedCSSFiles = new Array(); |
|
204 |
} |
|
205 |
// }}} |
|
206 |
, |
|
207 |
// {{{ loadCSS() |
|
208 |
/** |
|
209 |
* This method loads a CSS file(Cascading Style Sheet) dynamically - i.e. an alternative to <link> tag in the document. |
|
210 |
* |
|
211 |
* @param string cssFileName = New path to image files |
|
212 |
* @public |
|
213 |
*/ |
|
214 |
|
|
215 |
loadCSS : function(cssFileName) |
|
216 |
{ |
|
217 |
|
|
218 |
if(!this.loadedCSSFiles[cssFileName]){ |
|
219 |
this.loadedCSSFiles[cssFileName] = true; |
|
220 |
var linkTag = document.createElement('LINK'); |
|
221 |
if(!this.cssCacheStatus){ |
|
222 |
if(cssFileName.indexOf('?')>=0)cssFileName = cssFileName + '&'; else cssFileName = cssFileName + '?'; |
|
223 |
cssFileName = cssFileName + 'rand='+ Math.random(); // To prevent caching |
|
224 |
} |
|
225 |
linkTag.href = DHTMLSuite.configObj.cssPath + cssFileName; |
|
226 |
linkTag.rel = 'stylesheet'; |
|
227 |
linkTag.media = 'screen'; |
|
228 |
linkTag.type = 'text/css'; |
|
229 |
document.getElementsByTagName('HEAD')[0].appendChild(linkTag); |
|
230 |
|
|
231 |
} |
|
232 |
} |
|
233 |
// }}} |
|
234 |
, |
|
235 |
// {{{ getTopPos() |
|
236 |
/** |
|
237 |
* This method will return the top coordinate(pixel) of an object |
|
238 |
* |
|
239 |
* @param Object inputObj = Reference to HTML element |
|
240 |
* @public |
|
241 |
*/ |
|
242 |
getTopPos : function(inputObj) |
|
243 |
{ |
|
244 |
var returnValue = inputObj.offsetTop; |
|
245 |
while((inputObj = inputObj.offsetParent) != null){ |
|
246 |
if(inputObj.tagName!='HTML'){ |
|
247 |
returnValue += (inputObj.offsetTop - inputObj.scrollTop); |
|
248 |
if(document.all)returnValue+=inputObj.clientTop; |
|
249 |
} |
|
250 |
} |
|
251 |
return returnValue; |
|
252 |
} |
|
253 |
// }}} |
|
254 |
, |
|
255 |
// {{{ setCssCacheStatus() |
|
256 |
/** |
|
257 |
* Specify if css files should be cached or not. |
|
258 |
* |
|
259 |
* @param Boolean cssCacheStatus = true = cache on, false = cache off |
|
260 |
* |
|
261 |
* @public |
|
262 |
*/ |
|
263 |
setCssCacheStatus : function(cssCacheStatus) |
|
264 |
{ |
|
265 |
this.cssCacheStatus = cssCacheStatus; |
|
266 |
} |
|
267 |
// }}} |
|
268 |
, |
|
269 |
// {{{ getLeftPos() |
|
270 |
/** |
|
271 |
* This method will return the left coordinate(pixel) of an object |
|
272 |
* |
|
273 |
* @param Object inputObj = Reference to HTML element |
|
274 |
* @public |
|
275 |
*/ |
|
276 |
getLeftPos : function(inputObj) |
|
277 |
{ |
|
278 |
var returnValue = inputObj.offsetLeft; |
|
279 |
while((inputObj = inputObj.offsetParent) != null){ |
|
280 |
if(inputObj.tagName!='HTML'){ |
|
281 |
returnValue += inputObj.offsetLeft; |
|
282 |
if(document.all)returnValue+=inputObj.clientLeft; |
|
283 |
} |
|
284 |
} |
|
285 |
return returnValue; |
|
286 |
} |
|
287 |
// }}} |
|
288 |
, |
|
289 |
// {{{ cancelEvent() |
|
290 |
/** |
|
291 |
* |
|
292 |
* This function only returns false. It is used to cancel selections and drag |
|
293 |
* |
|
294 |
* |
|
295 |
* @public |
|
296 |
*/ |
|
297 |
|
|
298 |
cancelEvent : function() |
|
299 |
{ |
|
300 |
return false; |
|
301 |
} |
|
302 |
// }}} |
|
303 |
, |
|
304 |
// {{{ addEvent() |
|
305 |
/** |
|
306 |
* |
|
307 |
* This function adds an event listener to an element on the page. |
|
308 |
* |
|
309 |
* @param Object whichObject = Reference to HTML element(Which object to assigne the event) |
|
310 |
* @param String eventType = Which type of event, example "mousemove" or "mouseup" |
|
311 |
* @param functionName = Name of function to execute. |
|
312 |
* |
|
313 |
* @public |
|
314 |
*/ |
|
315 |
addEvent : function(whichObject,eventType,functionName) |
|
316 |
{ |
|
317 |
if(whichObject.attachEvent){ |
|
318 |
whichObject['e'+eventType+functionName] = functionName; |
|
319 |
whichObject[eventType+functionName] = function(){whichObject['e'+eventType+functionName]( window.event );} |
|
320 |
whichObject.attachEvent( 'on'+eventType, whichObject[eventType+functionName] ); |
|
321 |
} else |
|
322 |
whichObject.addEventListener(eventType,functionName,false); |
|
323 |
this.__addEventElement(whichObject); |
|
324 |
delete(whichObject); |
|
325 |
// whichObject = null; |
|
326 |
} |
|
327 |
// }}} |
|
328 |
, |
|
329 |
// {{{ removeEvent() |
|
330 |
/** |
|
331 |
* |
|
332 |
* This function removes an event listener from an element on the page. |
|
333 |
* |
|
334 |
* @param Object whichObject = Reference to HTML element(Which object to assigne the event) |
|
335 |
* @param String eventType = Which type of event, example "mousemove" or "mouseup" |
|
336 |
* @param functionName = Name of function to execute. |
|
337 |
* |
|
338 |
* @public |
|
339 |
*/ |
|
340 |
removeEvent : function(whichObject,eventType,functionName) |
|
341 |
{ |
|
342 |
if(whichObject.detachEvent){ |
|
343 |
whichObject.detachEvent('on'+eventType, whichObject[eventType+functionName]); |
|
344 |
whichObject[eventType+functionName] = null; |
|
345 |
} else |
|
346 |
whichObject.removeEventListener(eventType,functionName,false); |
|
347 |
} |
|
348 |
, |
|
349 |
// {{{ __clearGarbage() |
|
350 |
/** |
|
351 |
* |
|
352 |
* This function is used for Internet Explorer in order to clear memory when the page unloads. |
|
353 |
* |
|
354 |
* |
|
355 |
* @private |
|
356 |
*/ |
|
357 |
__clearGarbage : function() |
|
358 |
{ |
|
359 |
/* Example of event which causes memory leakage in IE |
|
360 |
|
|
361 |
DHTMLSuite.commonObj.addEvent(expandRef,"click",function(){ window.refToMyMenuBar[index].__changeMenuBarState(this); }) |
|
362 |
|
|
363 |
We got a circular reference. |
|
364 |
|
|
365 |
*/ |
|
366 |
|
|
367 |
if(!DHTMLSuite.clientInfoObj.isMSIE)return; |
|
368 |
|
|
369 |
for(var no in DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects){ |
|
370 |
DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[no] = false; |
|
371 |
} |
|
372 |
|
|
373 |
for(var no=0;no<DHTMLSuite.eventElements.length;no++){ |
|
374 |
DHTMLSuite.eventElements[no].onclick = null; |
|
375 |
DHTMLSuite.eventElements[no].onmousedown = null; |
|
376 |
DHTMLSuite.eventElements[no].onmousemove = null; |
|
377 |
DHTMLSuite.eventElements[no].onmouseout = null; |
|
378 |
DHTMLSuite.eventElements[no].onmouseover = null; |
|
379 |
DHTMLSuite.eventElements[no].onmouseup = null; |
|
380 |
DHTMLSuite.eventElements[no].onfocus = null; |
|
381 |
DHTMLSuite.eventElements[no].onblur = null; |
|
382 |
DHTMLSuite.eventElements[no].onkeydown = null; |
|
383 |
DHTMLSuite.eventElements[no].onkeypress = null; |
|
384 |
DHTMLSuite.eventElements[no].onkeyup = null; |
|
385 |
DHTMLSuite.eventElements[no].onselectstart = null; |
|
386 |
DHTMLSuite.eventElements[no].ondragstart = null; |
|
387 |
DHTMLSuite.eventElements[no].oncontextmenu = null; |
|
388 |
DHTMLSuite.eventElements[no].onscroll = null; |
|
389 |
|
|
390 |
} |
|
391 |
window.onunload = null; |
|
392 |
DHTMLSuite = null; |
|
393 |
|
|
394 |
} |
|
395 |
|
|
396 |
, |
|
397 |
// {{{ __addEventElement() |
|
398 |
/** |
|
399 |
* |
|
400 |
* Add element to garbage collection array. The script will loop through this array and remove event handlers onload in ie. |
|
401 |
* |
|
402 |
* |
|
403 |
* @private |
|
404 |
*/ |
|
405 |
__addEventElement : function(el) |
|
406 |
{ |
|
407 |
DHTMLSuite.eventElements[DHTMLSuite.eventElements.length] = el; |
|
408 |
} |
|
409 |
|
|
410 |
, |
|
411 |
// {{{ getSrcElement() |
|
412 |
/** |
|
413 |
* |
|
414 |
* Returns a reference to the element which triggered an event. |
|
415 |
* @param Event e = Event object |
|
416 |
* |
|
417 |
* |
|
418 |
* @private |
|
419 |
*/ |
|
420 |
getSrcElement : function(e) |
|
421 |
{ |
|
422 |
var el; |
|
423 |
// Dropped on which element |
|
424 |
if (e.target) el = e.target; |
|
425 |
else if (e.srcElement) el = e.srcElement; |
|
426 |
if (el.nodeType == 3) // defeat Safari bug |
|
427 |
el = el.parentNode; |
|
428 |
return el; |
|
429 |
} |
|
430 |
|
|
431 |
} |
|
432 |
|
|
433 |
|
|
434 |
/************************************************************************************************************ |
|
435 |
* Client info class |
|
436 |
* |
|
437 |
* Created: August, 18th, 2006 |
|
438 |
* Update log: |
|
439 |
* |
|
440 |
************************************************************************************************************/ |
|
441 |
|
|
442 |
/** |
|
443 |
* @constructor |
|
444 |
* @class Purpose of class: Provide browser information to the classes below. Instead of checking for |
|
445 |
* browser versions and browser types in the classes below, they should check this |
|
446 |
* easily by referncing properties in the class below. An object("DHTMLSuite.clientInfoObj") of this |
|
447 |
* class will always be accessible to the other classes. * @version 1.0 |
|
448 |
* @author Alf Magne Kalleland(www.dhtmlgoodies.com) |
|
449 |
**/ |
|
450 |
|
|
451 |
|
|
452 |
DHTMLSuite.clientInfo = function() |
|
453 |
{ |
|
454 |
var browser; // Complete user agent information |
|
455 |
|
|
456 |
var isOpera; // Is the browser "Opera" |
|
457 |
var isMSIE; // Is the browser "Internet Explorer" |
|
458 |
var isFirefox; // Is the browser "Firefox" |
|
459 |
var navigatorVersion; // Browser version |
|
460 |
} |
|
461 |
|
|
462 |
DHTMLSuite.clientInfo.prototype = { |
|
463 |
|
|
464 |
/** |
|
465 |
* Constructor |
|
466 |
* Params: none: |
|
467 |
* return value: none; |
|
468 |
**/ |
|
469 |
// {{{ init() |
|
470 |
/** |
|
471 |
* |
|
472 |
* |
|
473 |
* This method initializes the script |
|
474 |
* |
|
475 |
* |
|
476 |
* @public |
|
477 |
*/ |
|
478 |
|
|
479 |
init : function() |
|
480 |
{ |
|
481 |
this.browser = navigator.userAgent; |
|
482 |
this.isOpera = (this.browser.toLowerCase().indexOf('opera')>=0)?true:false; |
|
483 |
this.isFirefox = (this.browser.toLowerCase().indexOf('firefox')>=0)?true:false; |
|
484 |
this.isMSIE = (this.browser.toLowerCase().indexOf('msie')>=0)?true:false; |
|
485 |
this.isSafari = (this.browser.toLowerCase().indexOf('safari')>=0)?true:false; |
|
486 |
this.navigatorVersion = navigator.appVersion.replace(/.*?MSIE (\d\.\d).*/g,'$1')/1; |
|
487 |
} |
|
488 |
// }}} |
|
489 |
} |
|
490 |
|
|
491 |
/************************************************************************************************************ |
|
492 |
* DHTML menu model item class |
|
493 |
* |
|
494 |
* Created: October, 30th, 2006 |
|
495 |
* @class Purpose of class: Save data about a menu item. |
|
496 |
* |
|
497 |
* |
|
498 |
* |
|
499 |
* Update log: |
|
500 |
* |
|
501 |
************************************************************************************************************/ |
|
502 |
|
|
503 |
DHTMLSuite.menuModelItem = function() |
|
504 |
{ |
|
505 |
var id; // id of this menu item. |
|
506 |
var itemText; // Text for this menu item |
|
507 |
var itemIcon; // Icon for this menu item. |
|
508 |
var url; // url when click on this menu item |
|
509 |
var parentId; // id of parent element |
|
510 |
var separator; // is this menu item a separator |
|
511 |
var jsFunction; // Js function to call onclick |
|
512 |
var depth; // Depth of this menu item. |
|
513 |
var hasSubs; // Does this menu item have sub items. |
|
514 |
var type; // Menu item type - possible values: "top" or "sub". |
|
515 |
var helpText; // Help text for this item - appear when you move your mouse over the item. |
|
516 |
var state; |
|
517 |
var submenuWidth; // Width of sub menu items. |
|
518 |
var visible; // Visibility of menu item. |
|
519 |
var frameTarget; |
|
520 |
|
|
521 |
this.state = 'regular'; |
|
522 |
} |
|
523 |
|
|
524 |
DHTMLSuite.menuModelItem.prototype = { |
|
525 |
|
|
526 |
setMenuVars : function(id,itemText,itemIcon,url,parentId,helpText,jsFunction,type,submenuWidth,frameTarget) |
|
527 |
{ |
|
528 |
this.id = id; |
|
529 |
this.itemText = itemText; |
|
530 |
this.itemIcon = itemIcon; |
|
531 |
this.url = url; |
|
532 |
this.parentId = parentId; |
|
533 |
this.jsFunction = jsFunction; |
|
534 |
this.separator = false; |
|
535 |
this.depth = false; |
|
536 |
this.hasSubs = false; |
|
537 |
this.helpText = helpText; |
|
538 |
this.submenuWidth = submenuWidth; |
|
539 |
this.visible = true; |
|
540 |
this.frameTarget = frameTarget; |
|
541 |
if(!type){ |
|
542 |
if(this.parentId)this.type = 'top'; else this.type='sub'; |
|
543 |
}else this.type = type; |
|
544 |
|
|
545 |
|
|
546 |
} |
|
547 |
// }}} |
|
548 |
, |
|
549 |
// {{{ setState() |
|
550 |
/** |
|
551 |
* Update the state attribute of a menu item. |
|
552 |
* |
|
553 |
* @param String newState New state of this item |
|
554 |
* @public |
|
555 |
*/ |
|
556 |
setAsSeparator : function(id,parentId) |
|
557 |
{ |
|
558 |
this.id = id; |
|
559 |
this.parentId = parentId; |
|
560 |
this.separator = true; |
|
561 |
this.visible = true; |
|
562 |
if(this.parentId)this.type = 'top'; else this.type='sub'; |
|
563 |
} |
|
564 |
// }}} |
|
565 |
, |
|
566 |
// {{{ setState() |
|
567 |
/** |
|
568 |
* Update the visible attribute of a menu item. |
|
569 |
* |
|
570 |
* @param Boolean visible true = visible, false = hidden. |
|
571 |
* @public |
|
572 |
*/ |
|
573 |
setVisibility : function(visible) |
|
574 |
{ |
|
575 |
this.visible = visible; |
|
576 |
} |
|
577 |
// }}} |
|
578 |
, |
|
579 |
// {{{ getState() |
|
580 |
/** |
|
581 |
* Return the state attribute of a menu item. |
|
582 |
* |
|
583 |
* @public |
|
584 |
*/ |
|
585 |
getState : function() |
|
586 |
{ |
|
587 |
return this.state; |
|
588 |
} |
|
589 |
// }}} |
|
590 |
, |
|
591 |
// {{{ setState() |
|
592 |
/** |
|
593 |
* Update the state attribute of a menu item. |
|
594 |
* |
|
595 |
* @param String newState New state of this item |
|
596 |
* @public |
|
597 |
*/ |
|
598 |
setState : function(newState) |
|
599 |
{ |
|
600 |
this.state = newState; |
|
601 |
} |
|
602 |
// }}} |
|
603 |
, |
|
604 |
// {{{ setSubMenuWidth() |
|
605 |
/** |
|
606 |
* Specify width of direct subs of this item. |
|
607 |
* |
|
608 |
* @param int newWidth Width of sub menu group(direct sub of this item) |
|
609 |
* @public |
|
610 |
*/ |
|
611 |
setSubMenuWidth : function(newWidth) |
|
612 |
{ |
|
613 |
this.submenuWidth = newWidth; |
|
614 |
} |
|
615 |
// }}} |
|
616 |
, |
|
617 |
// {{{ setIcon() |
|
618 |
/** |
|
619 |
* Specify new menu icon |
|
620 |
* |
|
621 |
* @param String iconPath Path to new menu icon |
|
622 |
* @public |
|
623 |
*/ |
|
624 |
setIcon : function(iconPath) |
|
625 |
{ |
|
626 |
this.itemIcon = iconPath; |
|
627 |
} |
|
628 |
// }}} |
|
629 |
, |
|
630 |
// {{{ setText() |
|
631 |
/** |
|
632 |
* Specify new text for the menu item. |
|
633 |
* |
|
634 |
* @param String newText New text for the menu item. |
|
635 |
* @public |
|
636 |
*/ |
|
637 |
setText : function(newText) |
|
638 |
{ |
|
639 |
this.itemText = newText; |
|
640 |
} |
|
641 |
} |
|
642 |
|
|
643 |
/************************************************************************************************************ |
|
644 |
* DHTML menu model class |
|
645 |
* |
|
646 |
* Created: October, 30th, 2006 |
|
647 |
* @class Purpose of class: Saves menu item data |
|
648 |
* |
|
649 |
* |
|
650 |
* Demos of this class: demo-menu-strip.html |
|
651 |
* |
|
652 |
* Update log: |
|
653 |
* |
|
654 |
************************************************************************************************************/ |
|
655 |
|
|
656 |
|
|
657 |
/** |
|
658 |
* @constructor |
|
659 |
* @class Purpose of class: Organize menu items for different menu widgets. demos of menus: (<a href="../../demos/demo-menu-strip.html" target="_blank">Demo</a>) |
|
660 |
* @version 1.0 |
|
661 |
* @author Alf Magne Kalleland(www.dhtmlgoodies.com) |
|
662 |
*/ |
|
663 |
|
|
664 |
|
|
665 |
DHTMLSuite.menuModel = function() |
|
666 |
{ |
|
667 |
var menuItems; // Array of menuModelItem objects |
|
668 |
var menuItemsOrder; // This array is needed in order to preserve the correct order of the array above. the array above is associative |
|
669 |
// And some browsers will loop through that array in different orders than Firefox and IE. |
|
670 |
var submenuType; // Direction of menu items(one item for each depth) |
|
671 |
var mainMenuGroupWidth; // Width of menu group - useful if the first group of items are listed below each other |
|
672 |
this.menuItems = new Array(); |
|
673 |
this.menuItemsOrder = new Array(); |
|
674 |
this.submenuType = new Array(); |
|
675 |
this.submenuType[1] = 'top'; |
|
676 |
for(var no=2;no<20;no++){ |
|
677 |
this.submenuType[no] = 'sub'; |
|
678 |
} |
|
679 |
if(!standardObjectsCreated)DHTMLSuite.createStandardObjects(); |
|
680 |
} |
|
681 |
|
|
682 |
DHTMLSuite.menuModel.prototype = { |
|
683 |
// {{{ addItem() |
|
684 |
/** |
|
685 |
* Add separator (special type of menu item) |
|
686 |
* |
|
687 |
* |
|
688 |
* |
|
689 |
* @param int id of menu item |
|
690 |
* @param string itemText = text of menu item |
|
691 |
* @param string itemIcon = file name of menu icon(in front of menu text. Path will be imagePath for the DHTMLSuite + file name) |
|
692 |
* @param string url = Url of menu item |
|
693 |
* @param int parent id of menu item |
|
694 |
* @param String jsFunction Name of javascript function to execute. It will replace the url param. The function with this name will be called and the element triggering the action will be |
|
695 |
* sent as argument. Name of the element which triggered the menu action may also be sent as a second argument. That depends on the widget. The context menu is an example where |
|
696 |
* the element triggering the context menu is sent as second argument to this function. |
|
697 |
* |
|
698 |
* @public |
|
699 |
*/ |
|
700 |
addItem : function(id,itemText,itemIcon,url,parentId,helpText,jsFunction,type,submenuWidth) |
|
701 |
{ |
|
702 |
if(!id)id = this.__getUniqueId(); // id not present - create it dynamically. |
|
703 |
this.menuItems[id] = new DHTMLSuite.menuModelItem(); |
|
704 |
this.menuItems[id].setMenuVars(id,itemText,itemIcon,url,parentId,helpText,jsFunction,type,submenuWidth); |
|
705 |
this.menuItemsOrder[this.menuItemsOrder.length] = id; |
|
706 |
return this.menuItems[id]; |
|
707 |
} |
|
708 |
, |
|
709 |
// {{{ addItemsFromMarkup() |
|
710 |
/** |
|
711 |
* This method creates all the menuModelItem objects by reading it from existing markup on your page. |
|
712 |
* Example of HTML markup: |
|
713 |
*<br> |
|
714 |
<ul id="menuModel"> |
|
715 |
<br> <li id="50000" itemIcon="../images/disk.gif"><a href="#" title="Open the file menu">File</a> |
|
716 |
<br> <ul width="150"> |
|
717 |
<br> <li id="500001" jsFunction="saveWork()" itemIcon="../images/disk.gif"><a href="#" title="Save your work">Save</a></li> |
|
718 |
<br> <li id="500002"><a href="#">Save As</a></li> |
|
719 |
<br> <li id="500004" itemType="separator"></li> |
|
720 |
<br> <li id="500003"><a href="#">Open</a></li> |
|
721 |
<br> </ul> |
|
722 |
<br> </li> |
|
723 |
<br> <li id="50001"><a href="#">View</a> |
|
724 |
<br> <ul width="130"> |
|
725 |
<br> <li id="500011"><a href="#">Source</a></li> |
|
726 |
<br> <li id="500012"><a href="#">Debug info</a></li> |
|
727 |
<br> <li id="500013"><a href="#">Layout</a> |
|
728 |
<br> <ul width="150"> |
|
729 |
<br> <li id="5000131"><a href="#">CSS</a> |
|
730 |
<br> <li id="5000132"><a href="#">HTML</a> |
|
731 |
<br> <li id="5000133"><a href="#">Javascript</a> |
|
732 |
<br> </ul> |
|
733 |
<br> </li> |
|
734 |
<br> |
|
735 |
<br> </ul> |
|
736 |
<br> </li> |
|
737 |
<br> <li id="50003" itemType="separator"></li> |
|
738 |
<br> <li id="50002"><a href="#">Tools</a></li> |
|
739 |
<br> </ul> |
|
740 |
* |
|
741 |
* @param String ulId = ID of <UL> tag on your page. |
|
742 |
* |
|
743 |
* @public |
|
744 |
*/ |
|
745 |
addItemsFromMarkup : function(ulId) |
|
746 |
{ |
|
747 |
if(!document.getElementById(ulId)){ |
|
748 |
alert('<UL> tag with id ' + ulId + ' does not exist'); |
|
749 |
return; |
|
750 |
} |
|
751 |
var ulObj = document.getElementById(ulId); |
|
752 |
var liTags = ulObj.getElementsByTagName('LI'); |
|
753 |
for(var no=0;no<liTags.length;no++){ // Walking through all <li> tags in the <ul> tree |
|
754 |
|
|
755 |
var id = liTags[no].id.replace(/[^0-9]/gi,''); // Get id of item. |
|
756 |
if(!id)id = this.__getUniqueId(); |
|
757 |
this.menuItems[id] = new DHTMLSuite.menuModelItem(); // Creating new menuModelItem object |
|
758 |
this.menuItemsOrder[this.menuItemsOrder.length] = id; |
|
759 |
// Get the attributes for this new menu item. |
|
760 |
|
|
761 |
var parentId = 0; // Default parent id |
|
762 |
if(liTags[no].parentNode!=ulObj)parentId = liTags[no].parentNode.parentNode.id; // parent node exists, set parentId equal to id of parent <li>. |
|
763 |
|
|
764 |
/* Checking type */ |
|
765 |
var type = liTags[no].getAttribute('itemType'); |
|
766 |
if(!type)type = liTags[no].itemType; |
|
767 |
if(type=='separator'){ // Menu item of type "separator" |
|
768 |
this.menuItems[id].setAsSeparator(id,parentId); |
|
769 |
continue; |
|
770 |
} |
|
771 |
if(parentId)type='sub'; else type = 'top'; |
|
772 |
|
|
773 |
var aTag = liTags[no].getElementsByTagName('A')[0]; // Get a reference to sub <a> tag |
|
774 |
if(!aTag){ |
|
775 |
continue; |
|
776 |
} |
|
777 |
if(aTag)var itemText = aTag.innerHTML; // Item text is set to the innerHTML of the <a> tag. |
|
778 |
var itemIcon = liTags[no].getAttribute('itemIcon'); // Item icon is set from the itemIcon attribute of the <li> tag. |
|
779 |
var url = aTag.href; // url is set to the href attribute of the <a> tag |
|
780 |
if(url=='#' || url.substr(url.length-1,1)=='#')url=''; // # = empty url. |
|
781 |
var frameTarget = aTag.target; |
|
782 |
|
|
783 |
var jsFunction = liTags[no].getAttribute('jsFunction'); // jsFunction is set from the jsFunction attribute of the <li> tag. |
|
784 |
|
|
785 |
var submenuWidth = false; // Not set from the <li> tag. |
|
786 |
var helpText = aTag.getAttribute('title'); |
|
787 |
if(!helpText)helpText = aTag.title; |
|
788 |
|
|
789 |
this.menuItems[id].setMenuVars(id,itemText,itemIcon,url,parentId,helpText,jsFunction,type,submenuWidth,frameTarget); |
|
790 |
} |
|
791 |
var subUls = ulObj.getElementsByTagName('UL'); |
|
792 |
for(var no=0;no<subUls.length;no++){ |
|
793 |
var width = subUls[no].getAttribute('width'); |
|
794 |
if(!width)width = subUls[no].width; |
|
795 |
if(width){ |
|
796 |
var id = subUls[no].parentNode.id.replace(/[^0-9]/gi,''); |
|
797 |
this.setSubMenuWidth(id,width); |
|
798 |
} |
|
799 |
} |
|
800 |
ulObj.style.display='none'; |
|
801 |
|
|
802 |
} |
|
803 |
// }}} |
|
804 |
, |
|
805 |
// {{{ setSubMenuWidth() |
|
806 |
/** |
|
807 |
* This method specifies the width of a sub menu group. This is a useful method in order to get a correct width in IE6 and prior. |
|
808 |
* |
|
809 |
* @param int id = ID of parent menu item |
|
810 |
* @param String newWidth = Width of sub menu items. |
|
811 |
* @public |
|
812 |
*/ |
|
813 |
setSubMenuWidth : function(id,newWidth) |
|
814 |
{ |
|
815 |
this.menuItems[id].setSubMenuWidth(newWidth); |
|
816 |
} |
|
817 |
, |
|
818 |
// {{{ setMainMenuGroupWidth() |
|
819 |
/** |
|
820 |
* Add separator (special type of menu item) |
|
821 |
* |
|
822 |
* @param String newWidth = Size of a menu group |
|
823 |
* @param int parent id of menu item |
|
824 |
* @public |
|
825 |
*/ |
|
826 |
setMainMenuGroupWidth : function(newWidth) |
|
827 |
{ |
|
828 |
this.mainMenuGroupWidth = newWidth; |
|
829 |
} |
|
830 |
, |
|
831 |
// {{{ addSeparator() |
|
832 |
/** |
|
833 |
* Add separator (special type of menu item) |
|
834 |
* |
|
835 |
* @param int parent id of menu item |
|
836 |
* @public |
|
837 |
*/ |
|
838 |
addSeparator : function(parentId) |
|
839 |
{ |
|
840 |
id = this.__getUniqueId(); // Get unique id |
|
841 |
if(!parentId)parentId = 0; |
|
842 |
this.menuItems[id] = new DHTMLSuite.menuModelItem(); |
|
843 |
this.menuItems[id].setAsSeparator(id,parentId); |
|
844 |
this.menuItemsOrder[this.menuItemsOrder.length] = id; |
|
845 |
return this.menuItems[id]; |
|
846 |
} |
|
847 |
, |
|
848 |
// {{{ init() |
|
849 |
/** |
|
850 |
* Initilizes the menu model. This method should be called when all items has been added to the model. |
|
851 |
* |
|
852 |
* |
|
853 |
* @public |
|
854 |
*/ |
|
855 |
init : function() |
|
856 |
{ |
|
857 |
this.__getDepths(); |
|
858 |
this.__setHasSubs(); |
|
859 |
|
|
860 |
} |
|
861 |
// }}} |
|
862 |
, |
|
863 |
// {{{ setMenuItemVisibility() |
|
864 |
/** |
|
865 |
* Save visibility of a menu item. |
|
866 |
* |
|
867 |
* @param int id = Id of menu item.. |
|
868 |
* @param Boolean visible = Visibility of menu item. |
|
869 |
* |
|
870 |
* @public |
|
871 |
*/ |
|
872 |
setMenuItemVisibility : function(id,visible) |
|
873 |
{ |
|
874 |
this.menuItems[id].setVisibility(visible); |
|
875 |
} |
|
876 |
// }}} |
|
877 |
, |
|
878 |
// {{{ setSubMenuType() |
|
879 |
/** |
|
880 |
* Set menu type for a specific menu depth. |
|
881 |
* |
|
882 |
* @param int depth = 1 = Top menu, 2 = Sub level 1... |
|
883 |
* @param String newType = New menu type(possible values: "top" or "sub") |
|
884 |
* |
|
885 |
* @private |
|
886 |
*/ |
|
887 |
setSubMenuType : function(depth,newType) |
|
888 |
{ |
|
889 |
this.submenuType[depth] = newType; |
|
890 |
|
|
891 |
} |
|
892 |
// }}} |
|
893 |
, |
|
894 |
// {{{ __getDepths() |
|
895 |
/** |
|
896 |
* Create variable for the depth of each menu item. |
|
897 |
* |
|
898 |
* |
|
899 |
* @private |
|
900 |
*/ |
|
901 |
getItems : function(parentId,returnArray) |
|
902 |
{ |
|
903 |
if(!parentId)return this.menuItems; |
|
904 |
if(!returnArray)returnArray = new Array(); |
|
905 |
for(var no=0;no<this.menuItemsOrder.length;no++){ |
|
906 |
var id = this.menuItemsOrder[no]; |
|
907 |
if(!id)continue; |
|
908 |
if(this.menuItems[id].parentId==parentId){ |
|
909 |
returnArray[returnArray.length] = this.menuItems[id]; |
|
910 |
if(this.menuItems[id].hasSubs)return this.getItems(this.menuItems[id].id,returnArray); |
|
911 |
} |
|
912 |
} |
|
913 |
return returnArray; |
|
914 |
|
|
915 |
} |
|
916 |
// }}} |
|
917 |
, |
|
918 |
// {{{ __getUniqueId() |
|
919 |
/** |
|
920 |
* Returns a unique id for a menu item. This method is used by the addSeparator function in case an id isn't sent to the method. |
|
921 |
* |
|
922 |
* |
|
923 |
* @private |
|
924 |
*/ |
|
925 |
__getUniqueId : function() |
|
926 |
{ |
|
927 |
var num = Math.random() + ''; |
|
928 |
num = num.replace('.',''); |
|
929 |
num = '99' + num; |
|
930 |
num = num /1; |
|
931 |
while(this.menuItems[num]){ |
|
932 |
num = Math.random() + ''; |
|
933 |
num = num.replace('.',''); |
|
934 |
num = num /1; |
|
935 |
} |
|
936 |
return num; |
|
937 |
} |
|
938 |
// }}} |
|
939 |
, |
|
940 |
// {{{ __getDepths() |
|
941 |
/** |
|
942 |
* Create variable for the depth of each menu item. |
|
943 |
* |
|
944 |
* |
|
945 |
* @private |
|
946 |
*/ |
|
947 |
__getDepths : function() |
|
948 |
{ |
|
949 |
for(var no=0;no<this.menuItemsOrder.length;no++){ |
|
950 |
var id = this.menuItemsOrder[no]; |
|
951 |
if(!id)continue; |
|
952 |
this.menuItems[id].depth = 1; |
|
953 |
if(this.menuItems[id].parentId){ |
|
954 |
this.menuItems[id].depth = this.menuItems[this.menuItems[id].parentId].depth+1; |
|
955 |
|
|
956 |
} |
|
957 |
this.menuItems[id].type = this.submenuType[this.menuItems[id].depth]; // Save menu direction for this menu item. |
|
958 |
} |
|
959 |
} |
|
960 |
// }}} |
|
961 |
, |
|
962 |
// {{{ __setHasSubs() |
|
963 |
/** |
|
964 |
* Create variable for the depth of each menu item. |
|
965 |
* |
|
966 |
* |
|
967 |
* @private |
|
968 |
*/ |
|
969 |
__setHasSubs : function() |
|
970 |
{ |
|
971 |
for(var no=0;no<this.menuItemsOrder.length;no++){ |
|
972 |
var id = this.menuItemsOrder[no]; |
|
973 |
if(!id)continue; |
|
974 |
if(this.menuItems[id].parentId){ |
|
975 |
this.menuItems[this.menuItems[id].parentId].hasSubs = 1; |
|
976 |
|
|
977 |
} |
|
978 |
} |
|
979 |
} |
|
980 |
// }}} |
|
981 |
, |
|
982 |
// {{{ __hasSubs() |
|
983 |
/** |
|
984 |
* Does a menu item have sub elements ? |
|
985 |
* |
|
986 |
* |
|
987 |
* @private |
|
988 |
*/ |
|
989 |
// }}} |
|
990 |
__hasSubs : function(id) |
|
991 |
{ |
|
992 |
for(var no=0;no<this.menuItemsOrder.length;no++){ |
|
993 |
var id = this.menuItemsOrder[no]; |
|
994 |
if(!id)continue; |
|
995 |
if(this.menuItems[id].parentId==id)return true; |
|
996 |
} |
|
997 |
return false; |
|
998 |
} |
|
999 |
// }}} |
|
1000 |
, |
|
1001 |
// {{{ __deleteChildNodes() |
|
1002 |
/** |
|
1003 |
* Deleting child nodes of a specific parent id |
|
1004 |
* |
|
1005 |
* @param int parentId |
|
1006 |
* |
|
1007 |
* @private |
|
1008 |
*/ |
|
1009 |
// }}} |
|
1010 |
__deleteChildNodes : function(parentId,recursive) |
|
1011 |
{ |
|
1012 |
var itemsToDeleteFromOrderArray = new Array(); |
|
1013 |
for(var prop=0;prop<this.menuItemsOrder.length;prop++){ |
|
1014 |
var id = this.menuItemsOrder[prop]; |
|
1015 |
if(!id)continue; |
|
1016 |
|
|
1017 |
if(this.menuItems[id].parentId==parentId && parentId){ |
|
1018 |
this.menuItems[id] = false; |
|
1019 |
itemsToDeleteFromOrderArray[itemsToDeleteFromOrderArray.length] = id; |
|
1020 |
this.__deleteChildNodes(id,true); // Recursive call. |
|
1021 |
} |
|
1022 |
} |
|
1023 |
|
|
1024 |
if(!recursive){ |
|
1025 |
for(var prop=0;prop<itemsToDeleteFromOrderArray;prop++){ |
|
1026 |
if(!itemsToDeleteFromOrderArray[prop])continue; |
|
1027 |
this.__deleteItemFromItemOrderArray(itemsToDeleteFromOrderArray[prop]); |
|
1028 |
} |
|
1029 |
} |
|
1030 |
this.__setHasSubs(); |
|
1031 |
} |
|
1032 |
// }}} |
|
1033 |
, |
|
1034 |
// {{{ __deleteANode() |
|
1035 |
/** |
|
1036 |
* Deleting a specific node from the menu model |
|
1037 |
* |
|
1038 |
* @param int id = Id of node to delete. |
|
1039 |
* |
|
1040 |
* @private |
|
1041 |
*/ |
|
1042 |
// }}} |
|
1043 |
__deleteANode : function(id) |
|
1044 |
{ |
|
1045 |
this.menuItems[id] = false; |
|
1046 |
this.__deleteItemFromItemOrderArray(id); |
|
1047 |
} |
|
1048 |
, |
|
1049 |
// {{{ __deleteItemFromItemOrderArray() |
|
1050 |
/** |
|
1051 |
* Deleting a specific node from the menuItemsOrder array(The array controlling the order of the menu items). |
|
1052 |
* |
|
1053 |
* @param int id = Id of node to delete. |
|
1054 |
* |
|
1055 |
* @private |
|
1056 |
*/ |
|
1057 |
// }}} |
|
1058 |
__deleteItemFromItemOrderArray : function(id) |
|
1059 |
{ |
|
1060 |
for(var no=0;no<this.menuItemsOrder.length;no++){ |
|
1061 |
var tmpId = this.menuItemsOrder[no]; |
|
1062 |
if(!tmpId)continue; |
|
1063 |
if(this.menuItemsOrder[no]==id){ |
|
1064 |
this.menuItemsOrder.splice(no,1); |
|
1065 |
return; |
|
1066 |
} |
|
1067 |
} |
|
1068 |
|
|
1069 |
} |
|
1070 |
// }}} |
|
1071 |
, |
|
1072 |
// {{{ __appendMenuModel() |
|
1073 |
/** |
|
1074 |
* Replace the sub items of a menu item with items from a new menuModel. |
|
1075 |
* |
|
1076 |
* @param menuModel newModel = An object of class menuModel - the items of this menu model will be appended to the existing menu items. |
|
1077 |
* @param Int parentId = Id of parent element of the appended items. |
|
1078 |
* |
|
1079 |
* @private |
|
1080 |
*/ |
|
1081 |
// }}} |
|
1082 |
__appendMenuModel : function(newModel,parentId) |
|
1083 |
{ |
|
1084 |
if(!newModel)return; |
|
1085 |
var items = newModel.getItems(); |
|
1086 |
for(var no=0;no<newModel.menuItemsOrder.length;no++){ |
|
1087 |
var id = newModel.menuItemsOrder[no]; |
|
1088 |
if(!id)continue; |
|
1089 |
if(!items[id].parentId)items[id].parentId = parentId; |
|
1090 |
this.menuItems[id] = items[id]; |
|
1091 |
for(var no2=0;no2<this.menuItemsOrder.length;no2++){ // Check to see if this item allready exists in the menuItemsOrder array, if it does, remove it. |
|
1092 |
if(!this.menuItemsOrder[no2])continue; |
|
1093 |
if(this.menuItemsOrder[no2]==items[id].id){ |
|
1094 |
this.menuItemsOrder.splice(no2,1); |
|
1095 |
} |
|
1096 |
} |
|
1097 |
this.menuItemsOrder[this.menuItemsOrder.length] = items[id].id; |
|
1098 |
} |
|
1099 |
this.__getDepths(); |
|
1100 |
this.__setHasSubs(); |
|
1101 |
} |
|
1102 |
// }}} |
|
1103 |
} |
|
1104 |
|
|
1105 |
/* Class for menu items - a view */ |
|
1106 |
|
|
1107 |
/************************************************************************************************************ |
|
1108 |
* DHTML menu item class |
|
1109 |
* |
|
1110 |
* Created: October, 21st, 2006 |
|
1111 |
* @class Purpose of class: Creates the HTML for a single menu item. |
|
1112 |
* |
|
1113 |
* Css files used by this script: menu-item.css |
|
1114 |
* |
|
1115 |
* Demos of this class: demo-menu-strip.html |
|
1116 |
* |
|
1117 |
* Update log: |
|
1118 |
* |
|
1119 |
************************************************************************************************************/ |
|
1120 |
|
|
1121 |
/** |
|
1122 |
* @constructor |
|
1123 |
* @class Purpose of class: Creates the div(s) for a menu item. This class is used by the menuBar class. You can |
|
1124 |
* also create a menu item and add it where you want on your page. the createItem() method will return the div |
|
1125 |
* for the item. You can use the appendChild() method to add it to your page. |
|
1126 |
* |
|
1127 |
* @version 1.0 |
|
1128 |
* @author Alf Magne Kalleland(www.dhtmlgoodies.com) |
|
1129 |
*/ |
|
1130 |
|
|
1131 |
|
|
1132 |
DHTMLSuite.menuItem = function() |
|
1133 |
{ |
|
1134 |
var layoutCSS; |
|
1135 |
var divElement; // the <div> element created for this menu item |
|
1136 |
var expandElement; // Reference to the arrow div (expand sub items) |
|
1137 |
var cssPrefix; // Css prefix for the menu items. |
|
1138 |
var modelItemRef; // Reference to menuModelItem |
|
1139 |
|
|
1140 |
this.layoutCSS = 'menu-item.css'; |
|
1141 |
this.cssPrefix = 'DHTMLSuite_'; |
|
1142 |
|
|
1143 |
if(!standardObjectsCreated)DHTMLSuite.createStandardObjects(); |
|
1144 |
|
|
1145 |
|
|
1146 |
var objectIndex; |
|
1147 |
this.objectIndex = DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects.length; |
|
1148 |
|
|
1149 |
|
|
1150 |
} |
|
1151 |
|
|
1152 |
DHTMLSuite.menuItem.prototype = |
|
1153 |
{ |
|
1154 |
|
|
1155 |
/* |
|
1156 |
* Create a menu item. |
|
1157 |
* |
|
1158 |
* @param menuModelItem menuModelItemObj = An object of class menuModelItem |
|
1159 |
*/ |
|
1160 |
createItem : function(menuModelItemObj) |
|
1161 |
{ |
|
1162 |
DHTMLSuite.commonObj.loadCSS(this.layoutCSS); // Load css |
|
1163 |
|
|
1164 |
DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[this.objectIndex] = this; |
|
1165 |
|
|
1166 |
this.modelItemRef = menuModelItemObj; |
|
1167 |
this.divElement = document.createElement('DIV'); // Create main div |
|
1168 |
this.divElement.id = 'DHTMLSuite_menuItem' + menuModelItemObj.id; // Giving this menu item it's unque id |
|
1169 |
this.divElement.className = this.cssPrefix + 'menuItem_' + menuModelItemObj.type + '_regular'; |
|
1170 |
this.divElement.onselectstart = function() { return DHTMLSuite.commonObj.cancelEvent(false,this) }; |
|
1171 |
if(menuModelItemObj.helpText){ // Add "title" attribute to the div tag if helpText is defined |
|
1172 |
this.divElement.title = menuModelItemObj.helpText; |
|
1173 |
} |
|
1174 |
|
|
1175 |
// Menu item of type "top" |
|
1176 |
if(menuModelItemObj.type=='top'){ |
|
1177 |
this.__createMenuElementsOfTypeTop(this.divElement); |
|
1178 |
} |
|
1179 |
|
|
1180 |
if(menuModelItemObj.type=='sub'){ |
|
1181 |
this.__createMenuElementsOfTypeSub(this.divElement); |
|
1182 |
} |
|
1183 |
|
|
1184 |
if(menuModelItemObj.separator){ |
|
1185 |
this.divElement.className = this.cssPrefix + 'menuItem_separator_' + menuModelItemObj.type; |
|
1186 |
this.divElement.innerHTML = '<span></span>'; |
|
1187 |
}else{ |
|
1188 |
/* Add events */ |
|
1189 |
var tmpVar = this.objectIndex/1; |
|
1190 |
//this.divElement.onclick = function(e) { DHTMLSuite.variableStorage.arrayOfDhtmlSuiteObjects[tmpVar].__navigate(e); } |
|
1191 |
this.divElement.onmousedown = this.__clickMenuItem; // on mouse down effect |
|
1192 |
this.divElement.onmouseup = this.__rolloverMenuItem; // on mouse up effect |
|
1193 |
this.divElement.onmouseover = this.__rolloverMenuItem; // mouse over effect |
|
1194 |
this.divElement.onmouseout = this.__rolloutMenuItem; // mouse out effect. |
|
1195 |
DHTMLSuite.commonObj.__addEventElement(this.divElement); |
|
1196 |
} |
|
1197 |
return this.divElement; |
|
1198 |
} |
|
1199 |
// }}} |
|
1200 |
, |
|
1201 |
// {{{ setLayoutCss() |
|
1202 |
/** |
|
1203 |
* Creates the different parts of a menu item of type "top". |
|
1204 |
* |
|
1205 |
* @param String newLayoutCss = Name of css file used for the menu items. |
|
1206 |
* |
|
1207 |
* @public |
|
1208 |
*/ |
|
1209 |
setLayoutCss : function(newLayoutCss) |
|
1210 |
{ |
|
1211 |
this.layoutCSS = newLayoutCss; |
|
1212 |
|
|
1213 |
} |
|
1214 |
// }}} |
|
1215 |
, |
|
1216 |
// {{{ __createMenuElementsOfTypeTop() |
|
1217 |
/** |
|
1218 |
* Creates the different parts of a menu item of type "top". |
|
1219 |
* |
|
1220 |
* @param menuModelItem menuModelItemObj = Object of type menuModelItemObj |
|
1221 |
* @param Object parentEl = Reference to parent element |
|
1222 |
* |
|
1223 |
* @private |
|
1224 |
*/ |
|
1225 |
__createMenuElementsOfTypeTop : function(parentEl){ |
|
1226 |
if(this.modelItemRef.itemIcon){ |
|
1227 |
var iconDiv = document.createElement('DIV'); |
|
1228 |
iconDiv.innerHTML = '<img src="' + this.modelItemRef.itemIcon + '">'; |
|
1229 |
iconDiv.id = 'menuItemIcon' + this.modelItemRef.id |
|
1230 |
parentEl.appendChild(iconDiv); |
|
1231 |
} |
|
1232 |
if(this.modelItemRef.itemText){ |
|
1233 |
var div = document.createElement('DIV'); |
|
1234 |
div.innerHTML = this.modelItemRef.itemText; |
|
1235 |
div.className = this.cssPrefix + 'menuItem_textContent'; |
|
1236 |
div.id = 'menuItemText' + this.modelItemRef.id; |
|
1237 |
parentEl.appendChild(div); |
|
1238 |
} |
|
1239 |
/* Create div for the arrow -> Show sub items */ |
|
1240 |
var div = document.createElement('DIV'); |
|
1241 |
div.className = this.cssPrefix + 'menuItem_top_arrowShowSub'; |
|
1242 |
div.id = 'DHTMLSuite_menuBar_arrow' + this.modelItemRef.id; |
|
1243 |
parentEl.appendChild(div); |
|
1244 |
this.expandElement = div; |
|
1245 |
if(!this.modelItemRef.hasSubs)div.style.display='none'; |
|
1246 |
|
|
1247 |
} |
|
1248 |
// }}} |
|
1249 |
, |
|
1250 |
|
|
1251 |
// {{{ __createMenuElementsOfTypeSub() |
|
1252 |
/** |
|
1253 |
* Creates the different parts of a menu item of type "sub". |
|
1254 |
* |
|
1255 |
* @param menuModelItem menuModelItemObj = Object of type menuModelItemObj |
|
1256 |
* @param Object parentEl = Reference to parent element |
|
1257 |
* |
|
1258 |
* @private |
|
1259 |
*/ |
|
1260 |
__createMenuElementsOfTypeSub : function(parentEl){ |
|
1261 |
if(this.modelItemRef.itemIcon){ |
|
1262 |
parentEl.style.backgroundImage = 'url(\'' + this.modelItemRef.itemIcon + '\')'; |
|
1263 |
parentEl.style.backgroundRepeat = 'no-repeat'; |
|
1264 |
parentEl.style.backgroundPosition = 'left center'; |
|
1265 |
} |
|
1266 |
if(this.modelItemRef.itemText){ |
|
1267 |
var div; |
|
1268 |
if( this.modelItemRef.url ) |
|
1269 |
{ |
|
1270 |
div = document.createElement('a'); |
|
1271 |
div.href = this.modelItemRef.url; |
|
1272 |
div.target = this.modelItemRef.frameTarget; |
|
1273 |
div.style.display = 'block'; |
|
1274 |
} |
|
1275 |
else |
|
1276 |
div = document.createElement('div'); |
|
1277 |
|
|
1278 |
div.className = 'DHTMLSuite_textContent'; |
|
1279 |
div.innerHTML = this.modelItemRef.itemText; |
|
1280 |
div.className = this.cssPrefix + 'menuItem_textContent'; |
|
1281 |
div.id = 'menuItemText' + this.modelItemRef.id; |
|
1282 |
parentEl.appendChild(div); |
|
1283 |
} |
|
1284 |
|
|
1285 |
/* Create div for the arrow -> Show sub items */ |
|
1286 |
var div = document.createElement('DIV'); |
|
1287 |
div.className = this.cssPrefix + 'menuItem_sub_arrowShowSub'; |
|
1288 |
parentEl.appendChild(div); |
|
1289 |
div.id = 'DHTMLSuite_menuBar_arrow' + this.modelItemRef.id; |
|
1290 |
this.expandElement = div; |
|
1291 |
|
|
1292 |
if(!this.modelItemRef.hasSubs){ |
|
1293 |
div.style.display='none'; |
|
1294 |
}else{ |
|
1295 |
div.previousSibling.style.paddingRight = '15px'; |
|
1296 |
} |
|
1297 |
} |
|
1298 |
// }}} |
|
1299 |
, |
|
1300 |
// {{{ setCssPrefix() |
|
1301 |
/** |
|
1302 |
* Set css prefix for the menu item. default is 'DHTMLSuite_'. This is useful in case you want to have different menus on a page with different layout. |
|
1303 |
* |
|
1304 |
* @param String cssPrefix = New css prefix. |
|
1305 |
* |
|
1306 |
* @public |
|
1307 |
*/ |
|
1308 |
setCssPrefix : function(cssPrefix) |
|
1309 |
{ |
|
1310 |
this.cssPrefix = cssPrefix; |
|
1311 |
} |
|
1312 |
// }}} |
|
1313 |
, |
|
1314 |
// {{{ setMenuIcon() |
|
1315 |
/** |
|
1316 |
* Replace menu icon. |
|
1317 |
* |
|
1318 |
* @param String newPath - Path to new icon (false if no icon); |
|
1319 |
* |
|
1320 |
* @public |
|
1321 |
*/ |
|
1322 |
setIcon : function(newPath) |
|
1323 |
{ |
|
1324 |
this.modelItemRef.setIcon(newPath); |
|
1325 |
if(this.modelItemRef.type=='top'){ // Menu item is of type "top" |
|
1326 |
var div = document.getElementById('menuItemIcon' + this.modelItemRef.id); // Get a reference to the div where the icon is located. |
|
1327 |
var img = div.getElementsByTagName('IMG')[0]; // Find the image |
|
1328 |
if(!img){ // Image doesn't exists ? |
|
1329 |
img = document.createElement('IMG'); // Create new image |
|
1330 |
div.appendChild(img); |
|
1331 |
} |
|
1332 |
img.src = newPath; // Set image path |
|
1333 |
if(!newPath)img.parentNode.removeChild(img); // No newPath defined, remove the image. |
|
1334 |
} |
|
1335 |
if(this.modelItemRef.type=='sub'){ // Menu item is of type "sub" |
|
1336 |
this.divElement.style.backgroundImage = 'url(\'' + newPath + '\')'; // Set backgroundImage for the main div(i.e. menu item div) |
|
1337 |
} |
|
1338 |
} |
|
1339 |
// }}} |
|
1340 |
, |
|
1341 |
// {{{ setText() |
|
1342 |
/** |
|
1343 |
* Replace the text of a menu item |
|
1344 |
* |
|
1345 |
* @param String newText - New text for the menu item. |
|
1346 |
* |
|
1347 |
* @public |
|
1348 |
*/ |
|
1349 |
setText : function(newText) |
|
1350 |
{ |
|
1351 |
this.modelItemRef.setText(newText); |
|
1352 |
document.getElementById('menuItemText' + this.modelItemRef.id).innerHTML = newText; |
|
1353 |
|
|
1354 |
|
|
1355 |
} |
|
1356 |
|
|
1357 |
// }}} |
|
1358 |
, |
|
1359 |
// {{{ __clickMenuItem() |
|
1360 |
/** |
|
1361 |
* Effect - click on menu item |
|
1362 |
* |
|
1363 |
* |
|
1364 |
* @private |
|
1365 |
*/ |
|
1366 |
__clickMenuItem : function() |
|
1367 |
{ |
|
1368 |
this.className = this.className.replace('_regular','_click'); |
|
1369 |
this.className = this.className.replace('_over','_click'); |
|
1370 |
} |
|
1371 |
// }}} |
|
1372 |
, |
|
1373 |
// {{{ __rolloverMenuItem() |
|
1374 |
/** |
|
1375 |
* Roll over effect |
|
1376 |
* |
|
1377 |
* |
|
1378 |
* @private |
|
1379 |
*/ |
|
1380 |
__rolloverMenuItem : function() |
|
1381 |
{ |
|
1382 |
this.className = this.className.replace('_regular','_over'); |
|
1383 |
this.className = this.className.replace('_click','_over'); |
|
1384 |
} |
|
1385 |
// }}} |
|
1386 |
, |
|
1387 |
// {{{ __rolloutMenuItem() |
|
1388 |
/** |
|
1389 |
* Roll out effect |
|
1390 |
* |
|
1391 |
* |
|
1392 |
* @private |
|
1393 |
*/ |
|
1394 |
__rolloutMenuItem : function() |
|
1395 |
{ |
|
1396 |
this.className = this.className.replace('_over','_regular'); |
|
1397 |
|
|
1398 |
} |
|
1399 |
// }}} |
|
1400 |
, |
|
1401 |
// {{{ setState() |
|
1402 |
/** |
|
1403 |
* Set state of a menu item. |
|
1404 |
* |
|
1405 |
* @param String newState = New state for the menu item |
|
1406 |
* |
|
1407 |
* @public |
|
1408 |
*/ |
|
1409 |
setState : function(newState) |
|
1410 |
{ |
|
1411 |
this.divElement.className = this.cssPrefix + 'menuItem_' + this.modelItemRef.type + '_' + newState; |
|
1412 |
this.modelItemRef.setState(newState); |
|
1413 |
} |
|
1414 |
// }}} |
|
1415 |
, |
|
1416 |
// {{{ getState() |
|
1417 |
/** |
|
1418 |
* Return state of a menu item. |
|
1419 |
* |
|
1420 |
* |
|
1421 |
* @public |
|
1422 |
*/ |
|
1423 |
getState : function() |
|
1424 |
{ |
|
1425 |
var state = this.modelItemRef.getState(); |
|
1426 |
if(!state){ |
|
1427 |
if(this.divElement.className.indexOf('_over')>=0)state = 'over'; |
|
1428 |
if(this.divElement.className.indexOf('_click')>=0)state = 'click'; |
|
1429 |
this.modelItemRef.setState(state); |
|
1430 |
} |
|
1431 |
return state; |
|
1432 |
} |
|
1433 |
// }}} |
|
1434 |
, |
|
1435 |
// {{{ __setHasSub() |
|
1436 |
/** |
|
1437 |
* Update the item, i.e. show/hide the arrow if the element has subs or not. |
|
1438 |
* |
|
1439 |
* |
|
1440 |
* @private |
|
1441 |
*/ |
|
1442 |
__setHasSub : function(hasSubs) |
|
1443 |
{ |
|
1444 |
this.modelItemRef.hasSubs = hasSubs; |
|
1445 |
if(!hasSubs){ |
|
1446 |
document.getElementById(this.cssPrefix +'menuBar_arrow' + this.modelItemRef.id).style.display='none'; |
|
1447 |
}else{ |
|
1448 |
document.getElementById(this.cssPrefix +'menuBar_arrow' + this.modelItemRef.id).style.display='block'; |
|
1449 |
} |
|
1450 |
} |
|
1451 |
// }}} |
|
1452 |
, |
|
1453 |
// {{{ hide() |
|
1454 |
/** |
|
1455 |
* Hide the menu item. |
|
1456 |
* |
|
1457 |
* |
|
1458 |
* @public |
|
1459 |
*/ |
|
1460 |
hide : function() |
|
1461 |
{ |
|
1462 |
this.modelItemRef.setVisibility(false); |
|
1463 |
this.divElement.style.display='none'; |
|
1464 |
} |
|
1465 |
, |
|
1466 |
// {{{ show() |
|
1467 |
/** |
|
1468 |
* Show the menu item. |
|
1469 |
* |
|
1470 |
* |
|
1471 |
* @public |
|
1472 |
*/ |
|
1473 |
show : function() |
|
1474 |
{ |
|
1475 |
this.modelItemRef.setVisibility(true); |
|
1476 |
this.divElement.style.display='block'; |
|
1477 |
} |
|
1478 |
// }}} |
|
1479 |
, |
|
1480 |
// {{{ __hideGroup() |
|
1481 |
/** |
|
1482 |
* Hide the group the menu item is a part of. Example: if we're dealing with menu item 2.1, hide the group for all sub items of 2 |
|
1483 |
* |
|
1484 |
* |
|
1485 |
* @private |
|
1486 |
*/ |
|
1487 |
__hideGroup : function() |
|
1488 |
{ |
Auch abrufbar als: Unified diff
Revert "JS-Menü: Scrollbalken "etwas" gefixt."
This reverts commit 2c4715de6cadaed4c13a198cb26bea0a9c8c27f1.