OSDN Git Service

Version 0.6.133, fix for closure compiler - ADVANCED_OPTIMIZATIONS
[pettanr/clientJs.git] / 0.6.x / js / 02_dom / 04_XBoxModel.js
index f7fa20b..85b0c9c 100644 (file)
@@ -26,6 +26,20 @@ X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
                        'box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;-o-box-sizing:border-box;-ms-box-sizing:border-box;' )\r
                                                                                        .width() === 10;\r
        };\r
+       \r
+       /*\r
+        * 古い Gekco、 Presto、 WebKit では影がレイアウトに影響します。たとえば、width が 100% のボックスに外向きの box-shadow を指定すると、横スクロールバーが表示されてしまいます。\r
+        * TODO boxShadow が有効な要素に対して offsetWidth 等の補正(?)\r
+        */\r
+       if( X_Node_CSS_Support[ 'boxShadow' ] &&\r
+               node.cssText(\r
+                       X_Node_CSS_uncamelize( X_Node_CSS_VENDER_PREFIX[ 'boxShadow' ] ) + ':10px 10px 0 0 #000;width:10px;'\r
+                               ).width() !== 10\r
+        ){\r
+               console.log( node.cssText() + node.width() );\r
+               X_Node_CSS_Support[ 'boxShadowLayoutBug' ] = true;\r
+       };\r
+\r
        // padding\r
        // border\r
        // margin\r
@@ -37,6 +51,7 @@ X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
                        .firstChild().cssText( 'position:absolute;top:8px;left:8px;margin:16px;border:32px solid #666;padding:64px;' )\r
                        .y();\r
 \r
+\r
        node.cssText( '' ).empty();\r
 });\r
 \r
@@ -49,19 +64,13 @@ X_ViewPort.listenOnce( X_TEMP.SYSTEM_EVENT_INIT, function(){
  * getBoundingClientRect\r
  */\r
 Node.prototype.width = function(){\r
-       if( !this.parent ){// todo : _state で tree に所属しているか?判定\r
-               console.log( 'xnode.width() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.width() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+       \r
        if( X_UA_DOM.W3C ){\r
-               // this.css( X_Node_CSS_Unit.px, 'width' );\r
-               return this._rawObject.offsetWidth;\r
+               // TODO width : length + overflow : hidden ならそれを返す? <- block or inline\r
+               return this._rawObject ? this._rawObject.offsetWidth : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).offsetWidth;\r
@@ -71,19 +80,13 @@ Node.prototype.width = function(){
 };\r
 \r
 Node.prototype.height = function(){\r
-       if( !this.parent ){\r
-               console.log( 'xnode.height() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.height() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+       \r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'height' );\r
-               return this._rawObject.offsetHeight;\r
+               return this._rawObject ? this._rawObject.offsetHeight : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).offsetHeight;\r
@@ -93,19 +96,13 @@ Node.prototype.height = function(){
 };\r
 \r
 Node.prototype.clientWidth = function(){\r
-       if( !this.parent ){// todo : _state で tree に所属しているか?判定\r
-               console.log( 'xnode.width() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.width() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+       \r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'width' );\r
-               return this._rawObject.clientWidth;\r
+               return this._rawObject ? this._rawObject.clientWidth : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).clientWidth;\r
@@ -115,19 +112,13 @@ Node.prototype.clientWidth = function(){
 };\r
 \r
 Node.prototype.clientHeight = function(){\r
-       if( !this.parent ){\r
-               console.log( 'xnode.height() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.height() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+\r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'height' );\r
-               return this._rawObject.clientHeight;\r
+               return this._rawObject ? this._rawObject.clientHeight : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).clientHeight;\r
@@ -137,19 +128,13 @@ Node.prototype.clientHeight = function(){
 };\r
 \r
 Node.prototype.scrollWidth = function(){\r
-       if( !this.parent ){// todo : _state で tree に所属しているか?判定\r
-               console.log( 'xnode.width() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.width() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+\r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'width' );\r
-               return this._rawObject.scrollWidth;\r
+               return this._rawObject ? this._rawObject.scrollWidth : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).scrollWidth;\r
@@ -159,19 +144,12 @@ Node.prototype.scrollWidth = function(){
 };\r
 \r
 Node.prototype.scrollHeight = function(){\r
-       if( !this.parent ){\r
-               console.log( 'xnode.height() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.height() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+\r
        if( X_UA_DOM.W3C ){\r
-               // this.css( X_Node_CSS_Unit.px, 'height' );\r
-               return this._rawObject.scrollHeight;\r
+               return this._rawObject ? this._rawObject.scrollHeight : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).scrollHeight;\r
@@ -181,19 +159,13 @@ Node.prototype.scrollHeight = function(){
 };\r
 \r
 Node.prototype.scrollLeft = function(){\r
-       if( !this.parent ){// todo : _state で tree に所属しているか?判定\r
-               console.log( 'xnode.scrollLeft() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.scrollLeft() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+\r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'width' );\r
-               return this._rawObject.scrollLeft;\r
+               return this._rawObject ? this._rawObject.scrollLeft : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).scrollLeft;\r
@@ -203,19 +175,13 @@ Node.prototype.scrollLeft = function(){
 };\r
 \r
 Node.prototype.scrollTop = function(){\r
-       if( !this.parent ){// todo : _state で tree に所属しているか?判定\r
-               console.log( 'xnode.scrollTop() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.scrollTop() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+       \r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'width' );\r
-               return this._rawObject.scrollTop;\r
+               return this._rawObject ? this._rawObject.scrollTop : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).scrollTop;\r
@@ -233,22 +199,17 @@ Node.prototype.scrollTop = function(){
  */\r
 // X_Node_CSS_transform,\r
 Node.prototype.x = function(){\r
-       if( !this.parent ){\r
-               console.log( 'xnode.x() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.x() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+       \r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'left' );\r
                // this.css( X_Node_CSS_Unit.px, 'translateX' );\r
-               return this._rawObject.offsetLeft;\r
+               return this._rawObject ? this._rawObject.offsetLeft : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
+               // pixelLeft http://www.din.or.jp/~hagi3/JavaScript/JSTips/DHTML/ProbIE5.htm\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).offsetLeft;\r
        } else {\r
                \r
@@ -256,20 +217,14 @@ Node.prototype.x = function(){
 };\r
 \r
 Node.prototype.y = function(){\r
-       if( !this.parent ){\r
-               console.log( 'xnode.y() : no parent' );\r
-               return 0;\r
-       };\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return 0;\r
+       \r
        X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.y() : not belong tree.' );\r
-               return 0;\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+       \r
        if( X_UA_DOM.W3C ){\r
                // this.css( X_Node_CSS_Unit.px, 'top' );\r
                // this.css( X_Node_CSS_Unit.px, 'transisitonY' );\r
-               return this._rawObject.offsetTop;\r
+               return this._rawObject ? this._rawObject.offsetTop : 0;\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
                return ( this._rawObject || X_Node__ie4getRawNode( this ) ).offsetTop;          \r
@@ -279,36 +234,80 @@ Node.prototype.y = function(){
 };\r
 \r
 Node.prototype.offset = function( /* xnodeParent */ ){\r
-       var x = 0, y = 0, elm;\r
-       \r
-       if( !this.parent ){\r
-               console.log( 'xnode.offset() : no parent' );\r
-               return { x : 0, y : 0 };\r
-       };\r
-       X_Node_updateTimerID && X_Node_startUpdate();\r
-       if( !this._root ){\r
-               console.log( 'xnode.offset() : not belong tree.' );\r
-               return { x : 0, y : 0 };\r
-       };\r
-       if( this._state & X_Node_State.DISPLAY_NONE ) return 0;\r
+\r
+       if( ( this._flags & X_Node_State.IN_TREE ) === 0 || this._flags & X_Node_State.STYLE_IS_DISPLAY_NONE ) return { x : 0, y : 0 };\r
        \r
        if( X.Doc.body === this || X.Doc.html === this ){\r
                return { x : 0, y : 0 };\r
        };\r
        \r
+       X_Node_updateTimerID && X_Node_startUpdate();\r
+       \r
        if( X_UA_DOM.W3C ){\r
-               elm = this._rawObject;\r
+               return this._rawObject ? X_Node_getPosition( this._rawObject ) : { x : 0, y : 0 };\r
        } else\r
        if( X_UA_DOM.IE4 ){\r
-               elm = this._rawObject || X_Node__ie4getRawNode( this );         \r
+               return X_Node_getPosition( this._rawObject || X_Node__ie4getRawNode( this ) );\r
        } else {\r
                \r
        };\r
-       \r
-       while( elm && elm !== document.body ){\r
-               x += elm.offsetLeft;\r
-               y += elm.offsetTop;\r
-               elm = elm.offsetParent || elm.parentNode || elm.parentElement;\r
-       };\r
-       return { x : x, y : y };\r
 };\r
+\r
+// エレメントの座標取得 ~スクロール要素~\r
+// http://n-yagi.0r2.net/script/2009/06/post_14.html\r
+\r
+//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\r
+//  エレメントの絶対座標を得たい\r
+//------------------------------------------------------------------------------\r
+//  座標取得\r
+var X_Node_getPosition =\r
+       document.documentElement && document.documentElement.getBoundingClientRect ?\r
+               function( el ){\r
+               var pos  = el.getBoundingClientRect(),\r
+                       html = document.documentElement,\r
+                       body = document.body;\r
+               return  {   x:(pos.left +   (body.scrollLeft||html.scrollLeft)  -   html.clientLeft)\r
+                       ,   y:(pos.top  +   (body.scrollTop||html.scrollTop)    -   html.clientTop) };\r
+               } :\r
+       X_UA[ 'Opera' ] < 10 ?\r
+               function( el ){\r
+            var ex  =   0;\r
+            var ey  =   0;\r
+            do\r
+            { \r
+                ex  +=  el.offsetLeft;\r
+                ey  +=  el.offsetTop;\r
+            }\r
+            while(  el  =   el.offsetParent );\r
+            //\r
+            return  {x:ex,y:ey};\r
+               } :\r
+               function(target)\r
+        {\r
+            var ex  =   0;\r
+            var ey  =   0;\r
+            //\r
+            var el  =   target;\r
+            var bd  =   document.body;\r
+            \r
+            do\r
+            { \r
+                ex  +=  el.offsetLeft   ||  0;\r
+                ey  +=  el.offsetTop    ||  0;\r
+            }\r
+            while(  el  =   el.offsetParent );\r
+            //  要素内スクロール対応\r
+            el  =   target;\r
+            do\r
+            {\r
+                ex  -=  el.scrollLeft   ||  0;\r
+                ey  -=  el.scrollTop    ||  0;\r
+                el  =   el.parentNode;\r
+            }\r
+            while(  el!=bd  );\r
+            //\r
+            return  {x:ex,y:ey};\r
+        };\r
+\r
+//■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■\r
+\r