OSDN Git Service

Version 0.5.151, fix X.Timer.remove & X.OAuth2 is working.
[pettanr/clientJs.git] / 0.6.x / js / 01_core / 16_XViewPort.js
index a6eddb8..5a4ad5f 100644 (file)
@@ -1,15 +1,19 @@
 
 var X_ViewPort_readyState,
        X_ViewPort_active = !!window.parent || !!document.activeElement, // parent は frameに読み込まれた場合のieのerror回避
+       X_ViewPort_activeTimerID,
        X_ViewPort_rootElement,
        X_ViewPort_lock,
        X_ViewPort_width,
        X_ViewPort_height,
+       X_ViewPort_scrollX = 0,
+       X_ViewPort_scrollY = 0,
        X_ViewPort_baseFontSize,
        X_ViewPort_vScrollbarSize,
        X_ViewPort_hScrollbarSize,
        
-       X_Dom_detectFontSize = !( X_UA[ 'IE' ] < 9 || X_UA[ 'iOS' ] ) && function(){
+       X_ViewPort_useDetectionLoop = X_UA[ 'IE' ] < 9 || X_UA[ 'iOS' ],
+       X_ViewPort_detectFontSize = !X_ViewPort_useDetectionLoop && function(){
                        var size = X_Node_fontSizeNode[ '_rawObject' ].offsetHeight;
                        if( X_ViewPort_baseFontSize !== size ){
                                X_ViewPort_baseFontSize = size;
@@ -18,7 +22,7 @@ var X_ViewPort_readyState,
        },
 
        X_ViewPort_orientationFlag,     
-       X_Dom_orientationchange = window[ 'orientation' ] !== undefined && function( e ){
+       X_ViewPort_orientationchange = window[ 'orientation' ] !== undefined && function( e ){
                X_ViewPort_orientationFlag = true;
                !X_UA[ 'Android' ] && X_ViewPort_resize();
                //console.log( '-- orientationchange : ' + X[ 'ViewPort' ][ 'getSize' ][ 0 ] + ' ' + X[ 'ViewPort' ][ 'getSize' ][ 1 ] );
@@ -31,7 +35,7 @@ X_ViewPort = X_Class_override(
        {
 
                'handleEvent' : function( e ){
-                       var href, i, name;
+                       var href, i, name, active = false;
                        
                        switch( e.type ){
                                case 'beforeunload' :
@@ -42,6 +46,8 @@ X_ViewPort = X_Class_override(
                                        return X_ViewPort[ 'dispatch' ]( X_EVENT_BEFORE_UNLOAD );
                                        
                                case 'unload' :
+                                       //https://developer.mozilla.org/ja/docs/Web/JavaScript/A_re-introduction_to_JavaScript
+                                       //Firefox 1.5 の bfcache が無効になりますので、他に理由がない限り Firefox では unload リスナを登録するべきではないことに注意してください。
                                        X_ViewPort[ 'dispatch' ]( X_EVENT_UNLOAD );
                                //alert('unload');
                                        X_ViewPort_document[ 'kill' ]();
@@ -59,20 +65,22 @@ X_ViewPort = X_Class_override(
                                case 'webkitvisibilitychange' :
                                        X_ViewPort[ 'dispatch' ]( ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X_EVENT_VIEW_DEACTIVATE : X_EVENT_VIEW_ACTIVATE );
                                        break;
-                                       
-                               case 'pageshow' :
-                               case 'focus' :
-                                       if( !X_ViewPort_active ){
-                                               X_ViewPort_active = true;
-                                               X_ViewPort[ 'dispatch' ]( X_EVENT_VIEW_ACTIVATE );
-                                       };
-                                       break;
 
-                               case 'pagehide' :
+       
                                case 'blur' :
-                                       if( X_ViewPort_active ){
-                                               X_ViewPort_active = false;
-                                               X_ViewPort[ 'dispatch' ]( X_EVENT_VIEW_DEACTIVATE );                            
+                               case 'focusout' :
+                               case 'pagehide' :                               
+                                       active = true;
+                               case 'focus' :
+                               case 'focusin' :
+                               case 'pageshow' :
+                                       if( X_ViewPort_active === active ){
+                                               X_ViewPort_active = !active;
+                                               if( X_ViewPort_activeTimerID ){
+                                                       X_ViewPort_activeTimerID = X_Timer_remove( X_ViewPort_activeTimerID );
+                                               } else {
+                                                       X_ViewPort_activeTimerID = X_Timer_once( 1, X_ViewPort_changeFocus );
+                                               };
                                        };
                                        break;
                        };
@@ -81,6 +89,11 @@ X_ViewPort = X_Class_override(
        }
 );
 
+function X_ViewPort_changeFocus(){
+       X_ViewPort[ 'dispatch' ]( X_ViewPort_active ? X_EVENT_VIEW_ACTIVATE : X_EVENT_VIEW_DEACTIVATE );
+       X_ViewPort_activeTimerID = 0;
+};
+
 
 /**
  * window に相当する ViewPort 情報を提供するオブジェクト。
@@ -168,17 +181,17 @@ X[ 'ViewPort' ] = {
                window.pageXOffset !== undefined ?
                        ( function(){
                                X_Node_updateTimerID && X_Node_startUpdate();
-                               return[ window.pageXOffset, window.pageYOffset ];
+                               return[ X_ViewPort_scrollX = window.pageXOffset, X_ViewPort_scrollY = window.pageYOffset ];
                        } ) :
                window.scrollLeft  !== undefined ?
                        ( function(){
                                X_Node_updateTimerID && X_Node_startUpdate();
-                               return[ window.scrollLeft, window.scrollTop ];
+                               return[ X_ViewPort_scrollX = window.scrollLeft, X_ViewPort_scrollY = window.scrollTop ];
                        } ) :
                        ( function(){
                                X_Node_updateTimerID && X_Node_startUpdate();
                                // body は Safari2-
-                               return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
+                               return[ X_ViewPort_scrollX = X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_scrollY = X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
                        } ),
 
        'getScrollbarSize' : function(){
@@ -224,7 +237,7 @@ X[ 'ViewPort' ] = {
  */
                var X_ViewPort_resize =
                        // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
-                       X_UA[ 'IE' ] < 9 || X_UA[ 'iOS' ] ?
+                       X_ViewPort_useDetectionLoop ?
                                (function(){
                                        var size;
                                        if( !X_ViewPort_lock ){
@@ -232,7 +245,7 @@ X[ 'ViewPort' ] = {
                                                if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
                                                        X_ViewPort_width = size[ 0 ];
                                                        X_ViewPort_height = size[ 1 ];
-                                                       X_Timer_once( 100, X_Dom_detectFinishResizing );
+                                                       X_Timer_once( 100, X_ViewPort_detectFinishResizing );
                                                        X_ViewPort_lock = true;
                                                };
                                        };
@@ -247,16 +260,16 @@ X[ 'ViewPort' ] = {
                                (function( e ){
                                        console.log( '-- resize : ' + X_Timer_now() );
                                        
-                                       !X_ViewPort_lock && ( X_ViewPort_lock = true ) && X_Timer_once( 100, X_Dom_detectFinishResizing );
+                                       !X_ViewPort_lock && ( X_ViewPort_lock = true ) && X_Timer_once( 100, X_ViewPort_detectFinishResizing );
                                        return X_Callback_PREVENT_DEFAULT | X_Callback_STOP_PROPAGATION;
                                });
                
-               function X_Dom_detectFinishResizing(){
+               function X_ViewPort_detectFinishResizing(){
                        var size = X_ViewPort_getWindowSize();
                        if( X_ViewPort_width !== size[ 0 ] || X_ViewPort_height !== size[ 1 ] ){
                                X_ViewPort_width  = size[ 0 ];
                                X_ViewPort_height = size[ 1 ];
-                               X_Timer_once( 100, X_Dom_detectFinishResizing );
+                               X_Timer_once( 100, X_ViewPort_detectFinishResizing );
                        } else {
                                console.log( '-- detectFinishResizing : ' + X_Timer_now() );
                                
@@ -293,12 +306,27 @@ X[ 'ViewPort' ] = {
 
                        X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
 
-                       html = X[ 'Doc' ][ 'html' ] = X_Node_html = elmHtml && new Node( elmHtml );
+       /**
+        * Node( documentElement )
+        * @alias X.Doc.html
+        * @type {Node}
+        */
+                       X[ 'Doc' ][ 'html' ] = html = X_Node_html = elmHtml && new Node( elmHtml )[ 'removeClass' ]( 'js-disabled' )[ 'addClass' ]( X_UA_classNameForHTML );
                        html[ '_flags' ] |= X_Node_State.IN_TREE;
-                       
-                       head = X[ 'Doc' ][ 'head' ] = X_Node_head = elmHead && new Node( elmHead );
-               
-                       body = X[ 'Doc' ][ 'body' ] = X_Node_body = new Node( elmBody );
+
+       /**
+        * Node( head )
+        * @alias X.Doc.head
+        * @type {Node}
+        */                     
+                       X[ 'Doc' ][ 'head' ] = head = X_Node_head = elmHead && new Node( elmHead );
+
+       /**
+        * Node( documentElement )
+        * @alias X.Doc.body
+        * @type {Node}
+        */             
+                       X[ 'Doc' ][ 'body' ] = body = X_Node_body = new Node( elmBody );
 
                        body[ 'parent ' ] = head[ 'parent' ] = html;
                        html[ '_xnodes' ] = [ head, body ];
@@ -359,13 +387,13 @@ X[ 'ViewPort' ] = {
                                };
                                
                                //
-                               if( X_Dom_orientationchange ){
-                                       X_EventDispatcher_systemListen( X_ViewPort, 'orientationchange', X_Dom_orientationchange );
+                               if( X_ViewPort_orientationchange ){
+                                       X_EventDispatcher_systemListen( X_ViewPort, 'orientationchange', X_ViewPort_orientationchange );
                                };
                                
-                               if( X_Dom_detectFontSize ){
+                               if( X_ViewPort_detectFontSize ){
                                        X_EventDispatcher_systemListen( X_ViewPort, 'resize', X_ViewPort_resize );
-                                       X_Timer_add( 333, X_Dom_detectFontSize );
+                                       X_Timer_add( 333, X_ViewPort_detectFontSize );
                                } else {
                                        X_Timer_add( 333, X_ViewPort_resize );
                                };
@@ -394,6 +422,10 @@ X[ 'ViewPort' ] = {
                        } else
                        if( X_UA[ 'iOS' ] && window[ 'onpageshow' ] !== undefined ){
                                X_EventDispatcher_systemListen( X_ViewPort, [ 'pageshow', 'pagehide' ] );
+                       } else
+                       if( document[ 'onfocusin' ] !== undefined ){
+                               // https://github.com/ai/visibilityjs/blob/master/lib/visibility.fallback.js
+                               X_EventDispatcher_systemListen( X_ViewPort_document, [ 'focusin', 'focusout' ], X_ViewPort );
                        } else {
                                X_EventDispatcher_systemListen( X_ViewPort, [ 'focus', 'blur' ] );
                        };
@@ -402,8 +434,10 @@ X[ 'ViewPort' ] = {
                };
 
                function X_ViewPort_getWindowSize(){
-                       return X_UA[ 'IE' ] ?
+                       return X_UA[ 'IE' ] ? // Opera10.1 では ズーム時に表示領域のサイズが取れない!
                                [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
+                               X_UA[ 'Opera' ] < 12 ? // Opera10.1 では ズーム + resize 時に表示領域のサイズが取れない!
+                               [ X_ViewPort_rootElement.offsetWidth, X_ViewPort_rootElement.offsetHeight ] :
                                [ window.innerWidth, window.innerHeight ];
                };