forked from mozilla/zamboni
/
truncation.js
51 lines (51 loc) · 1.81 KB
/
truncation.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
$.fn.truncate = function(opts) {
opts = opts || {};
var showTitle = opts.showTitle || false,
dir = (opts.dir && opts.dir[0]) || 'h',
scrollProp = dir == "h" ? "scrollWidth" : "scrollHeight",
offsetProp = dir == "h" ? "offsetWidth" : "offsetHeight",
truncText = opts.truncText || "…",
textEl = opts.textEl || false,
split = [" ",""], counter, success;
this.each(function() {
var $el = $(this),
$tel = textEl ? $(textEl, $el) : $el,
txt, cutoff,
oldtext = $tel.attr("oldtext") || $tel.text();
if ($tel.attr("oldtext")) {
$tel.text(oldtext);
}
$tel.attr("oldtext", oldtext);
for (var i in split) {
delim = split[i];
txt = oldtext.split(delim);
cutoff = txt.length;
success = false;
if ((this[scrollProp] - this[offsetProp]) < 2) {
$el.removeClass("truncated");
break;
}
var chunk = Math.ceil(txt.length/2), oc=0, wid, delim;
for (counter = 0; counter < 10; counter++) {
$tel.html(txt.slice(0,cutoff).join(delim)+truncText);
wid = (this[scrollProp] - this[offsetProp]);
if (wid < 2 && chunk == oc || cutoff < 1) {
success = true;
$el.addClass("truncated");
break;
} else if (wid > 1) {
cutoff -= chunk;
} else {
cutoff += chunk;
}
oc = chunk;
chunk = Math.ceil(chunk/2);
}
if (success) break;
}
if (showTitle && oldtext != $tel.text()) {
$tel.attr("title", oldtext);
}
});
return this;
};