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
41 this.button = e.button;
\r
43 // https://developer.mozilla.org/ja/docs/DOM/DOM_event_reference/mousewheel
\r
47 // https://w3g.jp/blog/tools/wheelevent_crossbrowser
\r
48 // ホイール系イベント2014年版クロスブラウザ
\r
49 if( e.deltaY !== undefined ){
\r
50 this.deltaX = e.deltaX;
\r
51 this.deltaY = e.deltaY;
\r
53 if( e.wheelDeltaY !== undefined ){
\r
54 this.deltaX = e.wheelDeltaX / 120;
\r
55 this.deltaY = e.wheelDeltaY / 120;
\r
57 if( e.wheelDelta !== undefined ){
\r
59 this.deltaY = e.wheelDelta / -120;
\r
61 if( e.detail !== undefined ){
\r
63 this.deltaY = e.type === 'MozMousePixelScroll' ? e.detail / 45 : e.detail / 3; // 3
\r
65 this.deltaX = this.deltaY = 0;
\r
68 if( /* e.constructor === window.TouchEvent || */ e.touches /* for iOS3.13 */ ){
\r
70 this.touches = e.touches;
\r
71 this.changedTouches = e.changedTouches;
\r
72 this.targetTouches = e.targetTouches;
\r
73 this.metaKey = e.metaKey;
\r
74 this.force = e.force || e.webkitForce || 0;
\r
76 if( this.touches.length ){
\r
77 touch0 = this.touches[ 0 ];
\r
78 this.clientX = touch0.clientX;
\r
79 this.clientY = touch0.clientY;
\r
80 this.pageX = touch0.pageX;
\r
81 this.pageY = touch0.pageY;
\r
82 this.offsetX = touch0.offsetX || touch0.layerX;
\r
83 this.offsetY = touch0.offsetY || touch0.layerY;
\r
86 if( e.constructor === window.PointerEvent ){
\r
88 this.currentPoint = e.currentPoint;
\r
89 this.width = e.width;
\r
90 this.height = e.height;
\r
91 this.timeStamp = e.timeStamp;
\r
92 this.hwTimestamp = e.hwTimestamp;
\r
93 this.intermediatePoints = e.intermediatePoints;
\r
94 this.isPrimary = e.isPrimary;
\r
95 this.pointerId = e.pointerId;
\r
96 this.pointerType = e.pointerType;
\r
97 this.pressure = e.pressure;
\r
98 this.tiltX = e.tiltX;
\r
99 this.tiltY = e.tiltY;
\r
102 if( !window.PointerEvent && window.MSPointerEvent ){
\r
103 window.PointerEvent = window.MSPointerEvent;
\r
106 X.Dom.Event = function( e, xnode, element ){
\r
110 this.type = X.Dom.Event.RenameTo[ e.type ] || e.type;
\r
111 this.target = Node._getXNode( e.srcElement ); // xnode
\r
112 if( this.target && this.target._xnodeType === 3 ) this.target = this.target.parent; // ie4 の fake Textnode がヒットしていないか?
\r
113 this.currentTarget = xnode; // xnode
\r
114 this.relatedTarget = Node._getXNode( e.formElement ? e.formElement : e.toElement ); // xnode
\r
115 this.eventPhase = e.srcElement === element ? 2: 3;
\r
117 this.clientX = e.clientX;
\r
118 this.clientY = e.clientY;
\r
119 //this.screenX = e.screenX;
\r
120 //this.screenY = e.screenY;
\r
122 if( X.Dom._root ){ // uuu...
\r
124 this.pageX = e.clientX + X.Dom._root.scrollLeft;
\r
125 this.pageY = e.clientY + X.Dom._root.scrollTop;
\r
127 // DOMAssistant 2.8.1
\r
128 //event.pageX = DOMAssistant.def(e.pageX)? e.pageX : (event.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0));
\r
129 //event.pageY = DOMAssistant.def(e.pageY)? e.pageY : (event.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0));
\r
134 if( 5 <= X.UA.IE ){
\r
135 this.offsetX = e.offsetX; // イベントターゲット左上からの座標
\r
136 this.offsetY = e.offsetY;
\r
138 //if( e.srcElement ){
\r
139 // this.offsetX = e.x - e.srcElement.offsetLeft; // e.x はイベント発生要素の親要素を基準にした座標。
\r
140 // this.offsetY = e.y - e.srcElement.offsetTop;
\r
143 this.keyCode = e.keyCode;
\r
144 this.altKey = e.altKey;
\r
145 this.ctrlKey = e.ctrlKey;
\r
146 this.shiftKey = e.shiftKey;
\r
148 // http://www.programming-magic.com/20090127231544/
\r
149 switch( this.type ){
\r
154 case 'contextmenu' :
\r
162 btn & 2 ? 3 : 0; // 左:1(click:0), 中:4, 右:2
\r
164 this.button = this.which - 1;
\r
166 this.deltaY = e.wheelDelta / -120;
\r
168 if( this.type === 'wheel' ) console.log( e.wheelDelta );
\r
172 X.Dom.Event.DOM_PRE_INIT = ++X.Event._LAST_EVENT;
\r
173 X.Dom.Event.DOM_BUILDER_COMPLETE= ++X.Event._LAST_EVENT;
\r
174 X.Dom.Event.DOM_INIT = ++X.Event._LAST_EVENT;
\r
175 X.Dom.Event.XDOM_READY = ++X.Event._LAST_EVENT;
\r
176 X.Dom.Event.VIEW_ACTIVATE = ++X.Event._LAST_EVENT;
\r
177 X.Dom.Event.VIEW_DEACTIVATE = ++X.Event._LAST_EVENT;
\r
178 X.Dom.Event.VIEW_RESIZED = ++X.Event._LAST_EVENT;
\r
179 X.Dom.Event.VIEW_TURNED = ++X.Event._LAST_EVENT;
\r
180 X.Dom.Event.BASE_FONT_RESIZED = ++X.Event._LAST_EVENT;
\r
182 // on_screen_keyboard_show
\r
183 // on_screen_keyboard_hide
\r
184 // X.Dom.Event.BEFORE_UPDATE = ++X.Event._LAST_EVENT; // このイベントで要素のサイズを取得すると無限ループに!
\r
185 X.Dom.Event.AFTER_UPDATE = ++X.Event._LAST_EVENT;
\r
187 X.Dom.Event.BEFORE_UNLOAD = ++X.Event._LAST_EVENT;
\r
188 X.Dom.Event.UNLOAD = ++X.Event._LAST_EVENT;
\r
190 X.Dom.Event.ANIME_BEFORE_START = ++X.Event._LAST_EVENT;
\r
191 X.Dom.Event.ANIME_START = ++X.Event._LAST_EVENT;
\r
192 X.Dom.Event.ANIME = ++X.Event._LAST_EVENT;
\r
193 X.Dom.Event.ANIME_END = ++X.Event._LAST_EVENT;
\r
194 X.Dom.Event.ANIME_BEFORE_STOP = ++X.Event._LAST_EVENT; // xnode.stop() のみ、指定時間による停止では呼ばれない
\r
195 X.Dom.Event.ANIME_STOP = ++X.Event._LAST_EVENT;
\r
196 X.Dom.Event._LAST_EVENT = X.Event._LAST_EVENT; // ここに書いてあるイベントの最後の値 X.Dom.Event.ANIME_STOP と同じ値
\r
198 // TODO load -> readystatechange this.readyState === "loaded" || this.readyState === "complete"
\r
199 X.Dom.Event._LOAD_FIX_TAGS = {
\r
205 X.Dom.Event.Rename = {};
\r
206 X.Dom.Event.RenameTo = {};
\r
207 // https://github.com/georgeadamson/jQuery.prefixfree-events/blob/master/jQuery.prefixfree-events.js
\r
208 // https://developer.mozilla.org/en-US/docs/Web/Events/wheel
\r
210 if( document.onwheel === undefined ){
\r
212 if( X.UA.Gecko && window.MouseScrollEvent ){
\r
213 if( 2 <= X.UA.Gecko || ( 1.9 < X.UA.Gecko && 1 <= X.UA.GeckoPatch ) ){ // Gecko 1.9.1+ (firefox3.5+)
\r
214 console.log( 'wheel <= MozMousePixelScroll' );
\r
215 X.Dom.Event.Rename[ 'wheel' ] = 'MozMousePixelScroll';
\r
217 if( 0.9 < X.UA.Gecko && 7 <= X.UA.GeckoPatch ){ // Gecko 0.9.7+ (NN6.2+?)
\r
218 console.log( 'wheel <= DOMMouseScroll' );
\r
219 X.Dom.Event.Rename[ 'wheel' ] = 'DOMMouseScroll';
\r
222 X.Dom.Event.Rename[ 'wheel' ] = 'mousewheel';
\r
224 if( document.onmousewheel !== undefined ){ // Opera で判定失敗
\r
225 X.Dom.Event.Rename[ 'wheel' ] = 'mousewheel';
\r
231 if( window.onwebkitanimationend !== undefined && window.onanimationend === undefined ){
\r
232 console.log( 'animationend <= webkitAnimationEnd' );
\r
233 X.Dom.Event.Rename[ 'animationend' ] = 'webkitAnimationEnd';
\r
234 X.Dom.Event.Rename[ 'animationstart' ] = 'webkitAnimationStart';
\r
235 X.Dom.Event.Rename[ 'animationiteration' ] = 'webkitAnimationIteration';
\r
237 if( window.onoanimationend !== undefined && window.onanimationend === undefined ){
\r
238 console.log( 'animationend <= oAnimationEnd' );
\r
239 X.Dom.Event.Rename[ 'animationend' ] = 'oAnimationEnd';
\r
240 X.Dom.Event.Rename[ 'animationstart' ] = 'oAnimationStart';
\r
241 X.Dom.Event.Rename[ 'animationiteration' ] = 'oAnimationIteration';
\r
244 if( window.onmozanimationend !== undefined && window.onanimationend === undefined ){
\r
245 X.Dom.Event.Rename[ 'animationend' ] = 'mozAnimationEnd';
\r
246 X.Dom.Event.RenameTo[ 'mozAnimationEnd' ] = 'animationend';
\r
247 X.Dom.Event.Rename[ 'animationstart' ] = 'mozAnimationStart';
\r
248 X.Dom.Event.RenameTo[ 'mozAnimationStart' ] = 'animationstart';
\r
249 X.Dom.Event.Rename[ 'animationiteration' ] = 'mozAnimationIteration';
\r
250 X.Dom.Event.RenameTo[ 'mozAnimationIteration' ] = 'animationiteration';
\r
252 if( document.documentElement && document.documentElement.style.msAnimation !== undefined && document.documentElement.style.animation === undefined ){ //document.documentElement.style.msAnimation
\r
253 console.log( 'animationend <= MSAnimationEnd' );
\r
254 X.Dom.Event.Rename[ 'animationend' ] = 'MSAnimationEnd';
\r
255 X.Dom.Event.Rename[ 'animationstart' ] = 'MSAnimationStart';
\r
256 X.Dom.Event.Rename[ 'animationiteration' ] = 'MSAnimationIteration';
\r
258 // https://developer.mozilla.org/en-US/docs/Web/Events/transitionend
\r
259 // chrome1+, firefox4+, IE10+, Opera10.5+, Safari3.2+, Android2.1+
\r
260 if( window.onwebkittransitionend !== undefined && window.ontransitionend === undefined ){
\r
261 console.log( 'transitionend <= webkitTransitionEnd' );
\r
262 X.Dom.Event.Rename[ 'transitionend' ] = 'webkitTransitionEnd';
\r
264 if( window.onotransitionend !== undefined && window.ontransitionend === undefined ){
\r
265 if( X.UA.Opera < 12 ){
\r
266 console.log( 'transitionend <= oTransitionEnd|ver.' + X.UA.Opera );
\r
267 X.Dom.Event.Rename[ 'transitionend' ] = 'oTransitionEnd';
\r
269 console.log( 'transitionend <= otransitionEnd|ver.' + X.UA.Opera );
\r
270 X.Dom.Event.Rename[ 'transitionend' ] = 'otransitionEnd';
\r
273 if( window.onmoztransitionend !== undefined && window.ontransitionend === undefined ){
\r
274 console.log( 'transitionend <= mozTransitionEnd' );
\r
275 X.Dom.Event.Rename[ 'transitionend' ] = 'mozTransitionEnd';
\r
279 if( navigator.msPointerEnabled && !navigator.pointerEnabled ){
\r
280 console.log( 'pointerdown <= MSPointerDown' );
\r
281 X.Dom.Event.Rename[ 'pointerdown' ] = 'MSPointerDown';
\r
282 X.Dom.Event.Rename[ 'pointerup' ] = 'MSPointerUp';
\r
283 X.Dom.Event.Rename[ 'pointermove' ] = 'MSPointerMove';
\r
284 X.Dom.Event.Rename[ 'pointercancel' ] = 'MSPointerCancel';
\r
287 (function( rename, renameTo ){
\r
288 for( var k in rename ){
\r
289 renameTo[ rename[ k ] ] = k;
\r
291 })( X.Dom.Event.Rename, X.Dom.Event.RenameTo );
\r
295 // TODO handleEvent を拡張可能にするために、クロージャに移動する
\r
296 // Is this in regard to the Safari 1.x preventDefault bug on click/dblclick?
\r
297 // https://groups.google.com/forum/#!msg/comp.lang.javascript/uYEuCHjHxnw/yKoHtZJPa1QJ
\r
298 X.Dom.Node.prototype.handleEvent =
\r
299 X.UA.IE4 || X.UA.IE5678 ? // ie45678 EVENT_IE & EVENT_DOM0 for ie4
\r
303 if( event.type === 'readystatechange' && this._tag && X.Dom.Event._LOAD_FIX_TAGS[ this._tag ] ){
\r
304 //type = 'readystatechange';
\r
307 ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( event, this, this._rawNode ) );
\r
309 if( ret & X.Callback.STOP_PROPAGATION ){
\r
310 event.cancelBubble = true;
\r
312 if( ret & X.Callback.PREVENT_DEFAULT ){
\r
313 this._tag === 'A' && this._rawNode.blur();
\r
314 return event.returnValue = false;
\r
317 //X.Dom.EVENT_W3C & EVENT_DOM0
\r
319 var ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( e, this ) );
\r
321 if( ret & X.Callback.STOP_PROPAGATION ){
\r
322 e.stopPropagation();
\r
324 if( ret & X.Callback.PREVENT_DEFAULT ){
\r
325 this._tag === 'A' && this._rawNode.blur();
\r
326 e.preventDefault();
\r
327 if( X.UA.WebKit < 525.13 ){ // Safari3-
\r
328 if( e.type === 'click' || e.type === 'dbclick' ){
\r
329 X.Dom._safariPreventDefault = true;
\r
337 // イベントの退避、dom が画面から抜かれる場合に実施しておく
\r
338 X.Dom.Node.prototype._migrateEvent = function(){
\r
339 var hash = this._listeners,
\r
341 if( !hash ) return;
\r
342 for( type in hash ){
\r
344 /*'' + parseFloat( type ) !== type && */x_eventdispatcher_actualRemoveEvent( this, type );
\r
349 X.Dom.Node.prototype._restoreEvent = function(){
\r
350 var hash = this._listeners,
\r
352 if( !hash ) return;
\r
353 for( type in hash ){
\r
355 /*'' + parseFloat( type ) !== type && */ x_eventdispatcher_actualAddEvent( this, type );
\r
359 /* -----------------------------------------------
\r
361 * Dean Edwards/Matthias Miller/John Resig
\r
364 // SafariでJavaScriptのデバッグをする方法
\r
366 // http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
\r
368 /* for ie9+/Mozilla/Opera9 */
\r
369 if( X.Dom.EVENT_W3C ){
\r
370 Node._document.listenOnce( 'DOMContentLoaded', X.Dom._init );
\r
372 if( 6 <= X.UA.IE && X.inHead ){
\r
373 // if this script in Head
\r
374 document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
\r
375 X.Dom._script = document.getElementById( "__ie_onload" );
\r
376 X.Dom._script.onreadystatechange = function(){
\r
377 var s = X.Dom._script;
\r
378 if( s && s.readyState === 'complete' ){
\r
379 s.onreadystatechange = X.emptyFunction;
\r
380 s.onreadystatechange = null;
\r
381 s.parentNode.removeChild( s );
\r
382 delete X.Dom._script;
\r
383 X.Dom._init && X.Dom._init();
\r
387 // Re: onLoad doesn't work with Safari?
\r
388 // http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html
\r
389 if( X.UA.WebKit || X.UA.Safari < 3 ){ // sniff
\r
390 X.Timer.add( 10, function(){
\r
391 if( !X.Dom._init ) return X.Callback.UN_LISTEN;
\r
392 if( 'loaded|complete'.indexOf( document.readyState ) !== -1 ) return X.Dom._init();
\r
396 /* for other browsers */
\r
397 Node._window.listenOnce( 'load', X.Dom._init );
\r
399 X.Dom.listenOnce( X.Dom.Event.XDOM_READY, function(e){
\r
400 console.log( 'X.Dom XDomReady ' + X.Dom.readyState );
\r
403 if( X.UA.WebKit < 525.13 ){ // Safari3-
\r
404 document.documentElement.onclick =
\r
405 document.documentElement.ondbclick = function( e ){
\r
406 if( X.Dom._safariPreventDefault ){
\r
407 X.Dom._safariPreventDefault = false;
\r
408 e.preventDefault();
\r
414 X.Dom.listen( X.Dom.Event.VIEW_RESIZED, function(e){ console.log( 'X.Dom VIEW_RESIZED ' + e.w + 'x' + e.h ); } );
\r