This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 192
/
jquery.textareaCounter.plugin.js
195 lines (174 loc) · 6.09 KB
/
jquery.textareaCounter.plugin.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
/*
* jQuery Textarea Counter Plugin
* Copyright (c) 2010 Roy Jin
* Copyright (c) 2013 LeadSift
* Version: 3.0 (11-APR-2013)
* http://www.opensource.org/licenses/mit-license.php
* Requires: jQuery v1.4.2 or later
*/
(function($) {
$.fn.textareaCount = function(options, fn) {
var defaults = {
maxCharacterSize: -1
, truncate: true
, charCounter: 'standard'
, originalStyle: 'originalTextareaInfo'
, warningStyle: 'warningTextareaInfo'
, errorStyle: 'errorTextareaInfo'
, warningNumber: 20
, displayFormat: '#input characters | #words words'
}
, container = $(this)
, charLeftInfo
, numInput = 0
, maxCharacters = options.maxCharacterSize
, numLeft = 0
, numWords = 0
, charCounters = {}
;
charCounters.standard = function(content){
return content.length;
};
charCounters.twitter = function(content){
// function that counts urls as 22 chars
// regex to match various urls ... from http://stackoverflow.com/a/6427654
var url_length = 22
, replacement = Array(url_length+1).join("*")
, regex_str = "(https?:\/\/)?" + // SCHEME
"([a-z0-9+!*(),;?&=$_.-]+(:[a-z0-9+!*(),;?&=$_.-]+)?@)?" + // User and Pass
"([a-z0-9-.]*)\\.(travel|museum|[a-z]{2,4})" + // Host or IP
"(:[0-9]{2,5})?" + // Port
"(\/([a-z0-9+$_-]\\.?)+)*\/?" + // Path
"(\\?[a-z+&$_.-][a-z0-9;:@&%=+\/$_.-]*)?" + // GET Query
"(#[a-z_.-][a-z0-9+$_.-]*)?" // Anchor
, regex = new RegExp(regex_str, 'gi')
;
return content.replace(regex, replacement).length;
};
function getNewlineCount(content){
var newlineCount = 0
, i;
for(i=0; i<content.length; i++){
if(content.charAt(i) === '\n'){
newlineCount++;
}
}
return newlineCount;
}
function formatDisplayInfo(){
var format = options.displayFormat;
format = format.replace('#input', numInput);
format = format.replace('#words', numWords);
//When maxCharacters <= 0, #max, #left cannot be substituted.
if(maxCharacters > 0){
format = format.replace('#max', maxCharacters);
format = format.replace('#left', numLeft);
}
return format;
}
function getInfo(){
var info = {
input: numInput,
max: maxCharacters,
left: numLeft,
words: numWords
};
return info;
}
function getNextCharLeftInformation(container){
return container.next('.charleft');
}
function isWin(){
var strOS = navigator.appVersion;
if (strOS.toLowerCase().indexOf('win') !== -1){
return true;
}
return false;
}
function getCleanedWordString(content){
var fullStr = content + " "
, initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi
, left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "")
, non_alphanumerics_rExp = /[^A-Za-z0-9]+/gi
, cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ")
, splitString = cleanedStr.split(" ")
;
return splitString;
}
function countWord(cleanedWordString){
var word_count = cleanedWordString.length-1;
return word_count;
}
function countByCharacters(){
var content = container.val()
, lengthFunc = typeof(options.charCounter) === 'function'? options.charCounter : charCounters[options.charCounter]
, contentLength = lengthFunc(content)
, newlineCount
, systemmaxCharacterSize
, originalScrollTopPosition
;
// Start Cut
if(options.maxCharacterSize > 0){
// If copied content is already more than maxCharacterSize,
// chop it to maxCharacterSize only if truncate is true
if(options.truncate && contentLength >= options.maxCharacterSize) {
content = content.substring(0, options.maxCharacterSize);
}
newlineCount = getNewlineCount(content);
systemmaxCharacterSize = options.maxCharacterSize;
if (isWin()){
// newlineCount new line character. For windows, it occupies 2 characters
systemmaxCharacterSize = options.maxCharacterSize - newlineCount;
}
if(options.truncate && contentLength > systemmaxCharacterSize){
//avoid scroll bar moving
originalScrollTopPosition = this.scrollTop;
container.val(content.substring(0, systemmaxCharacterSize));
this.scrollTop = originalScrollTopPosition;
}
charLeftInfo.removeClass(options.warningStyle + ' ' + options.errorStyle);
if(systemmaxCharacterSize - contentLength <= options.warningNumber){
charLeftInfo.addClass(options.warningStyle);
}
if(systemmaxCharacterSize - contentLength < 0){
charLeftInfo.addClass(options.errorStyle);
}
numInput = contentLength;
if(isWin()){
numInput = contentLength + newlineCount;
}
numWords = countWord(getCleanedWordString(container.val()));
numLeft = maxCharacters - numInput;
} else {
//normal count, no cut
newlineCount = getNewlineCount(content);
numInput = contentLength;
if(isWin()){
numInput = contentLength + newlineCount;
}
numWords = countWord(getCleanedWordString(container.val()));
}
return formatDisplayInfo();
}
function limitTextAreaByCharacterCount(){
charLeftInfo.html(countByCharacters());
//function call back
if(typeof fn !== 'undefined'){
fn.call(this, getInfo());
}
return true;
}
options = $.extend(defaults, options);
$("<div class='charleft'> </div>").insertAfter(container);
charLeftInfo = getNextCharLeftInformation(container);
charLeftInfo.addClass(options.originalStyle);
limitTextAreaByCharacterCount();
container.bind('keyup', function(){
limitTextAreaByCharacterCount();}
).bind('mouseover paste', function(){
setTimeout(function(){
limitTextAreaByCharacterCount();
}, 10);
});
};
})(jQuery);