Columnizer-jQuery-Plugin
Columnizer-jQuery-Plugin copied to clipboard
Removeiffirst/removeiflast implementation wrong
It assumes you only have 1 of them. But if you have a sequence, it won't work.
I have adapted the code to remove as many as there are.
while ($inBox.children(".column").children(":first-child.removeiffirst").length!=0)
$inBox.children(".column").children(":first-child.removeiffirst").remove();
while ($inBox.children(".column").children(":last-child.removeiflast").length!=0)
$inBox.children(".column").children(":last-child.removeiflast").remove();
It's not ideal as it will mess with column height balancing, but I think it is good enough.
It's also not ideal because it only looks at elements, not text nodes. So if you have a text node, then a removeiffirst element, it will get removed incorrectly. I'm going to completely change how this is approached in my next post.
Ergh, was planning to move code into the columnize function, but then I realised this is not going to be called for the last column, and I think cause other issues because there's no "total height" anymore. So to keep things 'simple' I just had to leave the height balancing problem alone, because it's not really solvable.
Here we go...
for (var i = 0; i < numCols; i++) {
var $col = $inBox.children().eq(i);
var runOn;
do {
runOn=$col.children(":first-child.removeiffirst");
if (runOn.length!=0) {
// Protect if a relevant text node preceding
if (runOn[0].previousSibling && runOn[0].previousSibling.nodeType==3) {
if (runOn[0].previousSibling.nodeValue.replace(/\s/g,'')!='') {
break;
}
}
// Okay, strip then
$(runOn[0]).remove();
}
}
while (runOn.length!=0);
do {
runOn=$col.children(":last-child.removeiflast");
if (runOn.length!=0) {
// Protect if a relevant text node suceeding
if (runOn[0].nextSibling && runOn[0].nextSibling.nodeType==3) {
if (runOn[0].nextSibling.nodeValue.replace(/\s/g,'')!='') {
break;
}
}
// Okay, strip then
$(runOn[0]).remove();
}
}
while (runOn.length!=0);
}