-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtfill.js
187 lines (158 loc) · 4.74 KB
/
tfill.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
(function($) {
if ($.fn.cssImportant) {
return;
}
// Escape regex chars with \
var escape = function(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};
// For those who need them (< IE 9), add support for CSS functions
var isStyleFuncSupported = !!CSSStyleDeclaration.prototype.getPropertyValue;
if (!isStyleFuncSupported) {
CSSStyleDeclaration.prototype.getPropertyValue = function(a) {
return this.getAttribute(a);
};
Object.defineProperty(
CSSStyleDeclaration.prototype,
'setProperty',
{
enumerable:false,
writable: true,
value:function(styleName, value, priority) {
this.setAttribute(styleName, value);
var priority = typeof priority != 'undefined' ? priority : '';
if (priority != '') {
// Add priority manually
var rule = new RegExp(escape(styleName) + '\\s*:\\s*' + escape(value) +
'(\\s*;)?', 'gmi');
this.cssText =
this.cssText.replace(rule, styleName + ': ' + value + ' !' + priority + ';');
}
}
}
);
CSSStyleDeclaration.prototype.removeProperty = function(a) {
return this.removeAttribute(a);
};
CSSStyleDeclaration.prototype.getPropertyPriority = function(styleName) {
var rule = new RegExp(escape(styleName) + '\\s*:\\s*[^\\s]*\\s*!important(\\s*;)?',
'gmi');
return rule.test(this.cssText) ? 'important' : '';
}
}
// The style function
$.fn.cssImportant = function(a) {
// DOM node
var node = this.get(0);
// Ensure we have a DOM node
if (typeof node == 'undefined') {
return this;
}
// CSSStyleDeclaration
var style = this.get(0).style;
// Getter/Setter
for (i in a) {
styleName = i;
value = a[i];
priority="";
if (value.match(/!important/i)) {
priority = "important";
value=value.split("!")[0];
}
if (typeof styleName != 'undefined') {
if (typeof value != 'undefined') {
// Set style property
priority = typeof priority != 'undefined' ? priority : '';
style.setProperty(styleName, value, priority);
return this;
} else {
// Get style property
return style.getPropertyValue(styleName);
}
} else {
// Get CSSStyleDeclaration
return style;
}
}
};
})(jQuery);
(function($) {
if (typeof TFillJs !== "undefined") {
return;
}
function getNum(d) {
var myRegexp = /^([0-9\.]+)([a-zA-Z]+)$/i;
var p = myRegexp.exec(d);
return Number(p[1]);
}
$.fn.tf = function(options) {
return this.each(function(b,lp){
var elem = lp[0];
var myRegexp = /^([0-9\.]+)([a-zA-Z]+)$/i;
if (lp[1].match(myRegexp)) {
var p = myRegexp.exec(lp[1]);
var maxFontPixels = Number(p[1]);
var uniti = p[2];
var fontSize = maxFontPixels;
var ourText = elem;
var moins = 0;
elem.parent().children().each(function () {
if (!$(this).is(ourText)) {
moins+=$(this)[0].clientHeight+getNum($(this).css("margin-top"))+getNum($(this).css("margin-bottom"));
}
});
var maxHeight = elem.parent()[0].clientHeight - moins;
var maxWidth = elem.parent()[0].clientWidth;
var textHeight;
var textWidth;
var deb = ourText.css("visibility");
ourText.css("visibility","hidden");
do {
ourText.cssImportant({'font-size':fontSize+uniti+" !important;"});
textHeight = ourText[0].clientHeight;
textWidth = ourText[0].clientWidth;
fontSize = fontSize - 0.5;
} while ((textHeight > maxHeight) && fontSize > 0);
ourText.css("visibility",deb);
if (typeof lp[0].x === "undefined") {
lp[0].x = "S";
$(window).resize(function() {
$([[lp[0],lp[1]]]).tf();
});
}
}
});
}
this.TFillJs = function() {
this.reload();
}
TFillJs.prototype.reload = function() {
reload(this);
}
function reload(a) {
begin();
}
var p = /^tfill$/;
function prep(p) {
arr = [];
$("*").each(function(i, it) {
$t = $(this);
arr2 = null;
$.each(this.attributes, function() {
if (this.name.match(p)) {
arr2=this.value;
}
});
if (arr2!=null) {
arr.push([$t, arr2]);
}
});
return $(arr);
}
//tfill=
function begin() {
prep(p).tf();
}
}(jQuery));
var MyTFillJs = null;
$(document).ready(function() {MyTFillJs=new TFillJs();$(window).trigger("TFillJsReady");});