OSDN Git Service

Version 0.6.133, fix for closure compiler - ADVANCED_OPTIMIZATIONS
[pettanr/clientJs.git] / 0.6.x / js / 01_core / 16_XViewPort.js
index 226bf4f..21a3547 100644 (file)
@@ -6,11 +6,13 @@ var X_ViewPort_readyState,
        X_ViewPort_width,
        X_ViewPort_height,
        X_ViewPort_baseFontSize,
+       X_ViewPort_vScrollbarSize,
+       X_ViewPort_hScrollbarSize,
        
-       X_Dom_detectFontSize = !( X.UA.IE < 9 || X.UA.iOS ) && function(){
-                       var size = Node._fontSizeNode._rawObject.offsetHeight;
+       X_Dom_detectFontSize = !( X_UA[ 'IE' ] < 9 || X_UA[ 'iOS' ] ) && function(){
+                       var size = X_Node_fontSizeNode._rawObject.offsetHeight;
                        if( X_ViewPort_baseFontSize !== size ){
-                               X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( { type : X.Event.BASE_FONT_RESIZED, fontSize : size, w : X_ViewPort_width, h : X_ViewPort_height } );
+                               X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( X_Event.BASE_FONT_RESIZED );
                                X_ViewPort_baseFontSize = size;
                        };
        },
@@ -18,46 +20,60 @@ var X_ViewPort_readyState,
        X_ViewPort_orientationFlag,     
        X_Dom_orientationchange = window[ 'orientation' ] !== undefined && function( e ){
                X_ViewPort_orientationFlag = true;
-               !X.UA.Android && X_ViewPort_resize();
+               !X_UA[ 'Android' ] && X_ViewPort_resize();
                //console.log( '-- orientationchange : ' + X.ViewPort.getSize[ 0 ] + ' ' + X.ViewPort.getSize[ 1 ] );
-       };
+       },
+       
+       X_ViewPort_document = new X.EventDispatcher( document ),
 
-X_ViewPort = X.Class._override(
+X_ViewPort = X_Class_override(
        new X.EventDispatcher( window ),
        {
 
                handleEvent : function( e ){
-                       var href;
+                       var href, i, name;
+                       
                        switch( e.type ){
                                case 'beforeunload' :
-                                       
                                        // ie では a href="javascript" な要素でも beforeunload が起こる
                                        href = e.target && e.target.attr && e.target.attr( 'href' );
                                        if( href && href.indexOf && href.indexOf( 'javascript:' ) === 0 ) return X.Callback.PREVENT_DEFAULT | X.Callback.STOP_PROPAGATION;
                                        
-                                       return X_ViewPort.dispatch( X.Event.BEFORE_UNLOAD );
+                                       return X_ViewPort.dispatch( X_Event.BEFORE_UNLOAD );
+                                       
                                case 'unload' :
-                                       X_ViewPort.dispatch( X.Event.UNLOAD );
+                                       X_ViewPort.dispatch( X_Event.UNLOAD );
+                               //alert('unload');
+                                       X_ViewPort_document.kill();
+                                       this.kill();
                                        
-                                       X.ViewPort.unlisten();
-                                       Node._document.unlisten();
-                                       Node._html._actualRemove( true );
+                                       //X_System.dispatch( X_Event.SHUT_DOWN );
                                        break;
+
                                case 'visibilitychange' :
-                                       X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'hidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
+                                       X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'hidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
+                                       break;
+                               case 'mozvisibilitychange' :
+                                       X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'mozHidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
                                        break;
                                case 'webkitvisibilitychange' :
-                                       X_ViewPort.dispatch( { type : ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X.Event.VIEW_DEACTIVATE : X.Event.VIEW_ACTIVATE } );
+                                       X_ViewPort.dispatch( ( X_ViewPort_active = document[ 'webkitHidden' ] ) ? X_Event.VIEW_DEACTIVATE : X_Event.VIEW_ACTIVATE );
                                        break;
+                                       
                                case 'pageshow' :
                                case 'focus' :
-                                       X_ViewPort_active = true;
-                                       X_ViewPort.dispatch( X.Event.VIEW_ACTIVATE );
+                                       if( !X_ViewPort_active ){
+                                               X_ViewPort_active = true;
+                                               X_ViewPort.dispatch( X_Event.VIEW_ACTIVATE );
+                                       };
                                        break;
+
                                case 'pagehide' :
                                case 'blur' :
-                                       X_ViewPort_active = false;
-                                       X_ViewPort.dispatch( X.Event.VIEW_DEACTIVATE );
+                                       if( X_ViewPort_active ){
+                                               X_ViewPort_active = false;
+                                               X_ViewPort.dispatch( X_Event.VIEW_DEACTIVATE );                         
+                                       };
                                        break;
                        };
                }
@@ -66,15 +82,21 @@ X_ViewPort = X.Class._override(
 );
 
 
+/**
+ * window に相当する ViewPort 情報を提供するオブジェクト。
+ * @namespace X.ViewPort
+ * @alias X.ViewPort
+ */
 X.ViewPort = {
        
        listen : function( type, arg1, arg2, arg3 ){
                if( type <= X_ViewPort_readyState ){
                        /*
-                        * X.Event.XDOM_READY 以後に listen した場合の対策
+                        * X_Event.XDOM_READY 以後に listen した場合の対策
                         */
-                       X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
+                       X_ViewPort.asyncDispatch( type );
                };
+               // ie8-では keydown -> documentへ
                type && arg1 && X_ViewPort.listen( type, arg1, arg2, arg3 );
                return X.ViewPort;
        },
@@ -85,7 +107,7 @@ X.ViewPort = {
                        /*
                         * X.Event.XDOM_READY 以後に listen した場合の対策
                         */
-                       X_ViewPort.asyncDispatch( { type : type, w : X_ViewPort_width, h : X_ViewPort_height } );
+                       X_ViewPort.asyncDispatch( type );
                };
                type && arg1 && X_ViewPort.listenOnce( type, arg1, arg2, arg3 );
                return X.ViewPort;
@@ -100,6 +122,7 @@ X.ViewPort = {
                return X_ViewPort.listening( type, arg1, arg2, arg3 );
        },
        
+       // hammer のテストファイルだけが使用
        asyncDispatch : function(){
                return X_ViewPort.asyncDispatch.apply( X_ViewPort, arguments );
        },
@@ -108,7 +131,9 @@ X.ViewPort = {
                
        },
        
-       /* 要素が視界に入った  http://remysharp.com/2009/01/26/element-in-view-event-plugin/ */
+       /* 要素が視界に入った  http://remysharp.com/2009/01/26/element-in-view-event-plugin/
+        * TODO -> Node.call('inView')
+        */
        inView : function( elm ){
                
        },
@@ -127,8 +152,8 @@ X.ViewPort = {
        //http://onozaty.hatenablog.com/entry/20060803/p1
        // Safari2.0.4では標準・互換どちらも document.body
                
-               Node._body._updateTimerID && Node._body._startUpdate();
-               /*X.UA.Opera ?
+               X_Node_updateTimerID && X_Node_startUpdate();
+               /*X_UA[ 'Opera' ] ?
                        ( document.documentElement && document.documentElement.clientWidth ?
                                new Function( 'return[document.documentElement.clientWidth,document.documentElement.clientHeight]' ) :
                                new Function( 'return[document.body.clientWidth,document.body.clientHeight]' )
@@ -142,28 +167,28 @@ X.ViewPort = {
        getScrollPosition :
                window.pageXOffset !== undefined ?
                        ( function(){
-                               Node._body._updateTimerID && Node._body._startUpdate();
+                               X_Node_updateTimerID && X_Node_startUpdate();
                                return[ window.pageXOffset, window.pageYOffset ];
                        } ) :
                window.scrollLeft  !== undefined ?
                        ( function(){
-                               Node._body._updateTimerID && Node._body._startUpdate();
+                               X_Node_updateTimerID && X_Node_startUpdate();
                                return[ window.scrollLeft, window.scrollTop ];
                        } ) :
                        ( function(){
-                               Node._body._updateTimerID && Node._body._startUpdate();
+                               X_Node_updateTimerID && X_Node_startUpdate();
                                // body は Safari2-
                                return[ X_ViewPort_rootElement.scrollLeft || document.body.scrollLeft, X_ViewPort_rootElement.scrollTop || document.body.scrollTop ];
                        } ),
 
        getScrollbarSize : function(){
-               return [ X.Dom.BoxModel.vScrollbarSize, X.Dom.BoxModel.hScrollbarSize ];        
+               return [ X_ViewPort_vScrollbarSize, X_ViewPort_hScrollbarSize ];        
        },
        
        getBaseFontSize : function(){
-               if( Node._body._updateTimerID ){
-                       Node._body._startUpdate();
-                       return X_ViewPort_baseFontSize = Node._fontSizeNode._rawObject.offsetHeight;
+               if( X_Node_updateTimerID ){
+                       X_Node_startUpdate();
+                       return X_ViewPort_baseFontSize = X_Node_fontSizeNode._rawObject.offsetHeight;
                };
                return X_ViewPort_baseFontSize;
        }
@@ -199,7 +224,7 @@ X.ViewPort = {
  */
                var X_ViewPort_resize =
                        // iOS もループで回す,,,iOS3.1.3, iOS6 で確認
-                       X.UA.IE < 9 || X.UA.iOS ?
+                       X_UA[ 'IE' ] < 9 || X_UA[ 'iOS' ] ?
                                (function(){
                                        var size;
                                        if( !X_ViewPort_lock ){
@@ -212,9 +237,9 @@ X.ViewPort = {
                                                };
                                        };
                                        
-                                       size = Node._fontSizeNode._rawObject.offsetHeight;
+                                       size = X_Node_fontSizeNode._rawObject.offsetHeight;
                                        if( X_ViewPort_baseFontSize !== size ){
-                                               X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( { type : X.Event.BASE_FONT_RESIZED, fontSize : size, w : X_ViewPort_width, h : X_ViewPort_height  } );
+                                               X_ViewPort_baseFontSize && X_ViewPort.asyncDispatch( X_Event.BASE_FONT_RESIZED );
                                                X_ViewPort_baseFontSize = size;
                                        };
                                        
@@ -235,11 +260,11 @@ X.ViewPort = {
                        } else {
                                console.log( '-- detectFinishResizing : ' + X_Timer_now() );
                                
-                               X_ViewPort.asyncDispatch( { type : X.Event.VIEW_RESIZED, fontSize : X_ViewPort_baseFontSize, w : X_ViewPort_width, h : X_ViewPort_height } );
+                               X_ViewPort.asyncDispatch( X_Event.VIEW_RESIZED );
                                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 } );
                                };
                        };
                };
@@ -247,11 +272,13 @@ X.ViewPort = {
                X_TEMP.onDomContentLoaded = function(){
                        var s, size, elmHtml, elmHead, elmBody, html, head, body;
                        
+                       console.log( '> X_TEMP.onDomContentLoaded rs:' + X_ViewPort_readyState );
+                       
                        if( X_TEMP.SYSTEM_EVENT_PRE_INIT <= X_ViewPort_readyState ) return X_Callback_UN_LISTEN;
                        X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_PRE_INIT;
                        
                        // DOMContentLoaded の無いブラウザで X_TEMP.onDomContentLoaded への参照が残り続けるのを回避
-                       X_TEMP.onDomContentLoaded && Node._document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
+                       X_TEMP.onDomContentLoaded && X_ViewPort_document.unlisten( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
                        delete X_TEMP.onDomContentLoaded;
                        
                        elmHtml = document.documentElement ||
@@ -266,92 +293,157 @@ X.ViewPort = {
 
                        X_ViewPort_rootElement = document.compatMode !== 'CSS1Compat' ? elmBody : elmHtml || elmBody;
 
-                       html = Node._html = elmHtml && new Node( elmHtml );
-               
-                       head = Node._head = elmHead && new Node( elmHead );
-               
-                       body = Node._body = new Node( elmBody );
+                       html = X.Doc.html = X_Node_html = elmHtml && new Node( elmHtml );
+                       html._flags |= X_Node_State.IN_TREE;
                        
-                       body._root   = html._root = head._root = body;// _root は html だろ
+                       head = X.Doc.head = X_Node_head = elmHead && new Node( elmHead );
+               
+                       body = X.Doc.body = X_Node_body = new Node( elmBody );
+
                        body.parent  = head.parent = html;
-                       html._xnodes = [ head, body ];                  
+                       html._xnodes = [ head, body ];
                        
-                       Node.root = body; // 後方互換
-                       
-                       html.appendTo = html.appendToRoot = html.before = html.after = html.clone = html.remove = html.destroy = html.prevNode = html.nextNode =
+                       html.appendTo = html.appendToRoot = html.before = html.after = html.clone = html.remove = html.destroy = html.prev = html.next =
                        html.create = html.createText = html.createAt = html.createTextAt = html.append = html.appendAt = html.empty = html.html = html.text =
-                       head.appendTo = head.appendToRoot = head.before = head.after = head.clone = head.remove = head.destroy = head.prevNode = head.nextNode =
-                       head.empty = head.html = head.text =
-                       body.appendTo = body.appendToRoot = body.before = body.after = body.clone = body.remove = body.destroy = body.prevNode = body.nextNode = new Function( 'return this' );
+                       html.css = html.cssText =
+                       head.appendTo = head.appendToRoot = head.before = head.after = head.clone = head.remove = head.destroy =
+                       head.createText = head.createTextAt = head.empty = head.html = head.text = head.css = head.cssText =
+                       body.appendTo = body.appendToRoot = body.before = body.after = body.clone = body.remove = body.destroy = new Function( 'return this' );
 
-                       X.ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_PRE_INIT, function(){
+                       X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_PRE_INIT, function(){
                                X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_XTREE;
                                if( X_TEMP.X_Dom_useBuilder ) X_TEMP.X_Dom_useBuilder = !!document.body.children.length;
                                !X_TEMP.X_Dom_useBuilder && X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_XTREE );
                        } );
                        
-                       X.ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_XTREE, function(){
+                       X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_XTREE, function(){
                                X_ViewPort_readyState = X_TEMP.SYSTEM_EVENT_INIT;
-                               //X.UA.Opera7 && alert( 'bc' );
-                               Node._body.appendAt( 0,
-                                       Node._systemNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ),
-                                       Node._fontSizeNode = Node.create( 'div', { 'class' : 'hidden-system-node' } ).cssText( 'line-height:1;height:1em;' ).text( 'X' )
-                               )._startUpdate();
+                               //X_UA[ 'Opera7' ] && alert( 'bc' );
+                               X_Node_body.appendAt( 0,
+                                       X_Node_systemNode = X_Doc_create( 'div', { 'class' : 'hidden-system-node' } ),
+                                       X_Node_fontSizeNode = X_Doc_create( 'div', { 'class' : 'hidden-system-node' } ).cssText( 'line-height:1;height:1em;' ).text( 'X' )
+                               );
+                               X_Node_startUpdate();
 
                                X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_INIT );
                        } );
 
-                       X.ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
-                               var size = X_ViewPort_getWindowSize();
+                       X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
+                               var size = X_ViewPort_getWindowSize(),
+                               
+                               // http://jsdo.it/imaya/kTYg
+                                       body = document.body,
+                                       defaultOverflow = document.body.style.overflow,
+                                       w, h;
+                       
+                               body.style.overflow = 'hidden';
+                               w = body.clientWidth;
+                               h = body.clientHeight;
+                       
+                               body.style.overflow = 'scroll';
+                               w -= body.clientWidth;
+                               h -= body.clientHeight;
+                       
+                               if( !w ) w = body.offsetWidth  - body.clientWidth;
+                               if( !h ) h = body.offsetHeight - body.clientHeight;
+                               body.style.overflow = defaultOverflow; 
+                       
+                               X_ViewPort_vScrollbarSize = w;
+                               X_ViewPort_hScrollbarSize = h;
+                               if( h <= 0 ){ // ie6, ie11, firefox で 負の値が返る
+                                       console.log( 'invalid hScrollbarSize: ' + h );
+                                       X_ViewPort_hScrollbarSize = w;
+                               };
                                
+                               //
                                if( X_Dom_orientationchange ){
-                                       X.ViewPort.listen( 'orientationchange', X_Dom_orientationchange );
+                                       X_EventDispatcher_systemListen( X_ViewPort, 'orientationchange', X_Dom_orientationchange );
                                };
                                
                                if( X_Dom_detectFontSize ){
-                                       X.ViewPort.listen( 'resize', X_ViewPort_resize );
+                                       X_EventDispatcher_systemListen( X_ViewPort, 'resize', X_ViewPort_resize );
                                        X.Timer.add( 333, X_Dom_detectFontSize );
                                } else {
                                        X.Timer.add( 333, X_ViewPort_resize );
                                };
                                
-                               X_ViewPort_baseFontSize = Node._fontSizeNode._rawObject.offsetHeight;
+                               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_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 ] } );
                        } );
 
-                       console.log( X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT ) );        
+                       X_ViewPort.asyncDispatch( X_TEMP.SYSTEM_EVENT_PRE_INIT );       
+
 
-                       X.ViewPort
-                               .listen( 'beforeunload', X_ViewPort )
-                               .listenOnce( 'unload', X_ViewPort );
+                       X_EventDispatcher_systemListen( X_ViewPort, [ 'beforeunload', 'unload' ] );
 
 //ブラウザの戻るボタンで戻ったときに呼ばれるイベントとかキャッシュとかそこらへんのこと
 //http://d.hatena.ne.jp/koumiya/20080916/1221580149
 
-                       if( document[ 'hidden' ] !== undefined ) {// iOS 7+
-                               Node._document.listen( 'visibilitychange', X_ViewPort );
+                       if( document[ 'hidden' ] !== undefined ){// iOS 7+
+                               X_EventDispatcher_systemListen( X_ViewPort_document, 'visibilitychange', X_ViewPort );
                        } else
-                       if( document[ 'webkitHidden' ] !== undefined ) {
-                               Node._document.listen( 'webkitvisibilitychange', X_ViewPort );
+                       if( document[ 'mozHidden' ] !== undefined ){
+                               X_EventDispatcher_systemListen( X_ViewPort_document, 'mozvisibilitychange', X_ViewPort );
                        } else
-                       if( X.UA.iOS && window[ 'onpageshow' ] !== undefined ) {
-                               X.ViewPort.listen( 'pageshow', X_ViewPort )
-                                                       .listen( 'pagehide', X_ViewPort );      
+                       if( document[ 'webkitHidden' ] !== undefined ){
+                               X_EventDispatcher_systemListen( X_ViewPort_document, 'webkitvisibilitychange', X_ViewPort );
+                       } else
+                       if( X_UA[ 'iOS' ] && window[ 'onpageshow' ] !== undefined ){
+                               X_EventDispatcher_systemListen( X_ViewPort, [ 'pageshow', 'pagehide' ] );
                        } else {
-                               X.ViewPort.listen( 'focus', X_ViewPort )
-                                                       .listen( 'blur', X_ViewPort );
+                               X_EventDispatcher_systemListen( X_ViewPort, [ 'focus', 'blur' ] );
                        };
 
                        return X_Callback_UN_LISTEN;
                };
 
                function X_ViewPort_getWindowSize(){
-                       return X.UA.IE ?
+                       return X_UA[ 'IE' ] ?
                                [ X_ViewPort_rootElement.clientWidth, X_ViewPort_rootElement.clientHeight ] :
                                [ window.innerWidth, window.innerHeight ];
                };
 
 
 console.log( 'X.Dom dom:w3c=' + X_UA_DOM.W3C + ' ev:w3c=' + X_UA_EVENT.W3C );
+
+/* -----------------------------------------------
+ * Document Ready
+ *  Dean Edwards/Matthias Miller/John Resig
+ */
+
+// SafariでJavaScriptのデバッグをする方法
+// safari1.3 可
+// http://shimax.cocolog-nifty.com/search/2006/09/safarijavascrip_c54d.html
+
+/* for ie9+/Mozilla/Opera9 */
+if( X_UA_EVENT.W3C ){
+       X_ViewPort_document.listenOnce( 'DOMContentLoaded', X_TEMP.onDomContentLoaded );
+} else
+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" );
+       X_TEMP._script.onreadystatechange = function(){
+               var s = X_TEMP._script;
+               if( s && s.readyState === 'complete' ){
+                       s.onreadystatechange = X.emptyFunction;
+                       s.onreadystatechange = null;
+                       s.parentNode.removeChild( s );
+                       delete X_TEMP._script;
+                       X_TEMP.onDomContentLoaded && X_TEMP.onDomContentLoaded();
+               };
+       };
+};
+// Re: onLoad doesn't work with Safari?
+// http://lists.apple.com/archives/web-dev/2003/Oct/msg00036.html
+if( X_UA[ 'WebKit' ] || X_UA[ 'Safari' ]  < 3 ){ // sniff
+       X.Timer.add( 16, function(){
+               if( !X_TEMP.onDomContentLoaded ) return X_Callback_UN_LISTEN;
+               if( document.readyState === 'loaded' || document.readyState === 'complete' ) return X_TEMP.onDomContentLoaded();
+       });
+};
+
+/* for other browsers */
+X_ViewPort.listenOnce( 'load', X_TEMP.onDomContentLoaded );