MarkTraceur has uploaded a new change for review.
https://gerrit.wikimedia.org/r/92005
Change subject: Add lines option to jq.autoEllipsis
......................................................................
Add lines option to jq.autoEllipsis
Needed for multi-line descriptions in MultimediaViewer. See accompanying
commit, coming soon.
Change-Id: I316b9ad3f22762f93549ea20c3816964270e5d49
---
M resources/jquery/jquery.autoEllipsis.js
1 file changed, 38 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/05/92005/1
diff --git a/resources/jquery/jquery.autoEllipsis.js
b/resources/jquery/jquery.autoEllipsis.js
index 49a932a..d23d4d3 100644
--- a/resources/jquery/jquery.autoEllipsis.js
+++ b/resources/jquery/jquery.autoEllipsis.js
@@ -17,13 +17,16 @@
tooltip: false,
restoreText: false,
hasSpan: false,
+ // Use lines: 2 (anything above 1) to signify a height limit,
+ // instead of a width limit. Will truncate last line that fits.
+ lines: null,
matchText: null
}, options );
return this.each( function () {
var $trimmableText,
text, trimmableText, w, pw,
- l, r, i, side, m,
+ l, r, i, side, m, h,
// container element - used for measuring against
$container = $(this);
@@ -38,6 +41,12 @@
// trimmable text element - only the text within this element
will be trimmed
if ( options.hasSpan ) {
$trimmableText = $container.children( options.selector
);
+ } else if ( options.lines && options.lines > 1 ) {
+ $trimmableText = $( '<span>' )
+ .text( $container.text() );
+ $container
+ .empty()
+ .append( $trimmableText );
} else {
$trimmableText = $( '<span>' )
.css( 'whiteSpace', 'nowrap' )
@@ -49,6 +58,33 @@
text = $container.text();
trimmableText = $trimmableText.text();
+
+ if ( options.lines && options.lines > 1 ) {
+ // Figure out how much height a normal line has, then
+ // multiply by the number of lines we want - add 1
because
+ // otherwise it truncates too soon.
+ h = ( options.lines + 1 ) * $trimmableText.html(
' ' ).height();
+ $trimmableText.text( trimmableText );
+
+ l = 0;
+ r = trimmableText.length;
+
+ if ( $trimmableText.height() > h ) {
+ while ( l < r ) {
+ m = Math.ceil( ( l + r ) / 2 );
+ $trimmableText.text(
trimmableText.substr( 0, m ) + '...' );
+
+ if ( $trimmableText.height() > h ) {
+ r = m - 1;
+ } else {
+ l = m;
+ }
+ }
+
+ $trimmableText.text( trimmableText.substr( 0, l
) + '...' );
+ }
+ }
+
w = $container.width();
pw = 0;
@@ -146,4 +182,4 @@
} );
};
-}( jQuery ) );
\ No newline at end of file
+}( jQuery ) );
--
To view, visit https://gerrit.wikimedia.org/r/92005
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I316b9ad3f22762f93549ea20c3816964270e5d49
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MarkTraceur <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits