This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
forked from ractoon/jQuery-Text-Counter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textcounter.min.js
10 lines (10 loc) · 5.31 KB
/
textcounter.min.js
1
2
3
4
5
6
7
8
9
10
/*!
* jQuery Text Counter Plugin v0.9.0
* https://github.com/ractoon/jQuery-Text-Counter
*
* Copyright 2014 ractoon
* Released under the MIT license
*
* Forked by Tableau Software LLC, 2021.
*/
!function(t){t.textcounter=function(o,e){var n=this;n.$el=t(o),n.el=o,n.$el.data("textcounter",n),n.init=function(){n.options=t.extend({},t.textcounter.defaultOptions,e);var o=n.options.countDown?n.options.countDownText:n.options.counterText,r=n.options.countDown?n.options.max:0,s=t("<div/>").addClass(n.options.textCountMessageClass).attr("aria-live","assertive").attr("aria-atomic","true").html(o.replace("%d",'<span class="'+n.options.textCountClass+'">'+r+"</span>")),i=t("<div/>").addClass(n.options.countOverflowContainerClass);n.hideMessage(i),n.$container=t("<"+n.options.countContainerElement+"/>").addClass(n.options.countContainerClass).append(s).append(i),n.$text_counter=n.$container.find("span"),n.$el.after(n.$container),n.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",n.checkLimits),n.options.init(n.el)},n.checkLimits=function(o){var e=n.$el,r=(n.$container,e.val()),s=0,i=0,a=void 0!==o.originalEvent;if(t.isEmptyObject(r)||(s=n.textCount(r)),"auto"==n.options.max)void 0!==(u=n.$el.attr("maxlength"))&&!1!==u?n.options.max=u:n.$container.text("error: [maxlength] attribute not set");else if("autocustom"==n.options.max){var u;void 0!==(u=n.$el.attr(n.options.autoCustomAttr))&&!1!==u?n.options.max=u:n.$container.text("error: ["+n.options.autoCustomAttr+"] attribute not set")}if(i=n.options.countDown?n.options.max-s:s,n.setCount(i),n.options.min>0&&a&&(s<n.options.min?(n.setErrors("min"),n.options.minunder(n.el)):s>=n.options.min&&(n.options.mincount(n.el),n.clearErrors("min"))),-1!==n.options.max)if(s===n.options.max&&0!==n.options.max)n.options.maxcount(n.el),n.clearErrors("max");else if(s>n.options.max&&0!==n.options.max)if(n.options.stopInputAtMaximum){var c="";if("word"==n.options.type)for(var l=r.split(/[^\S\n]/g),p=0;p<l.length&&!(p>=n.options.max);)void 0!==l[p]&&(c+=l[p]+" ",p++);else{var x=n.options.twoCharCarriageReturn?n.options.max-n.twoCharCarriageReturnCount(r):n.options.max;if(n.options.countSpaces)c=r.substring(0,x);else{var m=r.split(""),C=m.length,d=0;for(p=0;d<x&&p<C;)" "!==m[p]&&d++,c+=m[p++]}}e.val(c.trim()),s=n.textCount(e.val()),i=n.options.countDown?n.options.max-s:s,n.setCount(i)}else n.setErrors("max");else n.options.maxunder(n.el),n.clearErrors("max")},n.textCount=function(t){return"word"==n.options.type?n.wordCount(t):n.characterCount(t)},n.wordCount=function(t){return t.trim().replace(/\s+/gi," ").split(" ").length},n.characterCount=function(t){var o=0,e=0;if(n.options.twoCharCarriageReturn&&(e=n.twoCharCarriageReturnCount(t)),o=n.options.countSpaces?t.replace(/[^\S\n|\r|\r\n]/g," ").length:t.replace(/\s/g,"").length,n.options.countExtendedCharacters){var r=t.match(/[^\x00-\xff]/gi);o=null==r?t.length:t.length+r.length}return n.options.twoCharCarriageReturn&&(o+=e),o},n.twoCharCarriageReturnCount=function(t){var o=t.match(/(\r\n|\n|\r)/g),e=0;return null!==o&&(e=o.length),e},n.setCount=function(t){n.$text_counter.text(t)},n.setErrors=function(t){var o=n.$el,e=n.$container,r="";switch(o.addClass(n.options.inputErrorClass),e.addClass(n.options.counterErrorClass),t){case"min":r=n.options.minimumErrorText;break;case"max":r=n.options.maximumErrorText,n.options.countOverflow&&n.setOverflowMessage()}n.options.displayErrorText&&(e.children(".error-text-"+t).length||e.append("<"+n.options.errorTextElement+' class="error-text error-text-'+t+'">'+r+"</"+n.options.errorTextElement+">"))},n.setOverflowMessage=function(){n.hideMessage(n.$container.find("."+n.options.textCountMessageClass)),n.removeOverflowMessage();var t=n.options.countOverflowText.replace("%d",n.textCount(n.$el.val())-n.options.max).replace("%type",n.options.type+"s"),o=n.$container.find("."+n.options.countOverflowContainerClass).append(t);n.showMessage(o)},n.removeOverflowMessage=function(){n.$container.find("."+n.options.countOverflowContainerClass).empty()},n.showMessage=function(t){t.css("display","inline")},n.hideMessage=function(t){t.css("display","none")},n.clearErrors=function(t){var o=n.$el,e=n.$container;e.children(".error-text-"+t).remove(),0==e.children(".error-text").length&&(n.removeOverflowMessage(),n.showMessage(n.$container.find("."+n.options.textCountMessageClass)),o.removeClass(n.options.inputErrorClass),e.removeClass(n.options.counterErrorClass))},n.init()},t.textcounter.defaultOptions={type:"character",min:0,max:200,autoCustomAttr:"counterlimit",countContainerElement:"div",countContainerClass:"text-count-wrapper",textCountMessageClass:"text-count-message",textCountClass:"text-count",inputErrorClass:"error",counterErrorClass:"error",counterText:"Total Count: %d",errorTextElement:"div",minimumErrorText:"Minimum not met",maximumErrorText:"Maximum exceeded",displayErrorText:!0,stopInputAtMaximum:!0,countSpaces:!1,countDown:!1,countDownText:"Remaining: %d",countExtendedCharacters:!1,twoCharCarriageReturn:!1,countOverflow:!1,countOverflowText:"Maximum %type exceeded by %d",countOverflowContainerClass:"text-count-overflow-wrapper",minDisplayCutoff:-1,maxDisplayCutoff:-1,maxunder:function(t){},minunder:function(t){},maxcount:function(t){},mincount:function(t){},init:function(t){}},t.fn.textcounter=function(o){return this.each(function(){new t.textcounter(this,o)})}}(jQuery);