04bb301c2334410f814ae8ad4373a8b300b6cfdf
2 * Plugin that automatically truncates the plain text contents of an element and adds an ellipsis
7 // Cache ellipsed substrings for every string-width-position combination
10 // Use a separate cache when match highlighting is enabled
13 $.fn
.autoEllipsis = function ( options
) {
21 $(this).each( function () {
22 var $container
, $trimmableText
,
23 text
, trimmableText
, w
, pw
,
26 if ( options
.restoreText
) {
27 if ( !$el
.data( 'autoEllipsis.originalText' ) ) {
28 $el
.data( 'autoEllipsis.originalText', $el
.text() );
30 $el
.text( $el
.data( 'autoEllipsis.originalText' ) );
34 // container element - used for measuring against
37 // trimmable text element - only the text within this element will be trimmed
38 if ( options
.hasSpan
) {
39 $trimmableText
= $el
.children( options
.selector
);
41 $trimmableText
= $( '<span>' )
42 .css( 'whiteSpace', 'nowrap' )
46 .append( $trimmableText
);
49 text
= $container
.text();
50 trimmableText
= $trimmableText
.text();
51 w
= $container
.width();
55 if ( options
.matchText
) {
56 if ( !( text
in matchTextCache
) ) {
57 matchTextCache
[text
] = {};
59 if ( !( options
.matchText
in matchTextCache
[text
] ) ) {
60 matchTextCache
[text
][options
.matchText
] = {};
62 if ( !( w
in matchTextCache
[text
][options
.matchText
] ) ) {
63 matchTextCache
[text
][options
.matchText
][w
] = {};
65 if ( options
.position
in matchTextCache
[text
][options
.matchText
][w
] ) {
66 $container
.html( matchTextCache
[text
][options
.matchText
][w
][options
.position
] );
67 if ( options
.tooltip
) {
68 $container
.attr( 'title', text
);
73 if ( !( text
in cache
) ) {
76 if ( !( w
in cache
[text
] ) ) {
79 if ( options
.position
in cache
[text
][w
] ) {
80 $container
.html( cache
[text
][w
][options
.position
] );
81 if ( options
.tooltip
) {
82 $container
.attr( 'title', text
);
88 if ( $trimmableText
.width() + pw
> w
) {
89 switch ( options
.position
) {
91 // Use binary search-like technique for efficiency
93 r
= trimmableText
.length
;
95 m
= Math
.ceil( ( l
+ r
) / 2 );
96 $trimmableText
.text( trimmableText
.substr( 0, m
) + '...' );
97 if ( $trimmableText
.width() + pw
> w
) {
104 $trimmableText
.text( trimmableText
.substr( 0, l
) + '...' );
107 // TODO: Use binary search like for 'right'
108 i
= [Math
.round( trimmableText
.length
/ 2 ), Math
.round( trimmableText
.length
/ 2 )];
109 // Begin with making the end shorter
111 while ( $trimmableText
.outerWidth() + pw
> w
&& i
[0] > 0 ) {
112 $trimmableText
.text( trimmableText
.substr( 0, i
[0] ) + '...' + trimmableText
.substr( i
[1] ) );
113 // Alternate between trimming the end and begining
115 // Make the begining shorter
119 // Make the end shorter
126 // TODO: Use binary search like for 'right'
128 while ( $trimmableText
.outerWidth() + pw
> w
&& r
< trimmableText
.length
) {
129 $trimmableText
.text( '...' + trimmableText
.substr( r
) );
135 if ( options
.tooltip
) {
136 $container
.attr( 'title', text
);
138 if ( options
.matchText
) {
139 $container
.highlightText( options
.matchText
);
140 matchTextCache
[text
][options
.matchText
][w
][options
.position
] = $container
.html();
142 cache
[text
][w
][options
.position
] = $container
.html();