Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated copyright.

  • Loading branch information...
commit 745c38e3e33b5337beba00e2fb2a00f42fe9950c 1 parent b6ec5e6
@polarblau authored
Showing with 45 additions and 45 deletions.
  1. +45 −45 src/jquery.smarttruncation.js
View
90 src/jquery.smarttruncation.js
@@ -1,12 +1,12 @@
/*!
* Smart truncation jQuery plugin
*
- * Copyright (c) 2011 Florian Plank (http://www.polarblau.com/)
+ * Copyright (c) 2012 Florian Plank (http://www.polarblau.com/)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* USAGE:
- *
+ *
* $('.text li').smartTruncation();
* $('.text-2 li').smartTruncation({
* "truncateCenter" : true
@@ -14,21 +14,21 @@
* $('.files li').smartTruncation({
* protectExtensions" : true
* });
- *
+ *
*/
(function($) {
$.fn.smartTruncation = function(options) {
-
+
var settings = $.merge(options || {}, {
"protectExtensions" : false,
"truncateCenter" : false
});
-
+
return $(this).each(function() {
var $this = $(this);
- // cache
+ // cache
if (!$(window).data('smarttruncation.sizecache')) $(window).data('smarttruncation.sizecache', {});
var fontAttributes = {
@@ -45,15 +45,15 @@
return key;
})(fontAttributes);
-
+
var sizes = {};
-
+
// has text with identical font properties been measure before?
if ($(window).data('smarttruncation.sizecache')[cacheKey]) {
sizes = $(window).data('smarttruncation.sizecache')[cacheKey];
-
+
} else {
-
+
// let's get the width of the most common characters
// in the current font-size
var letters = "a b c d e f g h i j k l m n o p q r s t u v w x y z".split(" ");
@@ -67,7 +67,7 @@
var $testWrapper = $('<span/>').css($.extend({
'visibility': 'hidden'
}, fontAttributes)).appendTo('body');
-
+
// place each character into the container and take it's width
$.each(all, function(i, e) {
sizes[e] = $testWrapper.text(e).width();
@@ -79,13 +79,13 @@
// wrap content in a inline element to get exact width
var $wrapper = $this
- .wrapInner('<span/>', {
+ .wrapInner('<span/>', {
'class' : 'smarttruncation-wrapper'
})
.find('span').css({
'whiteSpace': 'nowrap'
});
-
+
var origText = $.trim($wrapper.text());
var outerWidth = $wrapper.width();
var tracking = parseInt($this.css('letterSpacing'), 10) || -1;
@@ -93,37 +93,37 @@
// keep extension visibile if file name
var extension = "";
var fileName = origText;
-
+
if (settings.protectExtensions) {
var str = origText.split('.');
extension = str.pop();
fileName = str.join('.');
- }
-
+ }
+
// truncate if necessary and append ellipsis
var update = function() {
// how much do we need to shave off including the ellipsis we want to append?
var diff = $this.width() - outerWidth - 3 * sizes['.'];
var safety;
-
+
// do we need to truncate
if (diff <= 0) {
-
+
// split the string into separate characters
var chunks = fileName.split("");
-
-
+
+
// do we truncate from the inside out?
if (settings.truncateCenter) {
-
+
// cut the string in two, left holds one half, chunks the other
var left = chunks.slice(0, Math.floor(chunks.length/2));
var right = chunks;
var lengthLeft = left.length;
var lengthRight = right.length;
-
+
// take one character at time from which ever side is bigger off
- while (diff <= 0) {
+ while (diff <= 0) {
var next = lengthLeft > lengthRight? left[--lengthLeft] : left[--lengthRight];
// update the difference between wanted and actual size by checking the size
// of the character from the sizes dictionary and add tracking
@@ -131,15 +131,15 @@
// sizes dictionary
diff = diff + (sizes[next] || sizes['h']) + tracking;
}
-
- // put the truncated text back plus ellipsis and file extension
+
+ // put the truncated text back plus ellipsis and file extension
$wrapper.text(
- $.trim(left.slice(0, lengthLeft).join(""))
- + "..."
- + $.trim(right.slice(right.length - lengthRight).join(""))
+ $.trim(left.slice(0, lengthLeft).join(""))
+ + "..."
+ + $.trim(right.slice(right.length - lengthRight).join(""))
+ extension
);
-
+
// fallback: sometimes (3-5%) the string still doesn't fit:
// insure that text stays within bounds under all circumstances by popping
// one letter at time, while switching sides, trying to fit every time
@@ -147,15 +147,15 @@
while ($wrapper.width() > $this.width()) {
lengthLeft > lengthRight? lengthLeft-- : lengthRight--;
$wrapper.text(
- $.trim(left.slice(0, lengthLeft).join(""))
- + "..."
- + $.trim(right.slice(right.length - lengthRight).join(""))
+ $.trim(left.slice(0, lengthLeft).join(""))
+ + "..."
+ + $.trim(right.slice(right.length - lengthRight).join(""))
+ extension
);
}
} else {
-
- // allow for a little bit room, if we check for absolutes,
+
+ // allow for a little bit room, if we check for absolutes,
// the browser will get caught in a loop and the sky will come down
// let's use 40% of the fontsize
safety = parseInt(fontAttributes.fontSize, 10) * 0.4;
@@ -163,22 +163,22 @@
while ($wrapper.width() + safety < $this.width()) {
lengthLeft < lengthRight? lengthLeft++ : lengthRight++;
$wrapper.text(
- $.trim(left.slice(0, lengthLeft).join(""))
- + "..."
- + $.trim(right.slice(right.length - lengthRight).join(""))
+ $.trim(left.slice(0, lengthLeft).join(""))
+ + "..."
+ + $.trim(right.slice(right.length - lengthRight).join(""))
+ extension
);
}
}
}
-
+
// we only truncate the end, possibly keeping the file extension
} else {
-
+
var length = chunks.length;
-
+
while (diff <= 0) {
-
+
// update the difference between wanted and actual size by checking the size
// of the character from the sizes dictionary and add tracking
// use the letter "h" in case the current character does not exist in the
@@ -186,7 +186,7 @@
diff = diff + (sizes[--length] || sizes['h']) + tracking / 2;
}
$wrapper.text($.trim(chunks.slice(0, length).join("")) + "..." + extension);
-
+
// fallback: sometimes (3-5%) the string still doesn't fit:
// insure that text stays within bounds under all circumstances by popping
// one letter at time, trying to fit every time
@@ -195,8 +195,8 @@
$wrapper.text($.trim(chunks.slice(0, --length).join("")) + "..." + extension);
}
} else {
-
- // allow for a little bit room, if we check for absolutes,
+
+ // allow for a little bit room, if we check for absolutes,
// the browser will get caught in a loop and the sky will come down
// let's use 40% of the fontsize
safety = parseInt(fontAttributes.fontSize, 10) * 0.4;
@@ -219,7 +219,7 @@
// initialize
update();
-
+
});
};
})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.