Not only does it pass the default JSLint settings, it also no longer leaks *any*...
[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 // Deprecated
45 $.fn.onhover = $.fn.hover;
46
47 $.ready = function() {
48         if ( $.$$timer ) {
49                 clearInterval( $.$$timer );
50                 $.$$timer = null;
51                 for ( var i = 0; i < $.$$ready.length; i++ ) {
52                         $.apply( document, $.$$ready[i] );
53                 }
54                 $.$$ready = null;
55         }
56 };
57
58 if ( document.addEventListener ) {
59         document.addEventListener( "DOMContentLoaded", $.ready, null );
60 }
61
62 $.event.add( window, "load", $.ready );
63
64 $.fn.ready = function(f) {
65         return this.each(function(){
66                 if ( $.$$timer ) {
67                         $.$$ready.push( f );
68                 } else {
69                         var o = this;
70                         $.$$ready = [ f ];
71                         $.$$timer = setInterval( function(){
72                                 if ( o && o.getElementsByTagName && o.getElementById && o.body ) {
73                                         $.ready();
74                                 }
75                         }, 10 );
76                 }
77         });
78 };
79
80 // Deprecated
81 $.fn.onready = $.fn.ready;
82
83 $.fn.toggle = function(a,b) {
84         return a && b ? this.click(function(e){
85                 this.$$last = this.$$last == a ? b : a;
86                 e.preventDefault();
87                 return $.apply( this, this.$$last, [e] ) || false;
88         }) : this._toggle();
89 };