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
21 if( !X_UA.IE || 9 <= X_UA.IE ){
\r
22 X.Dom.Event = function( e, xnode ){
\r
23 var originalType = e.type,
\r
26 altKey, ctrlKey, metaKey, shiftKey, target, related,
\r
27 elm, i, n, time, touch, ev;
\r
30 this.type = type = X_Event_RenameTo[ originalType ] || originalType;
\r
32 console.log( 'original : ' + originalType + ' > ' + type );
\r
33 // http://msdn.microsoft.com/ja-jp/library/ie/dn304886%28v=vs.85%29.aspx
\r
35 if( e.pointerType ){
\r
37 if( X_Dom_Event_convertMSPointerType ){
\r
38 this.pointerType = X_Dom_Event_convertMSPointerType[ e.pointerType ];
\r
39 this.pressure = e.pressure || ( e.button !== -1 ? 0.5 : 0 );
\r
40 // ポインターの接触形状の スクリーン ピクセル単位の幅と高さ なので変換。(多分、、、)
\r
41 this.width = e.width / X_Dom_Event_devicePixelRatio;
\r
42 this.height = e.height / X_Dom_Event_devicePixelRatio;
\r
44 this.pointerType = e.pointerType;
\r
45 this.pressure = e.pressure;
\r
46 // ポインターの接触形状の CSS ピクセル単位の幅と高さ。
\r
47 this.width = e.width;
\r
48 this.height = e.height;
\r
51 switch( this.pointerType ){
\r
53 //Y-Z 平面と、ペンの軸が含まれる平面の間の角度を返します。Y 軸の範囲は -90 ~ +90 です。X の傾きの正の方向は右方向です。
\r
54 this.tiltX = e.tiltX;
\r
55 this.tiltY = e.tiltY;
\r
56 if( originalType === 'MSPointerHover' ){
\r
57 this.type = 'pointermove'; // ie10 には pointerhover と pointermoveがあり、ie11 で一本化。ie11 では buttons を見て hover 状態を判定
\r
60 this.radiusX = e.radiusX;
\r
61 this.radiusY = e.radiusY;
\r
62 this.rotationAngle = e.rotationAngle;
\r
66 this.button = e.button;
\r
67 this.buttons = e.buttons;
\r
69 this.pointerId = e.pointerId;
\r
70 this.target = X_Node_getXNode( e.target );
\r
71 this.relatedTarget = X_Node_getXNode( e.relatedTarget ); // xnode
\r
72 this.isPrimary = e.isPrimary;
\r
73 this.hwTimestamp = e.hwTimestamp;
\r
74 this.timestamp = e.timestamp;
\r
76 this.altKey = e.altKey;
\r
77 this.ctrlKey = e.ctrlKey;
\r
78 this.metaKey = e.metaKey;
\r
79 this.shiftKey = e.shiftKey;
\r
80 //this.screenX = touch.screenX;
\r
81 //this.screenY = touch.screenY;
\r
82 this.clientX = e.clientX;
\r
83 this.clientY = e.clientY;
\r
84 this.pageX = e.pageX;
\r
85 this.pageY = e.pageY;
\r
86 this.offsetX = e.offsetX;
\r
87 this.offsetY = e.offsetY;
\r
89 if( pointerType = X_Event_toPointer[ originalType ] ){
\r
92 /* e.constructor === window.TouchEvent -> e.touches for iOS3.13 */
\r
93 if( touches = e.changedTouches ){
\r
94 if( touches.length === 0 ){
\r
95 alert( 'e.changedTouches.length === 0' );
\r
97 xnode._cancelMouse = pointerType;
\r
101 ctrlKey = e.ctrlKey;
\r
102 metaKey = e.metaKey;
\r
103 shiftKey = e.shiftKey;
\r
104 time = X_Timer_now();
\r
105 for( i = touches.length; i; ){
\r
106 touch = touches[ --i ];
\r
107 target = touch.target;
\r
108 related = touch.relatedTarget;
\r
110 type : pointerType,
\r
111 pointerType : 'touch',
\r
112 target : X_Node_getXNode( target.nodeType === 3 ? target.parentNode : target ),// defeat Safari bug // xnodetouch.target,
\r
113 currentTarget : xnode,
\r
114 relatedTarget : X_Node_getXNode( related.nodeType === 3 ? related.parentNode : related ), // xnode
\r
116 hwTimestamp : time,
\r
118 buttons : e.button,
\r
119 buttons : e.buttons || e.button,
\r
123 shiftKey : shiftKey,
\r
124 pointerId : touch.identifier + 2,
\r
125 //screenX : touch.screenX,
\r
126 //screenY : touch.screenY,
\r
127 clientX : touch.clientX,
\r
128 clientY : touch.clientY,
\r
129 pageX : touch.pageX,
\r
130 pageY : touch.pageY,
\r
131 offsetX : touch.offsetX, // 要素上の座標を取得
\r
132 offsetY : touch.offsetY,
\r
133 radiusX : touch.radiusX || 0,
\r
134 radiusY : touch.radiusY || 0,
\r
135 rotationAngle : touch.rotationAngle || 0,
\r
136 pressure : touch.force || touch.webkitForce || ( isEnd ? 0 : 0.5 ),
\r
137 width : touch.width || 0,
\r
138 height : touch.height || 0
\r
141 return events.length === 1 ? events[ 0 ] : events;
\r
144 if( xnode._cancelMouse === pointerType ){
\r
145 delete xnode._cancelMouse;
\r
146 console.log( '**** xnode._cancelMouse ' + xnode._cancelMouse );
\r
152 this.pointerType = 'mouse';
\r
154 this.button = e.button !== undefined ? e.button :
\r
155 e.which !== undefined ? e.which - 1 : -1;
\r
156 this.buttons = e.buttons !== undefined ? e.buttons : this.button === 0 ? 1 : this.button === 1 ? 2 : this.button === 2 ? 4 : 0;
\r
157 this.pressure = ( this.button !== -1 ? 0.5 : 0 );
\r
160 this.target = X_Node_getXNode( elm.nodeType === 3 ? elm.parentNode : elm );// defeat Safari bug // xnodetouch.target;
\r
161 this.isPrimary = true;
\r
162 this.hwTimestamp = this.timestamp = X_Timer_now();
\r
163 this.altKey = e.altKey;
\r
164 this.ctrlKey = e.ctrlKey;
\r
165 this.metaKey = e.metaKey;
\r
166 this.shiftKey = e.shiftKey;
\r
167 this.pointerId = 1;
\r
168 //this.screenX = touch.screenX;
\r
169 //this.screenY = touch.screenY;
\r
170 // TODO http://uupaa-js.googlecode.com/svn-history/r8/trunk/doc/reference/symbols/src/trunk_uu.module.ui.js.html
\r
171 // Safari2ではclientX,YはpageX,Yと同じ値を返す
\r
172 this.clientX = e.clientX;
\r
173 this.clientY = e.clientY;
\r
174 this.pageX = e.pageX;
\r
175 this.pageY = e.pageY;
\r
176 this.offsetX = e.offsetX || e.layerX; // 要素上の座標を取得
\r
177 this.offsetY = e.offsetY || e.layerY;
\r
179 // http://www.programming-magic.com/20090127231544/
\r
180 // Opera で button==2の場合、コンテキストメニューイベントを発火 「ツール」->「設定」->「詳細設定」->「コンテンツ」->「Javascriptオプション」で「右クリックを制御するスクリプトを許可する」
\r
181 if( originalType === 'mousedown' && this.button === 2 && X_UA.Opera ){
\r
182 events = [ X.Object.clone( this ), X.Object.clone( this ) ];
\r
183 events[ 1 ].type = 'contextmenu';
\r
190 this.keyCode = e.keyCode || e.which;
\r
191 this.altKey = e.altKey;
\r
192 this.ctrlKey = e.ctrlKey;
\r
193 this.shiftKey = e.shiftKey;
\r
194 this.metaKey = e.metaKey;
\r
196 this.button = e.button !== undefined ? e.button :
\r
197 e.which !== undefined ? e.which - 1 : -1;
\r
198 this.buttons = e.buttons !== undefined ? e.buttons : this.button === 0 ? 1 : this.button === 1 ? 2 : this.button === 2 ? 4 : 0;
\r
200 //http://www.quirksmode.org/js/events_properties.html
\r
201 if( elm = e.target ){
\r
202 this.target = X_Node_getXNode( elm.nodeType === 3 ? elm.parentNode : elm );// defeat Safari bug // xnode
\r
204 if( elm = e.relatedTarget ){
\r
205 this.relatedTarget = X_Node_getXNode( elm.nodeType === 3 ? elm.parentNode : elm ); // xnode
\r
208 if( type === 'wheel' ){
\r
209 // https://developer.mozilla.org/ja/docs/DOM/DOM_event_reference/mousewheel
\r
211 // https://w3g.jp/blog/tools/wheelevent_crossbrowser
\r
212 // ホイール系イベント2014年版クロスブラウザ
\r
213 if( e.deltaY !== undefined ){
\r
214 this.deltaX = e.deltaX;
\r
215 this.deltaY = e.deltaY;
\r
216 this.deltaZ = e.deltaZ;
\r
218 if( e.wheelDeltaY !== undefined ){
\r
219 this.deltaX = e.wheelDeltaX / 120;
\r
220 this.deltaY = e.wheelDeltaY / 120;
\r
221 this.deltaZ = e.wheelDeltaZ / 120 || 0;
\r
223 if( e.wheelDelta !== undefined ){
\r
224 this.deltaX = this.deltaZ = 0;
\r
225 this.deltaY = e.wheelDelta / -120;
\r
227 if( e.detail !== undefined ){
\r
228 this.deltaX = this.deltaZ = 0;
\r
229 this.deltaY = originalType === 'MozMousePixelScroll' ? e.detail / 45 : e.detail / 3; // 3
\r
231 this.deltaX = this.deltaY = this.deltaZ = 0;
\r
236 this.currentTarget = xnode; // xnode
\r
237 this.eventPhase = e.eventPhase;
\r
238 this.detail = e.detail;
\r
242 X.Dom.Event = function( e, xnode, element ){
\r
243 var originalType = e.type, btn, type;
\r
245 this.type = X_Event_RenameTo[ originalType ] || originalType;
\r
246 this.target = X_Node_getXNode( e.srcElement ); // xnode
\r
247 if( this.target && this.target._xnodeType === 3 ) this.target = this.target.parent; // ie4 の fake Textnode がヒットしていないか?
\r
248 this.currentTarget = xnode; // xnode
\r
249 this.relatedTarget = X_Node_getXNode( e.formElement || e.toElement ); // xnode
\r
250 this.eventPhase = e.srcElement === element ? 2: 3;
\r
252 this.keyCode = e.keyCode;
\r
253 this.altKey = e.altKey;
\r
254 this.ctrlKey = e.ctrlKey;
\r
255 this.shiftKey = e.shiftKey;
\r
257 // http://www.programming-magic.com/20090127231544/
\r
258 switch( originalType ){
\r
263 case 'contextmenu' :
\r
267 // mouseup, mousedown
\r
272 btn & 2 ? 1 : -1; // 左:1(click:0), 中:4, 右:2
\r
275 this.buttons = e.button;
\r
278 this.deltaY = e.wheelDelta / -120;
\r
280 if( type = X_Event_toPointer[ originalType ] ){
\r
282 this.clientX = e.clientX;
\r
283 this.clientY = e.clientY;
\r
284 //this.screenX = e.screenX;
\r
285 //this.screenY = e.screenY;
\r
287 //if( X_ViewPort_rootElement ){ // uuu...
\r
288 this.pageX = e.clientX + X_ViewPort_rootElement.scrollLeft;
\r
289 this.pageY = e.clientY + X_ViewPort_rootElement.scrollTop;
\r
290 // DOMAssistant 2.8.1
\r
291 //event.pageX = DOMAssistant.def(e.pageX)? e.pageX : (event.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0));
\r
292 //event.pageY = DOMAssistant.def(e.pageY)? e.pageY : (event.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0));
\r
295 if( 5 <= X_UA.IE ){
\r
296 this.offsetX = e.offsetX; // イベントターゲット左上からの座標
\r
297 this.offsetY = e.offsetY;
\r
299 //if( e.srcElement ){
\r
300 // this.offsetX = e.x - e.srcElement.offsetLeft; // e.x はイベント発生要素の親要素を基準にした座標。
\r
301 // this.offsetY = e.y - e.srcElement.offsetTop;
\r
304 this.pointerId = 1;
\r
307 this.rotationAngle = 0;
\r
316 X.Dom.Event.ANIME_BEFORE_START = ++X_Event_last;
\r
317 X.Dom.Event.ANIME_START = ++X_Event_last;
\r
318 X.Dom.Event.ANIME = ++X_Event_last;
\r
319 X.Dom.Event.ANIME_END = ++X_Event_last;
\r
320 X.Dom.Event.ANIME_BEFORE_STOP = ++X_Event_last; // xnode.stop() のみ、指定時間による停止では呼ばれない
\r
321 X.Dom.Event.ANIME_STOP = ++X_Event_last;
\r
324 // TODO load -> readystatechange this.readyState === "loaded" || this.readyState === "complete"
\r
326 X.Dom.Event._LOAD_FIX_TAGS = {
\r
332 // https://github.com/georgeadamson/jQuery.prefixfree-events/blob/master/jQuery.prefixfree-events.js
\r
333 // https://developer.mozilla.org/en-US/docs/Web/Events/wheel
\r
335 if( document.onwheel === undefined ){
\r
337 if( X_UA.Gecko && window.MouseScrollEvent ){
\r
338 if( 2 <= X_UA.Gecko || ( 1.9 <= X_UA.Gecko && 1 <= X_UA.GeckoPatch ) ){ // Gecko 1.9.1+ (firefox3.5+)
\r
339 console.log( 'wheel <= MozMousePixelScroll' );
\r
340 X_Event_Rename[ 'wheel' ] = 'MozMousePixelScroll';
\r
342 if( 1 <= X_UA.Gecko || ( 0.9 <= X_UA.Gecko && 7 <= X_UA.GeckoPatch ) ){ // Gecko 0.9.7+ (NN6.2+?)
\r
343 console.log( 'wheel <= DOMMouseScroll' );
\r
344 X_Event_Rename[ 'wheel' ] = 'DOMMouseScroll';
\r
347 X_Event_Rename[ 'wheel' ] = 'mousewheel';
\r
349 //if( document.onmousewheel !== undefined ){ // Opera で判定失敗する
\r
350 // X_Event_Rename[ 'wheel' ] = 'mousewheel';
\r
356 if( window.onwebkitanimationend !== undefined && window.onanimationend === undefined ){
\r
357 console.log( 'animationend <= webkitAnimationEnd' );
\r
358 X_Event_Rename[ 'animationend' ] = 'webkitAnimationEnd';
\r
359 X_Event_Rename[ 'animationstart' ] = 'webkitAnimationStart';
\r
360 X_Event_Rename[ 'animationiteration' ] = 'webkitAnimationIteration';
\r
362 if( window.onoanimationend !== undefined && window.onanimationend === undefined ){
\r
363 console.log( 'animationend <= oAnimationEnd' );
\r
364 X_Event_Rename[ 'animationend' ] = 'oAnimationEnd';
\r
365 X_Event_Rename[ 'animationstart' ] = 'oAnimationStart';
\r
366 X_Event_Rename[ 'animationiteration' ] = 'oAnimationIteration';
\r
369 if( window.onmozanimationend !== undefined && window.onanimationend === undefined ){
\r
370 X_Event_Rename[ 'animationend' ] = 'mozAnimationEnd';
\r
371 X_Event_RenameTo[ 'mozAnimationEnd' ] = 'animationend';
\r
372 X_Event_Rename[ 'animationstart' ] = 'mozAnimationStart';
\r
373 X_Event_RenameTo[ 'mozAnimationStart' ] = 'animationstart';
\r
374 X_Event_Rename[ 'animationiteration' ] = 'mozAnimationIteration';
\r
375 X_Event_RenameTo[ 'mozAnimationIteration' ] = 'animationiteration';
\r
377 if( document.documentElement && document.documentElement.style.msAnimation !== undefined && document.documentElement.style.animation === undefined ){ //document.documentElement.style.msAnimation
\r
378 console.log( 'animationend <= MSAnimationEnd' );
\r
379 X_Event_Rename[ 'animationend' ] = 'MSAnimationEnd';
\r
380 X_Event_Rename[ 'animationstart' ] = 'MSAnimationStart';
\r
381 X_Event_Rename[ 'animationiteration' ] = 'MSAnimationIteration';
\r
383 // https://developer.mozilla.org/en-US/docs/Web/Events/transitionend
\r
384 // chrome1+, firefox4+, IE10+, Opera10.5+, Safari3.2+, Android2.1+
\r
385 if( window.onwebkittransitionend !== undefined && window.ontransitionend === undefined ){
\r
386 console.log( 'transitionend <= webkitTransitionEnd' );
\r
387 X_Event_Rename[ 'transitionend' ] = 'webkitTransitionEnd';
\r
389 if( window.onotransitionend !== undefined && window.ontransitionend === undefined ){
\r
390 if( X_UA.Opera < 12 ){
\r
391 console.log( 'transitionend <= oTransitionEnd|ver.' + X_UA.Opera );
\r
392 X_Event_Rename[ 'transitionend' ] = 'oTransitionEnd';
\r
394 console.log( 'transitionend <= otransitionEnd|ver.' + X_UA.Opera );
\r
395 X_Event_Rename[ 'transitionend' ] = 'otransitionEnd';
\r
398 if( window.onmoztransitionend !== undefined && window.ontransitionend === undefined ){
\r
399 console.log( 'transitionend <= mozTransitionEnd' );
\r
400 X_Event_Rename[ 'transitionend' ] = 'mozTransitionEnd';
\r
403 if( !navigator.pointerEnabled ){
\r
404 if( navigator.msPointerEnabled ){
\r
405 console.log( 'pointerdown <= MSPointerDown' );
\r
406 X_Event_Rename[ 'pointerdown' ] = 'MSPointerDown';
\r
407 X_Event_Rename[ 'pointerup' ] = 'MSPointerUp';
\r
408 X_Event_Rename[ 'pointermove' ] = [ 'MSPointerMove', 'MSPointerHover' ];// ie10 と ie11 でペンのhoverevent の値が異なる
\r
409 X_Event_Rename[ 'pointercancel' ] = 'MSPointerCancel';
\r
410 // http://msdn.microsoft.com/ja-jp/library/ie/dn304886%28v=vs.85%29.aspx
\r
412 if( X_UA_HID.TOUCH ){
\r
413 // touch のみ(iOS でも脱獄したら?)、 touch と mouse(Android), mouse のみ
\r
414 X_Event_Rename[ 'pointerdown' ] = [ 'touchdown', 'mousedown' ];
\r
415 X_Event_Rename[ 'pointerup' ] = [ 'touchup', 'mouseup' ];
\r
416 X_Event_Rename[ 'pointermove' ] = [ 'touchmove', 'mousemove' ];
\r
417 X_Event_Rename[ 'pointercancel' ] = 'touchcancel';
\r
418 // X_Event_Rename[ 'click' ] = [ 'touchdown', 'touchmove', 'touchup' ]; // ループになってしまう!直した!直ってない!
\r
420 X_Event_Rename[ 'pointerdown' ] = 'mousedown';
\r
421 X_Event_Rename[ 'pointerup' ] = 'mouseup';
\r
422 X_Event_Rename[ 'pointermove' ] = 'mousemove';
\r
423 X_Event_Rename[ 'pointercancel' ] = 'mouseleave';//??
\r
425 // Opera は ブラウザ設定から右クリックの通知を許可すると mousedown で e.button==2 が返る,キャンセルは可能??
\r
427 X_Event_Rename[ 'contextmenu' ] = 'mousedown' );
\r
430 * buttons の無いブラウザには mouseup, mousedown を監視して、buttons フラグを更新し続ける
\r