ontohub/ontohub

View on GitHub
app/assets/javascripts/jquery.truncate.js

Summary

Maintainability
A
0 mins
Test Coverage
(function(e){function t(e){return!isNaN(parseFloat(e))&&isFinite(e)}function n(e,t,r,i,s,o,u,a){var f=function(e){return(a?u:"")+e+(a?"":u)};var l,c,h;if(a){l=i===0?"":r.slice(-i);c=r.slice(-s)}else{l=r.slice(0,i);c=r.slice(0,s)}if(t<o.html(u)[e]()){return 0}if(o.html(f(c))[e]()<o.html(f(l))[e]()){return s}h=parseInt((i+s)/2,10);l=a?r.slice(-h):r.slice(0,h);o.html(f(l));if(o[e]()===t){return h}if(o[e]()>t){s=h-1}else{i=h+1}return n(e,t,r,i,s,o,u,a)}e.fn.truncate=function(r){if(typeof r.center!="undefined"&&typeof r.side=="undefined"){r.side="center";delete r.center}if(typeof r.side!="undefined")switch(r.side){case"left":case"center":case"right":break;default:delete r.side}var i={width:"auto",token:"É",side:"right",addclass:false,addtitle:false,multiline:false};r=e.extend(i,r);return this.each(function(){var i=e(this),s={fontFamily:i.css("fontFamily"),fontSize:i.css("fontSize"),fontStyle:i.css("fontStyle"),fontWeight:i.css("fontWeight"),"font-variant":i.css("font-variant"),"text-indent":i.css("text-indent"),"text-transform":i.css("text-transform"),"letter-spacing":i.css("letter-spacing"),"word-spacing":i.css("word-spacing"),display:"none"},o=i.text(),u=e("<span/>").css(s).html(o).appendTo("body"),a=u.width(),f=t(r.width)?r.width:i.width(),l="width",c,h,p;if(r.multiline){u.width(i.width());l="height";h=u.height();p=i.height()+1}else{h=a;p=f}if(h>p){u.text("");switch(r.side){case"left":c=r.token+o.slice(-1*n(l,p,o,0,o.length,u,r.token,true));break;case"center":p=parseInt(p/2,10)-1;c=o.slice(0,n(l,p,o,0,o.length,u,r.token,false))+r.token+o.slice(-1*n(l,p,o,0,o.length,u,"",true));break;case"right":c=o.slice(0,n(l,p,o,0,o.length,u,r.token,false))+r.token;break}if(r.addclass){i.addClass(r.addclass)}if(r.addtitle){i.attr("title",o)}i.empty().append(c)}u.remove()})}})(jQuery)