2 * Plugin that automatically truncates the plain text contents of an element and adds an ellipsis
6 // Cache ellipsed substrings for every string-width-position combination
8 // Use a separate cache when match highlighting is enabled
9 var matchTextCache = { };
11 $.fn.autoEllipsis = function( options ) {
19 $(this).each( function() {
21 if ( options.restoreText ) {
22 if ( !$el.data( 'autoEllipsis.originalText' ) ) {
23 $el.data( 'autoEllipsis.originalText', $el.text() );
25 $el.text( $el.data( 'autoEllipsis.originalText' ) );
29 // container element - used for measuring against
31 // trimmable text element - only the text within this element will be trimmed
32 var $trimmableText = null;
33 // protected text element - the width of this element is counted, but next is never trimmed from it
34 var $protectedText = null;
36 if ( options.hasSpan ) {
37 $trimmableText = $el.children( options.selector );
39 $trimmableText = $( '<span />' )
40 .css( 'whiteSpace', 'nowrap' )
44 .append( $trimmableText );
47 var text = $container.text();
48 var trimmableText = $trimmableText.text();
49 var w = $container.width();
50 var pw = $protectedText ? $protectedText.width() : 0;
52 if ( options.matchText ) {
53 if ( !( text in matchTextCache ) ) {
54 matchTextCache[text] = {};
56 if ( !( options.matchText in matchTextCache[text] ) ) {
57 matchTextCache[text][options.matchText] = {};
59 if ( !( w in matchTextCache[text][options.matchText] ) ) {
60 matchTextCache[text][options.matchText][w] = {};
62 if ( options.position in matchTextCache[text][options.matchText][w] ) {
63 $container.html( matchTextCache[text][options.matchText][w][options.position] );
64 if ( options.tooltip ) {
65 $container.attr( 'title', text );
70 if ( !( text in cache ) ) {
73 if ( !( w in cache[text] ) ) {
76 if ( options.position in cache[text][w] ) {
77 $container.html( cache[text][w][options.position] );
78 if ( options.tooltip ) {
79 $container.attr( 'title', text );
85 if ( $trimmableText.width() + pw > w ) {
86 switch ( options.position ) {
88 // Use binary search-like technique for efficiency
89 var l = 0, r = trimmableText.length;
91 var m = Math.ceil( ( l + r ) / 2 );
92 $trimmableText.text( trimmableText.substr( 0, m ) + '...' );
93 if ( $trimmableText.width() + pw > w ) {
100 $trimmableText.text( trimmableText.substr( 0, l ) + '...' );
103 // TODO: Use binary search like for 'right'
104 var i = [Math.round( trimmableText.length / 2 ), Math.round( trimmableText.length / 2 )];
105 var side = 1; // Begin with making the end shorter
106 while ( $trimmableText.outerWidth() + pw > w && i[0] > 0 ) {
107 $trimmableText.text( trimmableText.substr( 0, i[0] ) + '...' + trimmableText.substr( i[1] ) );
108 // Alternate between trimming the end and begining
110 // Make the begining shorter
114 // Make the end shorter
121 // TODO: Use binary search like for 'right'
123 while ( $trimmableText.outerWidth() + pw > w && r < trimmableText.length ) {
124 $trimmableText.text( '...' + trimmableText.substr( r ) );
130 if ( options.tooltip ) {
131 $container.attr( 'title', text );
133 if ( options.matchText ) {
134 $container.highlightText( options.matchText );
135 matchTextCache[text][options.matchText][w][options.position] = $container.html();
137 cache[text][w][options.position] = $container.html();