4 * http://d.hatena.ne.jp/uupaa/20100430/1272561922
\r
5 * event.offsetX と offsetY の互換性について
\r
7 * http://www.keynavi.net/ja/tipsj/gokan.html
\r
8 * Safari : マウスイベントは大丈夫ですが ページ全体に対するキーイベントをきちんと扱えない問題があります。
\r
9 * その他、リンクに対するフォーカス(element.focus())の動作がおかしい、 スクリーン上のマウス位置(event.screenX/Y)をブラウザ画面の 左下隅から計算する などの問題があり修正が待たれます。
\r
13 // http://msdn.microsoft.com/ja-jp/library/ie/dn255104%28v=vs.85%29.aspx
\r
14 var X_Dom_Event_devicePixelRatio = window.devicePixelRatio || ( window.screen.deviceXDPI / window.screen.logicalXDPI ),
\r
15 X_Dom_Event_convertMSPointerType = window.MSPointerEvent && [ 0, 0, 'touch', 'pen', 'mouse' ],/*{
\r
20 X_Dom_Event_CANCEL_MOUSE = {},
\r
23 if( !X_UA[ 'IE' ] || 9 <= X_UA[ 'IE' ] ){
\r
24 X_DomEvent = function( e, xnode ){
\r
25 var originalType = e.type,
\r
26 type, pointerEventType,
\r
28 altKey, ctrlKey, metaKey, shiftKey, target, related, force,
\r
29 elm, i, n, time, touch, ev;
\r
32 this[ 'type' ] = type = X_Event_RenameTo[ originalType ] || originalType;
\r
36 this[ 'data' ] = e.data;
\r
37 this[ 'origin' ] = e.origin;
\r
38 this[ 'source' ] = e.source;
\r
41 this[ 'lengthComputable' ] = e.lengthComputable;
\r
42 this[ 'loaded' ] = e.loaded;
\r
43 this[ 'total' ] = e.total;
\r
51 this[ 'dataTransfer' ] = e.dataTransfer;
\r
55 //console.log( 'original : ' + originalType + ' > ' + type );
\r
56 // http://msdn.microsoft.com/ja-jp/library/ie/dn304886%28v=vs.85%29.aspx
\r
58 if( e.pointerType ){
\r
60 if( X_Dom_Event_convertMSPointerType ){
\r
61 this[ 'pointerType' ] = X_Dom_Event_convertMSPointerType[ e.pointerType ];
\r
62 this[ 'pressure' ] = e.pressure || ( e.button !== -1 ? 0.5 : 0 );
\r
63 // ポインターの接触形状の スクリーン ピクセル単位の幅と高さ なので変換。(多分、、、)
\r
64 this[ 'width' ] = e.width / X_Dom_Event_devicePixelRatio;
\r
65 this[ 'height' ] = e.height / X_Dom_Event_devicePixelRatio;
\r
67 this[ 'pointerType' ] = e.pointerType;
\r
68 this[ 'pressure' ] = e.pressure;
\r
69 // ポインターの接触形状の CSS ピクセル単位の幅と高さ。
\r
70 this[ 'width' ] = e.width;
\r
71 this[ 'height' ] = e.height;
\r
74 switch( this[ 'pointerType' ] ){
\r
76 //Y-Z 平面と、ペンの軸が含まれる平面の間の角度を返します。Y 軸の範囲は -90 ~ +90 です。X の傾きの正の方向は右方向です。
\r
77 this[ 'tiltX' ] = e.tiltX;
\r
78 this[ 'tiltY' ] = e.tiltY;
\r
79 if( originalType === 'MSPointerHover' ){
\r
80 this[ 'type' ] = 'pointermove'; // ie10 には pointerhover と pointermoveがあり、ie11 で一本化。ie11 では buttons を見て hover 状態を判定
\r
83 this[ 'radiusX' ] = e.radiusX;
\r
84 this[ 'radiusY' ] = e.radiusY;
\r
85 this[ 'rotationAngle' ] = e.rotationAngle;
\r
89 this[ 'button' ] = e.button;
\r
90 this[ 'buttons' ] = e.buttons;
\r
92 this[ 'pointerId' ] = e.pointerId;
\r
93 this[ 'target' ] = X_Node_getXNode( e.target );
\r
94 this[ 'relatedTarget' ] = X_Node_getXNode( e.relatedTarget ); // xnode
\r
95 this[ 'isPrimary' ] = e.isPrimary;
\r
96 this[ 'hwTimestamp' ] = e.hwTimestamp;
\r
97 this[ 'timestamp' ] = e.timestamp;
\r
99 this[ 'altKey' ] = e.altKey;
\r
100 this[ 'ctrlKey' ] = e.ctrlKey;
\r
101 this[ 'metaKey' ] = e.metaKey;
\r
102 this[ 'shiftKey' ] = e.shiftKey;
\r
103 //this[ 'screenX' ] = touch.screenX;
\r
104 //this[ 'screenY' ] = touch.screenY;
\r
105 this[ 'clientX' ] = e.clientX;
\r
106 this[ 'clientY' ] = e.clientY;
\r
107 this[ 'pageX' ] = e.pageX;
\r
108 this[ 'pageY' ] = e.pageY;
\r
109 this[ 'offsetX' ] = e.offsetX;
\r
110 this[ 'offsetY' ] = e.offsetY;
\r
112 if( pointerEventType = X_Event_toPointer[ originalType ] ){
\r
114 //console.log( originalType + ' => ' + pointerEventType );
\r
116 /* e.constructor === window.TouchEvent -> e.touches for iOS3.13 */
\r
117 if( touches = e.changedTouches ){
\r
118 //console.log( originalType + ' => ' + pointerEventType );
\r
119 if( touches.length === 0 ){
\r
120 alert( 'e.changedTouches.length === 0' );
\r
122 X_Dom_Event_CANCEL_MOUSE[ xnode[ '_uid' ] ] = pointerEventType;
\r
126 ctrlKey = e.ctrlKey;
\r
127 metaKey = e.metaKey;
\r
128 shiftKey = e.shiftKey;
\r
129 time = X_Timer_now();
\r
130 force = originalType === 'touchend' || originalType === 'touchcancel' ? 0 : 0.5;
\r
131 for( i = touches.length; i; ){
\r
132 touch = touches[ --i ];
\r
133 target = touch.target;
\r
134 related = touch.relatedTarget;
\r
136 'type' : pointerEventType,
\r
137 'pointerType' : 'touch',
\r
138 'target' : X_Node_getXNode( target.nodeType === 3 ? target.parentNode : target ),// defeat Safari bug // xnodetouch.target,
\r
139 'currentTarget' : xnode,
\r
140 'relatedTarget' : related && X_Node_getXNode( related.nodeType === 3 ? related.parentNode : related ), // xnode iOS3 には relatedTarget がない
\r
141 'isPrimary' : true,
\r
142 'hwTimestamp' : time,
\r
143 'timestamp' : time,
\r
144 'button' : /*e.button ||*/ ( force ? 0 : -1 ),
\r
145 'buttons' : /*e.buttons || e.button ||*/ ( force ? 1 : 0 ),
\r
147 'ctrlKey' : ctrlKey,
\r
148 'metaKey' : metaKey,
\r
149 'shiftKey' : shiftKey,
\r
150 'pointerId' : touch.identifier + 2, // iOS4 は 変換が必要!
\r
151 //screenX : touch.screenX,
\r
152 //screenY : touch.screenY,
\r
153 'clientX' : touch.clientX || ( touch.pageX - X_ViewPort_scrollX ), // iOS4以下は clientX が undefined, コードでは入れ子のスクロールに対応できない
\r
154 'clientY' : touch.clientY || ( touch.pageY - X_ViewPort_scrollY ),
\r
155 'pageX' : touch.pageX,
\r
156 'pageY' : touch.pageY,
\r
157 'offsetX' : touch.offsetX, // 要素上の座標を取得
\r
158 'offsetY' : touch.offsetY,
\r
159 'radiusX' : touch.radiusX || 0,
\r
160 'radiusY' : touch.radiusY || 0,
\r
161 'rotationAngle' : touch.rotationAngle || 0,
\r
162 'pressure' : touch.force || touch.webkitForce || force,
\r
163 'width' : touch.width || 0,
\r
164 'height' : touch.height || 0
\r
166 console.log( 'e.pointerId = ' + touch.identifier );
\r
168 return events.length === 1 ? events[ 0 ] : events;
\r
171 if( X_Dom_Event_CANCEL_MOUSE[ xnode[ '_uid' ] ] === pointerEventType ){
\r
172 delete X_Dom_Event_CANCEL_MOUSE[ xnode[ '_uid' ] ];
\r
177 this[ 'type' ] = pointerEventType;
\r
178 this[ 'pointerType' ] = 'mouse';
\r
180 this[ 'button' ] = e.button !== undefined ? e.button :
\r
181 e.which !== undefined ? e.which - 1 : -1;
\r
182 this[ 'buttons' ] = e.buttons !== undefined ? e.buttons : this[ 'button' ] === 0 ? 1 : this[ 'button' ] === 1 ? 2 : this[ 'button' ] === 2 ? 4 : 0;
\r
183 this[ 'pressure' ] = ( this[ 'button' ] !== -1 ? 0.5 : 0 );
\r
186 this[ 'target' ] = X_Node_getXNode( elm.nodeType === 3 ? elm.parentNode : elm );// defeat Safari bug // xnodetouch.target;
\r
187 this[ 'isPrimary' ] = true;
\r
188 this[ 'hwTimestamp' ] = this[ 'timestamp' ] = X_Timer_now();
\r
189 this[ 'altKey' ] = e.altKey;
\r
190 this[ 'ctrlKey' ] = e.ctrlKey;
\r
191 this[ 'metaKey' ] = e.metaKey;
\r
192 this[ 'shiftKey' ] = e.shiftKey;
\r
193 this[ 'pointerId' ] = 1;
\r
194 //this[ 'screenX' ] = touch.screenX;
\r
195 //this[ 'screenY' ] = touch.screenY;
\r
196 // TODO http://uupaa-js.googlecode.com/svn-history/r8/trunk/doc/reference/symbols/src/trunk_uu.module.ui.js.html
\r
197 // Safari2ではclientX,YはpageX,Yと同じ値を返す
\r
198 this[ 'clientX' ] = e.clientX;
\r
199 this[ 'clientY' ] = e.clientY;
\r
200 this[ 'pageX' ] = e.pageX;
\r
201 this[ 'pageY' ] = e.pageY;
\r
202 this[ 'offsetX' ] = e.offsetX || e.layerX; // 要素上の座標を取得
\r
203 this[ 'offsetY' ] = e.offsetY || e.layerY;
\r
205 // http://www.programming-magic.com/20090127231544/
\r
206 // Opera で button==2の場合、コンテキストメニューイベントを発火 「ツール」->「設定」->「詳細設定」->「コンテンツ」->「Javascriptオプション」で「右クリックを制御するスクリプトを許可する」
\r
207 if( originalType === 'mousedown' && this[ 'button' ] === 2 && X_UA[ 'Opera' ] ){
\r
208 events = [ X_Object_clone( this ), X_Object_clone( this ) ];
\r
209 events[ 1 ].type = 'contextmenu';
\r
216 this[ 'keyCode' ] = e.keyCode || e.which;
\r
217 this[ 'altKey' ] = e.altKey;
\r
218 this[ 'ctrlKey' ] = e.ctrlKey;
\r
219 this[ 'shiftKey' ] = e.shiftKey;
\r
220 this[ 'metaKey' ] = e.metaKey;
\r
222 this[ 'button' ] = e.button !== undefined ? e.button :
\r
223 e.which !== undefined ? e.which - 1 : -1;
\r
224 this[ 'buttons' ] = e.buttons !== undefined ? e.buttons : this[ 'button' ] === 0 ? 1 : this[ 'button' ] === 1 ? 2 : this[ 'button' ] === 2 ? 4 : 0;
\r
226 //http://www.quirksmode.org/js/events_properties.html
\r
227 if( elm = e.target ){
\r
228 this[ 'target' ] = X_Node_getXNode( elm.nodeType === 3 ? elm.parentNode : elm );// defeat Safari bug // xnode
\r
230 if( elm = e.relatedTarget ){
\r
231 this[ 'relatedTarget' ] = X_Node_getXNode( elm.nodeType === 3 ? elm.parentNode : elm ); // xnode
\r
234 if( type === 'wheel' ){
\r
235 // https://developer.mozilla.org/ja/docs/DOM/DOM_event_reference/mousewheel
\r
237 // https://w3g.jp/blog/tools/wheelevent_crossbrowser
\r
238 // ホイール系イベント2014年版クロスブラウザ
\r
239 if( e.deltaY !== undefined ){
\r
240 this[ 'deltaX' ] = e.deltaX;
\r
241 this[ 'deltaY' ] = e.deltaY;
\r
242 this[ 'deltaZ' ] = e.deltaZ || 0;
\r
244 if( e.wheelDeltaY !== undefined ){
\r
245 this[ 'deltaX' ] = e.wheelDeltaX / 120;
\r
246 this[ 'deltaY' ] = e.wheelDeltaY / 120;
\r
247 this[ 'deltaZ' ] = e.wheelDeltaZ / 120 || 0;
\r
249 if( e.wheelDelta !== undefined ){
\r
250 this[ 'deltaX' ] = this[ 'deltaZ' ] = 0;
\r
251 this[ 'deltaY' ] = e.wheelDelta / -120;
\r
253 if( e.detail !== undefined ){
\r
254 this[ 'deltaX' ] = this[ 'deltaZ' ] = 0;
\r
255 this[ 'deltaY' ] = originalType === 'MozMousePixelScroll' ? e.detail / 45 : e.detail / 3; // 3
\r
257 this[ 'deltaX' ] = this[ 'deltaY' ] = this[ 'deltaZ' ] = 0;
\r
262 this[ 'currentTarget' ] = xnode; // xnode
\r
263 this[ 'eventPhase' ] = e.eventPhase;
\r
264 this[ 'detail' ] = e.detail;
\r
267 X_DomEvent = function( e, xnode, element ){
\r
268 var originalType = e.type, btn, type;
\r
270 this[ 'type' ] = X_Event_RenameTo[ originalType ] || originalType;
\r
271 this[ 'target' ] = X_Node_getXNode( e.srcElement ); // xnode
\r
272 if( this[ 'target' ] && !this[ 'target' ][ '_tag' ] ) this[ 'target' ] = this[ 'target' ].parent; // ie4 の fake Textnode がヒットしていないか?
\r
273 this[ 'currentTarget' ] = xnode; // xnode
\r
274 this[ 'relatedTarget' ] = X_Node_getXNode( e.formElement || e.toElement ); // xnode
\r
275 this[ 'eventPhase' ] = e.srcElement === element ? 2: 3;
\r
277 this[ 'keyCode' ] = e.keyCode;
\r
278 this[ 'altKey' ] = e.altKey;
\r
279 this[ 'ctrlKey' ] = e.ctrlKey;
\r
280 this[ 'shiftKey' ] = e.shiftKey;
\r
283 switch( this[ 'type' ] ){
\r
285 this[ 'data' ] = e.data;
\r
286 this[ 'origin' ] = e.origin;
\r
287 this[ 'source' ] = e.source;
\r
290 this[ 'loaded' ] = e.loaded;
\r
291 this[ 'total' ] = e.total;
\r
295 // http://www.programming-magic.com/20090127231544/
\r
296 switch( originalType ){
\r
299 this[ 'button' ] = 0;
\r
301 case 'contextmenu' :
\r
302 this[ 'button' ] = 2;
\r
305 // mouseup, mousedown
\r
310 btn & 2 ? 1 : -1; // 左:1(click:0), 中:4, 右:2
\r
313 this[ 'buttons' ] = e.button;
\r
315 this[ 'deltaX' ] = 0;
\r
316 this[ 'deltaY' ] = e.wheelDelta / -120;
\r
318 if( type = X_Event_toPointer[ originalType ] ){
\r
319 this[ 'type' ] = type;
\r
320 this[ 'clientX' ] = e.clientX;
\r
321 this[ 'clientY' ] = e.clientY;
\r
322 //this[ 'screenX' ] = e.screenX;
\r
323 //this[ 'screenY' ] = e.screenY;
\r
325 //if( X_ViewPort_rootElement ){ // uuu...
\r
326 this[ 'pageX' ] = e.clientX + X_ViewPort_rootElement.scrollLeft;
\r
327 this[ 'pageY' ] = e.clientY + X_ViewPort_rootElement.scrollTop;
\r
328 // DOMAssistant 2.8.1
\r
329 //event.pageX = DOMAssistant.def(e.pageX)? e.pageX : (event.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0));
\r
330 //event.pageY = DOMAssistant.def(e.pageY)? e.pageY : (event.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0));
\r
333 if( 5 <= X_UA[ 'IE' ] ){
\r
334 this[ 'offsetX' ] = e.offsetX; // イベントターゲット左上からの座標
\r
335 this[ 'offsetY' ] = e.offsetY;
\r
337 //if( e.srcElement ){
\r
338 // this[ 'offsetX' ] = e.x - e.srcElement.offsetLeft; // e.x はイベント発生要素の親要素を基準にした座標。
\r
339 // this[ 'offsetY' ] = e.y - e.srcElement.offsetTop;
\r
342 this[ 'pointerId' ] = 1;
\r
343 this[ 'radiusX' ] = 0;
\r
344 this[ 'radiusY' ] = 0;
\r
345 this[ 'rotationAngle' ] = 0;
\r
346 this[ 'width' ] = 0;
\r
347 this[ 'height' ] = 0;
\r
348 this[ 'tiltX' ] = 0;
\r
349 this[ 'tiltY' ] = 0;
\r
354 // TODO load -> readystatechange this.readyState === "loaded" || this.readyState === "complete"
\r
356 // https://github.com/georgeadamson/jQuery.prefixfree-events/blob/master/jQuery.prefixfree-events.js
\r
357 // https://developer.mozilla.org/en-US/docs/Web/Events/wheel
\r
359 if( document.onwheel === undefined ){
\r
361 if( X_UA[ 'Gecko' ] && window.MouseScrollEvent ){
\r
362 if( 2 <= X_UA[ 'Gecko' ] || ( 1.9 <= X_UA[ 'Gecko' ] && 1 <= X_UA[ 'GeckoPatch' ] ) ){ // Gecko 1.9.1+ (firefox3.5+)
\r
363 console.log( 'wheel <= MozMousePixelScroll' );
\r
364 X_Event_Rename[ 'wheel' ] = 'MozMousePixelScroll';
\r
366 if( 1 <= X_UA[ 'Gecko' ] || ( 0.9 <= X_UA[ 'Gecko' ] && 7 <= X_UA[ 'GeckoPatch' ] ) ){ // Gecko 0.9.7+ (NN6.2+?)
\r
367 console.log( 'wheel <= DOMMouseScroll' );
\r
368 X_Event_Rename[ 'wheel' ] = 'DOMMouseScroll';
\r
371 X_Event_Rename[ 'wheel' ] = 'mousewheel';
\r
373 //if( document.onmousewheel !== undefined ){ // Opera で判定失敗する
\r
374 // X_Event_Rename[ 'wheel' ] = 'mousewheel';
\r
380 if( window.onwebkitanimationend !== undefined && window.onanimationend === undefined ){
\r
381 console.log( 'animationend <= webkitAnimationEnd' );
\r
382 X_Event_Rename[ 'animationend' ] = 'webkitAnimationEnd';
\r
383 X_Event_Rename[ 'animationstart' ] = 'webkitAnimationStart';
\r
384 X_Event_Rename[ 'animationiteration' ] = 'webkitAnimationIteration';
\r
386 if( window.onoanimationend !== undefined && window.onanimationend === undefined ){
\r
387 console.log( 'animationend <= oAnimationEnd' );
\r
388 X_Event_Rename[ 'animationend' ] = 'oAnimationEnd';
\r
389 X_Event_Rename[ 'animationstart' ] = 'oAnimationStart';
\r
390 X_Event_Rename[ 'animationiteration' ] = 'oAnimationIteration';
\r
393 if( window.onmozanimationend !== undefined && window.onanimationend === undefined ){
\r
394 X_Event_Rename[ 'animationend' ] = 'mozAnimationEnd';
\r
395 X_Event_RenameTo[ 'mozAnimationEnd' ] = 'animationend';
\r
396 X_Event_Rename[ 'animationstart' ] = 'mozAnimationStart';
\r
397 X_Event_RenameTo[ 'mozAnimationStart' ] = 'animationstart';
\r
398 X_Event_Rename[ 'animationiteration' ] = 'mozAnimationIteration';
\r
399 X_Event_RenameTo[ 'mozAnimationIteration' ] = 'animationiteration';
\r
401 if( document.documentElement && document.documentElement.style.msAnimation !== undefined && document.documentElement.style.animation === undefined ){ //document.documentElement.style.msAnimation
\r
402 console.log( 'animationend <= MSAnimationEnd' );
\r
403 X_Event_Rename[ 'animationend' ] = 'MSAnimationEnd';
\r
404 X_Event_Rename[ 'animationstart' ] = 'MSAnimationStart';
\r
405 X_Event_Rename[ 'animationiteration' ] = 'MSAnimationIteration';
\r
407 // https://developer.mozilla.org/en-US/docs/Web/Events/transitionend
\r
408 // chrome1+, firefox4+, IE10+, Opera10.5+, Safari3.2+, Android2.1+
\r
409 if( window.onwebkittransitionend !== undefined && window.ontransitionend === undefined ){
\r
410 console.log( 'transitionend <= webkitTransitionEnd' );
\r
411 X_Event_Rename[ 'transitionend' ] = 'webkitTransitionEnd';
\r
413 if( window.onotransitionend !== undefined && window.ontransitionend === undefined ){
\r
414 if( X_UA[ 'Opera' ] < 12 ){
\r
415 console.log( 'transitionend <= oTransitionEnd|ver.' + X_UA[ 'Opera' ] );
\r
416 X_Event_Rename[ 'transitionend' ] = 'oTransitionEnd';
\r
418 console.log( 'transitionend <= otransitionEnd|ver.' + X_UA[ 'Opera' ] );
\r
419 X_Event_Rename[ 'transitionend' ] = 'otransitionEnd';
\r
422 if( window.onmoztransitionend !== undefined && window.ontransitionend === undefined ){
\r
423 console.log( 'transitionend <= mozTransitionEnd' );
\r
424 X_Event_Rename[ 'transitionend' ] = 'mozTransitionEnd';
\r
427 if( !navigator.pointerEnabled ){
\r
428 if( navigator.msPointerEnabled ){
\r
429 console.log( 'pointerdown <= MSPointerDown' );
\r
430 X_Event_Rename[ 'pointerdown' ] = 'MSPointerDown';
\r
431 X_Event_Rename[ 'pointerup' ] = 'MSPointerUp';
\r
432 X_Event_Rename[ 'pointermove' ] = [ 'MSPointerMove', 'MSPointerHover' ];// ie10 と ie11 でペンのhoverevent の値が異なる
\r
433 X_Event_Rename[ 'pointercancel' ] = 'MSPointerCancel';
\r
434 // http://msdn.microsoft.com/ja-jp/library/ie/dn304886%28v=vs.85%29.aspx
\r
436 if( X_UA_HID.TOUCH ){
\r
437 // touch のみ(iOS でも脱獄したら?)、 touch と mouse(Android), mouse のみ
\r
438 X_Event_Rename[ 'pointerdown' ] = [ 'touchstart', 'mousedown' ];
\r
439 X_Event_Rename[ 'pointerup' ] = [ 'touchend', 'mouseup' ];
\r
440 X_Event_Rename[ 'pointermove' ] = [ 'touchmove', 'mousemove' ];
\r
441 X_Event_Rename[ 'pointercancel' ] = 'touchcancel';
\r
442 // X_Event_Rename[ 'click' ] = [ 'touchstart', 'touchmove', 'touchend' ]; // ループになってしまう!直した!直ってない!
\r
444 X_Event_Rename[ 'pointerdown' ] = 'mousedown';
\r
445 X_Event_Rename[ 'pointerup' ] = 'mouseup';
\r
446 X_Event_Rename[ 'pointermove' ] = 'mousemove';
\r
447 X_Event_Rename[ 'pointercancel' ] = document.documentElement.onmouseleave !== undefined ? 'mouseleave' : 'mouseout';//??
\r
449 // Opera は ブラウザ設定から右クリックの通知を許可すると mousedown で e.button==2 が返る,キャンセルは可能??
\r
450 X_UA[ 'Opera' ] && ( X_Event_Rename[ 'contextmenu' ] = 'mousedown' );
\r
453 * buttons の無いブラウザには mouseup, mousedown を監視して、buttons フラグを更新し続ける
\r