5 if( window.addEventListener ){
6 X.Dom.Event = function( e ){
9 this.target = e.target; // xnode
10 this.currentTarget = e.currentTarget; // xnode
11 this.relatedTarget = e.relatedTarget; // xnode
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;
20 this.offsetX = e.offsetX || e.layerX;
21 this.offsetY = e.offsetY || e.layerY;
23 this.keyCode = e.keyCode;
24 this.altKey = e.altKey;
25 this.ctrlKey = e.ctrlKey;
26 this.shiftKey = e.shiftKey;
28 // http://www.programming-magic.com/20090127231544/
29 this.which = e.which || ( e.button + 1 ); // 左:1, 中:2, 右:3
31 // https://developer.mozilla.org/ja/docs/DOM/DOM_event_reference/mousewheel
32 if( e.wheelDeltaY !== undefined ){
33 this.wheelDeltaX = e.wheelDeltaX / 12;
34 this.wheelDeltaY = e.wheelDeltaY / 12;
36 if( e.wheelDelta !== undefined ){
37 this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;
39 this.wheelDeltaX = this.wheelDeltaY = - e.detail * 3;
42 if( e.constructor === window.TouchEvent ){
44 this.touches = e.touches;
45 this.changedTouches = e.changedTouches;
46 this.targetTouches = e.targetTouches;
47 //this.altKey = e.altKey;
48 //this.ctrlKey = e.ctrlKey;
49 this.metaKey = e.metaKey;
50 //this.shiftKey = e.shiftKey;
52 //this.target = e.target;
54 if( e.constructor === window.PointerEvent ){
56 this.currentPoint = e.currentPoint;
58 this.height = e.height;
59 this.timeStamp = e.timeStamp;
60 this.hwTimestamp = e.hwTimestamp;
61 this.intermediatePoints = e.intermediatePoints;
62 this.isPrimary = e.isPrimary;
63 this.pointerId = e.pointerId;
64 this.pointerType = e.pointerType;
65 this.pressure = e.pressure;
71 X.Dom.Event = function( e, element ){
76 this.target = e.srcElement; // xnode
77 this.currentTarget = element; // xnode
78 this.relatedTarget = e.formElement ? e.formElement : e.toElement; // xnode
79 this.eventPhase = e.srcElement === element ? 2: 3;
81 this.clientX = e.clientX;
82 this.clientY = e.clientY;
83 //this.screenX = e.screenX;
84 //this.screenY = e.screenY;
85 this.pageX = e.clientX + document.body.scrollLeft;
86 this.pageY = e.clientY + document.body.scrollTop;
88 if( X.UA.IE && 5 <= X.UA.IE ){
89 this.offsetX = e.offsetX;
90 this.offsetY = e.offsetY;
93 this.keyCode = e.keyCode;
94 this.altKey = e.altKey;
95 this.ctrlKey = e.ctrlKey;
96 this.shiftKey = e.shiftKey;
98 // http://www.programming-magic.com/20090127231544/
112 btn & 2 ? 3 : 0; // 左:1(click:0), 中:4, 右:2
114 this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;
118 X.Dom.Event.DOM_PREINIT = 0;
119 X.Dom.Event.DOM_INIT = 1;
120 X.Dom.Event.XDOM_READY = 2;
121 X.Dom.Event.VIEW_RESIZED = 3;
122 X.Dom.Event._LAST_EVENT = 3;
124 X.Dom.Event._Helper =
125 document.addEventListener ?
126 (function( elm, type ){
127 elm.addEventListener( type, this, false );
129 document.attachEvent ?
130 (function( elm, type ){
131 elm.attachEvent( 'on' + type, ( this.callback = X.Callback.create( this ) ) );
133 (function( elm, type ){
134 this.callback = elm[ 'on' + type ] = X.Callback.create( this );
137 X.Dom.Event._Helper.prototype.removeEvent =
138 document.removeEventListener ?
139 (function( migrate ){
140 this.type && this.elm.removeEventListener( this.type, this, false );
141 if( migrate ) return;
146 document.detachEvent ?
147 (function( migrate ){
148 this.type && this.elm.detachEvent( 'on' + this.type, this.callback );
149 X.Callback._correct( this.callback );
150 if( migrate ) return;
154 delete this.callback;
156 (function( migrate ){
157 this.elm[ 'on' + this.type ] = X.emptyFunction;
158 this.elm[ 'on' + this.type ] = '';
159 X.Callback._correct( this.callback );
160 if( migrate ) return;
164 delete this.callback;
168 * eventDispatch 中の Event.remove への対処.
169 * 現在 dispatch 中の function より以前の function が抜かれた場合の対策
171 X.Dom.Event._Helper.prototype.handleEvent =
172 document.removeEventListener ?
174 var list = this.list,
175 e = new X.Dom.Event( e ),
180 for( ; i < list.length; ){
181 list.pointer = i - list.removed;
182 f = list[ list.pointer ];
185 if( r & X.Callback.UN_LISTEN ){
186 list.splice( list.pointer, 1 );
187 X.Callback._correct( f );
188 if( list.length === 0 ){
195 if( r & X.Callback.STOP_NOW ) break;
199 if( ret & X.Callback.STOP_PROPAGATION ){
200 event.cancelBubble = true;
202 if( ret & X.Callback.PREVENT_DEFAULT ){
203 return event.returnValue = false;
207 var list = this.list,
208 e = new X.Dom.Event( event, this.elm ),
213 for( ; i < list.length; ){
214 list.pointer = i - list.removed;
215 f = list[ list.pointer ];
218 if( r & X.Callback.UN_LISTEN ){
219 list.splice( list.pointer, 1 );
220 X.Callback._correct( f );
221 if( list.length === 0 ){
228 if( r & X.Callback.STOP_NOW ) break;
232 if( ret & X.Callback.STOP_PROPAGATION ){
235 if( ret & X.Callback.PREVENT_DEFAULT ){
241 X.Dom.Event._chashe = [ {}, {}, null, {} ]; // window, document, documentElement, body
242 X.Dom.Event._chashe2 = [];
244 X.Dom.Event.add = function( element, type, arg2, arg3, arg4 /* [ listener || ( context + function ) || function ][ arguments ] */ ){
245 var XEvent = X.Dom.Event,
246 hash = XEvent._getHash( element, true ),
247 helper = hash[ type ],
249 if( typeof arg2 === 'function' ){
250 callback = X.Callback.create( element, arg2, arg3 );
252 callback = X.Callback.create( arg2, arg3, arg4 );
256 list.indexOf( callback ) === -1 && ( list[ list.length ] = callback );
258 helper = hash[ type ] = new XEvent._Helper( element, type );
259 helper.elm = element;
261 helper.list = [ callback ];
265 X.Dom.Event._getHash = function( element, create ){
267 if( element === window ){
268 return this._chashe[ 0 ];
270 if( element === document ){
271 return this._chashe[ 1 ];
273 if( element === document.body ){
274 return this._chashe[ 3 ];
276 if( element === document.documentElement ){
277 return this._chashe[ 2 ] || ( create && ( this._chashe[ 2 ] = {} ) );
280 if( X.UA.IE && X.UA.IE < 5 ){
281 if( uid = 2 + parseFloat( element.getAttribute( 'UID' ) ) ){
282 if( ret = this._chashe[ uid ] ) return ret;
283 if( create ) return ( this._chashe[ uid ] = {} );
286 if( uid = parseFloat( element.getAttribute( '_UID' ) ) ){
287 return this._chashe2[ uid ];
290 uid = this._chashe2.length;
291 element.setAttrivute( '_UID', '' + uid );
292 return this._chashe2[ uid ] = {};
296 if( uid = 2 + element.UID ){
297 if( ret = this._chashe[ uid ] ) return ret;
298 if( create ) return ( this._chashe[ uid ] = {} );
301 if( uid = element._UID ){
302 return this._chashe2[ uid ];
305 element._UID = uid = this._chashe2.length;
306 return this._chashe2[ uid ] = {};
312 X.Dom.Event.remove = function( /* element, type, arg2, arg3, arg4 */ ){
313 var element = arguments[ 0 ],
314 type = arguments[ 1 ],
316 hash = X.Dom.Event._getHash( element ),
317 helper, callback, list, i;
320 switch( arguments.length ){
325 X.Dom.Event.remove( element, type );
327 X.Dom.Event._chashe.splice( X.Dom.Event._chashe.indexOf( hash ), 1 );
330 if( helper = hash[ type ] ){
334 callback = list[ --i ];
335 X.Callback._correct( callback );
338 helper.removeEvent();
343 arg2 = arguments[ 2 ];
344 arg3 = arguments[ 3 ];
345 arg4 = arguments[ 4 ];
346 callback = typeof arg2 === 'function' ? X.Callback.create( element, arg2, arg3 ) : X.Callback.create( arg2, arg3, arg4 );
348 i = list.indexOf( callback );
351 if( list.pointer && i <= list.pointer ){
354 X.Callback._correct( callback );
355 if( list.length === 0 ){
356 helper.removeEvent();
363 // イベントの退避、dom が画面から抜かれる場合に実施しておく
364 X.Dom.Event.migrate = function( element ){
365 var XEvent = X.Dom.Event,
366 hash = XEvent._getHash( element ),
370 hash[ type ].removeEvent( true );
375 X.Dom.Event.restore = function( element ){
376 var XEvent = X.Dom.Event,
377 hash = XEvent._getHash( element ),
378 helper = XEvent._Helper,
382 helper.call( hash[ type ], element, type );
388 /* -----------------------------------------------
390 * Dean Edwards/Matthias Miller/John Resig
392 /* for ie9+/Mozilla/Opera9 */
393 if( document.addEventListener ){
394 X.Dom.Event.add( document, 'DOMContentLoaded', X.Dom._init );
396 if( 5 <= X.UA.IE && X.inHead ){
397 // if this script in Head
398 document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
399 X.Dom._script = document.getElementById( "__ie_onload" );
400 X.Dom._script.onreadystatechange = function(){
401 this.readyState === 'complete' && X.Dom._init();
404 if( X.UA.WebKit ){ // sniff
405 X.Timer.add( 10, function(){
406 if( !X.Dom._init ) return X.Callback.UN_LISTEN;
407 if( 'loaded|complete'.indexOf( document.readyState ) !== -1 ) return X.Dom._init();
411 /* for other browsers */
412 X.Dom.Event.add( window, 'load', X.Dom._init );
415 X.Dom.listenOnce( X.Dom.Event.XDOM_READY, function(e){ console.log( 'X.Dom XDomReady ' + X.Dom.ready ) } );
417 X.Dom.listenOnce( X.Dom.Event.VIEW_RESIZED, function(e){ console.log( 'X.Dom VIEW_RESIZED ' + e.w + 'x' + e.h ) } );