4 * http://d.hatena.ne.jp/uupaa/20100430/1272561922
\r
8 if( !X.UA.IE || 9 <= X.UA.IE ){
\r
9 X.Dom.Event = function( e, xnode ){
\r
12 this.type = X.Dom.Event.RenameTo[ e.type ] || e.type;
\r
14 //http://www.quirksmode.org/js/events_properties.html
\r
16 this.target = Node._getXNode( e.target.nodeType === 3 ? e.target.parentNode : e.target );// defeat Safari bug // xnode
\r
18 if( e.relatedTarget ){
\r
19 this.relatedTarget = Node._getXNode( e.relatedTarget.nodeType === 3 ? e.relatedTarget.parentNode : e.relatedTarget ); // xnode
\r
22 this.currentTarget = xnode; // xnode
\r
23 this.eventPhase = e.eventPhase;
\r
25 this.clientX = e.clientX;
\r
26 this.clientY = e.clientY;
\r
27 //this.screenX = e.screenX;
\r
28 //this.screenY = e.screenY;
\r
29 this.pageX = e.pageX;
\r
30 this.pageY = e.pageY;
\r
31 this.offsetX = e.offsetX || e.layerX;
\r
32 this.offsetY = e.offsetY || e.layerY;
\r
34 this.keyCode = e.keyCode;
\r
35 this.altKey = e.altKey;
\r
36 this.ctrlKey = e.ctrlKey;
\r
37 this.shiftKey = e.shiftKey;
\r
39 // http://www.programming-magic.com/20090127231544/
\r
40 this.which = e.which || ( e.button + 1 ); // 左:1, 中:2, 右:3
\r
42 // https://developer.mozilla.org/ja/docs/DOM/DOM_event_reference/mousewheel
\r
46 // https://w3g.jp/blog/tools/wheelevent_crossbrowser
\r
47 // ホイール系イベント2014年版クロスブラウザ
\r
48 if( e.wheelDeltaY !== undefined ){
\r
49 this.wheelDeltaX = e.wheelDeltaX / 12;
\r
50 this.wheelDeltaY = e.wheelDeltaY / 12;
\r
52 if( e.wheelDelta !== undefined ){
\r
53 this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;
\r
55 if( e.detail !== undefined ){
\r
56 this.wheelDeltaX = this.wheelDeltaY = - e.detail * 3;
\r
58 this.wheelDeltaX = this.wheelDeltaY = 0;
\r
61 if( /* e.constructor === window.TouchEvent || */ e.touches /* for iOS3.13 */ ){
\r
63 this.touches = e.touches;
\r
64 this.changedTouches = e.changedTouches;
\r
65 this.targetTouches = e.targetTouches;
\r
66 this.metaKey = e.metaKey;
\r
67 this.force = e.force || e.webkitForce || 0;
\r
69 if( this.touches.length ){
\r
70 touch0 = this.touches[ 0 ];
\r
71 this.clientX = touch0.clientX;
\r
72 this.clientY = touch0.clientY;
\r
73 this.pageX = touch0.pageX;
\r
74 this.pageY = touch0.pageY;
\r
75 this.offsetX = touch0.offsetX || touch0.layerX;
\r
76 this.offsetY = touch0.offsetY || touch0.layerY;
\r
79 if( e.constructor === window.PointerEvent ){
\r
81 this.currentPoint = e.currentPoint;
\r
82 this.width = e.width;
\r
83 this.height = e.height;
\r
84 this.timeStamp = e.timeStamp;
\r
85 this.hwTimestamp = e.hwTimestamp;
\r
86 this.intermediatePoints = e.intermediatePoints;
\r
87 this.isPrimary = e.isPrimary;
\r
88 this.pointerId = e.pointerId;
\r
89 this.pointerType = e.pointerType;
\r
90 this.pressure = e.pressure;
\r
91 this.tiltX = e.tiltX;
\r
92 this.tiltY = e.tiltY;
\r
95 if( !window.PointerEvent && window.MSPointerEvent ){
\r
96 window.PointerEvent = window.MSPointerEvent;
\r
99 X.Dom.Event = function( e, xnode, element ){
\r
103 this.type = e.type;
\r
104 this.target = Node._getXNode( e.srcElement ); // xnode
\r
105 if( this.target && this.target._xnodeType === 3 ) this.target = this.target.parent; // ie4 の fake Textnode がヒットしていないか?
\r
106 this.currentTarget = xnode; // xnode
\r
107 this.relatedTarget = Node._getXNode( e.formElement ? e.formElement : e.toElement ); // xnode
\r
108 this.eventPhase = e.srcElement === element ? 2: 3;
\r
110 this.clientX = e.clientX;
\r
111 this.clientY = e.clientY;
\r
112 //this.screenX = e.screenX;
\r
113 //this.screenY = e.screenY;
\r
115 if( X.Dom._root ){ // uuu...
\r
117 this.pageX = e.clientX + X.Dom._root.scrollLeft;
\r
118 this.pageY = e.clientY + X.Dom._root.scrollTop;
\r
120 // DOMAssistant 2.8.1
\r
121 //event.pageX = DOMAssistant.def(e.pageX)? e.pageX : (event.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0));
\r
122 //event.pageY = DOMAssistant.def(e.pageY)? e.pageY : (event.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0));
\r
127 if( 5 <= X.UA.IE ){
\r
128 this.offsetX = e.offsetX; // イベントターゲット左上からの座標
\r
129 this.offsetY = e.offsetY;
\r
131 //if( e.srcElement ){
\r
132 // this.offsetX = e.x - e.srcElement.offsetLeft; // e.x はイベント発生要素の親要素を基準にした座標。
\r
133 // this.offsetY = e.y - e.srcElement.offsetTop;
\r
136 this.keyCode = e.keyCode;
\r
137 this.altKey = e.altKey;
\r
138 this.ctrlKey = e.ctrlKey;
\r
139 this.shiftKey = e.shiftKey;
\r
141 // http://www.programming-magic.com/20090127231544/
\r
142 switch( this.type ){
\r
147 case 'contextmenu' :
\r
155 btn & 2 ? 3 : 0; // 左:1(click:0), 中:4, 右:2
\r
157 this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;
\r
161 X.Dom.Event.DOM_PRE_INIT = ++X.Event._LAST_EVENT;
\r
162 X.Dom.Event.DOM_BUILDER_COMPLETE= ++X.Event._LAST_EVENT;
\r
163 X.Dom.Event.DOM_INIT = ++X.Event._LAST_EVENT;
\r
164 X.Dom.Event.XDOM_READY = ++X.Event._LAST_EVENT;
\r
165 X.Dom.Event.VIEW_ACTIVATE = ++X.Event._LAST_EVENT;
\r
166 X.Dom.Event.VIEW_DEACTIVATE = ++X.Event._LAST_EVENT;
\r
167 X.Dom.Event.VIEW_RESIZED = ++X.Event._LAST_EVENT;
\r
168 X.Dom.Event.VIEW_TURNED = ++X.Event._LAST_EVENT;
\r
169 X.Dom.Event.BASE_FONT_RESIZED = ++X.Event._LAST_EVENT;
\r
171 // on_screen_keyboard_show
\r
172 // on_screen_keyboard_hide
\r
173 // X.Dom.Event.BEFORE_UPDATE = ++X.Event._LAST_EVENT; // このイベントで要素のサイズを取得すると無限ループに!
\r
174 X.Dom.Event.AFTER_UPDATE = ++X.Event._LAST_EVENT;
\r
176 X.Dom.Event.BEFORE_UNLOAD = ++X.Event._LAST_EVENT;
\r
177 X.Dom.Event.UNLOAD = ++X.Event._LAST_EVENT;
\r
179 X.Dom.Event.ANIME_BEFORE_START = ++X.Event._LAST_EVENT;
\r
180 X.Dom.Event.ANIME_START = ++X.Event._LAST_EVENT;
\r
181 X.Dom.Event.ANIME = ++X.Event._LAST_EVENT;
\r
182 X.Dom.Event.ANIME_END = ++X.Event._LAST_EVENT;
\r
183 X.Dom.Event.ANIME_BEFORE_STOP = ++X.Event._LAST_EVENT; // xnode.stop() のみ、指定時間による停止では呼ばれない
\r
184 X.Dom.Event.ANIME_STOP = ++X.Event._LAST_EVENT;
\r
185 X.Dom.Event._LAST_EVENT = X.Event._LAST_EVENT; // ここに書いてあるイベントの最後の値 X.Dom.Event.ANIME_STOP と同じ値
\r
187 // TODO load -> readystatechange this.readyState === "loaded" || this.readyState === "complete"
\r
188 X.Dom.Event._LOAD_FIX_TAGS = {
\r
194 X.Dom.Event.Rename = {};
\r
195 X.Dom.Event.RenameTo = {};
\r
196 // https://github.com/georgeadamson/jQuery.prefixfree-events/blob/master/jQuery.prefixfree-events.js
\r
199 if( X.UA.Gecko && window.MouseScrollEvent ){
\r
200 if( 2 <= X.UA.Gecko || ( 1.9 < X.UA.Gecko && 1 <= X.UA.GeckoPatch ) ){ // Gecko 1.9.1+ (firefox3.5+)
\r
201 console.log( 'mousewheel <= MozMousePixelScroll' );
\r
202 X.Dom.Event.Rename[ 'mousewheel' ] = 'MozMousePixelScroll';
\r
204 if( 0.9 < X.UA.Gecko && 7 <= X.UA.GeckoPatch ){ // Gecko 0.9.7+ (NN6.2+?)
\r
205 console.log( 'mousewheel <= DOMMouseScroll' );
\r
206 X.Dom.Event.Rename[ 'mousewheel' ] = 'DOMMouseScroll';
\r
210 if( window.onwebkitanimationend !== undefined && window.onanimationend === undefined ){
\r
211 console.log( 'animationend <= webkitAnimationEnd' );
\r
212 X.Dom.Event.Rename[ 'animationend' ] = 'webkitAnimationEnd';
\r
213 X.Dom.Event.Rename[ 'animationstart' ] = 'webkitAnimationStart';
\r
214 X.Dom.Event.Rename[ 'animationiteration' ] = 'webkitAnimationIteration';
\r
216 if( window.onoanimationend !== undefined && window.onanimationend === undefined ){
\r
217 console.log( 'animationend <= oAnimationEnd' );
\r
218 X.Dom.Event.Rename[ 'animationend' ] = 'oAnimationEnd';
\r
219 X.Dom.Event.Rename[ 'animationstart' ] = 'oAnimationStart';
\r
220 X.Dom.Event.Rename[ 'animationiteration' ] = 'oAnimationIteration';
\r
223 if( window.onmozanimationend !== undefined && window.onanimationend === undefined ){
\r
224 X.Dom.Event.Rename[ 'animationend' ] = 'mozAnimationEnd';
\r
225 X.Dom.Event.RenameTo[ 'mozAnimationEnd' ] = 'animationend';
\r
226 X.Dom.Event.Rename[ 'animationstart' ] = 'mozAnimationStart';
\r
227 X.Dom.Event.RenameTo[ 'mozAnimationStart' ] = 'animationstart';
\r
228 X.Dom.Event.Rename[ 'animationiteration' ] = 'mozAnimationIteration';
\r
229 X.Dom.Event.RenameTo[ 'mozAnimationIteration' ] = 'animationiteration';
\r
231 if( document.documentElement && document.documentElement.style.msAnimation !== undefined && document.documentElement.style.animation === undefined ){ //document.documentElement.style.msAnimation
\r
232 console.log( 'animationend <= MSAnimationEnd' );
\r
233 X.Dom.Event.Rename[ 'animationend' ] = 'MSAnimationEnd';
\r
234 X.Dom.Event.Rename[ 'animationstart' ] = 'MSAnimationStart';
\r
235 X.Dom.Event.Rename[ 'animationiteration' ] = 'MSAnimationIteration';
\r
237 // https://developer.mozilla.org/en-US/docs/Web/Events/transitionend
\r
238 // chrome1+, firefox4+, IE10+, Opera10.5+, Safari3.2+, Android2.1+
\r
239 if( window.onwebkittransitionend !== undefined && window.ontransitionend === undefined ){
\r
240 console.log( 'transitionend <= webkitTransitionEnd' );
\r
241 X.Dom.Event.Rename[ 'transitionend' ] = 'webkitTransitionEnd';
\r
243 if( window.onotransitionend !== undefined && window.ontransitionend === undefined ){
\r
244 if( X.UA.Opera < 12 ){
\r
245 console.log( 'transitionend <= oTransitionEnd|ver.' + X.UA.Opera );
\r
246 X.Dom.Event.Rename[ 'transitionend' ] = 'oTransitionEnd';
\r
248 console.log( 'transitionend <= otransitionEnd|ver.' + X.UA.Opera );
\r
249 X.Dom.Event.Rename[ 'transitionend' ] = 'otransitionEnd';
\r
252 if( window.onmoztransitionend !== undefined && window.ontransitionend === undefined ){
\r
253 console.log( 'transitionend <= mozTransitionEnd' );
\r
254 X.Dom.Event.Rename[ 'transitionend' ] = 'mozTransitionEnd';
\r
258 if( navigator.msPointerEnabled && !navigator.pointerEnabled ){
\r
259 console.log( 'pointerdown <= MSPointerDown' );
\r
260 X.Dom.Event.Rename[ 'pointerdown' ] = 'MSPointerDown';
\r
261 X.Dom.Event.Rename[ 'pointerup' ] = 'MSPointerUp';
\r
262 X.Dom.Event.Rename[ 'pointermove' ] = 'MSPointerMove';
\r
263 X.Dom.Event.Rename[ 'pointercancel' ] = 'MSPointerCancel';
\r
266 (function( rename, renameTo ){
\r
267 for( var k in rename ){
\r
268 renameTo[ rename[ k ] ] = k;
\r
270 })( X.Dom.Event.Rename, X.Dom.Event.RenameTo );
\r
274 X.Dom.Node.prototype.listen = function( type, arg1, arg2, arg3 /* [ listener || ( context + function ) || function ][ arguments ] */ ){
\r
275 if( this._xnodeType === 0 || this._xnodeType === 3 ) return this;
\r
277 if( X.Type.isArray( type ) ){
\r
278 return X.EventDispatcher.prototype.listen.apply( this, arguments );
\r
281 ( !this._listeners || !this._listeners[ type ] ) && X.Type.isString( type ) && this._addEvent( type );
\r
283 return X.EventDispatcher.prototype.listen.apply( this, arguments );
\r
286 X.Dom.Node.prototype._addEvent =
\r
287 // Days on the Moon DOM Events とブラウザの実装
\r
288 // http://nanto.asablo.jp/blog/2007/03/23/1339502
\r
289 // Safari 2 では関数オブジェクトしか EventListener として使えませんが、Safari のナイトリービルドでは handleEvent メソッドを持つオブジェクトも EventListener として使えるようです。
\r
290 X.Dom.EVENT_W3C && ( X.UA.WebKit < 525.13 || X.UA.Opera7 || X.UA.NetFront < 4 ) ? // Safari3-
\r
292 var raw = this._rawNode;
\r
294 this._handleEvent = this._handleEvent || X.Callback.create( this );
\r
295 if( raw.addEventListener ){
\r
296 raw.addEventListener( type, this._handleEvent, false );
\r
298 // Safari は Image, Opera7 は window
\r
299 raw[ 'on' + type ] = this._handleEvent;
\r
304 this._rawNode && this._rawNode.addEventListener( X.Dom.Event.Rename[ type ] || type, this, false );
\r
308 var raw = this._rawNode;
\r
310 //if( type === 'load' && this._tag && X.Dom.Event._LOAD_FIX_TAGS[ this._tag ] ){
\r
311 // type = 'readystatechange';
\r
313 this._handleEvent = this._handleEvent || X.Callback.create( this );
\r
314 if( raw.attachEvent ){
\r
315 raw.attachEvent( 'on' + type, this._handleEvent );
\r
317 raw[ 'on' + type ] = this._handleEvent;
\r
321 var raw = this._rawNode || ( this._ie4getRawNode && this._ie4getRawNode() );
\r
323 raw[ 'on' + type ] = this._handleEvent = this._handleEvent || X.Callback.create( this );
\r
327 X.Dom.Node.prototype.unlisten = function( type, arg1, arg2, arg3 ){
\r
328 var list = this._listeners, l;
\r
330 if( X.Type.isArray( type ) ){
\r
331 return list ? X.EventDispatcher.prototype.unlisten.apply( this, arguments ) : this;
\r
334 l = !this._dispatching && list && type !== undefined && list[ type ] && list[ type ].length;
\r
336 X.EventDispatcher.prototype.unlisten.apply( this, arguments );
\r
338 l && !list[ type ] && X.Type.isString( type ) && this._removeEvent( type );
\r
343 X.Dom.Node.prototype._removeEvent =
\r
344 X.Dom.EVENT_W3C && ( X.UA.WebKit < 525.13 || X.UA.Opera7 || X.UA.NetFront < 4 ) ? // Safari3-
\r
346 var raw = this._rawNode;
\r
349 if( raw.addEventListener ){ // Image
\r
350 raw.removeEventListener( type, this._handleEvent, false );
\r
352 raw[ 'on' + type ] = null;
\r
354 if( !this._listeners ){
\r
355 X.Callback._correct( this._handleEvent );
\r
356 delete this._handleEvent;
\r
361 var raw = this._rawNode;
\r
363 raw.removeEventListener( X.Dom.Event.Rename[ type ] || type, this, false );
\r
367 var raw = this._rawNode;
\r
369 //if( type === 'load' && this._tag && X.Dom.Event._LOAD_FIX_TAGS[ this._tag ] ){
\r
370 // type = 'readystatechange';
\r
372 if( raw.attachEvent ){
\r
373 raw.detachEvent( 'on' + type, this._handleEvent );
\r
375 raw[ 'on' + type ] = X.emptyFunction;
\r
376 raw[ 'on' + type ] = '';
\r
378 if( !this._listeners ){
\r
379 X.Callback._correct( this._handleEvent );
\r
380 delete this._handleEvent;
\r
384 var raw = this._rawNode || ( this._ie4getRawNode && this._ie4getRawNode() );
\r
386 raw[ 'on' + type ] = X.emptyFunction;
\r
387 raw[ 'on' + type ] = '';
\r
388 if( !this._listeners ){
\r
389 X.Callback._correct( this._handleEvent );
\r
390 delete this._handleEvent;
\r
394 // Is this in regard to the Safari 1.x preventDefault bug on click/dblclick?
\r
395 // https://groups.google.com/forum/#!msg/comp.lang.javascript/uYEuCHjHxnw/yKoHtZJPa1QJ
\r
396 X.Dom.Node.prototype.handleEvent =
\r
397 X.UA.IE4 || X.UA.IE5678 ? // ie45678 EVENT_IE & EVENT_DOM0 for ie4
\r
401 if( event.type === 'readystatechange' && this._tag && X.Dom.Event._LOAD_FIX_TAGS[ this._tag ] ){
\r
402 //type = 'readystatechange';
\r
405 ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( event, this, this._rawNode ) );
\r
407 if( ret & X.Callback.STOP_PROPAGATION ){
\r
408 event.cancelBubble = true;
\r
410 if( ret & X.Callback.PREVENT_DEFAULT ){
\r
411 this._tag === 'A' && this._rawNode.blur();
\r
412 return event.returnValue = false;
\r
415 //X.Dom.EVENT_W3C & EVENT_DOM0
\r
417 var ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( e, this ) );
\r
419 if( ret & X.Callback.STOP_PROPAGATION ){
\r
420 e.stopPropagation();
\r
422 if( ret & X.Callback.PREVENT_DEFAULT ){
\r
423 this._tag === 'A' && this._rawNode.blur();
\r
424 e.preventDefault();
\r
425 if( X.UA.WebKit < 525.13 ){ // Safari3-
\r
426 if( e.type === 'click' || e.type === 'dbclick' ){
\r
427 X.Dom._safariPreventDefault = true;
\r
435 // イベントの退避、dom が画面から抜かれる場合に実施しておく
\r
436 X.Dom.Node.prototype._migrateEvent = function(){
\r
437 var hash = this._listeners,
\r
439 if( !hash ) return;
\r
440 for( type in hash ){
\r
442 /*'' + parseFloat( type ) !== type && */this._removeEvent( type );
\r
447 X.Dom.Node.prototype._restoreEvent = function(){
\r
448 var hash = this._listeners,
\r
450 if( !hash ) return;
\r
451 for( type in hash ){
\r
453 /*'' + parseFloat( type ) !== type && */ this._addEvent( type );
\r
457 /* -----------------------------------------------
\r
459 * Dean Edwards/Matthias Miller/John Resig
\r
462 // SafariでJavaScriptのデバッグをする方法
\r
464 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
\r
466 /* for ie9+/Mozilla/Opera9 */
\r
467 if( X.Dom.EVENT_W3C ){
\r
468 Node._document.listenOnce( 'DOMContentLoaded', X.Dom._init );
\r
470 if( 6 <= X.UA.IE && X.inHead ){
\r
471 // if this script in Head
\r
472 document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
\r
473 X.Dom._script = document.getElementById( "__ie_onload" );
\r
474 X.Dom._script.onreadystatechange = function(){
\r
475 var s = X.Dom._script;
\r
476 if( s && s.readyState === 'complete' ){
\r
477 s.onreadystatechange = X.emptyFunction;
\r
478 s.onreadystatechange = null;
\r
479 s.parentNode.removeChild( s );
\r
480 delete X.Dom._script;
\r
481 X.Dom._init && X.Dom._init();
\r
485 // Re: onLoad doesn't work with Safari?
\r
486 // http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html
\r
487 if( X.UA.WebKit || ( X.UA.Safari && X.UA.Safari < 3 ) ){ // sniff
\r
488 X.Timer.add( 10, function(){
\r
489 if( !X.Dom._init ) return X.Callback.UN_LISTEN;
\r
490 if( 'loaded|complete'.indexOf( document.readyState ) !== -1 ) return X.Dom._init();
\r
494 /* for other browsers */
\r
495 Node._window.listenOnce( 'load', X.Dom._init );
\r
497 X.Dom.listenOnce( X.Dom.Event.XDOM_READY, function(e){
\r
498 console.log( 'X.Dom XDomReady ' + X.Dom.readyState );
\r
501 if( X.UA.WebKit < 525.13 ){ // Safari3-
\r
502 document.documentElement.onclick =
\r
503 document.documentElement.ondbclick = function( e ){
\r
504 if( X.Dom._safariPreventDefault ){
\r
505 X.Dom._safariPreventDefault = false;
\r
506 e.preventDefault();
\r
512 X.Dom.listen( X.Dom.Event.VIEW_RESIZED, function(e){ console.log( 'X.Dom VIEW_RESIZED ' + e.w + 'x' + e.h ); } );
\r