-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.czText.js
140 lines (125 loc) · 4.49 KB
/
jquery.czText.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
/**
*
*/
(function ($) {
if (typeof jQuery === 'undefined') console.error('CzText requires jQuery.');
var CzText = function (element, options) {
this.element = $(element);
this.options = $.extend({}, CzText.DEFAULTS, options);
this.html = this.element.html();
this.excluded = false;
this.init();
// this.initFixOnResize();
};
CzText.DEFAULTS = {
debug: false,
elementHtmlAttributeName: 'data-im-original-html'
};
CzText.prototype = {
init: function () {
var words;
if (!this.element.attr(this.options.elementHtmlAttributeName)) {
this.element.attr(this.options.elementHtmlAttributeName, this.html)
} else {
this.html = this.element.attr(this.options.elementHtmlAttributeName);
this.element.html(this.html);
}
words = this.html.trim().split(' ');
var index = 0;
if(words[index].indexOf('<') === 0){
var tagName = words[0].substr(1);
this.html = '';
for (index; index < words.length; index++) {
this.html = this.html + ' ' + words[index];
if(words[index].indexOf('</'+tagName) !== -1) break;
}
this.element.html(this.html);
}else{
this.element.html(words[index]);
}
this.html = this.element.html();
index++;
for (index; index < words.length; index++) {
this.processText(words, index);
}
},
refresh: function () {
this.init();
},
/**
* @param words
* @param i
*/
processText: function (words, i) {
var currentWord = words[i];
var lastWord = words[i - 1];
var lastLastWord = words[i - 2];
var height = this.element.height();
if(currentWord.length === 0){
return;
}
this.html += ' ' + currentWord;
this.element.html(this.html);
this._processOneLettersEachOther(currentWord, lastWord, lastLastWord);
this._processLastLetter(currentWord, lastWord, lastLastWord, height);
this.excluded = false;
},
/**
*
* @param currentWord
* @param lastWord
* @param lastLastWord
* @param height
* @private
*/
_processLastLetter: function (currentWord, lastWord, lastLastWord, height) {
if (this.element.height() > height && this.excluded === false) {
if ((lastWord.length === 1 || (lastWord.length === 2 && lastWord.indexOf('.') !== -1))) {
this._appendLineBrakeBefore(currentWord, lastWord, lastLastWord);
}
}
},
/**
*
* @param currentWord
* @param lastWord
* @param lastLastWord
* @private
*/
_processOneLettersEachOther: function (currentWord, lastWord, lastLastWord) {
if (lastLastWord && lastLastWord.length === 1 && lastWord.length === 1) {
this.excluded = true;
this._appendLineBrakeBefore(currentWord, lastWord, lastLastWord);
}
},
/**
*
* @param currentWord
* @param lastWord
* @param lastLastWord
* @private
*/
_appendLineBrakeBefore: function (currentWord, lastWord, lastLastWord) {
var matchingWords = (lastWord + ' ' + currentWord);
if (lastLastWord && lastLastWord.length === 1) {
matchingWords = (lastLastWord + ' ' + lastWord + ' ' + currentWord);
}
var htm = this.html.slice(0, -(matchingWords).length);
var br = "<br>";
if (this.options.debug === true) {
br = "<br><span style=\"color: red\">|</span>";
}
this.html = htm + (br + matchingWords);
this.element.html(this.html);
}
};
$.fn.czText = function (option) {
return this.each(function () {
var $this = $(this);
var data = $this.data('cz-textWrap');
var options = $.extend({}, CzText.DEFAULTS, option);
if (!data) $this.data('cz-textWrap', (data = new CzText(this, options)));
if (typeof option == 'string') data[option]()
});
}
})(jQuery);