Files

15 lines
6.0 KiB
JavaScript

/*
* jQuery dotdotdot 1.6.7
*
* Copyright (c) 2013 Fred Heusschen
* www.frebsite.nl
*
* Plugin website:
* dotdotdot.frebsite.nl
*
* Dual licensed under the MIT and GPL licenses.
* http://en.wikipedia.org/wiki/MIT_License
* http://en.wikipedia.org/wiki/GNU_General_Public_License
*/
!function(a,b){function d(a,b,c){var d=a.children(),e=!1;a.empty();for(var f=0,h=d.length;h>f;f++){var i=d.eq(f);if(a.append(i),c&&a.append(c),g(a,b)){i.remove(),e=!0;break}c&&c.detach()}return e}function e(b,c,d,h,i){var j=b.contents(),k=!1;b.empty();for(var l="table, thead, tbody, tfoot, tr, col, colgroup, object, embed, param, ol, ul, dl, blockquote, select, optgroup, option, textarea, script, style",m=0,n=j.length;n>m&&!k;m++){var o=j[m],p=a(o);"undefined"==typeof o||3==o.nodeType&&0==a.trim(o.data).length||(b.append(p),i&&b[b.is(l)?"after":"append"](i),g(d,h)&&(k=3==o.nodeType?f(p,c,d,h,i):e(p,c,d,h,i),k||(p.remove(),k=!0)),k||i&&i.detach())}return k}function f(b,c,d,e,f){var i=b[0];if(!i)return!1;var l=k(i),n=-1!==l.indexOf(" ")?" ":"\u3000",o="letter"==e.wrap?"":n,p=l.split(o),q=-1,r=-1,s=0,t=p.length-1;for(e.fallbackToLetter&&0==t&&t==s&&(o="",p=l.split(o),t=p.length-1);t>=s&&(0!=s||0!=t);){var u=Math.floor((s+t)/2);if(u==r)break;r=u,j(i,p.slice(0,r+1).join(o)+e.ellipsis),g(d,e)?t=r:(q=r,s=r)}if(-1==q||1==p.length&&0==p[0].length){var v=b.parent();b.remove();var w=f&&f.closest(v).length?f.length:0;v.contents().length>w?i=m(v.contents().eq(-1-w),c):(i=m(v,c,!0),w||v.remove()),i&&(l=h(k(i),e),j(i,l),w&&f&&a(i).parent().append(f))}else l=h(p.slice(0,q+1).join(o),e),j(i,l);return!0}function g(a,b){return a.innerHeight()>b.maxHeight}function h(b,c){for(;a.inArray(b.slice(-1),c.lastCharacter.remove)>-1;)b=b.slice(0,-1);return a.inArray(b.slice(-1),c.lastCharacter.noEllipsis)<0&&(b+=c.ellipsis),b}function i(a){return{width:a.innerWidth(),height:a.innerHeight()}}function j(a,b){a.innerText?a.innerText=b:a.nodeValue?a.nodeValue=b:a.textContent&&(a.textContent=b)}function k(a){return a.innerText?a.innerText:a.nodeValue?a.nodeValue:a.textContent?a.textContent:""}function l(a){do a=a.previousSibling;while(a&&1!==a.nodeType&&3!==a.nodeType);return a}function m(b,c,d){var f,e=b&&b[0];if(e){if(!d){if(3===e.nodeType)return e;if(a.trim(b.text()))return m(b.contents().last(),c)}for(f=l(e);!f;){if(b=b.parent(),b.is(c)||!b.length)return!1;f=l(b[0])}if(f)return m(a(f),c)}return!1}function n(b,c){return b?"string"==typeof b?(b=a(b,c),b.length?b:!1):b.jquery?b:!1:!1}function o(a){for(var b=a.innerHeight(),c=["paddingTop","paddingBottom"],d=0,e=c.length;e>d;d++){var f=parseInt(a.css(c[d]),10);isNaN(f)&&(f=0),b-=f}return b}function p(a,b){return a?(b="string"==typeof b?"dotdotdot: "+b:["dotdotdot:",b],"undefined"!=typeof window.console&&"undefined"!=typeof window.console.log&&window.console.log(b),!1):!1}if(!a.fn.dotdotdot){a.fn.dotdotdot=function(b){if(0==this.length)return b&&b.debug===!1||p(!0,'No element found for "'+this.selector+'".'),this;if(this.length>1)return this.each(function(){a(this).dotdotdot(b)});var f=this;f.data("dotdotdot")&&f.trigger("destroy.dot"),f.data("dotdotdot-style",f.attr("style")||""),f.css("word-wrap","break-word"),"nowrap"===f.css("white-space")&&f.css("white-space","normal"),f.bind_events=function(){return f.bind("update.dot",function(b,c){b.preventDefault(),b.stopPropagation(),j.maxHeight="number"==typeof j.height?j.height:o(f),j.maxHeight+=j.tolerance,"undefined"!=typeof c&&(("string"==typeof c||c instanceof HTMLElement)&&(c=a("<div />").append(c).contents()),c instanceof a&&(h=c)),q=f.wrapInner('<div class="dotdotdot" />').children(),q.empty().append(h.clone(!0)).find("br").replaceWith(" <br /> ").end().css({height:"auto",width:"auto",border:"none",padding:0,margin:0});var i=!1,l=!1;return k.afterElement&&(i=k.afterElement.clone(!0),i.show(),k.afterElement.remove()),g(q,j)&&(l="children"==j.wrap?d(q,j,i):e(q,f,q,j,i)),q.replaceWith(q.contents()),q=null,a.isFunction(j.callback)&&j.callback.call(f[0],l,h),k.isTruncated=l,l}).bind("isTruncated.dot",function(a,b){return a.preventDefault(),a.stopPropagation(),"function"==typeof b&&b.call(f[0],k.isTruncated),k.isTruncated}).bind("originalContent.dot",function(a,b){return a.preventDefault(),a.stopPropagation(),"function"==typeof b&&b.call(f[0],h),h}).bind("destroy.dot",function(a){a.preventDefault(),a.stopPropagation(),f.unwatch().unbind_events().empty().append(h).attr("style",f.data("dotdotdot-style")||"").data("dotdotdot",!1)}),f},f.unbind_events=function(){return f.unbind(".dot"),f},f.watch=function(){if(f.unwatch(),"window"==j.watch){var b=a(window),c=b.width(),d=b.height();b.bind("resize.dot"+k.dotId,function(){c==b.width()&&d==b.height()&&j.windowResizeFix||(c=b.width(),d=b.height(),m&&clearInterval(m),m=setTimeout(function(){f.trigger("update.dot")},10))})}else l=i(f),m=setInterval(function(){var a=i(f);(l.width!=a.width||l.height!=a.height)&&(f.trigger("update.dot"),l=i(f))},100);return f},f.unwatch=function(){return a(window).unbind("resize.dot"+k.dotId),m&&clearInterval(m),f};var h=f.contents(),j=a.extend(!0,{},a.fn.dotdotdot.defaults,b),k={},l={},m=null,q=null;return j.lastCharacter.remove instanceof Array||(j.lastCharacter.remove=a.fn.dotdotdot.defaultArrays.lastCharacter.remove),j.lastCharacter.noEllipsis instanceof Array||(j.lastCharacter.noEllipsis=a.fn.dotdotdot.defaultArrays.lastCharacter.noEllipsis),k.afterElement=n(j.after,f),k.isTruncated=!1,k.dotId=c++,f.data("dotdotdot",!0).bind_events().trigger("update.dot"),j.watch&&f.watch(),f},a.fn.dotdotdot.defaults={ellipsis:"... ",wrap:"word",fallbackToLetter:!0,lastCharacter:{},tolerance:0,callback:null,after:null,height:null,watch:!1,windowResizeFix:!0,debug:!1},a.fn.dotdotdot.defaultArrays={lastCharacter:{remove:[" ","\u3000",",",";",".","!","?"],noEllipsis:[]}};var c=1,q=a.fn.html;a.fn.html=function(c){return c!=b&&!a.isFunction(c)&&this.data("dotdotdot")?this.trigger("update",[c]):q.apply(this,arguments)};var r=a.fn.text;a.fn.text=function(c){return c!=b&&!a.isFunction(c)&&this.data("dotdotdot")?(c=a("<div />").text(c).html(),this.trigger("update",[c])):r.apply(this,arguments)}}}(jQuery);