6056 |
efrain |
1 |
(function($) {
|
|
|
2 |
"use strict";
|
|
|
3 |
if (!$.event.special.destroyed) {
|
|
|
4 |
$.event.special.destroyed = {
|
|
|
5 |
remove: function(o) {
|
|
|
6 |
if (o.handler) {
|
|
|
7 |
o.handler();
|
|
|
8 |
}
|
|
|
9 |
}
|
|
|
10 |
};
|
|
|
11 |
}
|
|
|
12 |
$.fn.extend({
|
|
|
13 |
maxlength: function(options, callback) {
|
|
|
14 |
var documentBody = $("body"), defaults = {
|
|
|
15 |
showOnReady: false,
|
|
|
16 |
alwaysShow: true,
|
|
|
17 |
threshold: 0,
|
|
|
18 |
warningClass: "small form-text text-muted",
|
|
|
19 |
limitReachedClass: "small form-text text-danger",
|
|
|
20 |
limitExceededClass: "",
|
|
|
21 |
separator: " / ",
|
|
|
22 |
preText: "",
|
|
|
23 |
postText: "",
|
|
|
24 |
showMaxLength: true,
|
|
|
25 |
placement: "bottom-right-inside",
|
|
|
26 |
message: null,
|
|
|
27 |
showCharsTyped: true,
|
|
|
28 |
validate: false,
|
|
|
29 |
utf8: false,
|
|
|
30 |
appendToParent: false,
|
|
|
31 |
twoCharLinebreak: true,
|
|
|
32 |
customMaxAttribute: null,
|
|
|
33 |
customMaxClass: "overmax",
|
|
|
34 |
allowOverMax: false,
|
|
|
35 |
zIndex: 1099
|
|
|
36 |
};
|
|
|
37 |
if ($.isFunction(options) && !callback) {
|
|
|
38 |
callback = options;
|
|
|
39 |
options = {};
|
|
|
40 |
}
|
|
|
41 |
options = $.extend(defaults, options);
|
|
|
42 |
function utf8CharByteCount(character) {
|
|
|
43 |
var c = character.charCodeAt();
|
|
|
44 |
return !c ? 0 : c < 128 ? 1 : c < 2048 ? 2 : 3;
|
|
|
45 |
}
|
|
|
46 |
function utf8Length(string) {
|
|
|
47 |
return string.split("").map(utf8CharByteCount).concat(0).reduce((function(sum, val) {
|
|
|
48 |
return sum + val;
|
|
|
49 |
}));
|
|
|
50 |
}
|
|
|
51 |
function inputLength(input) {
|
|
|
52 |
var text = input.val();
|
|
|
53 |
if (options.twoCharLinebreak) {
|
|
|
54 |
text = text.replace(/\r(?!\n)|\n(?!\r)/g, "\r\n");
|
|
|
55 |
} else {
|
|
|
56 |
text = text.replace(/(?:\r\n|\r|\n)/g, "\n");
|
|
|
57 |
}
|
|
|
58 |
var currentLength = 0;
|
|
|
59 |
if (options.utf8) {
|
|
|
60 |
currentLength = utf8Length(text);
|
|
|
61 |
} else {
|
|
|
62 |
currentLength = text.length;
|
|
|
63 |
}
|
|
|
64 |
if (input.prop("type") === "file" && input.val() !== "") {
|
|
|
65 |
currentLength -= 12;
|
|
|
66 |
}
|
|
|
67 |
return currentLength;
|
|
|
68 |
}
|
|
|
69 |
function truncateChars(input, maxlength) {
|
|
|
70 |
var text = input.val();
|
|
|
71 |
if (options.twoCharLinebreak) {
|
|
|
72 |
text = text.replace(/\r(?!\n)|\n(?!\r)/g, "\r\n");
|
|
|
73 |
if (text[text.length - 1] === "\n") {
|
|
|
74 |
maxlength -= text.length % 2;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
if (options.utf8) {
|
|
|
78 |
var indexedSize = text.split("").map(utf8CharByteCount);
|
|
|
79 |
for (var removedBytes = 0, bytesPastMax = utf8Length(text) - maxlength; removedBytes < bytesPastMax; removedBytes += indexedSize.pop()) ;
|
|
|
80 |
maxlength -= maxlength - indexedSize.length;
|
|
|
81 |
}
|
|
|
82 |
input.val(text.substr(0, maxlength));
|
|
|
83 |
}
|
|
|
84 |
function charsLeftThreshold(input, threshold, maxlength) {
|
|
|
85 |
var output = true;
|
|
|
86 |
if (!options.alwaysShow && maxlength - inputLength(input) > threshold) {
|
|
|
87 |
output = false;
|
|
|
88 |
}
|
|
|
89 |
return output;
|
|
|
90 |
}
|
|
|
91 |
function remainingChars(input, maxlength) {
|
|
|
92 |
var length = maxlength - inputLength(input);
|
|
|
93 |
return length;
|
|
|
94 |
}
|
|
|
95 |
function showRemaining(currentInput, indicator) {
|
|
|
96 |
indicator.css({
|
|
|
97 |
display: "block"
|
|
|
98 |
});
|
|
|
99 |
currentInput.trigger("maxlength.shown");
|
|
|
100 |
}
|
|
|
101 |
function hideRemaining(currentInput, indicator) {
|
|
|
102 |
if (options.alwaysShow) {
|
|
|
103 |
return;
|
|
|
104 |
}
|
|
|
105 |
indicator.css({
|
|
|
106 |
display: "none"
|
|
|
107 |
});
|
|
|
108 |
currentInput.trigger("maxlength.hidden");
|
|
|
109 |
}
|
|
|
110 |
function updateMaxLengthHTML(currentInputText, maxLengthThisInput, typedChars) {
|
|
|
111 |
var output = "";
|
|
|
112 |
if (options.message) {
|
|
|
113 |
if (typeof options.message === "function") {
|
|
|
114 |
output = options.message(currentInputText, maxLengthThisInput);
|
|
|
115 |
} else {
|
|
|
116 |
output = options.message.replace("%charsTyped%", typedChars).replace("%charsRemaining%", maxLengthThisInput - typedChars).replace("%charsTotal%", maxLengthThisInput);
|
|
|
117 |
}
|
|
|
118 |
} else {
|
|
|
119 |
if (options.preText) {
|
|
|
120 |
output += options.preText;
|
|
|
121 |
}
|
|
|
122 |
if (!options.showCharsTyped) {
|
|
|
123 |
output += maxLengthThisInput - typedChars;
|
|
|
124 |
} else {
|
|
|
125 |
output += typedChars;
|
|
|
126 |
}
|
|
|
127 |
if (options.showMaxLength) {
|
|
|
128 |
output += options.separator + maxLengthThisInput;
|
|
|
129 |
}
|
|
|
130 |
if (options.postText) {
|
|
|
131 |
output += options.postText;
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
return output;
|
|
|
135 |
}
|
|
|
136 |
function manageRemainingVisibility(remaining, currentInput, maxLengthCurrentInput, maxLengthIndicator) {
|
|
|
137 |
if (maxLengthIndicator) {
|
|
|
138 |
maxLengthIndicator.html(updateMaxLengthHTML(currentInput.val(), maxLengthCurrentInput, maxLengthCurrentInput - remaining));
|
|
|
139 |
if (remaining > 0) {
|
|
|
140 |
if (charsLeftThreshold(currentInput, options.threshold, maxLengthCurrentInput)) {
|
|
|
141 |
showRemaining(currentInput, maxLengthIndicator.removeClass(options.limitReachedClass + " " + options.limitExceededClass).addClass(options.warningClass));
|
|
|
142 |
} else {
|
|
|
143 |
hideRemaining(currentInput, maxLengthIndicator);
|
|
|
144 |
}
|
|
|
145 |
} else {
|
|
|
146 |
if (!options.limitExceededClass) {
|
|
|
147 |
showRemaining(currentInput, maxLengthIndicator.removeClass(options.warningClass).addClass(options.limitReachedClass));
|
|
|
148 |
} else {
|
|
|
149 |
if (remaining === 0) {
|
|
|
150 |
showRemaining(currentInput, maxLengthIndicator.removeClass(options.warningClass + " " + options.limitExceededClass).addClass(options.limitReachedClass));
|
|
|
151 |
} else {
|
|
|
152 |
showRemaining(currentInput, maxLengthIndicator.removeClass(options.warningClass + " " + options.limitReachedClass).addClass(options.limitExceededClass));
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
if (options.customMaxAttribute) {
|
|
|
158 |
if (remaining < 0) {
|
|
|
159 |
currentInput.addClass(options.customMaxClass);
|
|
|
160 |
} else {
|
|
|
161 |
currentInput.removeClass(options.customMaxClass);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
function getPosition(currentInput) {
|
|
|
166 |
var el = currentInput[0];
|
|
|
167 |
return $.extend({}, typeof el.getBoundingClientRect === "function" ? el.getBoundingClientRect() : {
|
|
|
168 |
width: el.offsetWidth,
|
|
|
169 |
height: el.offsetHeight
|
|
|
170 |
}, currentInput.offset());
|
|
|
171 |
}
|
|
|
172 |
function placeWithCSS(placement, maxLengthIndicator) {
|
|
|
173 |
if (!placement || !maxLengthIndicator) {
|
|
|
174 |
return;
|
|
|
175 |
}
|
|
|
176 |
var POSITION_KEYS = [ "top", "bottom", "left", "right", "position" ];
|
|
|
177 |
var cssPos = {};
|
|
|
178 |
$.each(POSITION_KEYS, (function(i, key) {
|
|
|
179 |
var val = options.placement[key];
|
|
|
180 |
if (typeof val !== "undefined") {
|
|
|
181 |
cssPos[key] = val;
|
|
|
182 |
}
|
|
|
183 |
}));
|
|
|
184 |
maxLengthIndicator.css(cssPos);
|
|
|
185 |
return;
|
|
|
186 |
}
|
|
|
187 |
function place(currentInput, maxLengthIndicator) {
|
|
|
188 |
var pos = getPosition(currentInput);
|
|
|
189 |
if ($.type(options.placement) === "function") {
|
|
|
190 |
options.placement(currentInput, maxLengthIndicator, pos);
|
|
|
191 |
return;
|
|
|
192 |
}
|
|
|
193 |
if ($.isPlainObject(options.placement)) {
|
|
|
194 |
placeWithCSS(options.placement, maxLengthIndicator);
|
|
|
195 |
return;
|
|
|
196 |
}
|
|
|
197 |
var inputOuter = currentInput.outerWidth(), outerWidth = maxLengthIndicator.outerWidth(), actualWidth = maxLengthIndicator.width(), actualHeight = maxLengthIndicator.height();
|
|
|
198 |
if (options.appendToParent) {
|
|
|
199 |
pos.top -= currentInput.parent().offset().top;
|
|
|
200 |
pos.left -= currentInput.parent().offset().left;
|
|
|
201 |
}
|
|
|
202 |
switch (options.placement) {
|
|
|
203 |
case "bottom":
|
|
|
204 |
maxLengthIndicator.css({
|
|
|
205 |
top: pos.top + pos.height,
|
|
|
206 |
left: pos.left + pos.width / 2 - actualWidth / 2
|
|
|
207 |
});
|
|
|
208 |
break;
|
|
|
209 |
|
|
|
210 |
case "top":
|
|
|
211 |
maxLengthIndicator.css({
|
|
|
212 |
top: pos.top - actualHeight,
|
|
|
213 |
left: pos.left + pos.width / 2 - actualWidth / 2
|
|
|
214 |
});
|
|
|
215 |
break;
|
|
|
216 |
|
|
|
217 |
case "left":
|
|
|
218 |
maxLengthIndicator.css({
|
|
|
219 |
top: pos.top + pos.height / 2 - actualHeight / 2,
|
|
|
220 |
left: pos.left - actualWidth
|
|
|
221 |
});
|
|
|
222 |
break;
|
|
|
223 |
|
|
|
224 |
case "right":
|
|
|
225 |
maxLengthIndicator.css({
|
|
|
226 |
top: pos.top + pos.height / 2 - actualHeight / 2,
|
|
|
227 |
left: pos.left + pos.width
|
|
|
228 |
});
|
|
|
229 |
break;
|
|
|
230 |
|
|
|
231 |
case "bottom-right":
|
|
|
232 |
maxLengthIndicator.css({
|
|
|
233 |
top: pos.top + pos.height,
|
|
|
234 |
left: pos.left + pos.width
|
|
|
235 |
});
|
|
|
236 |
break;
|
|
|
237 |
|
|
|
238 |
case "top-right":
|
|
|
239 |
maxLengthIndicator.css({
|
|
|
240 |
top: pos.top - actualHeight,
|
|
|
241 |
left: pos.left + inputOuter
|
|
|
242 |
});
|
|
|
243 |
break;
|
|
|
244 |
|
|
|
245 |
case "top-left":
|
|
|
246 |
maxLengthIndicator.css({
|
|
|
247 |
top: pos.top - actualHeight,
|
|
|
248 |
left: pos.left - outerWidth
|
|
|
249 |
});
|
|
|
250 |
break;
|
|
|
251 |
|
|
|
252 |
case "bottom-left":
|
|
|
253 |
maxLengthIndicator.css({
|
|
|
254 |
top: pos.top + currentInput.outerHeight(),
|
|
|
255 |
left: pos.left - outerWidth
|
|
|
256 |
});
|
|
|
257 |
break;
|
|
|
258 |
|
|
|
259 |
case "centered-right":
|
|
|
260 |
maxLengthIndicator.css({
|
|
|
261 |
top: pos.top + actualHeight / 2,
|
|
|
262 |
left: pos.left + inputOuter - outerWidth - 3
|
|
|
263 |
});
|
|
|
264 |
break;
|
|
|
265 |
|
|
|
266 |
case "bottom-right-inside":
|
|
|
267 |
maxLengthIndicator.css({
|
|
|
268 |
top: pos.top + pos.height,
|
|
|
269 |
left: pos.left + pos.width - outerWidth
|
|
|
270 |
});
|
|
|
271 |
break;
|
|
|
272 |
|
|
|
273 |
case "top-right-inside":
|
|
|
274 |
maxLengthIndicator.css({
|
|
|
275 |
top: pos.top - actualHeight,
|
|
|
276 |
left: pos.left + inputOuter - outerWidth
|
|
|
277 |
});
|
|
|
278 |
break;
|
|
|
279 |
|
|
|
280 |
case "top-left-inside":
|
|
|
281 |
maxLengthIndicator.css({
|
|
|
282 |
top: pos.top - actualHeight,
|
|
|
283 |
left: pos.left
|
|
|
284 |
});
|
|
|
285 |
break;
|
|
|
286 |
|
|
|
287 |
case "bottom-left-inside":
|
|
|
288 |
maxLengthIndicator.css({
|
|
|
289 |
top: pos.top + currentInput.outerHeight(),
|
|
|
290 |
left: pos.left
|
|
|
291 |
});
|
|
|
292 |
break;
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
function isPlacementMutable() {
|
|
|
296 |
return options.placement === "bottom-right-inside" || options.placement === "top-right-inside" || typeof options.placement === "function" || options.message && typeof options.message === "function";
|
|
|
297 |
}
|
|
|
298 |
function getMaxLength(currentInput) {
|
|
|
299 |
var max = currentInput.attr("maxlength") || options.customMaxAttribute;
|
|
|
300 |
if (options.customMaxAttribute && !options.allowOverMax) {
|
|
|
301 |
var custom = currentInput.attr(options.customMaxAttribute);
|
|
|
302 |
if (!max || custom < max) {
|
|
|
303 |
max = custom;
|
|
|
304 |
}
|
|
|
305 |
}
|
|
|
306 |
if (!max) {
|
|
|
307 |
max = currentInput.attr("size");
|
|
|
308 |
}
|
|
|
309 |
return max;
|
|
|
310 |
}
|
|
|
311 |
return this.each((function() {
|
|
|
312 |
var currentInput = $(this), maxLengthCurrentInput, maxLengthIndicator;
|
|
|
313 |
$(window).resize((function() {
|
|
|
314 |
if (maxLengthIndicator) {
|
|
|
315 |
place(currentInput, maxLengthIndicator);
|
|
|
316 |
}
|
|
|
317 |
}));
|
|
|
318 |
function firstInit() {
|
|
|
319 |
var maxlengthContent = updateMaxLengthHTML(currentInput.val(), maxLengthCurrentInput, "0");
|
|
|
320 |
maxLengthCurrentInput = getMaxLength(currentInput);
|
|
|
321 |
if (!maxLengthIndicator) {
|
|
|
322 |
maxLengthIndicator = $('<span class="bootstrap-maxlength"></span>').css({
|
|
|
323 |
display: "none",
|
|
|
324 |
position: "absolute",
|
|
|
325 |
whiteSpace: "nowrap",
|
|
|
326 |
zIndex: options.zIndex
|
|
|
327 |
}).html(maxlengthContent);
|
|
|
328 |
}
|
|
|
329 |
if (currentInput.is("textarea")) {
|
|
|
330 |
currentInput.data("maxlenghtsizex", currentInput.outerWidth());
|
|
|
331 |
currentInput.data("maxlenghtsizey", currentInput.outerHeight());
|
|
|
332 |
currentInput.mouseup((function() {
|
|
|
333 |
if (currentInput.outerWidth() !== currentInput.data("maxlenghtsizex") || currentInput.outerHeight() !== currentInput.data("maxlenghtsizey")) {
|
|
|
334 |
place(currentInput, maxLengthIndicator);
|
|
|
335 |
}
|
|
|
336 |
currentInput.data("maxlenghtsizex", currentInput.outerWidth());
|
|
|
337 |
currentInput.data("maxlenghtsizey", currentInput.outerHeight());
|
|
|
338 |
}));
|
|
|
339 |
}
|
|
|
340 |
if (options.appendToParent) {
|
|
|
341 |
currentInput.parent().append(maxLengthIndicator);
|
|
|
342 |
currentInput.parent().css("position", "relative");
|
|
|
343 |
} else {
|
|
|
344 |
documentBody.append(maxLengthIndicator);
|
|
|
345 |
}
|
|
|
346 |
var remaining = remainingChars(currentInput, getMaxLength(currentInput));
|
|
|
347 |
manageRemainingVisibility(remaining, currentInput, maxLengthCurrentInput, maxLengthIndicator);
|
|
|
348 |
place(currentInput, maxLengthIndicator);
|
|
|
349 |
}
|
|
|
350 |
if (options.showOnReady) {
|
|
|
351 |
currentInput.ready((function() {
|
|
|
352 |
firstInit();
|
|
|
353 |
}));
|
|
|
354 |
} else {
|
|
|
355 |
currentInput.focus((function() {
|
|
|
356 |
firstInit();
|
|
|
357 |
}));
|
|
|
358 |
}
|
|
|
359 |
currentInput.on("maxlength.reposition", (function() {
|
|
|
360 |
place(currentInput, maxLengthIndicator);
|
|
|
361 |
}));
|
|
|
362 |
currentInput.on("destroyed", (function() {
|
|
|
363 |
if (maxLengthIndicator) {
|
|
|
364 |
maxLengthIndicator.remove();
|
|
|
365 |
}
|
|
|
366 |
}));
|
|
|
367 |
currentInput.on("blur", (function() {
|
|
|
368 |
if (maxLengthIndicator && !options.showOnReady) {
|
|
|
369 |
maxLengthIndicator.remove();
|
|
|
370 |
}
|
|
|
371 |
}));
|
|
|
372 |
currentInput.on("input", (function() {
|
|
|
373 |
var maxlength = getMaxLength(currentInput), remaining = remainingChars(currentInput, maxlength), output = true;
|
|
|
374 |
if (options.validate && remaining < 0) {
|
|
|
375 |
truncateChars(currentInput, maxlength);
|
|
|
376 |
output = false;
|
|
|
377 |
} else {
|
|
|
378 |
manageRemainingVisibility(remaining, currentInput, maxLengthCurrentInput, maxLengthIndicator);
|
|
|
379 |
}
|
|
|
380 |
return output;
|
|
|
381 |
}));
|
|
|
382 |
}));
|
|
|
383 |
}
|
|
|
384 |
});
|
|
|
385 |
})(jQuery);
|