OSDN Git Service

Version 0.6.7
[pettanr/clientJs.git] / 0.6.x / js / dom / 13_XDomEvent.js
1 /**
2  * use X.Callback
3  */
4
5 if( window.addEventListener ){
6         X.Dom.Event = function( e ){
7                 this._event        = e;
8                 this.type          = e.type;
9                 this.target        = e.target;
10                 this.currentTarget = e.currentTarget;
11                 this.relatedTarget = e.relatedTarget;
12                 this.eventPhase    = e.eventPhase;
13                 
14                 this.clientX       = e.clientX;
15                 this.clientY       = e.clientY;
16                 this.screenX       = e.screenX;
17                 this.screenY       = e.screenY;
18                 
19                 this.keyCode       = e.keyCode;
20                 this.altKey        = e.altKey;
21                 this.ctrlKey       = e.ctrlKey;
22                 this.shiftKey      = e.shiftKey;
23                 
24                 this.wheelDelta    = e.wheelDelta;
25         };
26 } else {
27         X.Dom.Event = function( e, element ){
28                 this._event        = e;
29                 this.type          = e.type;
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;
34                 
35                 this.clientX       = e.clientX;
36                 this.clientY       = e.clientY;
37                 this.screenX       = e.screenX;
38                 this.screenY       = e.screenY;
39                 
40                 this.keyCode       = e.keyCode;
41                 this.altKey        = e.altKey;
42                 this.ctrlKey       = e.ctrlKey;
43                 this.shiftKey      = e.shiftKey;
44                 
45                 this.wheelDelta    = e.wheelDelta;
46         };
47 };
48
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 ) ){
53                 list = helper.list;
54                 if( list.indexOf( callback ) === -1 ) list[ list.length ] = callback;
55         } else {
56                 xhe._LIST[ xhe._LIST.length ] = new xhe._Helper( element, type, callback );
57         };
58 };
59
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 ),
63                 i;
64         if( t && c ){
65                 i = t.list.indexOf( c );
66                 if( i !== -1 ){
67                         t.list.splice( i, 1 );
68                         X.Callback._correct( c );
69                 };
70                 t.list.length === 0 && t.removeEvent();
71         };
72 };
73
74 X.Dom.Event._LIST = [];
75
76 X.Dom.Event._find = function( element, type ){
77         var list = X.Dom.Event._LIST,
78                 i    = list.length,
79                 helper;
80         for( ; i; ){
81                 helper = list[ --i ];
82                 if( helper.elm === element && helper.type === type ) return helper;
83         };
84 };
85
86 X.Dom.Event._Helper =
87         document.addEventListener ?
88                 (function( elm, type, callback ){
89                         this.elm  = elm;
90                         this.type = type;
91                         this.list = [ callback ];
92                         elm.addEventListener( type, this, false );
93                 }) :
94         document.attachEvent ?
95                 (function( elm, type, callback ){
96                         this.elm  = elm;
97                         this.type = type;
98                         this.list = [ callback ];
99                         elm.attachEvent( 'on' + type, ( this.callback = X.Callback.create( this ) ) );
100                 }) :
101                 (function( elm, type, callback ){
102                         this.elm      = elm;
103                         this.type     = type;
104                         this.list     = [ callback ];
105                         this.callback = elm[ 'on' + type ] = X.Callback.create( this );
106                 });
107
108 X.Dom.Event._Helper.prototype.handleEvent =
109         window.addEventListener ?
110                 (function( e ){
111                         var list = this.list,
112                                 e    = new X.Dom.Event( e ),
113                                 i    = 0,
114                                 ret  = 0,
115                                 f, r;
116                         for( ; i < list.length; ){
117                                 f = list[ i ];
118                                 r = f( e );
119                                 ret |= r;
120                                 if( r & X.Callback.UN_LISTEN ){
121                                         list.splice( i, 1 );
122                                         X.Callback._correct( f );
123                                         list.length === 0 && this.removeEvent();
124                                 } else {
125                                         ++i;
126                                 };
127                                 if( r & X.Callback.CANCEL_NOW ) break;
128                         };
129                         if( ( ret & X.Callback.STOP_PROPAGATION ) ){
130                                 e.stopPropagation();
131                         };
132                         if( ( ret & X.Callback.PREVENT_DEFAULT ) ){
133                                 e.preventDefault();
134                                 return false;
135                         };
136                 }) :
137                 (function(){
138                         var list = this.list,
139                                 e    = new X.Dom.Event( event, this.elm ),
140                                 i    = 0,
141                                 ret  = 0,
142                                 r, f;
143                         for( ; i < list.length; ){
144                                 f = list[ i ];
145                                 r = f( e );
146                                 ret |= r;
147                                 if( r & X.Callback.UN_LISTEN ){
148                                         list.splice( i, 1 );
149                                         X.Callback._correct( f );
150                                         list.length === 0 && this.removeEvent();
151                                 } else {
152                                         ++i;
153                                 };
154                                 if( r & X.Callback.CANCEL_NOW ) break;
155                         };
156                         if( ( ret & X.Callback.STOP_PROPAGATION ) ){
157                                 event.cancelBubble = true;
158                         };
159                         if( ( ret & X.Callback.PREVENT_DEFAULT ) ){
160                                 return event.returnValue = false;
161                         };
162                 });
163
164 X.Dom.Event._Helper.prototype.removeEvent =
165         document.removeEventListener ?
166                 (function(){
167                         this.elm.removeEventListener( this.type, this );
168                         delete this.elm;
169                         delete this.type;
170                         delete this.list;
171                         X.Dom.Event._LIST.splice( X.Dom.Event._LIST.indexOf( this ), 1 );
172                 }) :
173         document.detachEvent ?
174                 (function(){
175                         this.elm.detachEvent( 'on' + this.type, this.callback );
176                         X.Callback._correct( this.callback );
177                         delete this.elm;
178                         delete this.type;
179                         delete this.list;
180                         delete this.callback;
181                         X.Dom.Event._LIST.splice( X.Dom.Event._LIST.indexOf( this ), 1 );
182                 }) :
183                 (function(){
184                         this.elm[ 'on' + this.type ] = X.Dom.Event.emptyFunc;
185                         this.elm[ 'on' + this.type ] = '';
186                         X.Callback._correct( this.callback );
187                         delete this.elm;
188                         delete this.type;
189                         delete this.list;
190                         X.Dom.Event._LIST.splice( X.Dom.Event._LIST.indexOf( this ), 1 );
191                 });
192
193 if( !document.removeEventListener && !document.detachEvent ){
194         X.Dom.Event.emptyFunc = new Function();
195 };