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