diff --git a/README.md b/README.md index 1022e72..d7e15c6 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ Fires when counter is under min limit. ```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, 'autocustom' to use a custom attribute for the length (must set "autoCustomAttr") +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 @@ -133,6 +133,8 @@ twoCharCarriageReturn : false, // count carriage 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 +minDisplayCutoff : -1, // maximum number of characters/words above the minimum to display a count +maxDisplayCutoff : -1, // maximum number of characters/words below the maximum to display a count // Callback API maxunder : function(el){}, // Callback: function(element) - Fires when counter is under max limit @@ -165,3 +167,7 @@ init : function(el){} // Callback: func - [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 +- [trevorloflin](https://github.com/trevorloflin) - `minDisplayCutoff` and `maxDisplayCutoff` options +- [t3mujin](https://github.com/t3mujin) - autocustom support (maxlength workaround), text fixes +- [goranmiric ](https://github.com/goranmiric) - accessibility enhancements +- [ameshkin](https://github.com/ameshkin) - accessibility adjustments \ No newline at end of file diff --git a/bower.json b/bower.json index 0a53c10..84748bf 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "jquery-text-counter", - "version": "0.7.0", + "version": "0.9.1", "main": "textcounter.js", "license": "MIT", "ignore": [ diff --git a/package.json b/package.json index 57a0945..a481955 100644 --- a/package.json +++ b/package.json @@ -11,10 +11,10 @@ "minimum", "maximum" ], - "version": "0.7.0", + "version": "0.9.1", "author": { "name": "ractoon", - "url": "http://www.ractoon.com" + "url": "https://www.ractoon.com" }, "licenses": [ { diff --git a/textcounter.jquery.json b/textcounter.jquery.json deleted file mode 100644 index 66a1d2c..0000000 --- a/textcounter.jquery.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "name": "textcounter", - "title": "jQuery Text Counter", - "description": "jQuery plugin to count text/characters in a textarea or input. Allows error notifications on minimum or maximum number of characters/words.", - "keywords": [ - "words", - "characters", - "counter", - "minimum", - "maximum" - ], - "version": "0.7.0", - "author": { - "name": "ractoon", - "url": "http://www.ractoon.com" - }, - "licenses": [ - { - "type": "MIT", - "url": "https://github.com/ractoon/jQuery-Text-Counter/blob/master/LICENSE.md" - } - ], - "bugs": "https://github.com/ractoon/jQuery-Text-Counter/issues", - "homepage": "https://github.com/ractoon/jQuery-Text-Counter", - "docs": "https://github.com/ractoon/jQuery-Text-Counter", - "dependencies": { - "jquery": ">=1.5" - } -} \ No newline at end of file diff --git a/textcounter.js b/textcounter.js index 7ac76eb..7db2a81 100644 --- a/textcounter.js +++ b/textcounter.js @@ -1,5 +1,5 @@ /*! -* jQuery Text Counter Plugin v0.7.0 +* jQuery Text Counter Plugin v0.9.1 * https://github.com/ractoon/jQuery-Text-Counter * * Copyright 2014 ractoon @@ -25,6 +25,7 @@ var counterText = base.options.countDown ? base.options.countDownText : base.options.counterText, counterNum = base.options.countDown ? base.options.max : 0, $formatted_counter_text = $('
').addClass(base.options.textCountMessageClass) + .attr('aria-live', 'polite').attr('aria-atomic', 'true') .html(counterText.replace('%d', '')), $count_overflow_text = $('').addClass(base.options.countOverflowContainerClass); @@ -161,6 +162,17 @@ base.clearErrors('max'); } } + + // hide the counter if it doesn't meet either the minimum or maximum display cutoff + if (base.options.minDisplayCutoff == -1 && base.options.maxDisplayCutoff == -1) { + base.$container.show(); + } else if (textCount <= base.options.min + base.options.minDisplayCutoff) { + base.$container.show(); + } else if (base.options.max !== -1 && textCount >= base.options.max - base.options.maxDisplayCutoff) { + base.$container.show(); + } else { + base.$container.hide(); + } }; base.textCount = function(text) { @@ -326,6 +338,8 @@ '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 + 'minDisplayCutoff' : -1, // maximum number of characters/words above the minimum to display a count + 'maxDisplayCutoff' : -1, // maximum number of characters/words below the maximum to display a count // Callback API 'maxunder' : function(el){}, // Callback: function(element) - Fires when counter under max limit diff --git a/textcounter.min.js b/textcounter.min.js index be6bb72..f7831ef 100644 --- a/textcounter.min.js +++ b/textcounter.min.js @@ -1,8 +1,8 @@ /*! -* jQuery Text Counter Plugin v0.7.0 +* jQuery Text Counter Plugin v0.9.1 * https://github.com/ractoon/jQuery-Text-Counter * * 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