-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.flextext.js
201 lines (168 loc) · 6.42 KB
/
jquery.flextext.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
195
196
197
198
199
200
;(function($){
var FlexText = (function(window, document){
'use strict';
var listForResizeUpdate = [];
function bindEvents(){
window.addEventListener('resize', onWindowResize__flextText, false);
}
function onWindowResize__flextText() {
listForResizeUpdate.forEach(function(block){
makeFlexText.call(block);
});
}
function onOverflow(params){
var resultFs,
overflowSize;
if (params) {
params.fs < this._options.min ? resultFs = this._options.min : resultFs = this._baseFs;
overflowSize = params.overflowSize;
}
this._options.onOverflow.call(this, {
block: this._block,
fontSize: resultFs,
overflowSize: overflowSize
});
}
bindEvents();
function getBlockNode(block){
var _block;
switch (typeof block) {
// string with css-selector
case 'string':
_block = document.querySelector(block);
break;
case 'object':
// DOM-node
if (block.nodeType > 0) {
_block = block;
} else {
throw new Error('FlexText: wrong block type');
}
break;
}
this._block = _block;
}
function makeFlexText() {
var node = this._block.cloneNode(true),
text = document.createElement('div'),
checkContainer = document.createElement('div'),
resultFs = this._currentFs || this._baseFs;
node.style.width = window.getComputedStyle(this._block, null).getPropertyValue('width');
node.style.height = window.getComputedStyle(this._block, null).getPropertyValue('height');
text.innerHTML = node.innerHTML;
node.innerHTML = '';
node.appendChild(text);
checkContainer.style.position = 'fixed';
checkContainer.style.visibility = 'hidden';
checkContainer.style.zIndex = -9999;
checkContainer.appendChild(node);
document.body.appendChild(checkContainer);
var nodeHeight = + node.style.height.replace('px', ''),
textHeight = window.getComputedStyle(text, null).getPropertyValue('height').replace('px', '');
if (textHeight > nodeHeight) {
if (Object.keys(this._options.origin).length === 1 && this._options.origin.onOverflow) {
onOverflow.call(this, {
overflowSize: textHeight - nodeHeight
});
return;
}
while(window.getComputedStyle(text, null).getPropertyValue('height').replace('px', '') > nodeHeight) {
if (resultFs > this._options.min) {
resultFs -= this._options.step;
text.style.fontSize = resultFs + 'px';
} else {
onOverflow.call(this, { fs: resultFs });
break;
}
}
} else if (textHeight < nodeHeight) {
while(window.getComputedStyle(text, null).getPropertyValue('height').replace('px', '') < nodeHeight){
if (resultFs < this._options.max) {
resultFs += this._options.step;
text.style.fontSize = resultFs + 'px';
} else {
break;
}
}
if (window.getComputedStyle(text, null).getPropertyValue('height').replace('px', '') > nodeHeight) {
resultFs -= this._options.step;
}
}
resultFs > this._options.max ? resultFs = this._options.max : null;
resultFs < this._options.min ? resultFs = this._options.min : null;
this._currentFs = resultFs;
this._block.style.fontSize = resultFs + 'px';
checkContainer.parentNode.removeChild(checkContainer);
}
function collectParams(){
this._baseFs = parseInt(window.getComputedStyle(this._block, null).getPropertyValue('font-size'), 10);
}
function enableLiveUpdate(){
var interval = typeof this._options.live === 'number' ? this._options.live : 50;
setInterval(function(){
makeFlexText.call(this);
}.bind(this), interval);
}
function FlexText(block, options){
var resultOptions = {
min: 5,
max: 999,
step: 0.1,
live: false,
resize: false,
onOverflow: function(){}
};
options = options || {};
//extend ops
Object.keys(options).forEach(function(prop){
resultOptions[prop] = options[prop];
});
this._options = resultOptions;
this._options.origin = options;
if (options.resize === true) {
listForResizeUpdate.push(this);
}
getBlockNode.call(this, block);
collectParams.call(this);
makeFlexText.call(this);
if (this._options.live) {
enableLiveUpdate.call(this);
}
}
//Methods
FlexText.prototype = {
constructor: FlexText,
update: function(){
makeFlexText.call(this);
}
};
return FlexText;
})(window, document);
var methods = {
init: function(params) {
if (this.data('flex-text')) return;
//$.extend({}, params, {
//
//});
this.data('flex-text', new FlexText(this[0], params));
return this;
},
update: function(){
this.data('flex-text').update();
}
};
$.fn.flexText = function(method){
// немного магии
if ( methods[method] ) {
if (!this.data('flex-text')) return;
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
// если первым параметром идет объект, либо совсем пусто
// выполняем метод init
return methods.init.apply( this, arguments );
} else {
// если ничего не получилось
$.error( 'Method "' + method + '" not found at jQuery.flexText');
}
};
})(jQuery);