From 139db2328e25d3319edc98c6d171194bf1f3456e Mon Sep 17 00:00:00 2001 From: Tom Alterman Date: Fri, 8 Mar 2013 16:18:55 +0000 Subject: [PATCH 1/8] removed all trailing whitespace and mixture of space and tabs --- src/jquery.columnizer.js | 138 +++++++++++++++++++-------------------- 1 file changed, 67 insertions(+), 71 deletions(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index a4bf32c..60c03af 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -41,7 +41,7 @@ cssClassPrefix : "" }; var options = $.extend(defaults, options); - + if(typeof(options.width) == "string"){ options.width = parseInt(options.width); if(isNaN(options.width)){ @@ -50,7 +50,7 @@ } return this.each(function() { - var $inBox = options.target ? $(options.target) : $(this); + var $inBox = options.target ? $(options.target) : $(this); var maxHeight = $(this).height(); var $cache = $('
'); // this is where we'll put the real content var lastWidth = 0; @@ -61,38 +61,38 @@ cssClassPrefix = options.cssClassPrefix; } - + var adjustment = 0; - + $cache.append($(this).contents().clone(true)); - - // images loading after dom load - // can screw up the column heights, - // so recolumnize after images load - if(!options.ignoreImageLoading && !options.target){ - if(!$inBox.data("imageLoaded")){ - $inBox.data("imageLoaded", true); - if($(this).find("img").length > 0){ - // only bother if there are - // actually images... - var func = function($inBox,$cache){ return function(){ - if(!$inBox.data("firstImageLoaded")){ - $inBox.data("firstImageLoaded", "true"); - $inBox.empty().append($cache.children().clone(true)); - $inBox.columnize(options); - } - }}($(this), $cache); - $(this).find("img").one("load", func); - $(this).find("img").one("abort", func); - return; - } - } - } - + + // images loading after dom load + // can screw up the column heights, + // so recolumnize after images load + if(!options.ignoreImageLoading && !options.target){ + if(!$inBox.data("imageLoaded")){ + $inBox.data("imageLoaded", true); + if($(this).find("img").length > 0){ + // only bother if there are + // actually images... + var func = function($inBox,$cache){ return function(){ + if(!$inBox.data("firstImageLoaded")){ + $inBox.data("firstImageLoaded", "true"); + $inBox.empty().append($cache.children().clone(true)); + $inBox.columnize(options); + } + }}($(this), $cache); + $(this).find("img").one("load", func); + $(this).find("img").one("abort", func); + return; + } + } + } + $inBox.empty(); - + columnizeIt(); - + if(!options.buildOnce){ $(window).resize(function() { if(!options.buildOnce){ @@ -103,7 +103,7 @@ } }); } - + function prefixTheClassName(className, withDot){ var dot = withDot ? "." : ""; if(cssClassPrefix.length){ @@ -111,8 +111,7 @@ } return dot + className; } - - + /** * this fuction builds as much of a column as it can without * splitting nodes in half. If the last node in the new column @@ -134,7 +133,7 @@ // add as many nodes to the column as we can, // but stop once our height is too tall while((manualBreaks || $parentColumn.height() < targetHeight) && - $pullOutHere[0].childNodes.length){ + $pullOutHere[0].childNodes.length){ var node = $pullOutHere[0].childNodes[0] // // Because we're not cloning, jquery will actually move the element" @@ -152,14 +151,13 @@ $putInHere.append(node); } if($putInHere[0].childNodes.length == 0) return; - + // now we're too tall, so undo the last one var kids = $putInHere[0].childNodes; var lastKid = kids[kids.length-1]; $putInHere[0].removeChild(lastKid); var $item = $(lastKid); - - // + // now lets try to split that last node // to fit as much of it as we can into this column if($item[0].nodeType == 3){ @@ -179,7 +177,7 @@ } latestTextNode = document.createTextNode(columnText); $putInHere.append(latestTextNode); - + if(oText.length > counter2 && indexOfSpace != -1){ oText = oText.substring(indexOfSpace); }else{ @@ -197,16 +195,16 @@ return false; // we ate the whole text node, move on to the next node } } - + if($pullOutHere.contents().length){ $pullOutHere.prepend($item); }else{ $pullOutHere.append($item); } - + return $item[0].nodeType == 3; } - + /** * Split up an element, which is more complex than splitting text. We need to create * two copies of the element with it's contents divided between each @@ -227,7 +225,7 @@ // // make sure we're splitting an element if($cloneMe.get(0).nodeType != 1) return; - + // // clone the node with all data and events var $clone = $cloneMe.clone(true); @@ -244,7 +242,7 @@ // keep adding until we hit a manual break $putInHere.append($clone); $cloneMe.remove(); - }else if($clone.get(0).nodeType == 1 && !$clone.hasClass(prefixTheClassName("dontend"))){ + }else if($clone.get(0).nodeType == 1 && !$clone.hasClass(prefixTheClassName("dontend"))){ $putInHere.append($clone); if($clone.is("img") && $parentColumn.height() < targetHeight + 20){ // @@ -288,21 +286,21 @@ } } } - - + + function singleColumnizeIt() { if ($inBox.data("columnized") && $inBox.children().length == 1) { return; } $inBox.data("columnized", true); $inBox.data("columnizing", true); - + $inBox.empty(); $inBox.append($("
")); //" + + prefixTheClassName("first") + " " + + prefixTheClassName("last") + " " + + prefixTheClassName("column") + " " + + "' style='width:100%; float: " + options.columnFloat + ";'>")); //" $col = $inBox.children().eq($inBox.children().length-1); $destroyable = $cache.clone(true); if(options.overflow){ @@ -312,7 +310,7 @@ if(!$destroyable.contents().find(":first-child").hasClass(prefixTheClassName("dontend"))){ split($col, $destroyable, $col, targetHeight); } - + while($col.contents(":last").length && checkDontEndColumn($col.contents(":last").get(0))){ var $lastKid = $col.contents(":last"); $lastKid.remove(); @@ -341,13 +339,13 @@ $col.append($destroyable); } $inBox.data("columnizing", false); - + if(options.overflow && options.overflow.doneFunc){ options.overflow.doneFunc(); } - + } - + /** * returns true if the input dom node * should not end a column. @@ -359,10 +357,10 @@ // is not 100% whitespace if(/^\s+$/.test(dom.nodeValue)){ // - // ok, it's 100% whitespace, - // so we should return checkDontEndColumn - // of the inputs previousSibling - if(!dom.previousSibling) return false; + // ok, it's 100% whitespace, + // so we should return checkDontEndColumn + // of the inputs previousSibling + if(!dom.previousSibling) return false; return checkDontEndColumn(dom.previousSibling); } return false; @@ -372,15 +370,13 @@ if(dom.childNodes.length == 0) return false; return checkDontEndColumn(dom.childNodes[dom.childNodes.length-1]); } - - - + function columnizeIt() { //reset adjustment var adjustment = 0; if(lastWidth == $inBox.width()) return; lastWidth = $inBox.width(); - + var numCols = Math.round($inBox.width() / options.width); var optionWidth = options.width; var optionHeight = options.height; @@ -389,7 +385,7 @@ numCols = $cache.find(prefixTheClassName("columnbreak", true)).length + 1; optionWidth = false; } - + // if ($inBox.data("columnized") && numCols == $inBox.children().length) { // return; // } @@ -399,14 +395,14 @@ if($inBox.data("columnizing")) return; $inBox.data("columnized", true); $inBox.data("columnizing", true); - + $inBox.empty(); $inBox.append($("
")); //" $col = $inBox.children(":last"); $col.append($cache.clone()); maxHeight = $col.height(); $inBox.empty(); - + var targetHeight = maxHeight / numCols; var firstTime = true; var maxLoops = 3; @@ -419,7 +415,7 @@ targetHeight = optionHeight; scrollHorizontally = true; } - + // // We loop as we try and workout a good height to use. We know it initially as an average // but if the last column is higher than the first ones (which can happen, depending on split @@ -446,7 +442,7 @@ var className = (i == numCols - 1) ? (prefixTheClassName("last") + " " + className) : className; $inBox.append($("
")); //" } - + // fill all but the last column (unless overflowing) var i = 0; while(i < numCols - (options.overflow ? 0 : 1) || scrollHorizontally && $destroyable.contents().length){ @@ -461,14 +457,14 @@ columnize($col, $destroyable, $col, targetHeight); // make sure that the last item in the column isn't a "dontend" split($col, $destroyable, $col, targetHeight); - + while($col.contents(":last").length && checkDontEndColumn($col.contents(":last").get(0))){ var $lastKid = $col.contents(":last"); $lastKid.remove(); $destroyable.prepend($lastKid); } i++; - + // // https://github.com/adamwulf/Columnizer-jQuery-Plugin/issues/47 // @@ -498,7 +494,7 @@ numCols ++; } } - + } if(options.overflow && !scrollHorizontally){ var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/; @@ -606,4 +602,4 @@ } }); }; -})(jQuery); +})(jQuery); \ No newline at end of file From 0f24a3d070f937e28299d728c12f52f7f5b2e757 Mon Sep 17 00:00:00 2001 From: Tom Alterman Date: Fri, 8 Mar 2013 16:20:47 +0000 Subject: [PATCH 2/8] using === to compare to 0 --- src/jquery.columnizer.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index 60c03af..f19937e 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -150,7 +150,7 @@ } $putInHere.append(node); } - if($putInHere[0].childNodes.length == 0) return; + if($putInHere[0].childNodes.length === 0) return; // now we're too tall, so undo the last one var kids = $putInHere[0].childNodes; @@ -278,7 +278,7 @@ // split class and move on. $cloneMe.addClass(prefixTheClassName("split")); } - if($clone.get(0).childNodes.length == 0){ + if($clone.get(0).childNodes.length === 0){ // it was split, but nothing is in it :( $clone.remove(); } @@ -323,7 +323,7 @@ var kid = $destroyable[0].childNodes[0]; if(kid.attributes){ for(var i=0;i< numCols; i++) { /* create column */ - var className = (i == 0) ? prefixTheClassName("first") : ""; + var className = (i === 0) ? prefixTheClassName("first") : ""; className += " " + prefixTheClassName("column"); var className = (i == numCols - 1) ? (prefixTheClassName("last") + " " + className) : className; $inBox.append($("
")); //" @@ -475,7 +475,7 @@ // // this results in empty columns being added with the dontsplit item // perpetually waiting to get put into a column. lets force the issue here - if($col.contents().length == 0 && $destroyable.contents().length){ + if($col.contents().length === 0 && $destroyable.contents().length){ // // ok, we're building zero content columns. this'll happen forever // since nothing can ever get taken out of destroyable. @@ -505,7 +505,7 @@ while($destroyable[0].childNodes.length > 0){ var kid = $destroyable[0].childNodes[0]; for(var i=0;i Date: Fri, 8 Mar 2013 16:22:00 +0000 Subject: [PATCH 3/8] updated another comparison to 0 to === --- src/jquery.columnizer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index f19937e..fdfe2c6 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -578,7 +578,7 @@ $inBox.children().each(function(i){ $col = $inBox.children().eq(i); $col.width(optionWidth + "px"); - if(i==0){ + if(i === 0){ $col.addClass(prefixTheClassName("first")); }else if(i==$inBox.children().length-1){ $col.addClass(prefixTheClassName("last")); From c2822a6eee1a8779adde7cc19711a140aa3b35ce Mon Sep 17 00:00:00 2001 From: Tom Alterman Date: Fri, 8 Mar 2013 16:25:51 +0000 Subject: [PATCH 4/8] added missing semicolons --- src/jquery.columnizer.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index fdfe2c6..9619e27 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -76,12 +76,13 @@ // only bother if there are // actually images... var func = function($inBox,$cache){ return function(){ - if(!$inBox.data("firstImageLoaded")){ - $inBox.data("firstImageLoaded", "true"); - $inBox.empty().append($cache.children().clone(true)); - $inBox.columnize(options); - } - }}($(this), $cache); + if(!$inBox.data("firstImageLoaded")){ + $inBox.data("firstImageLoaded", "true"); + $inBox.empty().append($cache.children().clone(true)); + $inBox.columnize(options); + } + }; + }($(this), $cache); $(this).find("img").one("load", func); $(this).find("img").one("abort", func); return; @@ -134,7 +135,7 @@ // but stop once our height is too tall while((manualBreaks || $parentColumn.height() < targetHeight) && $pullOutHere[0].childNodes.length){ - var node = $pullOutHere[0].childNodes[0] + var node = $pullOutHere[0].childNodes[0]; // // Because we're not cloning, jquery will actually move the element" // http://welcome.totheinter.net/2009/03/19/the-undocumented-life-of-jquerys-append/ @@ -543,7 +544,8 @@ if(h < min) min = h; numberOfColumnsThatDontEndInAColumnBreak++; } - }}($inBox)); + }; + }($inBox)); var avgH = totalH / numberOfColumnsThatDontEndInAColumnBreak; if(totalH === 0){ From 625b3c664a532ce0935e6c310a39f33f6e0abad4 Mon Sep 17 00:00:00 2001 From: Tom Alterman Date: Fri, 8 Mar 2013 16:27:53 +0000 Subject: [PATCH 5/8] removed repeated var declarations --- src/jquery.columnizer.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index 9619e27..f10998a 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -40,7 +40,7 @@ // default to empty string for backwards compatibility cssClassPrefix : "" }; - var options = $.extend(defaults, options); + options = $.extend(defaults, options); if(typeof(options.width) == "string"){ options.width = parseInt(options.width); @@ -440,12 +440,12 @@ /* create column */ var className = (i === 0) ? prefixTheClassName("first") : ""; className += " " + prefixTheClassName("column"); - var className = (i == numCols - 1) ? (prefixTheClassName("last") + " " + className) : className; + className = (i == numCols - 1) ? (prefixTheClassName("last") + " " + className) : className; $inBox.append($("
")); //" } // fill all but the last column (unless overflowing) - var i = 0; + i = 0; while(i < numCols - (options.overflow ? 0 : 1) || scrollHorizontally && $destroyable.contents().length){ if($inBox.children().length <= i){ // we ran out of columns, make another From 04b140624ad1382b1d3afe30a44029606f9da89d Mon Sep 17 00:00:00 2001 From: Tom Alterman Date: Fri, 8 Mar 2013 16:30:24 +0000 Subject: [PATCH 6/8] using !== to compare to null and removed repeated var declaration --- src/jquery.columnizer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index f10998a..c2ae633 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -185,7 +185,7 @@ oText = ""; } } - if($parentColumn.height() >= targetHeight && latestTextNode != null){ + if($parentColumn.height() >= targetHeight && latestTextNode !== null){ // too tall :( $putInHere[0].removeChild(latestTextNode); oText = latestTextNode.nodeValue + oText; @@ -505,7 +505,7 @@ var div = document.createElement('DIV'); while($destroyable[0].childNodes.length > 0){ var kid = $destroyable[0].childNodes[0]; - for(var i=0;i Date: Fri, 8 Mar 2013 16:33:17 +0000 Subject: [PATCH 7/8] define radix for parseint --- src/jquery.columnizer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index c2ae633..3dfa08a 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -43,7 +43,7 @@ options = $.extend(defaults, options); if(typeof(options.width) == "string"){ - options.width = parseInt(options.width); + options.width = parseInt(options.width,10); if(isNaN(options.width)){ options.width = defaults.width; } From 5b161b0571f130e3493d0fcb64ac5785bc7fdbec Mon Sep 17 00:00:00 2001 From: Tom Alterman Date: Fri, 8 Mar 2013 16:38:34 +0000 Subject: [PATCH 8/8] ensure vars are declared out of loop and are not out of scope --- src/jquery.columnizer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/jquery.columnizer.js b/src/jquery.columnizer.js index 3dfa08a..16ac5eb 100644 --- a/src/jquery.columnizer.js +++ b/src/jquery.columnizer.js @@ -427,7 +427,7 @@ // options that would cause an infinite loop, then this'll definitely stop it. for(var loopCount=0;loopCount< 20;loopCount++){ $inBox.empty(); - var $destroyable; + var $destroyable, className, $col, $lastKid; try{ $destroyable = $cache.clone(true); }catch(e){ @@ -438,7 +438,7 @@ // create the columns for (var i = 0; i < numCols; i++) { /* create column */ - var className = (i === 0) ? prefixTheClassName("first") : ""; + className = (i === 0) ? prefixTheClassName("first") : ""; className += " " + prefixTheClassName("column"); className = (i == numCols - 1) ? (prefixTheClassName("last") + " " + className) : className; $inBox.append($("
")); //" @@ -451,7 +451,7 @@ // we ran out of columns, make another $inBox.append($("
")); //" } - var $col = $inBox.children().eq(i); + $col = $inBox.children().eq(i); if(scrollHorizontally){ $col.width(optionWidth + "px"); } @@ -460,7 +460,7 @@ split($col, $destroyable, $col, targetHeight); while($col.contents(":last").length && checkDontEndColumn($col.contents(":last").get(0))){ - var $lastKid = $col.contents(":last"); + $lastKid = $col.contents(":last"); $lastKid.remove(); $destroyable.prepend($lastKid); }