module Webglimpse { function childWidth ( child ) { var usePrefWidth = ( ! hasval ( child . layoutOptions ) || child . layoutOptions . width === undefined || child . layoutOptions . width === 'pref' ) ; return ( usePrefWidth ? child . prefSize . w : child . layoutOptions . width ) ; } export function newColumnLayout ( leftToRight = true ) { return { updatePrefSize : function ( parentPrefSize , children ) { var childrenToPlace = < LayoutEntry [ ] > [ ] ; for ( var c = 0 ; c < children . length ; c ++ ) { var child = children [ c ] ; if ( isNumber ( child . layoutArg ) && ! ( child . layoutOptions && child . layoutOptions . hide ) ) { childrenToPlace . push ( child ) ; } } var hMax = 0 ; var wSum = 0 ; for ( var c = 0 ; c < childrenToPlace . length ; c ++ ) { var child = childrenToPlace [ c ] ; var honorChildHeight = ! ( child . layoutOptions && child . layoutOptions . ignoreHeight ) ; if ( honorChildHeight ) { var h = child . prefSize . h ; if ( hasval ( hMax ) && hasval ( h ) ) { hMax = Math . max ( hMax , h ) ; } else { hMax = null ; } } var w = childWidth ( child ) ; if ( hasval ( wSum ) && hasval ( w ) ) { wSum += w ; } else { wSum = null ; } } parentPrefSize . w = wSum ; parentPrefSize . h = hMax ; } , updateChildViewports : function ( children , parentViewport ) { var childrenToPlace = < LayoutEntry [ ] > [ ] ; var childrenToHide = < LayoutEntry [ ] > [ ] ; for ( var c = 0 ; c < children . length ; c ++ ) { var child = children [ c ] ; if ( isNumber ( child . layoutArg ) && ! ( child . layoutOptions && child . layoutOptions . hide ) ) { childrenToPlace . push ( child ) ; } else { childrenToHide . push ( child ) ; } } var indexProp = 'webglimpse_columnLayout_index' ; for ( var c = 0 ; c < childrenToPlace . length ; c ++ ) { var child = childrenToPlace [ c ] ; child [ indexProp ] = c ; } childrenToPlace . sort ( function ( a , b ) { var orderDiff = a . layoutArg - b . layoutArg ; return ( orderDiff !== 0 ? orderDiff : ( a [ indexProp ] - b [ indexProp ] ) ) ; } ) ; var numFlexible = 0 ; var totalFlexWidth = parentViewport . w ; for ( var c = 0 ; c < childrenToPlace . length ; c ++ ) { var w = childWidth ( childrenToPlace [ c ] ) ; if ( hasval ( w ) ) { totalFlexWidth -= w ; } else { numFlexible ++ ; } } var flexWidth = totalFlexWidth / numFlexible ; if ( leftToRight ) { var jStart = parentViewport . jStart ; var jEnd = parentViewport . jEnd ; var iStart = parentViewport . iStart ; var iRemainder = 0 ; for ( var c = 0 ; c < childrenToPlace . length ; c ++ ) { var child = childrenToPlace [ c ] ; var iEnd ; var w = childWidth ( child ) ; if ( hasval ( w ) ) { iEnd = iStart + w ; } else { var iEnd0 = iStart + flexWidth + iRemainder ; iEnd = Math . round ( iEnd0 ) ; iRemainder = iEnd0 - iEnd ; } child . viewport . setEdges ( iStart , iEnd , jStart , jEnd ) ; iStart = iEnd ; } } else { var jStart = parentViewport . jStart ; var jEnd = parentViewport . jEnd ; var iEnd = parentViewport . iEnd ; var iRemainder = 0 ; for ( var c = 0 ; c < childrenToPlace . length ; c ++ ) { var child = childrenToPlace [ c ] ; var iStart ; var w = childWidth ( child ) ; if ( hasval ( w ) ) { iStart = iEnd - w ; } else { var iStart0 = iEnd - flexWidth - iRemainder ; iStart = Math . round ( iStart0 ) ; iRemainder = iStart - iStart0 ; } child . viewport . setEdges ( iStart , iEnd , jStart , jEnd ) ; iEnd = iStart ; } } for ( var c = 0 ; c < childrenToHide . length ; c ++ ) { childrenToHide [ c ] . viewport . setEdges ( 0 , 0 , 0 , 0 ) ; } } } ; } }