Fixed more formatting/tab problems.
[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 $.fn.ready = function(f) {
46         return this.each(function(){
47                 if ( this.$$timer ) {
48                         this.$$ready.push( f );
49                 } else {
50                         var obj = this;
51                         this.$$ready = [ f ];
52                         this.$$timer = setInterval( function(){
53                                 if ( obj && obj.getElementsByTagName && obj.getElementById && obj.body ) {
54                                         clearInterval( obj.$$timer );
55                                         obj.$$timer = null;
56                                         for ( var i = 0; i < obj.$$ready.length; i++ )
57                                                 $.apply( obj, obj.$$ready[i] );
58                                         obj.$$ready = null;
59                                 }
60                         }, 13 );
61                 }
62         });
63 };
64
65 // Deprecated
66 $.fn.onready = $.fn.ready;
67
68 $.fn.toggle = function(a,b) {
69         return a && b ? this.click(function(e){
70                 this.$$last = this.$$last == a ? b : a;
71                 e.preventDefault();
72                 return $.apply( this, this.$$last, [e] ) || false;
73         }) : this._toggle();
74 };