forked from ractoon/jQuery-Text-Counter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextcounter.min.js
More file actions
14 lines (14 loc) · 3.6 KB
/
textcounter.min.js
File metadata and controls
14 lines (14 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*!
* jQuery Text Counter Plugin v0.3.6
* https://github.com/ractoon/jQuery-Text-Counter
*
* Copyright 2014 ractoon
* Released under the MIT license
*/
(function(g){g.textcounter=function(c,k){var a=this;a.$el=g(c);a.el=c;a.$el.data("textcounter",a);a.init=function(){a.options=g.extend({},g.textcounter.defaultOptions,k);a.$el.after("<"+a.options.countContainerElement+' class="'+a.options.countContainerClass+'">'+(a.options.countDown?a.options.countDownText:a.options.counterText)+'<span class="'+a.options.textCountClass+'">'+(a.options.countDown?a.options.max:0)+"</span></"+a.options.countContainerElement+">");a.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",
a.checkLimits).trigger("click.textcounter");a.options.init(a.el)};a.checkLimits=function(d){var c=a.$el;c.nextAll("."+a.options.countContainerClass);var b=c.val(),e=0,f;d=void 0===d.originalEvent?!1:!0;g.isEmptyObject(b)||("word"==a.options.type?e=b.trim().replace(/\s+/gi," ").split(" ").length:(e=a.options.countSpaces?b.replace(/[^\S\n|\r|\r\n]/g," ").length:b.replace(/\s/g,"").length,a.options.countExtendedCharacters&&(e=b.match(/[^\x00-\xff]/gi),e=null==e?b.length:b.length+e.length)));"auto"==
a.options.max&&(f=a.$el.attr("maxlength"),"undefined"!==typeof f&&!1!==f?a.options.max=f:a.$el.nextAll("."+a.options.countContainerClass).text("error: [maxlength] attribute not set"));f=a.options.countDown?a.options.max-e:e;a.setCount(f);0<a.options.min&&d&&(e<a.options.min?(a.setErrors("min"),a.options.minunder(a.el)):e>=a.options.min&&(a.options.mincount(a.el),a.clearErrors("min")));if(-1!==a.options.max)if(e>=a.options.max&&0!=a.options.max)if(a.options.maxcount(a.el),a.options.stopInputAtMaximum){e=
"";if("word"==a.options.type)for(d=b.split(/[^\S\n]/g),b=0;b<d.length&&!(b>=a.options.max-1);)void 0!==d[b]&&(e+=d[b]+" ",b++);else if(a.options.countSpaces)e=b.substring(0,a.options.max);else{d=b.split("");f=d.length;for(var h=0,b=0;h<a.options.max&&b<f;)" "!==d[b]&&h++,e+=d[b++]}c.val(e.trim());f=a.options.countDown?0:a.options.max;a.setCount(f)}else a.setErrors("max");else a.options.maxunder(a.el),a.clearErrors("max")};a.setCount=function(d){a.$el.nextAll("."+a.options.countContainerClass).children("."+
a.options.textCountClass).text(d)};a.setErrors=function(d){var c=a.$el,b=c.nextAll("."+a.options.countContainerClass);c.addClass(a.options.inputErrorClass);b.addClass(a.options.counterErrorClass);if(a.options.displayErrorText){switch(d){case "min":errorText=a.options.minimumErrorText;break;case "max":errorText=a.options.maximumErrorText}b.children(".error-text-"+d).length||b.append("<"+a.options.errorTextElement+' class="error-text error-text-'+d+'">'+errorText+"</"+a.options.errorTextElement+">")}};
a.clearErrors=function(d){var c=a.$el,b=c.nextAll("."+a.options.countContainerClass);b.children(".error-text-"+d).remove();0==b.children(".error-text").length&&(c.removeClass(a.options.inputErrorClass),b.removeClass(a.options.counterErrorClass))};a.init()};g.textcounter.defaultOptions={type:"character",min:0,max:200,countContainerElement:"div",countContainerClass:"text-count-wrapper",textCountClass:"text-count",inputErrorClass:"error",counterErrorClass:"error",counterText:"Total Count: ",errorTextElement:"div",
minimumErrorText:"Minimum not met",maximumErrorText:"Maximum exceeded",displayErrorText:!0,stopInputAtMaximum:!0,countSpaces:!1,countDown:!1,countDownText:"Remaining: ",countExtendedCharacters:!1,maxunder:function(c){},minunder:function(c){},maxcount:function(c){},mincount:function(c){},init:function(c){}};g.fn.textcounter=function(c){return this.each(function(){new g.textcounter(this,c)})}})(jQuery);