2 * Plugin that automatically truncates the plain text contents of an element
3 * and adds an ellipsis.
8 // Cache ellipsed substrings for every string-width-position combination
11 // Use a separate cache when match highlighting is enabled
14 $.fn.autoEllipsis = function ( options ) {
23 return this.each( function () {
25 text, trimmableText, w, pw,
27 // container element - used for measuring against
30 if ( options.restoreText ) {
31 if ( !$container.data( 'autoEllipsis.originalText' ) ) {
32 $container.data( 'autoEllipsis.originalText', $container.text() );
34 $container.text( $container.data( 'autoEllipsis.originalText' ) );
38 // trimmable text element - only the text within this element will be trimmed
39 if ( options.hasSpan ) {
40 $trimmableText = $container.children( options.selector );
42 $trimmableText = $( '<span>' )
43 .css( 'whiteSpace', 'nowrap' )
44 .text( $container.text() );
47 .append( $trimmableText );
50 text = $container.text();
51 trimmableText = $trimmableText.text();
52 w = $container.width();
56 if ( options.matchText ) {
57 if ( !( text in matchTextCache ) ) {
58 matchTextCache[text] = {};
60 if ( !( options.matchText in matchTextCache[text] ) ) {
61 matchTextCache[text][options.matchText] = {};
63 if ( !( w in matchTextCache[text][options.matchText] ) ) {
64 matchTextCache[text][options.matchText][w] = {};
66 if ( options.position in matchTextCache[text][options.matchText][w] ) {
67 $container.html( matchTextCache[text][options.matchText][w][options.position] );
68 if ( options.tooltip ) {
69 $container.attr( 'title', text );
74 if ( !( text in cache ) ) {
77 if ( !( w in cache[text] ) ) {
80 if ( options.position in cache[text][w] ) {
81 $container.html( cache[text][w][options.position] );
82 if ( options.tooltip ) {
83 $container.attr( 'title', text );
89 if ( $trimmableText.width() + pw > w ) {
90 switch ( options.position ) {
92 // Use binary search-like technique for efficiency
94 r = trimmableText.length;
96 m = Math.ceil( ( l + r ) / 2 );
97 $trimmableText.text( trimmableText.substr( 0, m ) + '...' );
98 if ( $trimmableText.width() + pw > w ) {
105 $trimmableText.text( trimmableText.substr( 0, l ) + '...' );
108 // TODO: Use binary search like for 'right'
109 i = [Math.round( trimmableText.length / 2 ), Math.round( trimmableText.length / 2 )];
110 // Begin with making the end shorter
112 while ( $trimmableText.outerWidth() + pw > w && i[0] > 0 ) {
113 $trimmableText.text( trimmableText.substr( 0, i[0] ) + '...' + trimmableText.substr( i[1] ) );
114 // Alternate between trimming the end and begining
116 // Make the begining shorter
120 // Make the end shorter
127 // TODO: Use binary search like for 'right'
129 while ( $trimmableText.outerWidth() + pw > w && r < trimmableText.length ) {
130 $trimmableText.text( '...' + trimmableText.substr( r ) );
136 if ( options.tooltip ) {
137 $container.attr( 'title', text );
139 if ( options.matchText ) {
140 $container.highlightText( options.matchText );
141 matchTextCache[text][options.matchText][w][options.position] = $container.html();
143 cache[text][w][options.position] = $container.html();