Columnizer-jQuery-Plugin icon indicating copy to clipboard operation
Columnizer-jQuery-Plugin copied to clipboard

Removeiffirst/removeiflast implementation wrong

Open chrisgraham opened this issue 12 years ago • 3 comments

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();

chrisgraham avatar Jan 20 '12 12:01 chrisgraham

It's not ideal as it will mess with column height balancing, but I think it is good enough.

chrisgraham avatar Jan 20 '12 12:01 chrisgraham

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.

chrisgraham avatar Jan 20 '12 13:01 chrisgraham

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);
            }

chrisgraham avatar Jan 20 '12 13:01 chrisgraham