Added in some revised DOM Ready code, I've removed all the kludgey stuff. I'd prefer...
[jquery.git] / event / event.js
1 (function(){
2         var e = ["blur","focus","contextmenu","load","resize","scroll","unload",
3                 "click","dblclick","mousedown","mouseup","mouseenter","mouseleave",
4                 "mousemove","mouseover","mouseout","change","reset","select","submit",
5                 "keydown","keypress","keyup","abort","error","ready"];
6
7         for ( var i = 0; i < e.length; i++ ) {
8                 (function(){
9                         var o = e[i];
10                         $.fn[o] = function(f){ return this.bind(o, f); };
11                         $.fn["un"+o] = function(f){ return this.unbind(o, f); };
12                         $.fn["do"+o] = function(){ return this.trigger(o); };
13                         $.fn["one"+o] = function(f){ return this.bind(o, function(e){
14                                 if ( this[o+f] !== null ) { return true; }
15                                 this[o+f]++;
16                                 return $.apply(this,f,[e]);
17                         }); };
18                 
19                         // Deprecated
20                         //$.fn["on"+o] = function(f){ return this.bind(o, f); };
21                 })();
22         }
23 })();
24
25 $.fn.hover = function(f,g) {
26         // Check if mouse(over|out) are still within the same parent element
27         return this.each(function(){
28                 var obj = this;
29                 $.event.add(this, "mouseover", function(e) {
30                         var p = ( e.fromElement !== null ? e.fromElement : e.relatedTarget );
31                         while ( p && p != obj ) { p = p.parentNode; }
32                         if ( p == obj ) { return false; }
33                         return $.apply(obj,f,[e]);
34                 });
35                 $.event.add(this, "mouseout", function(e) {
36                         var p = ( e.toElement !== null ? e.toElement : e.relatedTarget );
37                         while ( p && p != obj ) { p = p.parentNode; }
38                         if ( p == obj ) { return false; }
39                         return $.apply(obj,g,[e]);
40                 });
41         });
42 };
43
44 // Handle when the DOM is ready
45 $.ready = function(isFinal) {
46         if ( $.$$ready ) {
47                 for ( var i = 0; i < $.$$ready.length; i++ ) {
48                         $.apply( document, $.$$ready[i] );
49                 }
50                 $.$$ready = [];
51         }
52 };
53
54 // Based off of:
55 // http://linguiste.org/projects/behaviour-DOMContentLoaded/example.html
56
57 // If Mozilla is used
58 if ( $.browser == "mozilla" ) {
59         // Use the handy event callback
60         document.addEventListener( "DOMContentLoaded", $.ready, null );
61
62 // If IE is used
63 } else if ( $.browser == "msie" ) {
64         // Use the defer script hack
65         var script = document.createElement('script');
66         //script.type = 'text/javascript';
67         script.src = 'javascript:void 0';
68         script.defer = true;
69         script.onreadystatechange = function() {
70                 if ( this.readyState == 'loading' ) {
71                         $.ready();
72                 }
73         };
74         document.getElementsByTagName('head')[0].appendChild(script);
75         script = null;
76
77 // If Safari or Opera is used
78 } else {
79         $.$$timer = setInterval(function(){
80                 if ( document.readyState == "loaded" || 
81                         document.readyState == "complete" ) {
82
83                         clearInterval( $.$$timer );
84                         $.$$timer = null;
85
86                         $.ready();
87                 }
88         }, 10);
89 }
90
91 // A fallback, that will always work, just in case
92 $.event.add( window, "load", function(){
93         $.ready(true);
94 });
95
96 /**
97  * Bind a function to fire when the DOM is ready.
98  */
99 $.fn.ready = function(f) {
100         return this.each(function(){
101                 if ( ! $.$$ready ) {
102                         $.$$ready = [];
103                 }
104
105                 $.$$ready.push( f );
106         });
107 };
108
109 $.fn.toggle = function(a,b) {
110         return a && b ? this.click(function(e){
111                 this.$$last = this.$$last == a ? b : a;
112                 e.preventDefault();
113                 return $.apply( this, this.$$last, [e] ) || false;
114         }) : this._toggle();
115 };