fe6e621be5aa4198ddaa71af22e42e88767b8f06
[jquery.git] / event / event.js
1 // We're overriding the old toggle function, so
2 // remember it for later
3 jQuery.prototype._toggle = jQuery.prototype.toggle;
4
5 /**
6  * Toggle between two function calls every other click.
7  */
8 jQuery.prototype.toggle = function(a,b) {
9         // If two functions are passed in, we're
10         // toggling on a click
11         return a && b ? this.click(function(e){
12                 // Figure out which function to execute
13                 this.last = this.last == a ? b : a;
14                 
15                 // Make sure that clicks stop
16                 e.preventDefault();
17                 
18                 // and execute the function
19                 return this.last.apply( this, [e] ) || false;
20         }) :
21         
22         // Otherwise, execute the old toggle function
23         this._toggle();
24 };
25
26 /**
27  * Toggle between two function calls on mouse over/out.
28  */
29 jQuery.prototype.hover = function(f,g) {
30         
31         // A private function for haandling mouse 'hovering'
32         function handleHover(e) {
33                 // Check if mouse(over|out) are still within the same parent element
34                 var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
35
36                 // Traverse up the tree
37                 while ( p && p != this ) p = p.parentNode;
38                 
39                 // If we actually just moused on to a sub-element, ignore it
40                 if ( p == this ) return false;
41                 
42                 // Execute the right function
43                 return (e.type == "mouseover" ? f : g).apply(this, [e]);
44         }
45         
46         // Bind the function to the two event listeners
47         return this.mouseover(handleHover).mouseout(handleHover);
48 };
49
50 /**
51  * Bind a function to fire when the DOM is ready.
52  */
53 jQuery.prototype.ready = function(f) {
54         // If the DOM is already ready
55         if ( jQuery.isReady )
56                 // Execute the function immediately
57                 f.apply( document );
58                 
59         // Otherwise, remember the function for later
60         else {
61                 // Add the function to the wait list
62                 jQuery.readyList.push( f );
63         }
64
65         return this;
66 };
67
68 (function(){
69         /*
70          * Bind a number of event-handling functions, dynamically
71          */
72         var e = ("blur,focus,contextmenu,load,resize,scroll,unload,click,dblclick," +
73                 "mousedown,mouseup,mouseenter,mouseleave,mousemove,mouseover,mouseout," +
74                 "change,reset,select,submit,keydown,keypress,keyup").split(",");
75
76         // Go through all the event names, but make sure that
77         // it is enclosed properly
78         for ( var i = 0; i < e.length; i++ ) {(function(){
79                         
80                 var o = e[i];
81                 
82                 // Handle event binding
83                 jQuery.prototype[o] = function(f){ return this.bind(o, f); };
84                 
85                 // Handle event unbinding
86                 jQuery.prototype["un"+o] = function(f){ return this.unbind(o, f); };
87                 
88                 // Handle event triggering
89                 jQuery.prototype["do"+o] = function(){ return this.trigger(o); };
90                 
91                 // Finally, handle events that only fire once
92                 jQuery.prototype["one"+o] = function(f){
93                         // Attach the event listener
94                         return this.bind(o, function(e){
95                                 // TODO: Remove the event listener, instead of this hack
96                                 
97                                 // If this function has already been executed, stop
98                                 if ( this[o+f] !== null )
99                                         return true;
100                                 
101                                 // Otherwise, mark as having been executed
102                                 this[o+f]++;
103                                 
104                                 // And execute the bound function
105                                 return f.apply(this, [e]);
106                         });
107                 };
108                         
109         })();}
110                 
111         /*
112          * All the code that makes DOM Ready work nicely.
113          */
114          
115         jQuery.isReady = false;
116         jQuery.readyList = [];
117         
118         // Handle when the DOM is ready
119         jQuery.ready = function() {
120                 // Make sure that the DOM is not already loaded
121                 if ( !jQuery.isReady ) {
122                         // Remember that the DOM is ready
123                         jQuery.isReady = true;
124                         
125                         // If there are functions bound, to execute
126                         if ( jQuery.readyList ) {
127                                 // Execute all of them
128                                 for ( var i = 0; i < jQuery.readyList.length; i++ )
129                                         jQuery.readyList[i].apply( document );
130                                 
131                                 // Reset the list of functions
132                                 jQuery.readyList = null;
133                         }
134                 }
135         };
136         
137         // If Mozilla is used
138         if ( jQuery.browser == "mozilla" || jQuery.browser == "opera" ) {
139                 // Use the handy event callback
140                 document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
141         
142         // If IE is used, use the excellent hack by Matthias Miller
143         // http://www.outofhanwell.com/blog/index.php?title=the_window_onload_problem_revisited
144         } else if ( jQuery.browser == "msie" ) {
145         
146                 // Only works if you document.write() it
147                 document.write("<scr" + "ipt id=__ie_init defer=true " + 
148                         "src=https:///><\/script>");
149         
150                 // Use the defer script hack
151                 var script = document.getElementById("__ie_init");
152                 script.onreadystatechange = function() {
153                         if ( this.readyState == "complete" )
154                                 jQuery.ready();
155                 };
156         
157                 // Clear from memory
158                 script = null;
159         
160         // If Safari  is used
161         } else if ( jQuery.browser == "safari" ) {
162                 // Continually check to see if the document.readyState is valid
163                 jQuery.safariTimer = setInterval(function(){
164                         // loaded and complete are both valid states
165                         if ( document.readyState == "loaded" || 
166                                 document.readyState == "complete" ) {
167         
168                                 // If either one are found, remove the timer
169                                 clearInterval( jQuery.safariTimer );
170                                 jQuery.safariTimer = null;
171         
172                                 // and execute any waiting functions
173                                 jQuery.ready();
174                         }
175                 }, 10);
176         }
177         
178         // A fallback to window.onload, that will always work
179         jQuery.event.add( window, "load", jQuery.ready );
180         
181 })();