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