1
/** $Id: domLib.js 1952 2005-07-17 16:24:05Z dallen $ */
5
* Copyright 2002-2005 Dan Allen, Mojavelinux.com (dan.allen@mojavelinux.com)
7
* Licensed under the Apache License, Version 2.0 (the "License");
8
* you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
24
* Title: DOM Library Core
28
* A set of commonly used functions that make it easier to create javascript
29
* applications that rely on the DOM.
33
* Maintainer: Dan Allen <dan.allen@mojavelinux.com>
34
* Maintainer: Jason Rust <jrust@rustyparts.com>
40
// {{{ global constants (DO NOT EDIT)
42
// -- Browser Detection --
43
var domLib_userAgent = navigator.userAgent.toLowerCase();
44
var domLib_isMac = navigator.appVersion.indexOf('Mac') != -1;
45
var domLib_isWin = domLib_userAgent.indexOf('windows') != -1;
46
// NOTE: could use window.opera for detecting Opera
47
var domLib_isOpera = domLib_userAgent.indexOf('opera') != -1;
48
var domLib_isOpera7up = domLib_userAgent.match(/opera.(7|8)/i);
49
var domLib_isSafari = domLib_userAgent.indexOf('safari') != -1;
50
var domLib_isKonq = domLib_userAgent.indexOf('konqueror') != -1;
51
// Both konqueror and safari use the khtml rendering engine
52
var domLib_isKHTML = (domLib_isKonq || domLib_isSafari || domLib_userAgent.indexOf('khtml') != -1);
53
var domLib_isIE = (!domLib_isKHTML && !domLib_isOpera && (domLib_userAgent.indexOf('msie 5') != -1 || domLib_userAgent.indexOf('msie 6') != -1 || domLib_userAgent.indexOf('msie 7') != -1));
54
var domLib_isIE5up = domLib_isIE;
55
var domLib_isIE50 = (domLib_isIE && domLib_userAgent.indexOf('msie 5.0') != -1);
56
var domLib_isIE55 = (domLib_isIE && domLib_userAgent.indexOf('msie 5.5') != -1);
57
var domLib_isIE5 = (domLib_isIE50 || domLib_isIE55);
58
// safari and konq may use string "khtml, like gecko", so check for destinctive /
59
var domLib_isGecko = domLib_userAgent.indexOf('gecko/') != -1;
60
var domLib_isMacIE = (domLib_isIE && domLib_isMac);
61
var domLib_isIE55up = domLib_isIE5up && !domLib_isIE50 && !domLib_isMacIE;
62
var domLib_isIE6up = domLib_isIE55up && !domLib_isIE55;
64
// -- Browser Abilities --
65
var domLib_standardsMode = (document.compatMode && document.compatMode == 'CSS1Compat');
66
var domLib_useLibrary = (domLib_isOpera7up || domLib_isKHTML || domLib_isIE5up || domLib_isGecko || domLib_isMacIE || document.defaultView);
68
var domLib_hasBrokenTimeout = (domLib_isMacIE || (domLib_isKonq && domLib_userAgent.match(/konqueror\/3.([2-9])/) === null));
69
var domLib_canFade = (domLib_isGecko || domLib_isIE || domLib_isSafari || domLib_isOpera);
70
var domLib_canDrawOverSelect = (domLib_isMac || domLib_isOpera || domLib_isGecko);
71
var domLib_canDrawOverFlash = (domLib_isMac || domLib_isWin);
73
// -- Event Variables --
74
var domLib_eventTarget = domLib_isIE ? 'srcElement' : 'currentTarget';
75
var domLib_eventButton = domLib_isIE ? 'button' : 'which';
76
var domLib_eventTo = domLib_isIE ? 'toElement' : 'relatedTarget';
77
var domLib_stylePointer = domLib_isIE ? 'hand' : 'pointer';
78
// NOTE: a bug exists in Opera that prevents maxWidth from being set to 'none', so we make it huge
79
var domLib_styleNoMaxWidth = domLib_isOpera ? '10000px' : 'none';
80
var domLib_hidePosition = '-1000px';
81
var domLib_scrollbarWidth = 14;
82
var domLib_autoId = 1;
83
var domLib_zIndex = 100;
86
var domLib_collisionElements;
87
var domLib_collisionsCached = false;
89
var domLib_timeoutStateId = 0;
90
var domLib_timeoutStates = new Hash();
93
// {{{ DOM enhancements
95
if (!document.ELEMENT_NODE)
97
document.ELEMENT_NODE = 1;
98
document.ATTRIBUTE_NODE = 2;
99
document.TEXT_NODE = 3;
100
document.DOCUMENT_NODE = 9;
101
document.DOCUMENT_FRAGMENT_NODE = 11;
104
function domLib_clone(obj)
112
if (value !== null && typeof(value) == 'object' && value != window && !value.nodeType)
114
copy[i] = domLib_clone(value);
136
this.numericLength = 0;
137
this.elementData = [];
138
for (var i = 0; i < arguments.length; i += 2)
140
if (typeof(arguments[i + 1]) != 'undefined')
142
this.elementData[arguments[i]] = arguments[i + 1];
144
if (arguments[i] == parseInt(arguments[i]))
146
this.numericLength++;
152
// using prototype as opposed to inner functions saves on memory
153
Hash.prototype.get = function(in_key)
155
return this.elementData[in_key];
158
Hash.prototype.set = function(in_key, in_value)
160
if (typeof(in_value) != 'undefined')
162
if (typeof(this.elementData[in_key]) == 'undefined')
165
if (in_key == parseInt(in_key))
167
this.numericLength++;
171
this.elementData[in_key] = in_value;
172
return this.elementData[in_key];
178
Hash.prototype.remove = function(in_key)
181
if (typeof(this.elementData[in_key]) != 'undefined')
184
if (in_key == parseInt(in_key))
186
this.numericLength--;
189
tmp_value = this.elementData[in_key];
190
delete this.elementData[in_key];
196
Hash.prototype.size = function()
201
Hash.prototype.has = function(in_key)
203
return typeof(this.elementData[in_key]) != 'undefined';
206
Hash.prototype.find = function(in_obj)
208
for (var tmp_key in this.elementData)
210
if (this.elementData[tmp_key] == in_obj)
217
Hash.prototype.merge = function(in_hash)
219
for (var tmp_key in in_hash.elementData)
221
if (typeof(this.elementData[tmp_key]) == 'undefined')
224
if (tmp_key == parseInt(tmp_key))
226
this.numericLength++;
230
this.elementData[tmp_key] = in_hash.elementData[tmp_key];
234
Hash.prototype.compare = function(in_hash)
236
if (this.length != in_hash.length)
241
for (var tmp_key in this.elementData)
243
if (this.elementData[tmp_key] != in_hash.elementData[tmp_key])
253
// {{{ domLib_isDescendantOf()
255
function domLib_isDescendantOf(in_object, in_ancestor)
257
if (in_object == in_ancestor)
262
while (in_object != document.documentElement)
266
if ((tmp_object = in_object.offsetParent) && tmp_object == in_ancestor)
270
else if ((tmp_object = in_object.parentNode) == in_ancestor)
276
in_object = tmp_object;
279
// in case we get some wierd error, just assume we haven't gone out yet
290
// {{{ domLib_detectCollisions()
293
* For any given target element, determine if elements on the page
294
* are colliding with it that do not obey the rules of z-index.
296
function domLib_detectCollisions(in_object, in_recover, in_useCache)
298
// the reason for the cache is that if the root menu is built before
299
// the page is done loading, then it might not find all the elements.
300
// so really the only time you don't use cache is when building the
301
// menu as part of the page load
302
if (!domLib_collisionsCached)
306
if (!domLib_canDrawOverFlash)
308
tags[tags.length] = 'object';
311
if (!domLib_canDrawOverSelect)
313
tags[tags.length] = 'select';
316
domLib_collisionElements = domLib_getElementsByTagNames(tags, true);
317
domLib_collisionsCached = in_useCache;
320
// if we don't have a tip, then unhide selects
323
for (var cnt = 0; cnt < domLib_collisionElements.length; cnt++)
325
var thisElement = domLib_collisionElements[cnt];
327
if (!thisElement.hideList)
329
thisElement.hideList = new Hash();
332
thisElement.hideList.remove(in_object.id);
333
if (!thisElement.hideList.length)
335
domLib_collisionElements[cnt].style.visibility = 'visible';
338
domLib_collisionElements[cnt].style.display = '';
345
else if (domLib_collisionElements.length === 0)
350
// okay, we have a tip, so hunt and destroy
351
var objectOffsets = domLib_getOffsets(in_object);
353
for (cnt = 0; cnt < domLib_collisionElements.length; cnt++)
355
thisElement = domLib_collisionElements[cnt];
357
// if collision element is in active element, move on
358
// WARNING: is this too costly?
359
if (domLib_isDescendantOf(thisElement, in_object))
364
// konqueror only has trouble with multirow selects
366
thisElement.tagName == 'SELECT' &&
367
(thisElement.size <= 1 && !thisElement.multiple))
372
if (!thisElement.hideList)
374
thisElement.hideList = new Hash();
377
var selectOffsets = domLib_getOffsets(thisElement);
378
var center2centerDistance = Math.sqrt(Math.pow(selectOffsets.get('leftCenter') - objectOffsets.get('leftCenter'), 2) + Math.pow(selectOffsets.get('topCenter') - objectOffsets.get('topCenter'), 2));
379
var radiusSum = selectOffsets.get('radius') + objectOffsets.get('radius');
380
// the encompassing circles are overlapping, get in for a closer look
381
if (center2centerDistance < radiusSum)
383
// tip is left of select
384
if ((objectOffsets.get('leftCenter') <= selectOffsets.get('leftCenter') && objectOffsets.get('right') < selectOffsets.get('left')) ||
385
// tip is right of select
386
(objectOffsets.get('leftCenter') > selectOffsets.get('leftCenter') && objectOffsets.get('left') > selectOffsets.get('right')) ||
387
// tip is above select
388
(objectOffsets.get('topCenter') <= selectOffsets.get('topCenter') && objectOffsets.get('bottom') < selectOffsets.get('top')) ||
389
// tip is below select
390
(objectOffsets.get('topCenter') > selectOffsets.get('topCenter') && objectOffsets.get('top') > selectOffsets.get('bottom')))
392
thisElement.hideList.remove(in_object.id);
393
if (!thisElement.hideList.length)
395
thisElement.style.visibility = 'visible';
398
thisElement.style.display = '';
404
thisElement.hideList.set(in_object.id, true);
405
thisElement.style.visibility = 'hidden';
408
thisElement.style.display = 'none';
416
// {{{ domLib_getOffsets()
418
function domLib_getOffsets(in_object)
420
var originalObject = in_object;
421
var originalWidth = in_object.offsetWidth;
422
var originalHeight = in_object.offsetHeight;
428
offsetLeft += in_object.offsetLeft;
429
offsetTop += in_object.offsetTop;
430
in_object = in_object.offsetParent;
433
// MacIE misreports the offsets (even with margin: 0 in body{}), still not perfect
434
if (domLib_isMacIE) {
442
'right', offsetLeft + originalWidth,
443
'bottom', offsetTop + originalHeight,
444
'leftCenter', offsetLeft + originalWidth/2,
445
'topCenter', offsetTop + originalHeight/2,
446
'radius', Math.max(originalWidth, originalHeight) );
450
// {{{ domLib_setTimeout()
452
function domLib_setTimeout(in_function, in_timeout, in_args)
454
if (typeof(in_args) == 'undefined')
459
if (in_timeout == -1)
461
// timeout event is disabled
464
else if (in_timeout === 0)
466
in_function(in_args);
470
// must make a copy of the arguments so that we release the reference
471
var args = domLib_clone(in_args);
473
if (!domLib_hasBrokenTimeout)
475
return setTimeout(function() { in_function(args); }, in_timeout);
479
var id = domLib_timeoutStateId++;
480
var data = new Hash();
481
data.set('function', in_function);
482
data.set('args', args);
483
domLib_timeoutStates.set(id, data);
485
data.set('timeoutId', setTimeout('domLib_timeoutStates.get(' + id + ').get(\'function\')(domLib_timeoutStates.get(' + id + ').get(\'args\')); domLib_timeoutStates.remove(' + id + ');', in_timeout));
491
// {{{ domLib_clearTimeout()
493
function domLib_clearTimeout(in_id)
495
if (!domLib_hasBrokenTimeout)
501
if (domLib_timeoutStates.has(in_id))
503
clearTimeout(domLib_timeoutStates.get(in_id).get('timeoutId'));
504
domLib_timeoutStates.remove(in_id);
510
// {{{ domLib_getEventPosition()
512
function domLib_getEventPosition(in_eventObj)
514
var eventPosition = new Hash('x', 0, 'y', 0, 'scrollX', 0, 'scrollY', 0);
516
// IE varies depending on standard compliance mode
519
var doc = (domLib_standardsMode ? document.documentElement : document.body);
520
// NOTE: events may fire before the body has been loaded
523
eventPosition.set('x', in_eventObj.clientX + doc.scrollLeft);
524
eventPosition.set('y', in_eventObj.clientY + doc.scrollTop);
525
eventPosition.set('scrollX', doc.scrollLeft);
526
eventPosition.set('scrollY', doc.scrollTop);
531
eventPosition.set('x', in_eventObj.pageX);
532
eventPosition.set('y', in_eventObj.pageY);
533
eventPosition.set('scrollX', in_eventObj.pageX - in_eventObj.clientX);
534
eventPosition.set('scrollY', in_eventObj.pageY - in_eventObj.clientY);
537
return eventPosition;
541
// {{{ domLib_cancelBubble()
543
function domLib_cancelBubble(in_event)
545
var eventObj = in_event ? in_event : window.event;
546
eventObj.cancelBubble = true;
550
// {{{ domLib_getIFrameReference()
552
function domLib_getIFrameReference(in_frame)
554
if (domLib_isGecko || domLib_isIE)
556
return in_frame.frameElement;
560
// we could either do it this way or require an id on the frame
561
// equivalent to the name
562
var name = in_frame.name;
563
if (!name || !in_frame.parent)
568
var candidates = in_frame.parent.document.getElementsByTagName('iframe');
569
for (var i = 0; i < candidates.length; i++)
571
if (candidates[i].name == name)
573
return candidates[i];
580
// {{{ domLib_getElementsByClass()
582
function domLib_getElementsByClass(in_class)
584
var elements = domLib_isIE5 ? document.all : document.getElementsByTagName('*');
587
for (var i = 0; i < elements.length; i++)
589
if ((" " + elements[i].className + " ").indexOf(" " + in_class + " ") != -1)
591
matches[cnt++] = elements[i];
601
function domLib_getElementsByTagNames(in_list, in_excludeHidden)
604
for (var i = 0; i < in_list.length; i++)
606
var matches = document.getElementsByTagName(in_list[i]);
607
for (var j = 0; j < matches.length; j++)
609
if (in_excludeHidden && domLib_getComputedStyle(matches[j], 'visibility') == 'hidden')
614
elements[elements.length] = matches[j];
624
function domLib_getComputedStyle(in_obj, in_property)
628
var humpBackProp = in_property.replace(/-(.)/, function (a, b) { return b.toUpperCase(); });
629
return eval('in_obj.currentStyle.' + humpBackProp);
631
// getComputedStyle() is broken in konqueror, so let's go for the style object
632
else if (domLib_isKonq)
634
humpBackProp = in_property.replace(/-(.)/, function (a, b) { return b.toUpperCase(); });
635
return eval('in_obj.style.' + in_property);
639
return document.defaultView.getComputedStyle(in_obj, null).getPropertyValue(in_property);