Skip to content

Commit 1406dec

Browse files
committed
better comments
1 parent 6b562cc commit 1406dec

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

src/jquery.columnizer.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,32 @@
9696
}
9797
});
9898
}
99-
99+
100100
/**
101-
* return a node that has a height
102-
* less than or equal to height
101+
* this fuction builds as much of a column as it can without
102+
* splitting nodes in half. If the last node in the new column
103+
* is a text node, then it will try to split that text node. otherwise
104+
* it will leave the node in $pullOutHere and return with a height
105+
* smaller than targetHeight.
106+
*
107+
* Returns a boolean on whether we did some splitting successfully at a text point
108+
* (so we know we don't need to split a real element). return false if the caller should
109+
* split a node if possible to end this column.
103110
*
104-
* @param putInHere, a dom element
105-
* @$pullOutHere, a jQuery element
111+
* @param putInHere, the jquery node to put elements into for the current column
112+
* @param $pullOutHere, the jquery node to pull elements out of (uncolumnized html)
113+
* @param $parentColumn, the jquery node for the currently column that's being added to
114+
* @param targetHeight, the ideal height for the column, get as close as we can to this height
106115
*/
107-
function columnize($putInHere, $pullOutHere, $parentColumn, height){
116+
function columnize($putInHere, $pullOutHere, $parentColumn, targetHeight){
108117
//
109118
// add as many nodes to the column as we can,
110119
// but stop once our height is too tall
111-
while($parentColumn.height() < height &&
120+
while($parentColumn.height() < targetHeight &&
112121
$pullOutHere[0].childNodes.length){
122+
//
123+
// Because we're not cloning, jquery will actually move the element"
124+
// http://welcome.totheinter.net/2009/03/19/the-undocumented-life-of-jquerys-append/
113125
$putInHere.append($pullOutHere[0].childNodes[0]);
114126
}
115127
if($putInHere[0].childNodes.length == 0) return;
@@ -131,7 +143,7 @@
131143
counter2 = options.accuracy;
132144
var columnText;
133145
var latestTextNode = null;
134-
while($parentColumn.height() < height && oText.length){
146+
while($parentColumn.height() < targetHeight && oText.length){
135147
if (oText.indexOf(' ', counter2) != '-1') {
136148
columnText = oText.substring(0, oText.indexOf(' ', counter2));
137149
} else {
@@ -146,7 +158,7 @@
146158
oText = "";
147159
}
148160
}
149-
if($parentColumn.height() >= height && latestTextNode != null){
161+
if($parentColumn.height() >= targetHeight && latestTextNode != null){
150162
// too tall :(
151163
$putInHere[0].removeChild(latestTextNode);
152164
oText = latestTextNode.nodeValue + oText;
@@ -167,7 +179,11 @@
167179
return $item[0].nodeType == 3;
168180
}
169181

170-
function split($putInHere, $pullOutHere, $parentColumn, height){
182+
/**
183+
* Split up an element, which is more complex than splitting text. We need to create
184+
* two copies of the element with it's contents divided between each
185+
*/
186+
function split($putInHere, $pullOutHere, $parentColumn, targetHeight){
171187
if($pullOutHere.children().length){
172188
var $cloneMe = $pullOutHere.children(":first");
173189
//
@@ -179,12 +195,12 @@
179195
if(($clone.prop && $clone.prop("nodeType") == 1 && !$clone.hasClass("dontend")) ||
180196
($clone.attr("nodeType") == 1 && !$clone.hasClass("dontend"))){
181197
$putInHere.append($clone);
182-
if($clone.is("img") && $parentColumn.height() < height + 20){
198+
if($clone.is("img") && $parentColumn.height() < targetHeight + 20){
183199
//
184200
// we can't split an img in half, so just add it
185201
// to the column and remove it from the pullOutHere section
186202
$cloneMe.remove();
187-
}else if(!$cloneMe.hasClass("dontsplit") && $parentColumn.height() < height + 20){
203+
}else if(!$cloneMe.hasClass("dontsplit") && $parentColumn.height() < targetHeight + 20){
188204
//
189205
// pretty close fit, and we're not allowed to split it, so just
190206
// add it to the column, remove from pullOutHere, and be done
@@ -201,12 +217,12 @@
201217
// the node in the column we're building, and start splitting
202218
// it in half, leaving some of it in pullOutHere
203219
$clone.empty();
204-
if(!columnize($clone, $cloneMe, $parentColumn, height)){
220+
if(!columnize($clone, $cloneMe, $parentColumn, targetHeight)){
205221
// this node still has non-text nodes to split
206222
// add the split class and then recur
207223
$cloneMe.addClass("split");
208224
if($cloneMe.children().length){
209-
split($clone, $cloneMe, $parentColumn, height);
225+
split($clone, $cloneMe, $parentColumn, targetHeight);
210226
}
211227
}else{
212228
// this node only has text node children left, add the
@@ -318,6 +334,10 @@
318334
scrollHorizontally = true;
319335
}
320336

337+
//
338+
// We loop as we try and workout a good height to use. We know it initially as an average
339+
// but if the last column is higher than the first ones (which can happen, depending on split
340+
// points) we need to raise 'adjustment'. We try this over a few iterations until we're 'solid'.
321341
for(var loopCount=0;loopCount<maxLoops;loopCount++){
322342
$inBox.empty();
323343
var $destroyable;

0 commit comments

Comments
 (0)