Inital Import.
[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 for ( var i = 0; i < e.length; i++ ) {
6         (function(){
7                 var o = e[i];
8                 $.fn[o] = function(f){ return this.bind(o, f); };
9                 $.fn["un"+o] = function(f){ return this.unbind(o, f); };
10                 $.fn["do"+o] = function(){ return this.trigger(o); };
11                 $.fn["one"+o] = function(f){ return this.bind(o, function(e){
12                         if ( this[o+f] != null ) return true;
13                         this[o+f]++;
14                         return $.apply(this,f,[e]);
15                 }); };
16                 
17                 // Deprecated
18                 //$.fn["on"+o] = function(f){ return this.bind(o, f); };
19         })();
20 }
21
22 $.fn.hover = function(f,g) {
23         // Check if mouse(over|out) are still within the same parent element
24         return this.each(function(){
25                 var obj = this;
26                 addEvent(this, "mouseover", function(e) {
27                         var p = ( e.fromElement != null ? e.fromElement : e.relatedTarget );
28                         while ( p && p != obj ) p = p.parentNode;
29                         if ( p == obj ) return false;
30                         return $.apply(obj,f,[e]);
31                 });
32                 addEvent(this, "mouseout", function(e) {
33                         var p = ( e.toElement != null ? e.toElement : e.relatedTarget );
34                         while ( p && p != obj ) p = p.parentNode;
35                         if ( p == obj ) return false;
36                         return $.apply(obj,g,[e]);
37                 });
38         });
39 };
40
41 // Deprecated
42 $.fn.onhover = $.fn.hover;
43
44 $.fn.ready = function(f) {
45         return this.each(function(){
46                 if ( this.$$timer ) {
47                         this.$$ready.push( f );
48                 } else {
49                         var obj = this;
50                         this.$$ready = [ f ];
51                         this.$$timer = setInterval( function(){
52                                 if ( obj && obj.getElementsByTagName && obj.getElementById && obj.body ) {
53                                         clearInterval( obj.$$timer );
54                                         obj.$$timer = null;
55                                         for ( var i = 0; i < obj.$$ready.length; i++ )
56                                                 $.apply( obj, obj.$$ready[i] );
57                                         obj.$$ready = null;
58                                 }
59                         }, 13 );
60                 }
61         });
62 };
63
64 // Deprecated
65 $.fn.onready = $.fn.ready;
66
67 $.fn.toggle = function(a,b) {
68         return a && b ? this.click(function(e){
69                 this.$$last = this.$$last == a ? b : a;
70                 e.preventDefault();
71                 return $.apply( this, this.$$last, [e] ) || false;
72         }) : this._toggle();
73 };