]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/events.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1153 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / events.js
1 // written by Dean Edwards, 2005
2 // with input from Tino Zijdel, Matthias Miller, Diego Perini
3 // http://dean.edwards.name/weblog/2005/10/add-event/
4 function addEvent(element, type, handler) {
5         // Modification by Tanny O'Haley, http://tanny.ica.com to add the
6         // DOMContentLoaded for all browsers.
7         if (type == "DOMContentLoaded" || type == "domload") {
8                 addDOMLoadEvent(handler);
9                 return;
10         }
11         
12         if (element.addEventListener) {
13                 element.addEventListener(type, handler, false);
14         } else {
15                 // assign each event handler a unique ID
16                 if (!handler.$$guid) handler.$$guid = addEvent.guid++;
17                 // create a hash table of event types for the element
18                 if (!element.events) element.events = {};
19                 // create a hash table of event handlers for each element/event pair
20                 var handlers = element.events[type];
21                 if (!handlers) {
22                         handlers = element.events[type] = {};
23                         // store the existing event handler (if there is one)
24                         if (element["on" + type]) {
25                                 handlers[0] = element["on" + type];
26                         }
27                 }
28                 // store the event handler in the hash table
29                 handlers[handler.$$guid] = handler;
30                 // assign a global event handler to do all the work
31                 element["on" + type] = handleEvent;
32         }
33 };
34 // a counter used to create unique IDs
35 addEvent.guid = 1;
36
37 function removeEvent(element, type, handler) {
38         if (element.removeEventListener) {
39                 element.removeEventListener(type, handler, false);
40         } else {
41                 // delete the event handler from the hash table
42                 if (element.events && element.events[type]) {
43                         delete element.events[type][handler.$$guid];
44                 }
45         }
46 };
47
48 function handleEvent(event) {
49         var returnValue = true;
50         // grab the event object (IE uses a global event object)
51         event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
52         // get a reference to the hash table of event handlers
53         var handlers = this.events[event.type];
54         // execute each event handler
55         for (var i in handlers) {
56                 this.$$handleEvent = handlers[i];
57                 if (this.$$handleEvent(event) === false) {
58                         returnValue = false;
59                 }
60         }
61         return returnValue;
62 };
63
64 function fixEvent(event) {
65         // add W3C standard event methods
66         event.preventDefault = fixEvent.preventDefault;
67         event.stopPropagation = fixEvent.stopPropagation;
68         return event;
69 };
70 fixEvent.preventDefault = function() {
71         this.returnValue = false;
72 };
73 fixEvent.stopPropagation = function() {
74         this.cancelBubble = true;
75 };
76
77 // End Dean Edwards addEvent.
78
79 // Tino Zijdel - crisp@xs4all.nl This little snippet fixes the problem that the onload attribute on 
80 // the body-element will overwrite previous attached events on the window object for the onload event.
81 if (!window.addEventListener) {
82         document.onreadystatechange = function(){
83                 if (window.onload && window.onload != handleEvent) {
84                         addEvent(window, 'load', window.onload);
85                         window.onload = handleEvent;
86                 }
87         }
88 }
89
90 // Here are my functions for adding the DOMContentLoaded event to browsers other
91 // than Mozilla.
92
93 // Array of DOMContentLoaded event handlers.
94 window.onDOMLoadEvents = new Array();
95 window.DOMContentLoadedInitDone = false;
96
97 // Function that adds DOMContentLoaded listeners to the array.
98 function addDOMLoadEvent(listener) {
99         window.onDOMLoadEvents[window.onDOMLoadEvents.length]=listener;
100 }
101
102 // Function to process the DOMContentLoaded events array.
103 function DOMContentLoadedInit() {
104         // quit if this function has already been called
105         if (window.DOMContentLoadedInitDone) return;
106
107         // flag this function so we don't do the same thing twice
108         window.DOMContentLoadedInitDone = true;
109
110         // iterates through array of registered functions 
111         for (var i=0; i<window.onDOMLoadEvents.length; i++) {
112                 var func = window.onDOMLoadEvents[i];
113                 func();
114         }
115 }
116
117 function DOMContentLoadedScheduler() {
118         // quit if the init function has already been called
119         if (window.DOMContentLoadedInitDone) return true;
120         
121         // First, check for Safari or KHTML.
122         // Second, check for IE.
123         //if DOM methods are supported, and the body element exists
124         //(using a double-check including document.body, for the benefit of older moz builds [eg ns7.1] 
125         //in which getElementsByTagName('body')[0] is undefined, unless this script is in the body section)
126         if(/KHTML|WebKit/i.test(navigator.userAgent)) {
127                 if(/loaded|complete/.test(document.readyState)) {
128                         DOMContentLoadedInit();
129                 } else {
130                         // Not ready yet, wait a little more.
131                         setTimeout("DOMContentLoadedScheduler()", 250);
132                 }
133         } else if(document.getElementById("__ie_onload")) {
134                 return true;
135         } else if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
136                 DOMContentLoadedInit();
137         } else {
138                 // Not ready yet, wait a little more.
139                 setTimeout("DOMContentLoadedScheduler()", 250);
140         }
141         
142         return true;
143 }
144
145 // Schedule to run the init function.
146 setTimeout("DOMContentLoadedScheduler()", 250);
147
148 // Just in case window.onload happens first, add it there too.
149 addEvent(window, "load", DOMContentLoadedInit);
150
151 // If addEventListener supports the DOMContentLoaded event.
152 if(document.addEventListener)
153         document.addEventListener("DOMContentLoaded", DOMContentLoadedInit, false);
154
155 /* for Internet Explorer */
156 /*@cc_on @*/
157 /*@if (@_win32)
158         document.write("<script id=__ie_onload defer src=\"//:\"><\/script>");
159         var script = document.getElementById("__ie_onload");
160         script.onreadystatechange = function() {
161                 if (this.readyState == "complete") {
162                         DOMContentLoadedInit(); // call the onload handler
163                 }
164         };
165 /*@end @*/