forked from kuzeko/jquery-truncate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.truncate.js
95 lines (81 loc) · 2.68 KB
/
jquery.truncate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(function ($) {
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd) {
var opt1,
opt2,
mid;
if (fromEnd) {
opt1 = start === 0 ? '' : text.slice(-start);
opt2 = text.slice(-end);
} else {
opt1 = text.slice(0, start);
opt2 = text.slice(0, end);
}
if ($workerEl.html(opt2 + token).width() < $workerEl.html(opt1 + token).width()) {
return end;
}
mid = parseInt((start + end) / 2, 10);
opt1 = fromEnd ? text.slice(-mid) : text.slice(0, mid);
$workerEl.html(opt1 + token);
if ($workerEl.width() === maxWidth) {
return mid;
}
if ($workerEl.width() > maxWidth) {
end = mid - 1;
} else {
start = mid + 1;
}
return findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd);
}
$.fn.truncate = function (options) {
var defaults = {
width: 'auto',
token: '…',
center: false,
addclass: false,
addtitle: false
};
options = $.extend(defaults, options);
return this.each(function () {
var $element = $(this),
fontCSS = {
'fontFamily': $element.css('fontFamily'),
'fontSize': $element.css('fontSize'),
'fontStyle': $element.css('fontStyle'),
'fontWeight': $element.css('fontWeight'),
'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'
},
elementText = $element.text(),
$truncateWorker = $('<span/>').css(fontCSS).html(elementText).appendTo('body'),
originalWidth = $truncateWorker.width(),
truncateWidth = isNumber(options.width) ? options.width : $element.width(),
truncatedText;
if (originalWidth > truncateWidth) {
$truncateWorker.text('');
if (options.center) {
truncateWidth = parseInt(truncateWidth / 2, 10) + 1;
truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false))
+ options.token
+ elementText.slice(-1 * findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, '', true));
} else {
truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false)) + options.token;
}
if (options.addclass) {
$element.addClass(options.addclass);
}
if (options.addtitle) {
$element.attr('title', elementText);
}
$element.html(truncatedText);
}
$truncateWorker.remove();
});
};
})(jQuery);