diff --git a/README.md b/README.md index 51cdb2f..1022e72 100644 --- a/README.md +++ b/README.md @@ -109,32 +109,37 @@ Fires when counter is under min limit. ## Options ```javascript -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 -countContainerElement : "div", // HTML element to wrap the text count in -countContainerClass : "text-count-wrapper", // class applied to the countContainerElement -textCountClass : "text-count", // class applied to the counter length -inputErrorClass : "error", // error class appended to the input element if error occurs -counterErrorClass : "error", // error class appended to the countContainerElement if error occurs -counterText : "Total Count: %d", // counter text, %d replaced with count value -errorTextElement : "div", // error text element -minimumErrorText : "Minimum not met", // error message for minimum not met, -maximumErrorText : "Maximum exceeded", // error message for maximum range exceeded, -displayErrorText : true, // display error text messages for minimum/maximum values -stopInputAtMaximum : true, // stop further text input if maximum reached -countSpaces : false, // count spaces as character (only for "character" type) -countDown : false, // if the counter should deduct from maximum characters/words rather than counting up -countDownText : "Remaining: %d", // count down text, %d replaced with remaining value -countExtendedCharacters : false, // count extended UTF-8 characters as 2 bytes (such as Chinese characters) -twoCharCarriageReturn : false, // count carriage returns/newlines as 2 characters +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") +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 +textCountMessageClass : "text-count-message", // class applied to the counter message +textCountClass : "text-count", // class applied to the counter length (the count number) +inputErrorClass : "error", // error class appended to the input element if error occurs +counterErrorClass : "error", // error class appended to the countContainerElement if error occurs +counterText : "Total Count: %d", // counter text +errorTextElement : "div", // error text element +minimumErrorText : "Minimum not met", // error message for minimum not met, +maximumErrorText : "Maximum exceeded", // error message for maximum range exceeded, +displayErrorText : true, // display error text messages for minimum/maximum values +stopInputAtMaximum : true, // stop further text input if maximum reached +countSpaces : false, // count spaces as character (only for "character" type) +countDown : false, // if the counter should deduct from maximum characters/words rather than counting up +countDownText : "Remaining: %d", // count down text +countExtendedCharacters : false, // count extended UTF-8 characters as 2 bytes (such as Chinese characters) +twoCharCarriageReturn : false, // count carriage returns/newlines as 2 characters +countOverflow : false, // display text overflow element +countOverflowText : "Maximum %type exceeded by %d", // count overflow text +countOverflowContainerClass : "text-count-overflow-wrapper", // class applied to the count overflow wrapper // Callback API -maxunder : function(el){}, // Callback: function(element) - Fires when counter is under max limit -minunder : function(el){}, // Callback: function(element) - Fires when counter is under min limit -maxcount : function(el){}, // Callback: function(element) - Fires when the counter hits the maximum word/character count -mincount : function(el){}, // Callback: function(element) - Fires when the counter hits the minimum word/character count -init : function(el){} // Callback: function(element) - Fires after the counter is initially setup +maxunder : function(el){}, // Callback: function(element) - Fires when counter is under max limit +minunder : function(el){}, // Callback: function(element) - Fires when counter is under min limit +maxcount : function(el){}, // Callback: function(element) - Fires when the counter hits the maximum word/character count +mincount : function(el){}, // Callback: function(element) - Fires when the counter hits the minimum word/character count +init : function(el){} // Callback: function(element) - Fires after the counter is initially setup ``` ## Development @@ -159,4 +164,4 @@ init : function(el){} // Callback: function(element - [juliovedovatto](https://github.com/juliovedovatto) / [alvaro-canepa](https://github.com/alvaro-canepa) - multiple classes support for counter container - [dtipson](https://github.com/dtipson) - multiple classes error fix - [jmichalicek](https://github.com/jmichalicek) - count carriage returns/newlines as 2 characters -- [diptopol](https://github.com/diptopol) - `stopInputAtMaximum` with `twoCharCarriageReturn` count fix, trimmed newline calculation fix, maximum text reached condition fix, text count overflow notification \ No newline at end of file +- [diptopol](https://github.com/diptopol) - `stopInputAtMaximum` with `twoCharCarriageReturn` count fix, trimmed newline calculation fix, maximum text reached condition fix, text count overflow notification diff --git a/textcounter.js b/textcounter.js index 4078a3d..7ac76eb 100644 --- a/textcounter.js +++ b/textcounter.js @@ -68,6 +68,16 @@ base.$container.text('error: [maxlength] attribute not set'); } } + else if (base.options.max == 'autocustom') { + 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'); + } + } // if this is a countdown counter deduct from the max characters/words textTotalCount = base.options.countDown ? base.options.max - textCount : textCount; @@ -294,7 +304,8 @@ $.textcounter.defaultOptions = { '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 + '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") + '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 'textCountMessageClass' : "text-count-message", // class applied to the counter message diff --git a/textcounter.min.js b/textcounter.min.js index ad6f19f..be6bb72 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(n,o){var e=this;e.$el=t(n),e.el=n,e.$el.data("textcounter",e),e.init=function(){e.options=t.extend({},t.textcounter.defaultOptions,o);var n=e.options.countDown?e.options.countDownText:e.options.counterText,r=e.options.countDown?e.options.max:0,s=t("
").addClass(e.options.textCountMessageClass).html(n.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(n){var o=e.$el,r=(e.$container,o.val()),s=0,i=0,a=void 0!==n.originalEvent;if(t.isEmptyObject(r)||(s=e.textCount(r)),"auto"==e.options.max){var u=e.$el.attr("maxlength");void 0!==u&&!1!==u?e.options.max=u:e.$container.text("error: [maxlength] attribute not set")}if(i=e.options.countDown?e.options.max-s:s,e.setCount(i),e.options.min>0&&a&&(s