|
1 | 1 | /*!
|
2 |
| -* jQuery Text Counter Plugin v0.6.4 |
| 2 | +* jQuery Text Counter Plugin v0.7.0 |
3 | 3 | * https://github.com/ractoon/jQuery-Text-Counter
|
4 | 4 | *
|
5 | 5 | * Copyright 2014 ractoon
|
|
24 | 24 | // append the count element
|
25 | 25 | var counterText = base.options.countDown ? base.options.countDownText : base.options.counterText,
|
26 | 26 | counterNum = base.options.countDown ? base.options.max : 0,
|
27 |
| - $formatted_counter_text = $('<div/>').html(counterText.replace('%d', '<span class="' + base.options.textCountClass + '">' + counterNum + '</span>')).contents(); |
| 27 | + $formatted_counter_text = $('<div/>').addClass(base.options.textCountMessage) |
| 28 | + .html(counterText.replace('%d', '<span class="' + base.options.textCountClass + '">' + counterNum + '</span>')), |
| 29 | + $count_overflow_text = $('<div/>').addClass(base.options.countOverflowContainerClass); |
| 30 | + |
| 31 | + base.hideMessage($count_overflow_text); |
| 32 | + |
| 33 | + base.$container = $('<' + base.options.countContainerElement + '/>') |
| 34 | + .addClass(base.options.countContainerClass) |
| 35 | + .append($formatted_counter_text) |
| 36 | + .append($count_overflow_text); |
28 | 37 |
|
29 |
| - base.$container = $('<' + base.options.countContainerElement + '/>').addClass(base.options.countContainerClass).append($formatted_counter_text); |
30 | 38 | base.$text_counter = base.$container.find('span');
|
31 | 39 | base.$el.after(base.$container);
|
32 | 40 |
|
|
86 | 94 | if (textCount === base.options.max && base.options.max !== 0) {
|
87 | 95 | // TextCounter: maxcount(el) Callback
|
88 | 96 | base.options.maxcount(base.el);
|
| 97 | + base.clearErrors('max'); |
89 | 98 |
|
90 | 99 | } else if (textCount > base.options.max && base.options.max !== 0) {
|
91 | 100 | if (base.options.stopInputAtMaximum) { // if the string should be trimmed at the maximum length
|
|
218 | 227 | $this.addClass(base.options.inputErrorClass);
|
219 | 228 | $countEl.addClass(base.options.counterErrorClass);
|
220 | 229 |
|
221 |
| - if (base.options.displayErrorText) { |
222 |
| - switch(type) { |
223 |
| - case 'min': |
| 230 | + switch(type) { |
| 231 | + case 'min': |
224 | 232 | errorText = base.options.minimumErrorText;
|
225 | 233 | break;
|
226 |
| - case 'max': |
| 234 | + case 'max': |
227 | 235 | errorText = base.options.maximumErrorText;
|
| 236 | + |
| 237 | + if (base.options.countOverflow) { |
| 238 | + base.setOverflowMessage(); |
| 239 | + } |
| 240 | + |
228 | 241 | break;
|
229 |
| - } |
| 242 | + } |
230 | 243 |
|
| 244 | + if (base.options.displayErrorText) { |
231 | 245 | if (!$countEl.children('.error-text-' + type).length) {
|
232 | 246 | $countEl.append('<' + base.options.errorTextElement + ' class="error-text error-text-' + type + '">' + errorText + '</' + base.options.errorTextElement + '>');
|
233 | 247 | }
|
234 | 248 | }
|
235 | 249 | };
|
236 | 250 |
|
| 251 | + base.setOverflowMessage = function () { |
| 252 | + base.hideMessage(base.$container.find('.' + base.options.textCountMessage)); |
| 253 | + |
| 254 | + base.removeOverflowMessage(); |
| 255 | + |
| 256 | + var overflowText = base.options.countOverflowText |
| 257 | + .replace('%d', base.textCount(base.$el.val()) - base.options.max) |
| 258 | + .replace('%type', base.options.type + 's'); |
| 259 | + |
| 260 | + var overflowDiv = base.$container.find('.' + base.options.countOverflowContainerClass).append(overflowText); |
| 261 | + base.showMessage(overflowDiv); |
| 262 | + }, |
| 263 | + |
| 264 | + base.removeOverflowMessage = function () { |
| 265 | + base.$container.find('.' + base.options.countOverflowContainerClass).empty(); |
| 266 | + }, |
| 267 | + |
| 268 | + base.showMessage = function ($selector) { |
| 269 | + $selector.css('display', 'inline'); |
| 270 | + }, |
| 271 | + |
| 272 | + base.hideMessage = function ($selector) { |
| 273 | + $selector.css('display', 'none'); |
| 274 | + }, |
| 275 | + |
237 | 276 | base.clearErrors = function(type) {
|
238 | 277 | var $this = base.$el,
|
239 | 278 | $countEl = base.$container;
|
240 | 279 |
|
241 | 280 | $countEl.children('.error-text-' + type).remove();
|
242 | 281 |
|
243 | 282 | if ($countEl.children('.error-text').length == 0) {
|
| 283 | + base.removeOverflowMessage(); |
| 284 | + base.showMessage(base.$container.find('.' + base.options.textCountMessage)); |
244 | 285 | $this.removeClass(base.options.inputErrorClass);
|
245 | 286 | $countEl.removeClass(base.options.counterErrorClass);
|
246 | 287 | }
|
|
251 | 292 | };
|
252 | 293 |
|
253 | 294 | $.textcounter.defaultOptions = {
|
254 |
| - 'type' : "character", // "character" or "word" |
255 |
| - 'min' : 0, // minimum number of characters/words |
256 |
| - 'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute |
257 |
| - 'countContainerElement' : "div", // HTML element to wrap the text count in |
258 |
| - 'countContainerClass' : "text-count-wrapper", // class applied to the countContainerElement |
259 |
| - 'textCountClass' : "text-count", // class applied to the counter length |
260 |
| - 'inputErrorClass' : "error", // error class appended to the input element if error occurs |
261 |
| - 'counterErrorClass' : "error", // error class appended to the countContainerElement if error occurs |
262 |
| - 'counterText' : "Total Count: %d", // counter text |
263 |
| - 'errorTextElement' : "div", // error text element |
264 |
| - 'minimumErrorText' : "Minimum not met", // error message for minimum not met, |
265 |
| - 'maximumErrorText' : "Maximum exceeded", // error message for maximum range exceeded, |
266 |
| - 'displayErrorText' : true, // display error text messages for minimum/maximum values |
267 |
| - 'stopInputAtMaximum' : true, // stop further text input if maximum reached |
268 |
| - 'countSpaces' : false, // count spaces as character (only for "character" type) |
269 |
| - 'countDown' : false, // if the counter should deduct from maximum characters/words rather than counting up |
270 |
| - 'countDownText' : "Remaining: %d", // count down text |
271 |
| - 'countExtendedCharacters' : false, // count extended UTF-8 characters as 2 bytes (such as Chinese characters) |
272 |
| - 'twoCharCarriageReturn' : false, // count carriage returns/newlines as 2 characters |
| 295 | + 'type' : "character", // "character" or "word" |
| 296 | + 'min' : 0, // minimum number of characters/words |
| 297 | + 'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute |
| 298 | + 'countContainerElement' : "div", // HTML element to wrap the text count in |
| 299 | + 'countContainerClass' : "text-count-wrapper", // class applied to the countContainerElement |
| 300 | + 'textCountMessage' : "text-count-message", // class applied to the counter message |
| 301 | + 'textCountClass' : "text-count", // class applied to the counter length (the count number) |
| 302 | + 'inputErrorClass' : "error", // error class appended to the input element if error occurs |
| 303 | + 'counterErrorClass' : "error", // error class appended to the countContainerElement if error occurs |
| 304 | + 'counterText' : "Total Count: %d", // counter text |
| 305 | + 'errorTextElement' : "div", // error text element |
| 306 | + 'minimumErrorText' : "Minimum not met", // error message for minimum not met, |
| 307 | + 'maximumErrorText' : "Maximum exceeded", // error message for maximum range exceeded, |
| 308 | + 'displayErrorText' : true, // display error text messages for minimum/maximum values |
| 309 | + 'stopInputAtMaximum' : true, // stop further text input if maximum reached |
| 310 | + 'countSpaces' : false, // count spaces as character (only for "character" type) |
| 311 | + 'countDown' : false, // if the counter should deduct from maximum characters/words rather than counting up |
| 312 | + 'countDownText' : "Remaining: %d", // count down text |
| 313 | + 'countExtendedCharacters' : false, // count extended UTF-8 characters as 2 bytes (such as Chinese characters) |
| 314 | + 'twoCharCarriageReturn' : false, // count carriage returns/newlines as 2 characters |
| 315 | + 'countOverflow' : false, // display text overflow element |
| 316 | + 'countOverflowText' : "Maximum %type exceeded by %d", // count overflow text |
| 317 | + 'countOverflowContainerClass' : "text-count-overflow-wrapper", // class applied to the count overflow wrapper |
273 | 318 |
|
274 | 319 | // Callback API
|
275 |
| - 'maxunder' : function(el){}, // Callback: function(element) - Fires when counter under max limit |
276 |
| - 'minunder' : function(el){}, // Callback: function(element) - Fires when counter under min limit |
277 |
| - 'maxcount' : function(el){}, // Callback: function(element) - Fires when the counter hits the maximum word/character count |
278 |
| - 'mincount' : function(el){}, // Callback: function(element) - Fires when the counter hits the minimum word/character count |
279 |
| - 'init' : function(el){} // Callback: function(element) - Fires after the counter is initially setup |
| 320 | + 'maxunder' : function(el){}, // Callback: function(element) - Fires when counter under max limit |
| 321 | + 'minunder' : function(el){}, // Callback: function(element) - Fires when counter under min limit |
| 322 | + 'maxcount' : function(el){}, // Callback: function(element) - Fires when the counter hits the maximum word/character count |
| 323 | + 'mincount' : function(el){}, // Callback: function(element) - Fires when the counter hits the minimum word/character count |
| 324 | + 'init' : function(el){} // Callback: function(element) - Fires after the counter is initially setup |
280 | 325 | };
|
281 | 326 |
|
282 | 327 | $.fn.textcounter = function(options) {
|
|
0 commit comments