|
2 | 2 | function isNumber(n) {
|
3 | 3 | return !isNaN(parseFloat(n)) && isFinite(n);
|
4 | 4 | }
|
5 |
| - function findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd, count) { |
6 |
| - var opt1 = '', |
7 |
| - opt2 = '', |
8 |
| - mid = 0; |
9 |
| - count++; |
| 5 | + function findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd) { |
| 6 | + var opt1, |
| 7 | + opt2, |
| 8 | + mid; |
| 9 | + |
10 | 10 | if (fromEnd) {
|
11 |
| - opt1 = start == 0 ? '' : text.slice(-start); |
| 11 | + opt1 = start === 0 ? '' : text.slice(-start); |
12 | 12 | opt2 = text.slice(-end);
|
13 | 13 | } else {
|
14 | 14 | opt1 = text.slice(0, start);
|
|
18 | 18 | if ($workerEl.html(opt2 + token).width() < $workerEl.html(opt1 + token).width()) {
|
19 | 19 | return end;
|
20 | 20 | }
|
21 |
| - mid = parseInt((start + end) / 2, 10); |
22 |
| - if (fromEnd) { |
23 |
| - opt1 = text.slice(-mid); |
24 |
| - } else { |
25 |
| - opt1 = text.slice(0, mid); |
26 |
| - } |
27 | 21 |
|
| 22 | + mid = parseInt((start + end) / 2, 10); |
| 23 | + opt1 = fromEnd ? text.slice(-mid) : text.slice(0, mid); |
28 | 24 |
|
29 |
| - if ($workerEl.html(opt1 + token).width() == maxWidth) { |
| 25 | + $workerEl.html(opt1 + token); |
| 26 | + if ($workerEl.width() === maxWidth) { |
30 | 27 | return mid;
|
31 | 28 | }
|
32 | 29 |
|
33 |
| - if ($workerEl.html(opt1 + token).width() > maxWidth) { |
34 |
| - return findTruncPoint(maxWidth, text, start, (mid - 1), $workerEl, token, fromEnd, count); |
| 30 | + if ($workerEl.width() > maxWidth) { |
| 31 | + end = mid - 1; |
35 | 32 | } else {
|
36 |
| - return findTruncPoint(maxWidth, text, (mid + 1), end, $workerEl, token, fromEnd, count); |
| 33 | + start = mid + 1; |
37 | 34 | }
|
| 35 | + |
| 36 | + return findTruncPoint(maxWidth, text, start, end, $workerEl, token, fromEnd); |
38 | 37 | }
|
39 | 38 |
|
40 | 39 | $.fn.truncate = function (options) {
|
|
62 | 61 | 'display': 'none'
|
63 | 62 | },
|
64 | 63 | elementText = $element.text(),
|
65 |
| - truncatedText = '', |
66 | 64 | $truncateWorker = $('<span/>').css(fontCSS).html(elementText).appendTo('body'),
|
67 | 65 | originalWidth = $truncateWorker.width(),
|
68 |
| - truncateWidth = isNumber(options.width) ? options.width : $element.width(); |
| 66 | + truncateWidth = isNumber(options.width) ? options.width : $element.width(), |
| 67 | + truncatedText; |
69 | 68 |
|
70 | 69 | if (originalWidth > truncateWidth) {
|
71 | 70 | $truncateWorker.text('');
|
72 | 71 | if (options.center) {
|
73 |
| - truncatedText = elementText.slice(0, findTruncPoint((parseInt(truncateWidth / 2, 10) + 1), elementText, 0, elementText.length, $truncateWorker, options.token, false, 0)) |
| 72 | + truncateWidth = parseInt(truncateWidth / 2, 10) + 1; |
| 73 | + truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false)) |
74 | 74 | + options.token
|
75 |
| - + elementText.slice(-1 * findTruncPoint((parseInt(truncateWidth / 2, 10) + 1), elementText, 0, elementText.length, $truncateWorker, '', true, 0)); |
76 |
| - } |
77 |
| - else { |
78 |
| - truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false, 0)) + options.token; |
| 75 | + + elementText.slice(-1 * findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, '', true)); |
| 76 | + } else { |
| 77 | + truncatedText = elementText.slice(0, findTruncPoint(truncateWidth, elementText, 0, elementText.length, $truncateWorker, options.token, false)) + options.token; |
79 | 78 | }
|
80 | 79 |
|
81 | 80 | if (options.addclass) {
|
|
0 commit comments