5 if( window.addEventListener ){
6 X.Dom.Event = function( e ){
9 this.target = e.target;
10 this.currentTarget = e.currentTarget;
11 this.relatedTarget = e.relatedTarget;
12 this.eventPhase = e.eventPhase;
14 this.clientX = e.clientX;
15 this.clientY = e.clientY;
16 this.screenX = e.screenX;
17 this.screenY = e.screenY;
19 this.keyCode = e.keyCode;
20 this.altKey = e.altKey;
21 this.ctrlKey = e.ctrlKey;
22 this.shiftKey = e.shiftKey;
24 this.wheelDelta = e.wheelDelta;
27 X.Dom.Event = function( e, element ){
30 this.target = e.srcElement;
31 this.currentTarget = element;
32 this.relatedTarget = e.formElement ? e.formElement : e.toElement;
33 this.eventPhase = e.srcElement === element ? 2: 3;
35 this.clientX = e.clientX;
36 this.clientY = e.clientY;
37 this.screenX = e.screenX;
38 this.screenY = e.screenY;
40 this.keyCode = e.keyCode;
41 this.altKey = e.altKey;
42 this.ctrlKey = e.ctrlKey;
43 this.shiftKey = e.shiftKey;
45 this.wheelDelta = e.wheelDelta;
49 X.Dom.Event.add = function( element, type, arg2, arg3, arg4 /* [ listener || ( context + function ) || function ][ arguments ] */ ){
50 var xhe = X.Dom.Event, callback, helper, list;
51 callback = typeof arg2 === 'function' ? X.Callback.create( element, arg2, arg3 ) : X.Callback.create( arg2, arg3, arg4 );
52 if( helper = xhe._find( element, type ) ){
54 if( list.indexOf( callback ) === -1 ) list[ list.length ] = callback;
56 xhe._LIST[ xhe._LIST.length ] = new xhe._Helper( element, type, callback );
60 X.Dom.Event.remove = function( element, type, arg2, arg3 ){
61 var t = X.Dom.Event._find( element, type ),
62 c = X.Callback._find( arg2, arg3 ),
65 i = t.list.indexOf( c );
67 t.list.splice( i, 1 );
68 X.Callback._correct( c );
70 t.list.length === 0 && t.removeEvent();
74 X.Dom.Event._LIST = [];
76 X.Dom.Event._find = function( element, type ){
77 var list = X.Dom.Event._LIST,
82 if( helper.elm === element && helper.type === type ) return helper;
87 document.addEventListener ?
88 (function( elm, type, callback ){
91 this.list = [ callback ];
92 elm.addEventListener( type, this, false );
94 document.attachEvent ?
95 (function( elm, type, callback ){
98 this.list = [ callback ];
99 elm.attachEvent( 'on' + type, ( this.callback = X.Callback.create( this ) ) );
101 (function( elm, type, callback ){
104 this.list = [ callback ];
105 this.callback = elm[ 'on' + type ] = X.Callback.create( this );
108 X.Dom.Event._Helper.prototype.handleEvent =
109 window.addEventListener ?
111 var list = this.list,
112 e = new X.Dom.Event( e ),
116 for( ; i < list.length; ){
120 if( r & X.Callback.UN_LISTEN ){
122 X.Callback._correct( f );
123 list.length === 0 && this.removeEvent();
127 if( r & X.Callback.CANCEL_NOW ) break;
129 if( ( ret & X.Callback.STOP_PROPAGATION ) ){
132 if( ( ret & X.Callback.PREVENT_DEFAULT ) ){
138 var list = this.list,
139 e = new X.Dom.Event( event, this.elm ),
143 for( ; i < list.length; ){
147 if( r & X.Callback.UN_LISTEN ){
149 X.Callback._correct( f );
150 list.length === 0 && this.removeEvent();
154 if( r & X.Callback.CANCEL_NOW ) break;
156 if( ( ret & X.Callback.STOP_PROPAGATION ) ){
157 event.cancelBubble = true;
159 if( ( ret & X.Callback.PREVENT_DEFAULT ) ){
160 return event.returnValue = false;
164 X.Dom.Event._Helper.prototype.removeEvent =
165 document.removeEventListener ?
167 this.elm.removeEventListener( this.type, this );
171 X.Dom.Event._LIST.splice( X.Dom.Event._LIST.indexOf( this ), 1 );
173 document.detachEvent ?
175 this.elm.detachEvent( 'on' + this.type, this.callback );
176 X.Callback._correct( this.callback );
180 delete this.callback;
181 X.Dom.Event._LIST.splice( X.Dom.Event._LIST.indexOf( this ), 1 );
184 this.elm[ 'on' + this.type ] = X.Dom.Event.emptyFunc;
185 this.elm[ 'on' + this.type ] = '';
186 X.Callback._correct( this.callback );
190 X.Dom.Event._LIST.splice( X.Dom.Event._LIST.indexOf( this ), 1 );
193 if( !document.removeEventListener && !document.detachEvent ){
194 X.Dom.Event.emptyFunc = new Function();