|
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/>').addClass('text-counter-div') |
| 27 | + $formatted_counter_text = $('<div/>').addClass(base.options.textCountMessage) |
28 | 28 | .html(counterText.replace('%d', '<span class="' + base.options.textCountClass + '">' + counterNum + '</span>')),
|
29 |
| - $count_overflow_text = $('<div/>').addClass(base.options.countOverflowTextCss); |
| 29 | + $count_overflow_text = $('<div/>').addClass(base.options.countOverflowContainerClass); |
30 | 30 |
|
31 | 31 | base.hideMessage($count_overflow_text);
|
32 | 32 |
|
|
229 | 229 |
|
230 | 230 | switch(type) {
|
231 | 231 | case 'min':
|
232 |
| - errorText = base.options.minimumErrorText; |
233 |
| - break; |
| 232 | + errorText = base.options.minimumErrorText; |
| 233 | + break; |
234 | 234 | case 'max':
|
235 |
| - errorText = base.options.maximumErrorText; |
| 235 | + errorText = base.options.maximumErrorText; |
236 | 236 |
|
237 |
| - if (base.options.countOverflow) { |
238 |
| - base.setOverflowMessage(); |
239 |
| - } |
| 237 | + if (base.options.countOverflow) { |
| 238 | + base.setOverflowMessage(); |
| 239 | + } |
240 | 240 |
|
241 |
| - break; |
| 241 | + break; |
242 | 242 | }
|
243 | 243 |
|
244 | 244 | if (base.options.displayErrorText) {
|
|
249 | 249 | };
|
250 | 250 |
|
251 | 251 | base.setOverflowMessage = function () {
|
252 |
| - base.hideMessage(base.$container.find('.text-counter-div')); |
| 252 | + base.hideMessage(base.$container.find('.' + base.options.textCountMessage)); |
253 | 253 |
|
254 | 254 | base.removeOverflowMessage();
|
255 | 255 |
|
256 | 256 | var overflowText = base.options.countOverflowText
|
257 | 257 | .replace('%d', base.textCount(base.$el.val()) - base.options.max)
|
258 |
| - .replace('%type', base.options.type); |
| 258 | + .replace('%type', base.options.type + 's'); |
259 | 259 |
|
260 |
| - |
261 |
| - var overflowDiv = base.$container.find('.' + base.options.countOverflowTextCss).append(overflowText); |
| 260 | + var overflowDiv = base.$container.find('.' + base.options.countOverflowContainerClass).append(overflowText); |
262 | 261 | base.showMessage(overflowDiv);
|
263 | 262 | },
|
264 | 263 |
|
265 | 264 | base.removeOverflowMessage = function () {
|
266 |
| - base.$container.find('.' + base.options.countOverflowTextCss).empty(); |
| 265 | + base.$container.find('.' + base.options.countOverflowContainerClass).empty(); |
267 | 266 | },
|
268 | 267 |
|
269 | 268 | base.showMessage = function ($selector) {
|
|
274 | 273 | $selector.css('display', 'none');
|
275 | 274 | },
|
276 | 275 |
|
277 |
| - |
278 | 276 | base.clearErrors = function(type) {
|
279 | 277 | var $this = base.$el,
|
280 | 278 | $countEl = base.$container;
|
|
283 | 281 |
|
284 | 282 | if ($countEl.children('.error-text').length == 0) {
|
285 | 283 | base.removeOverflowMessage();
|
286 |
| - base.showMessage(base.$container.find('.text-counter-div')); |
| 284 | + base.showMessage(base.$container.find('.' + base.options.textCountMessage)); |
287 | 285 | $this.removeClass(base.options.inputErrorClass);
|
288 | 286 | $countEl.removeClass(base.options.counterErrorClass);
|
289 | 287 | }
|
|
294 | 292 | };
|
295 | 293 |
|
296 | 294 | $.textcounter.defaultOptions = {
|
297 |
| - 'type' : "character", // "character" or "word" |
298 |
| - 'min' : 0, // minimum number of characters/words |
299 |
| - 'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute |
300 |
| - 'countContainerElement' : "div", // HTML element to wrap the text count in |
301 |
| - 'countContainerClass' : "text-count-wrapper", // class applied to the countContainerElement |
302 |
| - 'textCountClass' : "text-count", // class applied to the counter length |
303 |
| - 'inputErrorClass' : "error", // error class appended to the input element if error occurs |
304 |
| - 'counterErrorClass' : "error", // error class appended to the countContainerElement if error occurs |
305 |
| - 'counterText' : "Total Count: %d", // counter text |
306 |
| - 'errorTextElement' : "div", // error text element |
307 |
| - 'minimumErrorText' : "Minimum not met", // error message for minimum not met, |
308 |
| - 'maximumErrorText' : "Maximum exceeded", // error message for maximum range exceeded, |
309 |
| - 'displayErrorText' : true, // display error text messages for minimum/maximum values |
310 |
| - 'stopInputAtMaximum' : true, // stop further text input if maximum reached |
311 |
| - 'countSpaces' : false, // count spaces as character (only for "character" type) |
312 |
| - 'countDown' : false, // if the counter should deduct from maximum characters/words rather than counting up |
313 |
| - 'countDownText' : "Remaining: %d", // count down text |
314 |
| - 'countExtendedCharacters' : false, // count extended UTF-8 characters as 2 bytes (such as Chinese characters) |
315 |
| - 'twoCharCarriageReturn' : false, // count carriage returns/newlines as 2 characters |
316 |
| - 'countOverflow' : false, // count text overflow |
317 |
| - 'countOverflowText' : "About %d extra %type(s) have been entered", // text message for count overflow |
318 |
| - 'countOverflowTextCss' : "text-overflow-div", // css for overflow text count |
| 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 |
319 | 318 |
|
320 | 319 | // Callback API
|
321 |
| - 'maxunder' : function(el){}, // Callback: function(element) - Fires when counter under max limit |
322 |
| - 'minunder' : function(el){}, // Callback: function(element) - Fires when counter under min limit |
323 |
| - 'maxcount' : function(el){}, // Callback: function(element) - Fires when the counter hits the maximum word/character count |
324 |
| - 'mincount' : function(el){}, // Callback: function(element) - Fires when the counter hits the minimum word/character count |
325 |
| - '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 |
326 | 325 | };
|
327 | 326 |
|
328 | 327 | $.fn.textcounter = function(options) {
|
|
0 commit comments