jQuery is now JSLint (jslint.com) compatible, save for the eval stuff. Can't there b
[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                 }
52                 $.$$ready = null;
53         }
54 };
55
56 if ( document.addEventListener ) {
57         document.addEventListener( "DOMContentLoaded", $.ready, null );
58 }
59
60 addEvent( window, "load", $.ready );
61
62 $.fn.ready = function(f) {
63         return this.each(function(){
64                 if ( $.$$timer ) {
65                         $.$$ready.push( f );
66                 } else {
67                         var o = this;
68                         $.$$ready = [ f ];
69                         $.$$timer = setInterval( function(){
70                                 if ( o && o.getElementsByTagName && o.getElementById && o.body ) {
71                                         $.ready();
72                                 }
73                         }, 10 );
74                 }
75         });
76 };
77
78 // Deprecated
79 $.fn.onready = $.fn.ready;
80
81 $.fn.toggle = function(a,b) {
82         return a && b ? this.click(function(e){
83                 this.$$last = this.$$last == a ? b : a;
84                 e.preventDefault();
85                 return $.apply( this, this.$$last, [e] ) || false;
86         }) : this._toggle();
87 };