Skip to content

Commit

Permalink
v3.1
Browse files Browse the repository at this point in the history
callback and height option, bugfix in height calculation
  • Loading branch information
FrDH committed Nov 14, 2017
1 parent 0fb4c0f commit a1c7d57
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 29 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "jQuery.dotdotdot",
"version" : "3.0.5",
"version" : "3.1.0",
"main" : "dist/jquery.dotdotdot.js",
"authors" : "Fred Heusschen <[email protected]>",
"license" : "CC-BY-NC-4.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "jQuery.dotdotdot",
"version" : "3.0.5",
"version" : "3.1.0",
"authors" : "Fred Heusschen <[email protected]>",
"license" : "CC-BY-NC-4.0",
"description" : "Dotdotdot is an advanced jQuery plugin for truncating multiple line content with an ellipsis.",
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.dotdotdot.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "jquery.dotdotdot",
"version" : "3.0.5",
"version" : "3.1.0",
"main" : "dist/jquery.dotdotdot.js",
"author" : "Fred Heusschen <[email protected]>",
"license" : "CC-BY-NC-4.0",
Expand Down
68 changes: 44 additions & 24 deletions src/jquery.dotdotdot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* jQuery dotdotdot 3.0.5
* jQuery dotdotdot 3.1.0
* @requires jQuery 1.7.0 or later
*
* dotdotdot.frebsite.nl
Expand All @@ -15,7 +15,7 @@
'use strict';

var _PLUGIN_ = 'dotdotdot';
var _VERSION_ = '3.0.5';
var _VERSION_ = '3.1.0';

if ( $[ _PLUGIN_ ] && $[ _PLUGIN_ ].version > _VERSION_ )
{
Expand Down Expand Up @@ -55,10 +55,12 @@

$[ _PLUGIN_ ].defaults = {
ellipsis : '\u2026 ',
callback : function( isTruncated ) {},
truncate : 'word',
tolerance : 0,
keep : null,
watch : 'window'
watch : 'window',
height : null
};


Expand All @@ -71,8 +73,7 @@
this.uniqueId = $[ _PLUGIN_ ].uniqueId++;
this.originalContent = this.$dot.contents();
this.originalStyle = this.$dot.attr( 'style' ) || '';
this.maxHeight = this._getMaxHeight() + this.opts.tolerance;


if ( this.$dot.css( 'word-wrap' ) !== 'break-word' )
{
this.$dot.css( 'word-wrap', 'break-word' );
Expand All @@ -81,6 +82,11 @@
{
this.$dot.css( 'white-space', 'normal' );
}

if ( this.opts.height === null )
{
this.opts.height = this._getMaxHeight();
}
},

getInstance: function()
Expand Down Expand Up @@ -141,11 +147,12 @@
var e = this,
$e = $(this);

// Text nodes
if ( e.nodeType == 3 )
{

// Remove whitespace where it does not take up space in the DOM
if ( $e.prev().is( 'table, thead, tfoot, tr, th, td, dl, dt, dd, ul, ol, li, video' ) )
if ( $e.parent().is( 'table, thead, tfoot, tr, dl, ul, ol, video' ) )
{
$e.remove();
return;
Expand All @@ -168,7 +175,7 @@
}
}

// Remove comments
// Comment nodes
else if ( e.nodeType == 8 )
{
$e.remove();
Expand All @@ -178,6 +185,9 @@
);


this.maxHeight = this._getMaxHeight();


// Truncate the text
var isTruncated = this._truncateNode( this.$dot );
this.$dot[ isTruncated ? 'addClass' : 'removeClass' ]( _c.truncated );
Expand All @@ -204,7 +214,7 @@
this.$inner.replaceWith( this.$inner.contents() );
this.$inner = null;


this.opts.callback.call( this.$dot[ 0 ], isTruncated );
return isTruncated;
},

Expand Down Expand Up @@ -456,7 +466,7 @@

_fits: function()
{
return ( this.$inner.innerHeight() <= this.maxHeight );
return ( this.$inner.innerHeight() <= this.maxHeight + this.opts.tolerance );
},

_addEllipsis: function( txt )
Expand All @@ -474,37 +484,47 @@

_getMaxHeight: function()
{
if ( typeof this.opts.height == 'number' )
{
return this.opts.height;
}

// Find smallest CSS height
var arr = [ 'height', 'maxHeight' ],
var arr = [ 'maxHeight', 'height' ],
hgh = 0;

for ( var a = 0; a < arr.length; a++ )
{
var h = window.getComputedStyle( this.$dot[ 0 ] )[ arr[ a ] ];
if ( h.slice( -2 ) == 'px' )
hgh = window.getComputedStyle( this.$dot[ 0 ] )[ arr[ a ] ];
if ( hgh.slice( -2 ) == 'px' )
{
h = parseInt( h, 10 );
hgh = hgh ? Math.min( hgh, h ) : h;
hgh = parseFloat( hgh );
break;
}
}

// Remove padding-top/bottom when needed.
var arr = [];
switch ( this.$dot.css( 'boxSizing' ) )
{
case 'padding-box':
case 'border-box':
var arr = [ 'paddingTop', 'paddingBottom' ];
arr.push( 'borderTopWidth' );
arr.push( 'borderBottomWidth' );
// no break -> padding needs to be added too

for ( var a = 0; a < arr.length; a++ )
{
var p = window.getComputedStyle( this.$dot[ 0 ] )[ arr[ a ] ];
if ( p.slice( -2 ) == 'px' )
{
hgh -= parseInt( p, 10 );
}
}
case 'padding-box':
arr.push( 'paddingTop' );
arr.push( 'paddingBottom' );
break;
}
for ( var a = 0; a < arr.length; a++ )
{
var p = window.getComputedStyle( this.$dot[ 0 ] )[ arr[ a ] ];
if ( p.slice( -2 ) == 'px' )
{
hgh -= parseFloat( p );
}
}

// Sanitize
return Math.max( hgh, 0 );
Expand Down

0 comments on commit a1c7d57

Please sign in to comment.