forked from nathanford/widowadjust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widowtamer.js
189 lines (107 loc) · 3.54 KB
/
widowtamer.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
// Widow Tamer JS by Nathan C. Ford http://artequalswork.com
wt = {
// default options
opts : {
elements : 'p,li,dd',
chars : 14,
method : 'wordSpacing',
dir : 'pos',
event : 'orientationchange'
},
to : new Array(),
fix : function (opts) {
if (document.readyState === 'complete') wt.init(opts);
wt.bind('load', function () { wt.init(opts); });
},
init : function (opts) {
var m = opts.method;
switch (m) {
case 'padding-right': case 'nbsp': case 'padding-left': case 'word-spacing': case 'letter-spacing' : case undefined :
if (opts.dir == 'neg' && m && m.match('padding')) {
console.log('Invalid method. You cannot use neg with padding.');
} else wt.hunter(opts);
break;
default : console.log('Invalid method. Please use either padding-right, padding-left, word-spacing, or letter-spacing.');
}
},
hunter : function (uopts) {
var opts = new Object();
// overwrite defaults with options from user
if (uopts) {
for(i in wt.opts) {
if (uopts[i]) opts[i] = (i == 'method' && uopts[i].match('-')) ? wt.fixmethod(uopts[i]) : uopts[i];
else opts[i] = wt.opts[i];
}
}
else opts = wt.opts;
if (document.querySelectorAll) {
var eles = document.querySelectorAll(opts.elements),
i = 0;
while (i < eles.length) {
var t = eles[i];
if (t.offsetHeight > wt.getstyle(t, 'line-height', true)) {
// find a textnode longer than chars
var nodes = t.childNodes,
j = nodes.length - 1,
c = false;
while (j >= 0) {
var ntext = wt.text(nodes[j]);
if (ntext != undefined && ntext.length > opts.chars) {
c = nodes[j];
break;
}
j--;
}
t.style[opts.method] = '0';
var ctext = wt.text( c );
if (c) wt.tamer(c, t, ctext, 0, opts);
}
i++;
}
wt.bind(opts.event, function () { wt.onevent(opts); });
}
},
text : function (t) {
return t.innerText || t.textContent;
},
settext : function (e, t) {
if (e.innerText) e.innerText = t;
else e.textContent = t;
},
tamer : function (c, t, text, i, opts) {
var h = t.offsetHeight;
wt.settext(c, text.slice(0, (opts.chars * -1)));
if (t.offsetHeight < h) {
if (opts.method == 'nbsp') wt.settext(c, wt.text( c ) + text.slice((opts.chars * -1)).replace(/\s/g, '\u00a0'));
else {
var inc = (opts.method.match('padding')) ? (i / 10) : (i / 100);
if (opts.dir == 'neg') inc = inc * -1;
t.style[opts.method] = inc + 'em';
wt.settext(c, text);
wt.tamer(c, t, text, i + 1, opts);
}
}
else wt.settext(c, text);
},
onevent : function (opts) {
var to = "'" + opts.elements.replace(/[^a-zA-Z0-9\-\.]/g, '') + "'";
clearTimeout(wt.to[to]);
wt.to[to] = setTimeout(function () { wt.hunter(opts); }, 250);
},
bind : function (e, f) {
if (window.attachEvent) window.attachEvent('on' + e, f );
else window.addEventListener(e, f, true);
},
fixmethod : function (s) {
return s.replace(/-([a-zA-Z])/, function (m) {
return m.replace('-','').toUpperCase();
});
},
getstyle : function (t, s, n) {
var r;
if (t.currentStyle) r = t.currentStyle[s.replace(/-([A-z])/gi, function(a,b) {return b.toUpperCase();})];
else if (window.getComputedStyle) r = document.defaultView.getComputedStyle(t,null).getPropertyValue(s);
if (n) return parseFloat(r);
else return r;
}
};