Merge "Fixed dependencies for jquery.collapsibleTabs"
[lhc/web/wiklou.git] / resources / jquery / jquery.autoEllipsis.js
index 7bdc7f5..49a932a 100644 (file)
@@ -1,53 +1,57 @@
 /**
- * Plugin that automatically truncates the plain text contents of an element and adds an ellipsis
+ * Plugin that automatically truncates the plain text contents of an element
+ * and adds an ellipsis.
  */
-( function( $ ) {
+( function ( $ ) {
 
-// Cache ellipsed substrings for every string-width-position combination
-var cache = { };
-// Use a seperate cache when match highlighting is enabled
-var matchTextCache = { };
+var
+       // Cache ellipsed substrings for every string-width-position combination
+       cache = {},
 
-$.fn.autoEllipsis = function( options ) {
+       // Use a separate cache when match highlighting is enabled
+       matchTextCache = {};
+
+$.fn.autoEllipsis = function ( options ) {
        options = $.extend( {
-               'position': 'center',
-               'tooltip': false,
-               'restoreText': false,
-               'hasSpan': false,
-               'matchText': null
+               position: 'center',
+               tooltip: false,
+               restoreText: false,
+               hasSpan: false,
+               matchText: null
        }, options );
-       $(this).each( function() {
-               var $el = $(this);
+
+       return this.each( function () {
+               var $trimmableText,
+                       text, trimmableText, w, pw,
+                       l, r, i, side, m,
+                       // container element - used for measuring against
+                       $container = $(this);
+
                if ( options.restoreText ) {
-                       if ( !$el.data( 'autoEllipsis.originalText' ) ) {
-                               $el.data( 'autoEllipsis.originalText', $el.text() );
+                       if ( !$container.data( 'autoEllipsis.originalText' ) ) {
+                               $container.data( 'autoEllipsis.originalText', $container.text() );
                        } else {
-                               $el.text( $el.data( 'autoEllipsis.originalText' ) );
+                               $container.text( $container.data( 'autoEllipsis.originalText' ) );
                        }
                }
-               
-               // container element - used for measuring against
-               var $container = $el;
-               // trimmable text element - only the text within this element will be trimmed
-               var $trimmableText = null;
-               // protected text element - the width of this element is counted, but next is never trimmed from it
-               var $protectedText = null;
 
+               // trimmable text element - only the text within this element will be trimmed
                if ( options.hasSpan ) {
-                       $trimmableText = $el.children( options.selector );
+                       $trimmableText = $container.children( options.selector );
                } else {
-                       $trimmableText = $( '<span />' )
+                       $trimmableText = $( '<span>' )
                                .css( 'whiteSpace', 'nowrap' )
-                               .text( $el.text() );
-                       $el
+                               .text( $container.text() );
+                       $container
                                .empty()
                                .append( $trimmableText );
                }
-               
-               var text = $container.text();
-               var trimmableText = $trimmableText.text();
-               var w = $container.width();
-               var pw = $protectedText ? $protectedText.width() : 0;
+
+               text = $container.text();
+               trimmableText = $trimmableText.text();
+               w = $container.width();
+               pw = 0;
+
                // Try cache
                if ( options.matchText ) {
                        if ( !( text in matchTextCache ) ) {
@@ -78,18 +82,18 @@ $.fn.autoEllipsis = function( options ) {
                                if ( options.tooltip ) {
                                        $container.attr( 'title', text );
                                }
-                               console.log("YAY CACHE HIT");
                                return;
                        }
                }
-               
+
                if ( $trimmableText.width() + pw > w ) {
                        switch ( options.position ) {
                                case 'right':
                                        // Use binary search-like technique for efficiency
-                                       var l = 0, r = trimmableText.length;
+                                       l = 0;
+                                       r = trimmableText.length;
                                        do {
-                                               var m = Math.ceil( ( l + r ) / 2 );
+                                               m = Math.ceil( ( l + r ) / 2 );
                                                $trimmableText.text( trimmableText.substr( 0, m ) + '...' );
                                                if ( $trimmableText.width() + pw > w ) {
                                                        // Text is too long
@@ -102,9 +106,10 @@ $.fn.autoEllipsis = function( options ) {
                                        break;
                                case 'center':
                                        // TODO: Use binary search like for 'right'
-                                       var i = [Math.round( trimmableText.length / 2 ), Math.round( trimmableText.length / 2 )];
-                                       var side = 1; // Begin with making the end shorter
-                                       while ( $trimmableText.outerWidth() + pw > w  && i[0] > 0 ) {
+                                       i = [Math.round( trimmableText.length / 2 ), Math.round( trimmableText.length / 2 )];
+                                       // Begin with making the end shorter
+                                       side = 1;
+                                       while ( $trimmableText.outerWidth() + pw > w && i[0] > 0 ) {
                                                $trimmableText.text( trimmableText.substr( 0, i[0] ) + '...' + trimmableText.substr( i[1] ) );
                                                // Alternate between trimming the end and begining
                                                if ( side === 0 ) {
@@ -120,7 +125,7 @@ $.fn.autoEllipsis = function( options ) {
                                        break;
                                case 'left':
                                        // TODO: Use binary search like for 'right'
-                                       var r = 0;
+                                       r = 0;
                                        while ( $trimmableText.outerWidth() + pw > w && r < trimmableText.length ) {
                                                $trimmableText.text( '...' + trimmableText.substr( r ) );
                                                r++;
@@ -137,8 +142,8 @@ $.fn.autoEllipsis = function( options ) {
                } else {
                        cache[text][w][options.position] = $container.html();
                }
-               
+
        } );
 };
 
-} )( jQuery );
\ No newline at end of file
+}( jQuery ) );
\ No newline at end of file