Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated minified version

  • Loading branch information...
commit b855b5ea993380fd93d854b09e047fc709be2977 1 parent 3454622
@tbasse authored
Showing with 1 addition and 8 deletions.
  1. +1 −8 jquery.truncate.min.js
View
9 jquery.truncate.min.js
@@ -1,8 +1 @@
-(function(c){function a(d){return !isNaN(parseFloat(d))&&isFinite(d);}function b(g,i,o,d,f,j,e,h){var l=function(p){return(h?e:"")+p+(h?"":e);};var n,m,k;
-if(h){n=d===0?"":o.slice(-d);m=o.slice(-f);}else{n=o.slice(0,d);m=o.slice(0,f);}if(i<j.html(e)[g]()){return 0;}if(j.html(l(m))[g]()<j.html(l(n))[g]()){return f;
-}k=parseInt((d+f)/2,10);n=h?o.slice(-k):o.slice(0,k);j.html(l(n));if(j[g]()===i){return k;}if(j[g]()>i){f=k-1;}else{d=k+1;}return b(g,i,o,d,f,j,e,h);}c.fn.truncate=function(d){if(typeof d.center!="undefined"&&typeof d.side=="undefined"){d.side="center";
-delete d.center;}if(typeof d.side!="undefined"){switch(d.side){case"left":case"center":case"right":break;default:delete d.side;}}var e={width:"auto",token:"&hellip;",side:"right",addclass:false,addtitle:false,multiline:false};
-d=c.extend(e,d);return this.each(function(){var n=c(this),i={fontFamily:n.css("fontFamily"),fontSize:n.css("fontSize"),fontStyle:n.css("fontStyle"),fontWeight:n.css("fontWeight"),"font-variant":n.css("font-variant"),"text-indent":n.css("text-indent"),"text-transform":n.css("text-transform"),"letter-spacing":n.css("letter-spacing"),"word-spacing":n.css("word-spacing"),display:"none"},l=n.text(),k=c("<span/>").css(i).html(l).appendTo("body"),f=k.width(),o=a(d.width)?d.width:n.width(),h="width",j,g,m;
-if(d.multiline){k.width(n.width());h="height";g=k.height();m=n.height()+1;}else{g=f;m=o;}if(g>m){k.text("");switch(d.side){case"left":j=d.token+l.slice(-1*b(h,m,l,0,l.length,k,d.token,true));
-break;case"center":m=parseInt(m/2,10)-1;j=l.slice(0,b(h,m,l,0,l.length,k,d.token,false))+d.token+l.slice(-1*b(h,m,l,0,l.length,k,"",true));break;case"right":j=l.slice(0,b(h,m,l,0,l.length,k,d.token,false))+d.token;
-break;}if(d.addclass){n.addClass(d.addclass);}if(d.addtitle){n.attr("title",l);}n.empty().append(j);}k.remove();});};})(jQuery);
+(function($){"use strict";function findTruncPoint(dim,max,txt,start,end,$worker,token,reverse){var makeContent=function(content){return(reverse?token:"")+content+(reverse?"":token)};var opt1,opt2,mid,opt1dim,opt2dim;if(reverse){opt1=start===0?"":txt.slice(-start);opt2=txt.slice(-end)}else{opt1=txt.slice(0,start);opt2=txt.slice(0,end)}if(max<$worker.html(token)[dim]()){return 0}opt1dim=$worker.html(makeContent(opt2))[dim]();opt2dim=$worker.html(makeContent(opt1))[dim]();if(opt1dim<opt2dim){return end}mid=parseInt((start+end)/2,10);opt1=reverse?txt.slice(-mid):txt.slice(0,mid);$worker.html(makeContent(opt1));if($worker[dim]()===max){return mid}if($worker[dim]()>max){end=mid-1}else{start=mid+1}return findTruncPoint(dim,max,txt,start,end,$worker,token,reverse)}$.fn.truncate=function(options){if(!!options.center&&!options.side){options.side="center";delete options.center}if(!/^(left|right|center)$/.test(options.side)){delete options.side}var defaults={width:"auto",token:"&hellip;",side:"right",addclass:false,addtitle:false,multiline:false};options=$.extend(defaults,options);return this.each(function(){var $element=$(this);var fontCSS={fontFamily:$element.css("fontFamily"),fontSize:$element.css("fontSize"),fontStyle:$element.css("fontStyle"),fontWeight:$element.css("fontWeight"),"font-variant":$element.css("font-variant"),"text-indent":$element.css("text-indent"),"text-transform":$element.css("text-transform"),"letter-spacing":$element.css("letter-spacing"),"word-spacing":$element.css("word-spacing"),display:"none"};var elementText=$element.text();var $truncateWorker=$("<span/>").css(fontCSS).html(elementText).appendTo("body");var originalWidth=$truncateWorker.width();var truncateWidth=parseInt(options.width,10)||$element.width();var dimension="width";var truncatedText,originalDim,truncateDim;if(options.multiline){$truncateWorker.width($element.width());dimension="height";originalDim=$truncateWorker.height();truncateDim=$element.height()+1}else{originalDim=originalWidth;truncateDim=truncateWidth}if(originalDim>truncateDim){var truncPoint,truncPoint2;$truncateWorker.text("");if(options.side==="left"){truncPoint=findTruncPoint(dimension,truncateDim,elementText,0,elementText.length,$truncateWorker,options.token,true);truncatedText=[options.token,elementText.slice(-1*truncPoint)].join("")}else if(options.side==="center"){truncateDim=parseInt(truncateDim/2,10)-1;truncPoint=findTruncPoint(dimension,truncateDim,elementText,0,elementText.length,$truncateWorker,options.token,false);truncPoint2=findTruncPoint(dimension,truncateDim,elementText,0,elementText.length,$truncateWorker,"",true);truncatedText=[elementText.slice(0,truncPoint),options.token,elementText.slice(-1*truncPoint2)].join("")}else if(options.side==="right"){truncPoint=findTruncPoint(dimension,truncateDim,elementText,0,elementText.length,$truncateWorker,options.token,false);truncatedText=[elementText.slice(0,truncPoint),options.token].join("")}if(options.addclass){$element.addClass(options.addclass)}if(options.addtitle){$element.attr("title",elementText)}$element.empty().append(truncatedText)}$truncateWorker.remove()})}})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.