Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Created July 4, 2010 18:38
Show Gist options
  • Save badsyntax/463650 to your computer and use it in GitHub Desktop.
Save badsyntax/463650 to your computer and use it in GitHub Desktop.
/*
* jQuery UI Ellipsis
*
* @Author Richard Willis
* @Url http://github.com/badsyntax/jquery-plugins/tree/master/ellipsis/
* @Depends
* jquery.ui.core.js
* jquery.ui.widget.js
*
*/
(function($) {
$.widget('ui.ellipsis', {
options: {
chars: '...'
},
_create : function(){
this.element
.data('contents', this.element.contents() )
.addClass( this.widgetBaseClass )
.parent()
.addClass( this.widgetBaseClass + '-container' );
var
words = this.element.text().split(' '),
tries = 0,
helper = $( '<span />' )
.addClass( this.widgetBaseClass + '-helper' )
.append( this.element.contents() )
.appendTo( this.element );
do {
tries ++;
words.pop();
helper.html( words.join(' ') + this.options.chars );
} while ( ( helper.width() > this.element.width() ) && ( tries < 60 ) );
helper.parent().text( helper.text() ).end().remove();
},
destroy : function(){
$.Widget.prototype.destroy.apply(this, arguments);
this.element
.removeClass( this.widgetBaseClass )
.html( this.element.data( 'contents' ) )
.parent()
.removeClass( this.widgetBaseClass + '-container' );
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment