diff --git a/README.md b/README.md index 716a141..f956241 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ Fires when counter is under min limit. type : "character", // "character" or "word" min : 0, // minimum number of characters/words max : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute, , 'autocustom' to use a custom attribute for the length (must set "autoCustomAttr") +automax : false, // Automatic reload max from max attribute autoCustomAttr : "counterlimit", // custom attribute name with the counter limit if the max is 'autocustom' countContainerElement : "div", // HTML element to wrap the text count in countContainerClass : "text-count-wrapper", // class applied to the countContainerElement diff --git a/textcounter.js b/textcounter.js index beb0afe..eae8dcb 100644 --- a/textcounter.js +++ b/textcounter.js @@ -57,25 +57,15 @@ textCount = base.textCount($text); } - // if max is auto retrieve value - if (base.options.max == 'auto') { - var max = base.$el.attr('maxlength'); - - if (typeof max !== 'undefined' && max !== false) { - base.options.max = max; - } - else { - base.$container.text('error: [maxlength] attribute not set'); - } - } - else if (base.options.max == 'autocustom') { + if (base.options.max == 'auto' || base.options.max == 'autocustom' || base.options.automax) { + base.options.autoCustomAttr = (base.options.max == 'auto') ? 'maxlength' : base.options.autoCustomAttr; var max = base.$el.attr(base.options.autoCustomAttr); - + if (typeof max !== 'undefined' && max !== false) { base.options.max = max; } else { - base.$container.text('error: [' + base.options.autoCustomAttr + '] attribute not set'); + base.$container.text('error: [' + base.options.autoCustomAttr + '] attribute not set'); } } @@ -316,6 +306,7 @@ 'type' : "character", // "character" or "word" 'min' : 0, // minimum number of characters/words 'max' : 200, // maximum number of characters/words, -1 for unlimited, 'auto' to use maxlength attribute, 'autocustom' to use a custom attribute for the length (must set "autoCustomAttr") + 'automax' : false, // Automatic reload max from max attribute 'autoCustomAttr' : "counterlimit", // custom attribute name with the counter limit if the max is 'autocustom' 'countContainerElement' : "div", // HTML element to wrap the text count in 'countContainerClass' : "text-count-wrapper", // class applied to the countContainerElement diff --git a/textcounter.min.js b/textcounter.min.js index c5d0439..868c0d8 100644 --- a/textcounter.min.js +++ b/textcounter.min.js @@ -5,4 +5,4 @@ * Copyright 2014 ractoon * Released under the MIT license */ -!function(t){t.textcounter=function(o,n){var e=this;e.$el=t(o),e.el=o,e.$el.data("textcounter",e),e.init=function(){e.options=t.extend({},t.textcounter.defaultOptions,n);var o=e.options.countDown?e.options.countDownText:e.options.counterText,r=e.options.countDown?e.options.max:0,s=t("
").addClass(e.options.textCountMessageClass).html(o.replace("%d",'")),i=t("").addClass(e.options.countOverflowContainerClass);e.hideMessage(i),e.$container=t("<"+e.options.countContainerElement+"/>").addClass(e.options.countContainerClass).append(s).append(i),e.$text_counter=e.$container.find("span"),e.$el.after(e.$container),e.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",e.checkLimits).trigger("click.textcounter"),e.options.init(e.el)},e.checkLimits=function(o){var n=e.$el,r=(e.$container,n.val()),s=0,i=0,a=void 0!==o.originalEvent;if(t.isEmptyObject(r)||(s=e.textCount(r)),"auto"==e.options.max)void 0!==(u=e.$el.attr("maxlength"))&&!1!==u?e.options.max=u:e.$container.text("error: [maxlength] attribute not set");else if("autocustom"==e.options.max){var u=e.$el.attr(e.options.autoCustomAttr);void 0!==u&&!1!==u?e.options.max=u:e.$container.text("error: ["+e.options.autoCustomAttr+"] attribute not set")}if(i=e.options.countDown?e.options.max-s:s,e.setCount(i),e.options.min>0&&a&&(s=C.options.max-C.options.maxDisplayCutoff?C.$container.show():C.$container.hide()},C.textCount=function(t){return"word"==C.options.type?C.wordCount(t):C.characterCount(t)},C.wordCount=function(t){return t.trim().replace(/\s+/gi," ").split(" ").length},C.characterCount=function(t){var o=0,n=0;if(C.options.twoCharCarriageReturn&&(n=C.twoCharCarriageReturnCount(t)),o=C.options.countSpaces?t.replace(/[^\S\n|\r|\r\n]/g," ").length:t.replace(/\s/g,"").length,C.options.countExtendedCharacters){var e=t.match(/[^\x00-\xff]/gi);o=null==e?t.length:t.length+e.length}return C.options.twoCharCarriageReturn&&(o+=n),o},C.twoCharCarriageReturnCount=function(t){var o=t.match(/(\r\n|\n|\r)/g),n=0;return null!==o&&(n=o.length),n},C.setCount=function(t){C.$text_counter.text(t)},C.setErrors=function(t){var o=C.$el,n=C.$container,e="";switch(o.addClass(C.options.inputErrorClass),n.addClass(C.options.counterErrorClass),t){case"min":e=C.options.minimumErrorText;break;case"max":e=C.options.maximumErrorText,C.options.countOverflow&&C.setOverflowMessage()}C.options.displayErrorText&&(n.children(".error-text-"+t).length||n.append("<"+C.options.errorTextElement+' class="error-text error-text-'+t+'">'+e+""+C.options.errorTextElement+">"))},C.setOverflowMessage=function(){C.hideMessage(C.$container.find("."+C.options.textCountMessageClass)),C.removeOverflowMessage();var t=C.options.countOverflowText.replace("%d",C.textCount(C.$el.val())-C.options.max).replace("%type",C.options.type+"s"),o=C.$container.find("."+C.options.countOverflowContainerClass).append(t);C.showMessage(o)},C.removeOverflowMessage=function(){C.$container.find("."+C.options.countOverflowContainerClass).empty()},C.showMessage=function(t){t.css("display","inline")},C.hideMessage=function(t){t.css("display","none")},C.clearErrors=function(t){var o=C.$el,n=C.$container;n.children(".error-text-"+t).remove(),0==n.children(".error-text").length&&(C.removeOverflowMessage(),C.showMessage(C.$container.find("."+C.options.textCountMessageClass)),o.removeClass(C.options.inputErrorClass),n.removeClass(C.options.counterErrorClass))},C.init()},f.textcounter.defaultOptions={type:"character",min:0,max:200,automax:!1,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){}},f.fn.textcounter=function(t){return this.each(function(){new f.textcounter(this,t)})}}(jQuery); \ No newline at end of file