OSDN Git Service

Version 0.6.82, X.Timer.remove() work well while onTimeout.
[pettanr/clientJs.git] / 0.6.x / js / 01_dom / 12_XDomEvent.js
index 3ded30f..fd81b01 100644 (file)
@@ -2,6 +2,7 @@
  * use X.Callback\r
  * \r
  * http://d.hatena.ne.jp/uupaa/20100430/1272561922\r
+ * event.offsetX と offsetY の互換性について\r
  * \r
  */\r
 \r
@@ -28,8 +29,8 @@ if( !X.UA.IE || 9 <= X.UA.IE ){
                //this.screenY       = e.screenY;\r
                this.pageX         = e.pageX;\r
                this.pageY         = e.pageY;\r
-               this.offsetX       = e.offsetX || e.layerX;\r
-               this.offsetY       = e.offsetY || e.layerY;\r
+               this.offsetX       = e.offsetX || e.layerX || 0; // 要素上の座標を取得 \r
+               this.offsetY       = e.offsetY || e.layerY || 0;\r
                \r
                this.keyCode       = e.keyCode;\r
                this.altKey        = e.altKey;\r
@@ -38,6 +39,7 @@ if( !X.UA.IE || 9 <= X.UA.IE ){
                \r
                // http://www.programming-magic.com/20090127231544/\r
                this.which         = e.which || ( e.button + 1 ); // 左:1, 中:2, 右:3\r
+               this.button        = e.button;\r
                \r
                // https://developer.mozilla.org/ja/docs/DOM/DOM_event_reference/mousewheel\r
                \r
@@ -45,17 +47,23 @@ if( !X.UA.IE || 9 <= X.UA.IE ){
                // axis\r
                // https://w3g.jp/blog/tools/wheelevent_crossbrowser\r
                // ホイール系イベント2014年版クロスブラウザ\r
+               if( e.deltaY !== undefined ){\r
+                       this.deltaX = e.deltaX;\r
+                       this.deltaY = e.deltaY;\r
+               } else\r
                if( e.wheelDeltaY !== undefined ){\r
-                       this.wheelDeltaX = e.wheelDeltaX / 12;\r
-                       this.wheelDeltaY = e.wheelDeltaY / 12;\r
+                       this.deltaX = e.wheelDeltaX / 120;\r
+                       this.deltaY = e.wheelDeltaY / 120;\r
                } else\r
                if( e.wheelDelta !== undefined ){\r
-                       this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;\r
+                       this.deltaX = 0;\r
+                       this.deltaY = e.wheelDelta / -120;\r
                } else\r
                if( e.detail !== undefined ){\r
-                       this.wheelDeltaX = this.wheelDeltaY = - e.detail * 3;\r
+                       this.deltaX = 0;\r
+                       this.deltaY = e.type === 'MozMousePixelScroll' ? e.detail / 45 : e.detail / 3; // 3\r
                } else {\r
-                       this.wheelDeltaX = this.wheelDeltaY = 0;\r
+                       this.deltaX = this.deltaY = 0;\r
                };\r
                \r
                if( /* e.constructor === window.TouchEvent || */ e.touches /* for iOS3.13 */ ){\r
@@ -96,11 +104,11 @@ if( !X.UA.IE || 9 <= X.UA.IE ){
                window.PointerEvent = window.MSPointerEvent;\r
        };\r
 } else {\r
-       X.Dom.Event = function( e, xnode, element ){\r
+       X.Dom.Event = function( e, xnode, element, opt_type ){\r
                var btn;\r
                \r
                //this._event        = e;\r
-               this.type          = e.type;\r
+               this.type          = X.Dom.Event.RenameTo[ e.type ] || e.type || opt_type;\r
                this.target        = Node._getXNode( e.srcElement ); // xnode\r
                if( this.target && this.target._xnodeType === 3 ) this.target = this.target.parent; // ie4 の fake Textnode がヒットしていないか?\r
                this.currentTarget = xnode; // xnode\r
@@ -154,7 +162,11 @@ if( !X.UA.IE || 9 <= X.UA.IE ){
                                        btn & 4 ? 2 :\r
                                        btn & 2 ? 3 : 0; // 左:1(click:0), 中:4, 右:2\r
                };\r
-               this.wheelDeltaX = this.wheelDeltaY = e.wheelDelta / 12;\r
+               this.button      = this.which - 1;\r
+               this.deltaX      = 0;\r
+               this.deltaY      = e.wheelDelta / -120;\r
+               \r
+               if( this.type === 'wheel' ) console.log( e.wheelDelta );\r
        };\r
 };\r
 \r
@@ -194,19 +206,29 @@ X.Dom.Event._LOAD_FIX_TAGS = {
 X.Dom.Event.Rename = {};\r
 X.Dom.Event.RenameTo = {};\r
 // https://github.com/georgeadamson/jQuery.prefixfree-events/blob/master/jQuery.prefixfree-events.js\r
-\r
-// DOMMoseScroll\r
-if( X.UA.Gecko && window.MouseScrollEvent ){\r
-       if( 2 <= X.UA.Gecko || ( 1.9 < X.UA.Gecko && 1 <= X.UA.GeckoPatch ) ){ // Gecko 1.9.1+ (firefox3.5+)\r
-               console.log( 'mousewheel <= MozMousePixelScroll' );\r
-               X.Dom.Event.Rename[ 'mousewheel' ] = 'MozMousePixelScroll';\r
-       } else\r
-       if( 0.9 < X.UA.Gecko && 7 <= X.UA.GeckoPatch ){ // Gecko 0.9.7+ (NN6.2+?)\r
-               console.log( 'mousewheel <= DOMMouseScroll' );\r
-               X.Dom.Event.Rename[ 'mousewheel' ] = 'DOMMouseScroll';\r
+// https://developer.mozilla.org/en-US/docs/Web/Events/wheel\r
+//\r
+if( document.onwheel === undefined ){\r
+       // DOMMoseScroll\r
+       if( X.UA.Gecko && window.MouseScrollEvent ){\r
+               if( 2 <= X.UA.Gecko || ( 1.9 < X.UA.Gecko && 1 <= X.UA.GeckoPatch ) ){ // Gecko 1.9.1+ (firefox3.5+)\r
+                       console.log( 'wheel <= MozMousePixelScroll' );\r
+                       X.Dom.Event.Rename[ 'wheel' ] = 'MozMousePixelScroll';\r
+               } else\r
+               if( 0.9 < X.UA.Gecko && 7 <= X.UA.GeckoPatch ){ // Gecko 0.9.7+ (NN6.2+?)\r
+                       console.log( 'wheel <= DOMMouseScroll' );\r
+                       X.Dom.Event.Rename[ 'wheel' ] = 'DOMMouseScroll';\r
+               };\r
+       } else {\r
+               X.Dom.Event.Rename[ 'wheel' ] = 'mousewheel';\r
+       };\r
+       if( document.onmousewheel !== undefined ){ // Opera で判定失敗\r
+               X.Dom.Event.Rename[ 'wheel' ] = 'mousewheel';\r
        };\r
 };\r
 \r
+\r
+\r
 if( window.onwebkitanimationend !== undefined && window.onanimationend === undefined ){\r
        console.log( 'animationend <= webkitAnimationEnd' );\r
        X.Dom.Event.Rename[ 'animationend' ]        = 'webkitAnimationEnd';\r
@@ -270,190 +292,6 @@ if( navigator.msPointerEnabled && !navigator.pointerEnabled ){
 })( X.Dom.Event.Rename, X.Dom.Event.RenameTo );\r
 \r
 \r
-\r
-X.Dom.Node.prototype.listen = function( type, arg1, arg2, arg3 /* [ listener || ( context + function ) || function ][ arguments ] */ ){\r
-       if( this._xnodeType === 0 || this._xnodeType === 3 ) return this;\r
-       \r
-       if( X.Type.isArray( type ) ){\r
-               return X.EventDispatcher.prototype.listen.apply( this, arguments );\r
-       };\r
-       \r
-       ( !this._listeners || !this._listeners[ type ] ) && X.Type.isString( type ) && this._addEvent( type );\r
-       \r
-       return X.EventDispatcher.prototype.listen.apply( this, arguments );\r
-};\r
-\r
-X.Dom.Node.prototype._addEvent =\r
-       // Days on the Moon DOM Events とブラウザの実装 \r
-       // http://nanto.asablo.jp/blog/2007/03/23/1339502\r
-       // Safari 2 では関数オブジェクトしか EventListener として使えませんが、Safari のナイトリービルドでは handleEvent メソッドを持つオブジェクトも EventListener として使えるようです。\r
-       X.Dom.EVENT_W3C && ( X.UA.WebKit < 525.13 || X.UA.Opera7 || X.UA.NetFront < 4 ) ? // Safari3-\r
-               (function( type ){\r
-                       var raw = this._rawNode;\r
-                       if( !raw ) return;\r
-                       this._handleEvent = this._handleEvent || X.Callback.create( this );\r
-                       if( raw.addEventListener ){\r
-                               raw.addEventListener( type, this._handleEvent, false );\r
-                       } else {\r
-                               // Safari は Image, Opera7 は window\r
-                               raw[ 'on' + type ] = this._handleEvent;\r
-                       };\r
-               }) :\r
-       X.Dom.EVENT_W3C ?\r
-               (function( type ){\r
-                       this._rawNode && this._rawNode.addEventListener( X.Dom.Event.Rename[ type ] || type, this, false );\r
-               }) :\r
-       X.Dom.EVENT_IE ?\r
-               (function( type ){\r
-                       var raw = this._rawNode;\r
-                       if( !raw ) return;\r
-                       //if( type === 'load' && this._tag && X.Dom.Event._LOAD_FIX_TAGS[ this._tag ] ){\r
-                       //      type = 'readystatechange';\r
-                       //};\r
-                       this._handleEvent = this._handleEvent || X.Callback.create( this );\r
-                       if( raw.attachEvent ){\r
-                               raw.attachEvent( 'on' + type, this._handleEvent );\r
-                       } else {\r
-                               raw[ 'on' + type ] = this._handleEvent;\r
-                       };\r
-               }) :\r
-               (function( type ){\r
-                       var raw = this._rawNode || ( this._ie4getRawNode && this._ie4getRawNode() );\r
-                       if( !raw ) return;\r
-                       raw[ 'on' + type ] = this._handleEvent = this._handleEvent || X.Callback.create( this );\r
-               });\r
-\r
-\r
-X.Dom.Node.prototype.unlisten = function( type, arg1, arg2, arg3 ){\r
-       var list = this._listeners, l;\r
-       \r
-       if( X.Type.isArray( type ) ){\r
-               return list ? X.EventDispatcher.prototype.unlisten.apply( this, arguments ) : this;\r
-       };\r
-\r
-       l = !this._dispatching && list && type !== undefined && list[ type ] && list[ type ].length;\r
-       \r
-       X.EventDispatcher.prototype.unlisten.apply( this, arguments );\r
-       \r
-       l && !list[ type ] && X.Type.isString( type ) && this._removeEvent( type );\r
-       \r
-       return this;\r
-};\r
-\r
-X.Dom.Node.prototype._removeEvent =\r
-       X.Dom.EVENT_W3C && ( X.UA.WebKit < 525.13 || X.UA.Opera7 || X.UA.NetFront < 4 ) ? // Safari3-\r
-               (function( type ){\r
-                       var raw = this._rawNode;\r
-                       if( !raw ) return;\r
-                       \r
-                       if( raw.addEventListener ){ // Image\r
-                               raw.removeEventListener( type, this._handleEvent, false );\r
-                       } else {\r
-                               raw[ 'on' + type ] = null;\r
-                       };\r
-                       if( !this._listeners ){\r
-                               X.Callback._correct( this._handleEvent );\r
-                               delete this._handleEvent;\r
-                       };\r
-               }) :\r
-       X.Dom.EVENT_W3C ?\r
-               (function( type ){\r
-                       var raw = this._rawNode;\r
-                       if( !raw ) return;\r
-                       raw.removeEventListener( X.Dom.Event.Rename[ type ] || type, this, false );\r
-               }) :\r
-       X.Dom.EVENT_IE ?\r
-               (function( type ){\r
-                       var raw = this._rawNode;\r
-                       if( !raw ) return;\r
-                       //if( type === 'load' && this._tag && X.Dom.Event._LOAD_FIX_TAGS[ this._tag ] ){\r
-                       //      type = 'readystatechange';\r
-                       //};\r
-                       if( raw.attachEvent ){\r
-                               raw.detachEvent( 'on' + type, this._handleEvent );\r
-                       } else {\r
-                               raw[ 'on' + type ] = X.emptyFunction;\r
-                               raw[ 'on' + type ] = '';\r
-                       };\r
-                       if( !this._listeners ){\r
-                               X.Callback._correct( this._handleEvent );\r
-                               delete this._handleEvent;\r
-                       };\r
-               }) :\r
-               (function( type ){\r
-                       var raw = this._rawNode || ( this._ie4getRawNode && this._ie4getRawNode() );\r
-                       if( !raw ) return;\r
-                       raw[ 'on' + type ] = X.emptyFunction;\r
-                       raw[ 'on' + type ] = '';\r
-                       if( !this._listeners ){\r
-                               X.Callback._correct( this._handleEvent );\r
-                               delete this._handleEvent;\r
-                       };\r
-               });\r
-\r
-// Is this in regard to the Safari 1.x preventDefault bug on click/dblclick?\r
-// https://groups.google.com/forum/#!msg/comp.lang.javascript/uYEuCHjHxnw/yKoHtZJPa1QJ\r
-X.Dom.Node.prototype.handleEvent =\r
-       X.UA.IE4 || X.UA.IE5678 ? // ie45678 EVENT_IE & EVENT_DOM0 for ie4\r
-               (function(){\r
-                       var ret;\r
-                       \r
-                       if( event.type === 'readystatechange' && this._tag && X.Dom.Event._LOAD_FIX_TAGS[ this._tag ] ){\r
-                               //type = 'readystatechange';\r
-                       };\r
-                       \r
-                       ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( event, this, this._rawNode ) );\r
-\r
-                       if( ret & X.Callback.STOP_PROPAGATION ){\r
-                               event.cancelBubble = true;\r
-                       };\r
-                       if( ret & X.Callback.PREVENT_DEFAULT ){\r
-                               this._tag === 'A' && this._rawNode.blur();\r
-                               return event.returnValue = false;\r
-                       };\r
-               }) :\r
-       //X.Dom.EVENT_W3C & EVENT_DOM0\r
-               (function( e ){\r
-                       var ret = X.EventDispatcher.prototype.dispatch.call( this, new X.Dom.Event( e, this ) );\r
-                       \r
-                       if( ret & X.Callback.STOP_PROPAGATION ){\r
-                               e.stopPropagation();\r
-                       };\r
-                       if( ret & X.Callback.PREVENT_DEFAULT ){\r
-                               this._tag === 'A' && this._rawNode.blur();\r
-                               e.preventDefault();\r
-                               if( X.UA.WebKit < 525.13 ){ // Safari3-\r
-                                       if( e.type === 'click' || e.type === 'dbclick' ){\r
-                                               X.Dom._safariPreventDefault = true;\r
-                                       };\r
-                               };\r
-                               return false;\r
-                       };\r
-               });\r
-\r
-\r
-// イベントの退避、dom が画面から抜かれる場合に実施しておく\r
-X.Dom.Node.prototype._migrateEvent = function(){\r
-       var hash = this._listeners,\r
-               type;\r
-       if( !hash ) return;\r
-       for( type in hash ){\r
-               // 数字イベントの除外\r
-               /*'' + parseFloat( type ) !== type && */this._removeEvent( type );\r
-       };\r
-};\r
-\r
-// 退避したイベントの復帰\r
-X.Dom.Node.prototype._restoreEvent = function(){\r
-       var hash = this._listeners,\r
-               type;\r
-       if( !hash ) return;\r
-       for( type in hash ){\r
-               // 数字イベントの除外\r
-               /*'' + parseFloat( type ) !== type && */ this._addEvent( type );\r
-       };\r
-};\r
-\r
 /* -----------------------------------------------\r
  * Document Ready\r
  *  Dean Edwards/Matthias Miller/John Resig\r
@@ -484,7 +322,7 @@ if( 6 <= X.UA.IE && X.inHead ){
 };\r
 // Re: onLoad doesn't work with Safari?\r
 // http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html\r
-if( X.UA.WebKit || ( X.UA.Safari && X.UA.Safari < 3 ) ){ // sniff\r
+if( X.UA.WebKit || X.UA.Safari < 3 ){ // sniff\r
        X.Timer.add( 10, function(){\r
                if( !X.Dom._init ) return X.Callback.UN_LISTEN;\r
                if( 'loaded|complete'.indexOf( document.readyState ) !== -1 ) return X.Dom._init();\r
@@ -498,16 +336,5 @@ X.Dom.listenOnce( X.Dom.Event.XDOM_READY, function(e){
        console.log( 'X.Dom XDomReady ' + X.Dom.readyState );\r
 } );\r
 \r
-if( X.UA.WebKit < 525.13 ){ // Safari3-\r
-       document.documentElement.onclick =\r
-       document.documentElement.ondbclick = function( e ){\r
-                       if( X.Dom._safariPreventDefault ){\r
-                               X.Dom._safariPreventDefault = false;\r
-                               e.preventDefault();\r
-                               return false;\r
-                       };\r
-               };\r
-};\r
-\r
-X.Dom.listen( X.Dom.Event.VIEW_RESIZED, function(e){ console.log( 'X.Dom VIEW_RESIZED ' + e.w + 'x' + e.h ); } );\r
+X.Dom.listen( X.Dom.Event.VIEW_RESIZED, function(e){ console.log( 'X.Dom VIEW_RESIZED ' + e.w + ' x ' + e.h ); } );\r
 \r