Added in handling for DOMContentLoaded for Mozilla and window.onload, just in case.
[jquery.git] / event / event.js
1 var e = ["blur","focus","contextmenu","load","resize","scroll","unload",
2         "click","dblclick","mousedown","mouseup","mouseenter","mouseleave",
3         "mousemove","mouseover","mouseout","change","reset","select","submit",
4         "keydown","keypress","keyup","abort","error","ready"];
5
6 for ( var i = 0; i < e.length; i++ ) {
7         (function(){
8                 var o = e[i];
9                 $.fn[o] = function(f){ return this.bind(o, f); };
10                 $.fn["un"+o] = function(f){ return this.unbind(o, f); };
11                 $.fn["do"+o] = function(){ return this.trigger(o); };
12                 $.fn["one"+o] = function(f){ return this.bind(o, function(e){
13                         if ( this[o+f] != null ) return true;
14                         this[o+f]++;
15                         return $.apply(this,f,[e]);
16                 }); };
17                 
18                 // Deprecated
19                 //$.fn["on"+o] = function(f){ return this.bind(o, f); };
20         })();
21 }
22
23 $.fn.hover = function(f,g) {
24         // Check if mouse(over|out) are still within the same parent element
25         return this.each(function(){
26                 var obj = this;
27                 addEvent(this, "mouseover", function(e) {
28                         var p = ( e.fromElement != null ? e.fromElement : e.relatedTarget );
29                         while ( p && p != obj ) p = p.parentNode;
30                         if ( p == obj ) return false;
31                         return $.apply(obj,f,[e]);
32                 });
33                 addEvent(this, "mouseout", function(e) {
34                         var p = ( e.toElement != null ? e.toElement : e.relatedTarget );
35                         while ( p && p != obj ) p = p.parentNode;
36                         if ( p == obj ) return false;
37                         return $.apply(obj,g,[e]);
38                 });
39         });
40 };
41
42 // Deprecated
43 $.fn.onhover = $.fn.hover;
44
45 $.ready = function() {
46   if ( $.$$timer ) {
47           clearInterval( $.$$timer );
48           $.$$timer = null;
49           for ( var i = 0; i < $.$$ready.length; i++ )
50                   $.apply( document, $.$$ready[i] );
51           $.$$ready = null;
52   }
53 };
54
55 if ( document.addEventListener )
56         document.addEventListener( "DOMContentLoaded", $.ready, null );
57
58 addEvent( window, "load", $.ready );
59
60 $.fn.ready = function(f) {
61         return this.each(function(){
62                 if ( $.$$timer ) {
63                         $.$$ready.push( f );
64                 } else {
65       var o = this;
66                         $.$$ready = [ f ];
67                         $.$$timer = setInterval( function(){
68                                 if ( o && o.getElementsByTagName && o.getElementById && o.body )
69                                         $.ready();
70                         }, 10 );
71                 }
72         });
73 };
74
75 // Deprecated
76 $.fn.onready = $.fn.ready;
77
78 $.fn.toggle = function(a,b) {
79         return a && b ? this.click(function(e){
80                 this.$$last = this.$$last == a ? b : a;
81                 e.preventDefault();
82                 return $.apply( this, this.$$last, [e] ) || false;
83         }) : this._toggle();
84 };