@@ -60,7 +60,116 @@ ALMCSS.template.layout = function () {
6060 // TODO: Resolve this questions with Bert.
6161 //
6262
63- var wideColumns = function ( elementWidth , columns ) {
63+ // Widening Columns (current algorithm)
64+ // ----------------
65+
66+ var wideColumns2 = function ( elementWidth , columns ) {
67+
68+ var i , step , numberOfColumns , amount , areAllWidened = false ,
69+ intrinsicPreferredWidth , nonExpandableColumns , computedWidths ,
70+ availableWidth , message ;
71+
72+ nonExpandableColumns = [ ] ;
73+ computedWidths = [ ] ;
74+
75+ var isExpandableColumn = function ( column ) {
76+ for ( var i = 0 ; i < nonExpandableColumns . length ; i ++ ) {
77+ if ( nonExpandableColumns [ i ] === column ) {
78+ return false ;
79+ }
80+ }
81+ return true ;
82+ } ;
83+
84+ var isThereAvailableWidth = function ( ) {
85+ return availableWidth > 1 ;
86+ } ;
87+
88+ numberOfColumns = columns . length ;
89+ step = 1 ;
90+ availableWidth = elementWidth ;
91+
92+ for ( i = 0 ; i < columns . length ; i ++ ) {
93+ computedWidths [ i ] = columns [ i ] . getIntrinsicMinimumWidth ( ) ;
94+ }
95+
96+ while ( isThereAvailableWidth ( ) && ! areAllWidened ) {
97+
98+ assert ( step < 30 , 'Oops, something had to went wrong: too many steps' ) ;
99+ amount = availableWidth / numberOfColumns ;
100+ logger . group ( 'Step %d: Widening columns with an increment of %d pixels...' ,
101+ step , amount ) ;
102+
103+ // All columns are processed in each iteration.
104+ for ( i = 0 ; i < columns . length ; i ++ ) {
105+
106+ // If the column is not expandable (it has already reached its
107+ // intrinsic preferred width), there is nothing to do with it:
108+ // the widening process continues with the following one, if
109+ // there are left columns to be iterated in this step.
110+ if ( ! isExpandableColumn ( columns [ i ] ) ) {
111+ continue ;
112+ }
113+
114+ // Otherwise (the column may be widened), its intrinsic
115+ // preferred width is obtained
116+ intrinsicPreferredWidth = columns [ i ] . getIntrinsicPreferredWidth ( ) ;
117+
118+ // __No column can be wider than its preferred minimum width__.
119+ // If the column that is being currently processed can not
120+ // expand by the previously computed `amount` without
121+ // violating that constraint, then it is set to its
122+ // _preferred minimum width_. Note that the difference
123+ // between both values (`computedWidths[i] + amount` and
124+ // `intrinsicPreferredWidth`) would have to be
125+ // redistributed in subsequent iterations among the rest of
126+ // the columns that may still be widen.
127+
128+ if ( computedWidths [ i ] + amount > intrinsicPreferredWidth ) {
129+ computedWidths [ i ] = intrinsicPreferredWidth ;
130+ nonExpandableColumns . push ( columns [ i ] ) ;
131+ info ( 'Column ' + i + ' has been set to its intrinsic preferred width ' +
132+ 'and is not more expandable' ) ;
133+ }
134+
135+ // If the sum of the column width (`computedWidths[i]`)
136+ // plus the `amount` to be added in this step (remind:
137+ // the result of dividing the available width into the
138+ // number of expandable columns that was previously
139+ // calculated) _is less than or equal to the intrinsic
140+ // preferred width_ of the column that is being processed,
141+ // that would be the new computed width of this column.
142+ else {
143+ computedWidths [ i ] = computedWidths [ i ] + amount ;
144+ log ( 'Column %d has been set a width of %d pixels' , i , computedWidths [ i ] ) ;
145+ }
146+
147+ availableWidth = availableWidth - computedWidths [ i ] ;
148+ }
149+ if ( ! isThereAvailableWidth ( ) ) {
150+ log ( 'There is no more available width' ) ;
151+ } else {
152+ log ( 'After step %d, there are still %d pixels of available width' , step , availableWidth ) ;
153+ if ( nonExpandableColumns . length === columns . length ) {
154+ areAllWidened = true ;
155+ log ( 'But all columns have already be widened up to their maximum: we have to end' ) ;
156+ } else {
157+ numberOfColumns = columns . length - nonExpandableColumns . length ;
158+ message = numberOfColumns === 1 ? 'is' : 'are' ;
159+ log ( 'There %s still %d columns that can be widened' , message , numberOfColumns ) ;
160+ }
161+ }
162+ step = step + 1 ;
163+ logger . groupEnd ( ) ;
164+ }
165+ info ( 'All columns have been widened: ' + computedWidths ) ;
166+ return computedWidths ;
167+ } ;
168+
169+ // Widening Columns (old algorithm)
170+ // ----------------
171+
172+ var wideColumns1 = function ( elementWidth , columns ) {
64173
65174 var i , step , numberOfColumns , columnWidth , areAllWidened = false ,
66175 intrinsicPreferredWidth , nonExpandableColumns , computedWidths ,
@@ -158,6 +267,8 @@ ALMCSS.template.layout = function () {
158267 return computedWidths ;
159268 } ;
160269
270+ var wideColumns = wideColumns2 ;
271+
161272 var computeColumnWidths = function ( template ) {
162273 logger . group ( 'Computing the width of the template ' + template . getId ( ) + '...' ) ;
163274 info ( '(Currently, all templates are computed as is they had an a-priori width)' ) ;
0 commit comments