]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - resources/jquery/jquery.autoEllipsis.js
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / resources / jquery / jquery.autoEllipsis.js
1 /**
2  * Plugin that automatically truncates the plain text contents of an element and adds an ellipsis
3  */
4 ( function( $ ) {
5
6 // Cache ellipsed substrings for every string-width combination
7 var cache = { };
8 // Use a seperate cache when match highlighting is enabled
9 var matchTextCache = { };
10
11 $.fn.autoEllipsis = function( options ) {
12         options = $.extend( {
13                 'position': 'center',
14                 'tooltip': false,
15                 'restoreText': false,
16                 'hasSpan': false,
17                 'matchText': null
18         }, options );
19         $(this).each( function() {
20                 var $this = $(this);
21                 if ( options.restoreText ) {
22                         if ( ! $this.data( 'autoEllipsis.originalText' ) ) {
23                                 $this.data( 'autoEllipsis.originalText', $this.text() );
24                         } else {
25                                 $this.text( $this.data( 'autoEllipsis.originalText' ) );
26                         }
27                 }
28                 
29                 // container element - used for measuring against
30                 var $container = $this;
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;
35
36                 if ( options.hasSpan ) {
37                         $trimmableText = $this.children( options.selector );
38                 } else {
39                         $trimmableText = $( '<span />' )
40                                 .css( 'whiteSpace', 'nowrap' )
41                                 .text( $this.text() );
42                         $this
43                                 .empty()
44                                 .append( $trimmableText );
45                 }
46                 
47                 var text = $container.text();
48                 var trimmableText = $trimmableText.text();
49                 var w = $container.width();
50                 var pw = $protectedText ? $protectedText.width() : 0;
51                 // Try cache
52                 if ( !( text in cache ) ) {
53                         cache[text] = {};
54                 }
55                 if ( options.matchText && !( text in matchTextCache ) ) {
56                         matchTextCache[text] = {};
57                 }
58                 if ( options.matchText && !( options.matchText in matchTextCache[text] ) ) {
59                         matchTextCache[text][options.matchText] = {};
60                 }
61                 if ( !options.matchText && w in cache[text] ) {
62                         $container.html( cache[text][w] );
63                         if ( options.tooltip )
64                                 $container.attr( 'title', text );
65                         return;
66                 }
67                 if( options.matchText && options.matchText in matchTextCache[text] && w in matchTextCache[text][options.matchText] ) {
68                         $container.html( matchTextCache[text][options.matchText][w] );
69                         if ( options.tooltip )
70                                 $container.attr( 'title', text );
71                         return;
72                 }
73                 if ( $trimmableText.width() + pw > w ) {
74                         switch ( options.position ) {
75                                 case 'right':
76                                         // Use binary search-like technique for efficiency
77                                         var l = 0, r = trimmableText.length;
78                                         do {
79                                                 var m = Math.ceil( ( l + r ) / 2 );
80                                                 $trimmableText.text( trimmableText.substr( 0, m ) + '...' );
81                                                 if ( $trimmableText.width() + pw > w ) {
82                                                         // Text is too long
83                                                         r = m - 1;
84                                                 } else {
85                                                         l = m;
86                                                 }
87                                         } while ( l < r );
88                                         $trimmableText.text( trimmableText.substr( 0, l ) + '...' );
89                                         break;
90                                 case 'center':
91                                         // TODO: Use binary search like for 'right'
92                                         var i = [Math.round( trimmableText.length / 2 ), Math.round( trimmableText.length / 2 )];
93                                         var side = 1; // Begin with making the end shorter
94                                         while ( $trimmableText.outerWidth() + pw > w  && i[0] > 0 ) {
95                                                 $trimmableText.text( trimmableText.substr( 0, i[0] ) + '...' + trimmableText.substr( i[1] ) );
96                                                 // Alternate between trimming the end and begining
97                                                 if ( side == 0 ) {
98                                                         // Make the begining shorter
99                                                         i[0]--;
100                                                         side = 1;
101                                                 } else {
102                                                         // Make the end shorter
103                                                         i[1]++;
104                                                         side = 0;
105                                                 }
106                                         }
107                                         break;
108                                 case 'left':
109                                         // TODO: Use binary search like for 'right'
110                                         var r = 0;
111                                         while ( $trimmableText.outerWidth() + pw > w && r < trimmableText.length ) {
112                                                 $trimmableText.text( '...' + trimmableText.substr( r ) );
113                                                 r++;
114                                         }
115                                         break;
116                         }
117                 }
118                 if ( options.tooltip ) {
119                         $container.attr( 'title', text );
120                 }
121                 if ( options.matchText ) {
122                         $container.highlightText( options.matchText );
123                         matchTextCache[text][options.matchText][w] = $container.html();
124                 } else {
125                         cache[text][w] = $container.html();
126                 }
127                 
128         } );
129 };
130
131 } )( jQuery );