-
Notifications
You must be signed in to change notification settings - Fork 35
/
jquery.texttailor.js
194 lines (163 loc) · 4.13 KB
/
jquery.texttailor.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*!
* TextTailor - v0.1.2
* https://github.com/jpntex
*
* Copyright (c) 2020 João Teixeira; Licensed MIT
*
*/
(function($, window) {
'use strict';
var defaults = {
minFont: 1,
maxFont: 9999,
preWrapText: false,
lineHeight: 1.45,
resizable: true,
debounce: false,
fit: true,
ellipsis: true,
center: false,
justify: false
};
function Tailor(el, options) {
this.el = el;
this.options = $.extend({}, defaults, options);
this.init();
}
Tailor.prototype = {
init: function() {
var error = (this.options.minFont > this.options.maxFont),
el = $(this.el);
if (error && window.console) {
console.log('TextTailor error: minFont needs to be smaller than maxFont!');
}
// store original html source from element
this.HTML = this.el.innerHTML;
if (!error) {
var resize,
_self = this;
if (this.options.resizable) {
$(window).on('resize', function() {
if (_self.options.debounce) {
clearTimeout(resize);
resize = setTimeout(function() {
_self.start();
}, 200);
} else {
_self.start();
}
});
}
this.start();
}
},
start: function() {
var el = $(this.el);
// reset element
this.el.innerHTML = this.HTML;
el.wrapInner('<div/>');
this.wraped = $(this.el.firstChild);
this.wraped.css({
'line-height': this.options.lineHeight,
'tranform': 'translateZ(0)',
'height': 'auto'
});
if (this.options.preWrapText) this.wraped.css('white-space', 'pre-line');
this.maxHeight = el.height();
this.maxWidth = el.width();
this.fit().ellipsis().center().justify();
},
fit: function() {
if (this.options.fit) {
var _self = this,
el = this.wraped,
maxIter = 30,
iterCount = 0,
fitCalc = function calcMe(size, min, max) {
if (++iterCount === maxIter) return size;
if (size <= _self.options.minFont) return _self.options.minFont;
if (size >= _self.options.maxFont) return _self.options.maxFont;
if (min === max) return min;
// force DOM height update
el.css('fontSize', (size));
if (el[0].scrollHeight < _self.maxHeight) {
el.css('fontSize', (size + 1));
if (el[0].scrollHeight >= _self.maxHeight) return size;
return calcMe(Math.round((max + size) / 2), size, max);
} else {
el.css('fontSize', (size - 1));
return calcMe(Math.round((min + size) / 2), min, size);
}
};
var m = Math.round((this.options.maxFont + this.options.minFont) / 2);
el.css('fontSize', m);
var measure = fitCalc(m, this.options.minFont, this.options.maxFont);
el.css('fontSize', measure);
}
return this;
},
ellipsis: function() {
if (this.options.ellipsis) {
var el = this.wraped;
el.css({
'overflow': 'hidden',
'text-overflow': 'ellipsis'
});
if (el.height() > this.maxHeight) {
var tmpText = el.html();
el.html('O');
var rowHeight = el.height(),
start = 1,
end = tmpText.length;
while (start < end) {
var length = Math.ceil((start + end) / 2);
el.html(tmpText.slice(0, length) + '...');
if (el.height() <= this.maxHeight) {
start = length;
} else {
end = length - 1;
}
}
el.html(tmpText.slice(0, start) + '...');
}
}
return this;
},
center: function() {
if (this.options.center) {
var pos = $(this.el).css('position');
if (pos !== 'relative' && pos !== 'absolute') {
$(this.el).css("position", "relative");
}
this.wraped.css({
"position": "absolute",
"width": this.wraped.width() + "px",
"left": "0",
"right": "0",
"top": "0",
"bottom": "0",
"height": this.wraped.height() + "px",
"margin": "auto"
});
}
return this;
},
justify: function() {
if (this.options.justify) {
this.wraped.css({
"text-align": "justify"
});
}
return this;
}
};
$.fn.textTailor = function(options) {
return this.each(function() {
// prevent multiple instantiations
if (!$.data(this, 'TextTailor')) {
$.data(this, 'TextTailor',
new Tailor(this, options));
}
});
};
})(jQuery, window);