@@ -74,13 +74,13 @@ var FTColumnflow = (function () {
74
74
} ,
75
75
76
76
// CSS Style declarations
77
- cssStyles = '#[targetId] { position: relative; height: 100%; }\n'
78
- + '#[targetId] .[preloadAreaClassName].[pageClass] { visibility: hidden; position: absolute; overflow: hidden; }\n'
79
- + '#[targetId] .[preloadFixedAreaClassName] { visibility: hidden; position: absolute; }\n'
80
- + '#[targetId] .[pageClass] { position: absolute; width: [viewportWidth]px; height: [viewportHeight]px; [pageArrangement] }\n'
81
- + '#[targetId] .[columnClass] { position: absolute; width: [columnWidth]px; overflow: hidden; }\n'
82
- + '#[targetId] .[pageClass] .[fixedElementClassName] { position: absolute; }\n'
83
- + '#[targetId] .[pageClass] .[columnClass] > :first-child { margin-top: 0px; }\n' ,
77
+ cssStyles = '#[targetId] { position: relative; height: 100%; }\n' +
78
+ '#[targetId] .[preloadAreaClassName].[pageClass] { visibility: hidden; position: absolute; overflow: hidden; }\n' +
79
+ '#[targetId] .[preloadFixedAreaClassName] { visibility: hidden; position: absolute; }\n' +
80
+ '#[targetId] .[pageClass] { position: absolute; width: [viewportWidth]px; height: [viewportHeight]px; [pageArrangement] }\n' +
81
+ '#[targetId] .[columnClass] { position: absolute; width: [columnWidth]px; overflow: hidden; }\n' +
82
+ '#[targetId] .[pageClass] .[fixedElementClassName] { position: absolute; }\n' +
83
+ '#[targetId] .[pageClass] .[columnClass] > :first-child { margin-top: 0px; }\n' ,
84
84
85
85
cssColumnStyles = '#[targetId] .[columnClass].[columnClass]-[columnNum] { left: [leftPos]px; }\n' ,
86
86
@@ -124,7 +124,6 @@ var FTColumnflow = (function () {
124
124
fixedContent ,
125
125
126
126
// Dimensions
127
- maxColumnHeight ,
128
127
colDefaultBottom ,
129
128
colMiddle ,
130
129
minFixedPadding ,
@@ -361,6 +360,8 @@ var FTColumnflow = (function () {
361
360
config . layoutDimensions . columnWidth = ( ( config . layoutDimensions . pageInnerWidth + config . layoutDimensions . columnGap ) / config . layoutDimensions . columnCount ) - config . layoutDimensions . columnGap ;
362
361
}
363
362
}
363
+
364
+ config . layoutDimensions . columnHeight = config . lineHeight ? _roundDownToGrid ( config . layoutDimensions . pageInnerHeight ) : config . layoutDimensions . pageInnerHeight ;
364
365
}
365
366
366
367
@@ -522,6 +523,9 @@ var FTColumnflow = (function () {
522
523
config . lineHeight = _mode ( lineHeights ) ;
523
524
}
524
525
526
+ // Now the line-height is known, the column height can be determined
527
+ config . layoutDimensions . columnHeight = config . lineHeight ? _roundDownToGrid ( config . layoutDimensions . pageInnerHeight ) : config . layoutDimensions . pageInnerHeight ;
528
+
525
529
// For debugging, show the grid lines with CSS
526
530
if ( showGrid ) {
527
531
@@ -696,7 +700,7 @@ var FTColumnflow = (function () {
696
700
bottomSplitPoint = _roundUpToGrid ( elementTopPos + normalisedElementHeight , true ) ;
697
701
698
702
if ( topSplitPoint < 0 ) topSplitPoint = 0 ;
699
- if ( bottomSplitPoint > maxColumnHeight ) bottomSplitPoint = maxColumnHeight ;
703
+ if ( bottomSplitPoint > config . layoutDimensions . columnHeight ) bottomSplitPoint = config . layoutDimensions . columnHeight ;
700
704
break ;
701
705
702
706
case 'bottom' :
@@ -835,9 +839,8 @@ var FTColumnflow = (function () {
835
839
totalColumnHeight = 0 ;
836
840
837
841
// Set the maximum column height to a multiple of the lineHeight
838
- maxColumnHeight = config . lineHeight ? _roundDownToGrid ( config . layoutDimensions . pageInnerHeight ) : config . layoutDimensions . pageInnerHeight ;
839
- colDefaultBottom = maxColumnHeight + config . layoutDimensions . colDefaultTop ;
840
- colMiddle = config . layoutDimensions . colDefaultTop + ( maxColumnHeight / 2 ) ;
842
+ colDefaultBottom = config . layoutDimensions . columnHeight + config . layoutDimensions . colDefaultTop ;
843
+ colMiddle = config . layoutDimensions . colDefaultTop + ( config . layoutDimensions . columnHeight / 2 ) ;
841
844
minFixedPadding = config . minFixedPadding * config . lineHeight ;
842
845
fixedPadding = _roundUpToGrid ( minFixedPadding ) ;
843
846
@@ -897,36 +900,45 @@ var FTColumnflow = (function () {
897
900
898
901
function _addFlowedElement ( element , index ) {
899
902
900
- var originalMargin , existingMargin , totalElementHeight ,
901
- desiredElementHeight , newMargin , overflow , loopCount ,
903
+ var originalMargin , existingMargin , nextElementOffset , elementHeight ,
904
+ newMargin , largestMargin , overflow , loopCount ,
902
905
903
906
nextElement = element . nextSibling ;
904
907
905
908
// Check if it's necessary to sanitize elements to conform to the baseline grid
906
909
if ( config . standardiseLineHeight ) {
907
910
908
- originalMargin = parseFloat ( element . getAttribute ( 'data-cf-original-margin' ) , 10 ) || null ;
909
911
existingMargin = parseFloat ( window . getComputedStyle ( element ) . getPropertyValue ( 'margin-bottom' ) , 10 ) ;
910
912
911
- if ( null === originalMargin ) {
912
- originalMargin = existingMargin ;
913
- element . setAttribute ( 'data-cf-original-margin' , originalMargin ) ;
913
+ // If reflowing is enabled, try to read the original margin for the
914
+ // element, in case it was already modified
915
+ if ( config . allowReflow ) {
916
+ originalMargin = parseFloat ( element . getAttribute ( 'data-cf-original-margin' ) , 10 ) || null ;
917
+ if ( null === originalMargin ) {
918
+ originalMargin = existingMargin ;
919
+ element . setAttribute ( 'data-cf-original-margin' , originalMargin ) ;
920
+ } else if ( originalMargin !== existingMargin ) {
921
+
922
+ // Return the element to its original margin
923
+ element . style . marginBottom = originalMargin + 'px' ;
924
+ }
914
925
} else {
915
- existingMargin = originalMargin ;
926
+ originalMargin = existingMargin ;
916
927
}
917
928
918
- // Return the element to its original margin
919
- if ( originalMargin !== existingMargin ) {
920
- element . style . marginBottom = originalMargin + 'px' ;
921
- }
929
+ nextElementOffset = _getNextElementOffset ( element , nextElement ) ;
930
+ elementHeight = element . offsetHeight ;
922
931
923
- totalElementHeight = _getElementHeight ( element , nextElement ) ;
924
- desiredElementHeight = _roundUpToGrid ( totalElementHeight ) ;
932
+ // The next element's top is not aligned to the grid
933
+ if ( nextElementOffset % config . lineHeight ) {
925
934
926
- newMargin = desiredElementHeight - totalElementHeight + existingMargin ;
935
+ // Allow for collapsing margins
936
+ largestMargin = Math . max ( existingMargin , nextElement ? parseFloat ( window . getComputedStyle ( nextElement ) . getPropertyValue ( 'margin-top' ) , 10 ) : 0 ) ;
937
+ newMargin = _roundUpToGrid ( elementHeight ) - elementHeight + _roundUpToGrid ( largestMargin ) ;
927
938
928
- if ( newMargin !== existingMargin ) {
929
- element . style . marginBottom = newMargin + 'px' ;
939
+ if ( newMargin !== existingMargin ) {
940
+ element . style . marginBottom = newMargin + 'px' ;
941
+ }
930
942
}
931
943
}
932
944
@@ -944,7 +956,7 @@ var FTColumnflow = (function () {
944
956
if ( loopCount >= 30 ) console . error ( 'FTColumnflow: Caught and destroyed a loop when wrapping columns for element' , element . outerHTML . substr ( 0 , 200 ) + '...' ) ;
945
957
}
946
958
947
- function _getElementHeight ( element , nextElement ) {
959
+ function _getNextElementOffset ( element , nextElement ) {
948
960
if ( ! element . getBoundingClientRect ) {
949
961
return nextElement ? ( nextElement . offsetTop - element . offsetTop ) : element . offsetHeight ;
950
962
}
@@ -1267,7 +1279,7 @@ var FTColumnflow = (function () {
1267
1279
return {
1268
1280
elements : [ ] ,
1269
1281
overflow : 0 ,
1270
- height : maxColumnHeight ,
1282
+ height : config . layoutDimensions . columnHeight ,
1271
1283
top : config . layoutDimensions . colDefaultTop ,
1272
1284
bottom : colDefaultBottom
1273
1285
} ;
0 commit comments