/*
MIT LICENSE
Retrieved from: https://github.com/tbasse/jquery-truncate
Copyright (c) 2012-2013 Thorsten Basse and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function ($) {
'use strict';
function makeWorker($element, elementText) {
var fontCSS = {
'fontFamily': $element.css('fontFamily'),
'fontSize': $element.css('fontSize'),
'fontStyle': $element.css('fontStyle'),
'fontWeight': $element.css('fontWeight'),
'lineHeight': $element.css('lineHeight'),
'font-variant': $element.css('font-variant'),
'text-indent': $element.css('text-indent'),
'text-transform': $element.css('text-transform'),
'letter-spacing': $element.css('letter-spacing'),
'word-spacing': $element.css('word-spacing'),
'display': 'none'
};
return $('<span/>')
.css(fontCSS)
.text(elementText)
.appendTo('body');
}
function findTruncPoint(dim, max, txt, start, end, $worker, token, reverse) {
var makeContent = function (content) {
$worker.text(content);
$worker[reverse ? 'prepend' : 'append'](token);
};
var opt1, opt2, mid, opt1dim, opt2dim;
if (reverse) {
opt1 = start === 0 ? '' : txt.slice(-start);
opt2 = txt.slice(-end);
} else {
opt1 = txt.slice(0, start);
opt2 = txt.slice(0, end);
}
if (max < $worker.html(token)[dim]()) {
return 0;
}
makeContent(opt2);
opt1dim = $worker[dim]();
makeContent(opt1);
opt2dim = $worker[dim]();
if (opt1dim < opt2dim) {
return end;
}
mid = parseInt((start + end) / 2, 10);
opt1 = reverse ? txt.slice(-mid) : txt.slice(0, mid);
makeContent(opt1);
if ($worker[dim]() === max) {
return mid;
}
if ($worker[dim]() > max) {
end = mid - 1;
} else {
start = mid + 1;
}
return findTruncPoint(dim, max, txt, start, end, $worker, token, reverse);
}
// More efficient (log n) algorithm for doing center truncation on a rendered string.
// Precondition: caller has already verified truncation is necessary (txt dimension is greater than max)
function truncateCenter(dim, max, txt, $worker, token) {
var makeContent = function (before, after) {
$worker.text(before);
$worker.append(token);
$worker.append(after);
};
var txtLength = txt.length;
var midPoint, centerStart, centerEnd, delta
var before, after, truncatedDim, truncatedStringLength;
var bestFit = {before: txt, after: ''}, bestFitDim = 0, bestFitStringLength = 0;
midPoint = parseInt(txtLength / 2, 10);
// since we're assuming the caller detected the text needs truncation prior to calling this function,
// we don't have to test that the entire string is the best fit, so can start at midPoint +/- txtLength / 4
delta = parseInt(txtLength / 4, 10);
centerStart = midPoint - delta;
centerEnd = midPoint + delta;
while (delta !== 0) {
before = txt.slice(0, centerStart);
after = txt.slice(-1 * (txtLength - centerEnd));
makeContent(before, after);
truncatedDim = $worker[dim]();
truncatedStringLength = (before + after).length
delta = parseInt(delta / 2, 10);
if (truncatedDim > max) {
centerStart -= delta;
centerEnd += delta;
} else {
if (truncatedDim >= bestFitDim && truncatedStringLength > bestFitStringLength) {
bestFit.before = before;
bestFit.after = after;
bestFitDim = truncatedDim;
bestFitStringLength = truncatedStringLength;
}
centerStart += delta;
centerEnd -= delta;
}
}
return bestFit;
}
$.fn.truncate = function (options) {
// backward compatibility
if (options && !!options.center && !options.side) {
options.side = 'center';
delete options.center;
}
if (options && !(/^(left|right|center)$/).test(options.side)) {
delete options.side;
}
var defaults = {
width: 'auto',
height: 70,
token: '…',
side: 'right',
addclass: false,
addtitle: false,
multiline: false
};
options = $.extend(defaults, options);
return this.each(function () {
var $element = $(this);
var elementText = $element.text();
var $truncateWorker;
var dimension;
var truncatedText, originalDim, truncateDim;
// Performance: multiline can test whether truncation is necessary without any DOM manipulation/rendering.
// Do that check first so we can exit fast if no truncation is necessary.
if (options.multiline) {
originalDim = $element.height();
truncateDim = parseInt(options.height, 10)
if (originalDim <= truncateDim) {
return;
}
}
$truncateWorker = makeWorker($element, elementText);
if (options.multiline) {
$truncateWorker.width($element.width());
dimension = 'height';
}
else {
$truncateWorker.text(elementText);
originalDim = $truncateWorker.width();
truncateDim = parseInt(options.width, 10) || $element.width();
dimension = 'width';
}
truncatedText = {before: "", after: ""};
if (originalDim > truncateDim) {
var truncPoint, truncPoint2;
if (options.side === 'left') {
truncPoint = findTruncPoint(
dimension, truncateDim, elementText, 0, elementText.length,
$truncateWorker, options.token, true
);
truncatedText.after = elementText.slice(-1 * truncPoint);
} else if (options.side === 'center') {
truncatedText = truncateCenter(dimension, truncateDim, elementText, $truncateWorker, options.token);
} else if (options.side === 'right') {
truncPoint = findTruncPoint(
dimension, truncateDim, elementText, 0, elementText.length,
$truncateWorker, options.token, false
);
truncatedText.before = elementText.slice(0, truncPoint);
}
if (options.addclass) {
$element.addClass(options.addclass);
}
if (options.addtitle) {
$element.attr('title', elementText);
}
truncatedText.before = $truncateWorker.text(truncatedText.before).html();
truncatedText.after = $truncateWorker.text(truncatedText.after).html();
$element.empty().html(truncatedText.before + options.token + truncatedText.after);
}
$truncateWorker.remove();
});
};
})(jQuery);