-
Notifications
You must be signed in to change notification settings - Fork 42
/
jquery.truncate.min.js
1 lines (1 loc) · 2.23 KB
/
jquery.truncate.min.js
1
!function(t){"use strict";function e(t,s,n,i,a,r,l,c){var o,d,f,h,g,p=function(t){r.text(t),r[c?"prepend":"append"](l)};return c?(o=0===i?"":n.slice(-i),d=n.slice(-a)):(o=n.slice(0,i),d=n.slice(0,a)),s<r.html(l)[t]()?0:(p(d),h=r[t](),p(o),g=r[t](),g>h?a:(f=parseInt((i+a)/2,10),o=c?n.slice(-f):n.slice(0,f),p(o),r[t]()===s?f:(r[t]()>s?a=f-1:i=f+1,e(t,s,n,i,a,r,l,c))))}t.fn.truncate=function(s){s&&s.center&&!s.side&&(s.side="center",delete s.center),s&&!/^(left|right|center)$/.test(s.side)&&delete s.side;var n={width:"auto",token:"…",side:"right",addclass:!1,addtitle:!1,multiline:!1,assumeSameStyle:!1};s=t.extend(n,s);var i,a,r,l;return s.assumeSameStyle&&(a=t(this[0]),i={fontFamily:a.css("fontFamily"),fontSize:a.css("fontSize"),fontStyle:a.css("fontStyle"),fontWeight:a.css("fontWeight"),"font-variant":a.css("font-variant"),"text-indent":a.css("text-indent"),"line-height":a.css("line-height"),"text-transform":a.css("text-transform"),"letter-spacing":a.css("letter-spacing"),"word-spacing":a.css("word-spacing"),display:"none"},r=t("<span/>").css(i).appendTo("body")),this.each(function(){a=t(this),l=a.text(),s.assumeSameStyle?r.text(l):(i={fontFamily:a.css("fontFamily"),fontSize:a.css("fontSize"),fontStyle:a.css("fontStyle"),fontWeight:a.css("fontWeight"),"font-variant":a.css("font-variant"),"text-indent":a.css("text-indent"),"line-height":a.css("line-height"),"text-transform":a.css("text-transform"),"letter-spacing":a.css("letter-spacing"),"word-spacing":a.css("word-spacing"),display:"none"},r=t("<span/>").css(i).text(l).appendTo("body"));var n,c,o,d=r.width(),f=parseInt(s.width,10)||a.width(),h="width";if(s.multiline?(r.width(a.width()),h="height",c=r.height(),o=a.height()+1):(c=d,o=f),n={before:"",after:""},c>o){var g,p;r.text(""),"left"===s.side?(g=e(h,o,l,0,l.length,r,s.token,!0),n.after=l.slice(-1*g)):"center"===s.side?(o=parseInt(o/2,10)-1,g=e(h,o,l,0,l.length,r,s.token,!1),p=e(h,o,l,0,l.length,r,"",!0),n.before=l.slice(0,g),n.after=l.slice(-1*p)):"right"===s.side&&(g=e(h,o,l,0,l.length,r,s.token,!1),n.before=l.slice(0,g)),s.addclass&&a.addClass(s.addclass),s.addtitle&&a.attr("title",l),n.before=r.text(n.before).html(),n.after=r.text(n.after).html(),a.empty().html(n.before+s.token+n.after)}s.assumeSameStyle||r.remove()})}}(jQuery);