OSDN Git Service

Version 0.6.145, fix X.Audio, fix VIEW_ACTIVATE Event @X.ViewPort.
[pettanr/clientJs.git] / 0.6.x / js / 01_core / 16_XViewPort.js
index d31a5fe..d2540f4 100644 (file)
@@ -1,27 +1,31 @@
 
 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 && X_ViewPort[ 'asyncDispatch' ]( X_EVENT_BASE_FONT_RESIZED );
                                X_ViewPort_baseFontSize = size;
+                               X_ViewPort_baseFontSize && X_ViewPort[ 'asyncDispatch' ]( X_EVENT_BASE_FONT_RESIZED );
                        };
        },
 
        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 ] );
+               //console.log( '-- orientationchange : ' + X[ 'ViewPort' ][ 'getSize' ][ 0 ] + ' ' + X[ 'ViewPort' ][ 'getSize' ][ 1 ] );
        },
        
        X_ViewPort_document = X_EventDispatcher( document ),
@@ -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' :
@@ -59,20 +63,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,13 +87,18 @@ 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 情報を提供するオブジェクト。
  * @namespace X.ViewPort
  * @alias X.ViewPort
  */
-X.ViewPort = {
+X[ 'ViewPort' ] = {
        
        'listen' : function( type, arg1, arg2, arg3 ){
                if( type <= X_ViewPort_readyState ){
@@ -98,7 +109,7 @@ X.ViewPort = {
                };
                // ie8-では keydown -> documentへ
                type && arg1 && X_ViewPort[ 'listen' ]( type, arg1, arg2, arg3 );
-               return X.ViewPort;
+               return X[ 'ViewPort' ];
        },
        
        
@@ -110,12 +121,12 @@ X.ViewPort = {
                        X_ViewPort[ 'asyncDispatch' ]( type );
                };
                type && arg1 && X_ViewPort[ 'listenOnce' ]( type, arg1, arg2, arg3 );
-               return X.ViewPort;
+               return X[ 'ViewPort' ];
        },
        
        'unlisten' : function( type, arg1, arg2, arg3 ){
                type && arg1 && X_ViewPort[ 'unlisten' ]( type, arg1, arg2, arg3 );
-               return X.ViewPort;
+               return X[ 'ViewPort' ];
        },
        
        'listening' : function( type, arg1, arg2, arg3 ){
@@ -168,17 +179,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 +235,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 +243,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 +258,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() );
                                
@@ -264,7 +275,7 @@ X.ViewPort = {
                                X_ViewPort_lock = false;
                                if( X_ViewPort_orientationFlag ){
                                        X_ViewPort_orientationFlag = false;
-                                       X_ViewPort[ 'asyncDispatch' ]( 100, { type : X_EVENT_VIEW_TURNED, orientation : window.orientation } );
+                                       X_ViewPort[ 'asyncDispatch' ]( 100, { type : X_EVENT_VIEW_TURNED, 'orientation' : window.orientation } );
                                };
                        };
                };
@@ -293,7 +304,7 @@ X.ViewPort = {
 
                        X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
 
-                       html = X[ 'Doc' ][ 'html' ] = X_Node_html = elmHtml && new Node( elmHtml );
+                       html = X[ 'Doc' ][ '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 );
@@ -303,12 +314,12 @@ X.ViewPort = {
                        body[ 'parent ' ] = head[ 'parent' ] = html;
                        html[ '_xnodes' ] = [ head, body ];
                        
-                       html[ 'appendTo' ] = html[ 'before' ] = html[ 'after' ] = html[ 'clone' ] = html[ 'remove' ] = html[ 'destroy' ] = html[ 'prev' ] = html[ 'next' ] =
+                       html[ 'appendTo' ] = html[ 'prev' ] = html[ 'next' ] = html[ 'clone' ] = html[ 'remove' ] = html[ 'destroy' ] =
                        html[ 'create' ] = html[ 'createText' ] = html[ 'createAt' ] = html[ 'createTextAt' ] = html[ 'append' ] = html[ 'appendAt' ] = html[ 'empty' ] = html[ 'html' ] = html[ 'text' ] =
                        html[ 'css' ] = html[ 'cssText' ] =
-                       head[ 'appendTo' ] = head[ 'before' ] = head[ 'after' ] = head[ 'clone' ] = head[ 'remove' ] = head[ 'destroy' ] =
+                       head[ 'appendTo' ] = head[ 'prev' ] = head[ 'next' ] = head[ 'clone' ] = head[ 'remove' ] = head[ 'destroy' ] =
                        head[ 'createText' ] = head[ 'createTextAt' ] = head[ 'empty' ] = head[ 'html' ] = head[ 'text' ] = head[ 'css' ] = head[ 'cssText' ] =
-                       body[ 'appendTo' ] = body[ 'before' ] = body[ 'after' ] = body[ 'clone' ] = body[ 'remove' ] = body[ 'destroy' ] = new Function( 'return this' );
+                       body[ 'appendTo' ] = body[ 'prev' ] = body[ 'next' ] = body[ 'clone' ] = body[ 'remove' ] = body[ 'destroy' ] = new Function( 'return this' );
 
                        X_ViewPort[ 'listenOnce' ]( X_EVENT_PRE_INIT, function(){
                                X_ViewPort_readyState = X_EVENT_XTREE_READY;
@@ -336,6 +347,9 @@ X.ViewPort = {
                                        defaultOverflow = document.body.style.overflow,
                                        w, h;
                        
+                               X_ViewPort_width  = size[ 0 ];
+                               X_ViewPort_height = size[ 1 ];
+                       
                                body.style.overflow = 'hidden';
                                w = body.clientWidth;
                                h = body.clientHeight;
@@ -356,21 +370,20 @@ 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 );
                                };
                                
                                X_ViewPort_baseFontSize = X_Node_fontSizeNode[ '_rawObject' ].offsetHeight;
-                               
-                               X_ViewPort_readyState = X_EVENT_XDOM_READY;
-                               X_ViewPort[ 'asyncDispatch' ]( { type : X_EVENT_XDOM_READY, w : X_ViewPort_width = size[ 0 ], h : X_ViewPort_height = size[ 1 ] } );
+
+                               X_ViewPort[ 'asyncDispatch' ]( X_ViewPort_readyState = X_EVENT_XDOM_READY );
                        } );
 
                        X_ViewPort[ 'asyncDispatch' ]( X_EVENT_PRE_INIT );      
@@ -392,6 +405,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' ] );
                        };
@@ -400,8 +417,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 ];
                };
 
@@ -421,7 +440,7 @@ console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
 if( X_UA_EVENT.W3C ){
        X_ViewPort_document[ 'listenOnce' ]( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
 } else
-if( 6 <= X_UA[ 'IE' ] && X.inHead ){
+if( 6 <= X_UA[ 'IE' ] && X[ 'inHead' ] ){
        // if this script in Head
        document.write( "<script id=__ie_onload defer src=javascript:void(0)><\/script>" );
        X_TEMP._script = document.getElementById( "__ie_onload" );