From 1f9bdf9471a3c5f22eb9a9b23c2a93b736c86c3e Mon Sep 17 00:00:00 2001 From: Manex Date: Tue, 19 Feb 2019 15:58:09 +0100 Subject: [PATCH 1/4] Option for auto reloading maxlength --- textcounter.js | 3 ++- textcounter.min.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/textcounter.js b/textcounter.js index beb0afe..4fd5827 100644 --- a/textcounter.js +++ b/textcounter.js @@ -58,7 +58,7 @@ } // if max is auto retrieve value - if (base.options.max == 'auto') { + if (base.options.max == 'auto' || base.options.automax) { var max = base.$el.attr('maxlength'); if (typeof max !== 'undefined' && max !== false) { @@ -316,6 +316,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 maxlength '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..f687e91 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",''+r+"")),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=e.options.min&&(e.options.mincount(e.el),e.clearErrors("min"))),-1!==e.options.max)if(s===e.options.max&&0!==e.options.max)e.options.maxcount(e.el),e.clearErrors("max");else if(s>e.options.max&&0!==e.options.max)if(e.options.stopInputAtMaximum){var c="";if("word"==e.options.type)for(var p=r.split(/[^\S\n]/g),l=0;l=e.options.max);)void 0!==p[l]&&(c+=p[l]+" ",l++);else{var m=e.options.twoCharCarriageReturn?e.options.max-e.twoCharCarriageReturnCount(r):e.options.max;if(e.options.countSpaces)c=r.substring(0,m);else for(var x=r.split(""),C=x.length,f=0,l=0;f=e.options.max-e.options.maxDisplayCutoff?e.$container.show():e.$container.hide()},e.textCount=function(t){return"word"==e.options.type?e.wordCount(t):e.characterCount(t)},e.wordCount=function(t){return t.trim().replace(/\s+/gi," ").split(" ").length},e.characterCount=function(t){var o=0,n=0;if(e.options.twoCharCarriageReturn&&(n=e.twoCharCarriageReturnCount(t)),o=e.options.countSpaces?t.replace(/[^\S\n|\r|\r\n]/g," ").length:t.replace(/\s/g,"").length,e.options.countExtendedCharacters){var r=t.match(/[^\x00-\xff]/gi);o=null==r?t.length:t.length+r.length}return e.options.twoCharCarriageReturn&&(o+=n),o},e.twoCharCarriageReturnCount=function(t){var o=t.match(/(\r\n|\n|\r)/g),n=0;return null!==o&&(n=o.length),n},e.setCount=function(t){e.$text_counter.text(t)},e.setErrors=function(t){var o=e.$el,n=e.$container,r="";switch(o.addClass(e.options.inputErrorClass),n.addClass(e.options.counterErrorClass),t){case"min":r=e.options.minimumErrorText;break;case"max":r=e.options.maximumErrorText,e.options.countOverflow&&e.setOverflowMessage()}e.options.displayErrorText&&(n.children(".error-text-"+t).length||n.append("<"+e.options.errorTextElement+' class="error-text error-text-'+t+'">'+r+""))},e.setOverflowMessage=function(){e.hideMessage(e.$container.find("."+e.options.textCountMessageClass)),e.removeOverflowMessage();var t=e.options.countOverflowText.replace("%d",e.textCount(e.$el.val())-e.options.max).replace("%type",e.options.type+"s"),o=e.$container.find("."+e.options.countOverflowContainerClass).append(t);e.showMessage(o)},e.removeOverflowMessage=function(){e.$container.find("."+e.options.countOverflowContainerClass).empty()},e.showMessage=function(t){t.css("display","inline")},e.hideMessage=function(t){t.css("display","none")},e.clearErrors=function(t){var o=e.$el,n=e.$container;n.children(".error-text-"+t).remove(),0==n.children(".error-text").length&&(e.removeOverflowMessage(),e.showMessage(e.$container.find("."+e.options.textCountMessageClass)),o.removeClass(e.options.inputErrorClass),n.removeClass(e.options.counterErrorClass))},e.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); \ No newline at end of file +!function(f){f.textcounter=function(t,r){var C=this;C.$el=f(t),C.el=t,C.$el.data("textcounter",C),C.init=function(){C.options=f.extend({},f.textcounter.defaultOptions,r);var t=C.options.countDown?C.options.countDownText:C.options.counterText,o=C.options.countDown?C.options.max:0,n=f("
").addClass(C.options.textCountMessageClass).html(t.replace("%d",''+o+"")),e=f("
").addClass(C.options.countOverflowContainerClass);C.hideMessage(e),C.$container=f("<"+C.options.countContainerElement+"/>").addClass(C.options.countContainerClass).append(n).append(e),C.$text_counter=C.$container.find("span"),C.$el.after(C.$container),C.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",C.checkLimits).trigger("click.textcounter"),C.options.init(C.el)},C.checkLimits=function(t){var o=C.$el,n=(C.$container,o.val()),e=0,r=0,s=void 0!==t.originalEvent;if(f.isEmptyObject(n)||(e=C.textCount(n)),"auto"==C.options.max||C.options.automax)void 0!==(i=C.$el.attr("maxlength"))&&!1!==i?C.options.max=i:C.$container.text("error: [maxlength] attribute not set");else if("autocustom"==C.options.max){var i;void 0!==(i=C.$el.attr(C.options.autoCustomAttr))&&!1!==i?C.options.max=i:C.$container.text("error: ["+C.options.autoCustomAttr+"] attribute not set")}if(r=C.options.countDown?C.options.max-e:e,C.setCount(r),0=C.options.min&&(C.options.mincount(C.el),C.clearErrors("min"))),-1!==C.options.max)if(e===C.options.max&&0!==C.options.max)C.options.maxcount(C.el),C.clearErrors("max");else if(e>C.options.max&&0!==C.options.max)if(C.options.stopInputAtMaximum){var a="";if("word"==C.options.type)for(var u=n.split(/[^\S\n]/g),c=0;c=C.options.max);)void 0!==u[c]&&(a+=u[c]+" ",c++);else{var p=C.options.twoCharCarriageReturn?C.options.max-C.twoCharCarriageReturnCount(n):C.options.max;if(C.options.countSpaces)a=n.substring(0,p);else{var l=n.split(""),m=l.length,x=0;for(c=0;x=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.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 From 180eb4d0f93228d45a975f73d3ceb98415ad1ba6 Mon Sep 17 00:00:00 2001 From: Manex Agirre Date: Tue, 19 Feb 2019 15:58:09 +0100 Subject: [PATCH 2/4] Option for auto reloading maxlength --- textcounter.js | 3 ++- textcounter.min.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/textcounter.js b/textcounter.js index beb0afe..4fd5827 100644 --- a/textcounter.js +++ b/textcounter.js @@ -58,7 +58,7 @@ } // if max is auto retrieve value - if (base.options.max == 'auto') { + if (base.options.max == 'auto' || base.options.automax) { var max = base.$el.attr('maxlength'); if (typeof max !== 'undefined' && max !== false) { @@ -316,6 +316,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 maxlength '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..f687e91 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",''+r+"")),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=e.options.min&&(e.options.mincount(e.el),e.clearErrors("min"))),-1!==e.options.max)if(s===e.options.max&&0!==e.options.max)e.options.maxcount(e.el),e.clearErrors("max");else if(s>e.options.max&&0!==e.options.max)if(e.options.stopInputAtMaximum){var c="";if("word"==e.options.type)for(var p=r.split(/[^\S\n]/g),l=0;l=e.options.max);)void 0!==p[l]&&(c+=p[l]+" ",l++);else{var m=e.options.twoCharCarriageReturn?e.options.max-e.twoCharCarriageReturnCount(r):e.options.max;if(e.options.countSpaces)c=r.substring(0,m);else for(var x=r.split(""),C=x.length,f=0,l=0;f=e.options.max-e.options.maxDisplayCutoff?e.$container.show():e.$container.hide()},e.textCount=function(t){return"word"==e.options.type?e.wordCount(t):e.characterCount(t)},e.wordCount=function(t){return t.trim().replace(/\s+/gi," ").split(" ").length},e.characterCount=function(t){var o=0,n=0;if(e.options.twoCharCarriageReturn&&(n=e.twoCharCarriageReturnCount(t)),o=e.options.countSpaces?t.replace(/[^\S\n|\r|\r\n]/g," ").length:t.replace(/\s/g,"").length,e.options.countExtendedCharacters){var r=t.match(/[^\x00-\xff]/gi);o=null==r?t.length:t.length+r.length}return e.options.twoCharCarriageReturn&&(o+=n),o},e.twoCharCarriageReturnCount=function(t){var o=t.match(/(\r\n|\n|\r)/g),n=0;return null!==o&&(n=o.length),n},e.setCount=function(t){e.$text_counter.text(t)},e.setErrors=function(t){var o=e.$el,n=e.$container,r="";switch(o.addClass(e.options.inputErrorClass),n.addClass(e.options.counterErrorClass),t){case"min":r=e.options.minimumErrorText;break;case"max":r=e.options.maximumErrorText,e.options.countOverflow&&e.setOverflowMessage()}e.options.displayErrorText&&(n.children(".error-text-"+t).length||n.append("<"+e.options.errorTextElement+' class="error-text error-text-'+t+'">'+r+""))},e.setOverflowMessage=function(){e.hideMessage(e.$container.find("."+e.options.textCountMessageClass)),e.removeOverflowMessage();var t=e.options.countOverflowText.replace("%d",e.textCount(e.$el.val())-e.options.max).replace("%type",e.options.type+"s"),o=e.$container.find("."+e.options.countOverflowContainerClass).append(t);e.showMessage(o)},e.removeOverflowMessage=function(){e.$container.find("."+e.options.countOverflowContainerClass).empty()},e.showMessage=function(t){t.css("display","inline")},e.hideMessage=function(t){t.css("display","none")},e.clearErrors=function(t){var o=e.$el,n=e.$container;n.children(".error-text-"+t).remove(),0==n.children(".error-text").length&&(e.removeOverflowMessage(),e.showMessage(e.$container.find("."+e.options.textCountMessageClass)),o.removeClass(e.options.inputErrorClass),n.removeClass(e.options.counterErrorClass))},e.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); \ No newline at end of file +!function(f){f.textcounter=function(t,r){var C=this;C.$el=f(t),C.el=t,C.$el.data("textcounter",C),C.init=function(){C.options=f.extend({},f.textcounter.defaultOptions,r);var t=C.options.countDown?C.options.countDownText:C.options.counterText,o=C.options.countDown?C.options.max:0,n=f("
").addClass(C.options.textCountMessageClass).html(t.replace("%d",''+o+"")),e=f("
").addClass(C.options.countOverflowContainerClass);C.hideMessage(e),C.$container=f("<"+C.options.countContainerElement+"/>").addClass(C.options.countContainerClass).append(n).append(e),C.$text_counter=C.$container.find("span"),C.$el.after(C.$container),C.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",C.checkLimits).trigger("click.textcounter"),C.options.init(C.el)},C.checkLimits=function(t){var o=C.$el,n=(C.$container,o.val()),e=0,r=0,s=void 0!==t.originalEvent;if(f.isEmptyObject(n)||(e=C.textCount(n)),"auto"==C.options.max||C.options.automax)void 0!==(i=C.$el.attr("maxlength"))&&!1!==i?C.options.max=i:C.$container.text("error: [maxlength] attribute not set");else if("autocustom"==C.options.max){var i;void 0!==(i=C.$el.attr(C.options.autoCustomAttr))&&!1!==i?C.options.max=i:C.$container.text("error: ["+C.options.autoCustomAttr+"] attribute not set")}if(r=C.options.countDown?C.options.max-e:e,C.setCount(r),0=C.options.min&&(C.options.mincount(C.el),C.clearErrors("min"))),-1!==C.options.max)if(e===C.options.max&&0!==C.options.max)C.options.maxcount(C.el),C.clearErrors("max");else if(e>C.options.max&&0!==C.options.max)if(C.options.stopInputAtMaximum){var a="";if("word"==C.options.type)for(var u=n.split(/[^\S\n]/g),c=0;c=C.options.max);)void 0!==u[c]&&(a+=u[c]+" ",c++);else{var p=C.options.twoCharCarriageReturn?C.options.max-C.twoCharCarriageReturnCount(n):C.options.max;if(C.options.countSpaces)a=n.substring(0,p);else{var l=n.split(""),m=l.length,x=0;for(c=0;x=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.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 From 991d912c643812dada3c36b817fa8b33dcaedaae Mon Sep 17 00:00:00 2001 From: Manex Agirre Date: Tue, 19 Feb 2019 16:11:59 +0100 Subject: [PATCH 3/4] Accept autocustom attr for automax option --- README.md | 1 + textcounter.js | 20 +++++--------------- 2 files changed, 6 insertions(+), 15 deletions(-) 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 4fd5827..9f60726 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' || base.options.automax) { - var max = base.$el.attr('maxlength'); + if (base.options.max == 'auto' || base.options.max == 'autocustom' || base.options.automax) { + var attrName = (base.options.max == 'auto') ? 'maxlength' : base.options.autoCustomAttr: + var max = base.$el.attr(attrName); 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') { - 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: [' + attrName + '] attribute not set'); } } @@ -316,7 +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 maxlength + '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 From 3c2e0be0b58792732b54b864a68d38bad84ad8cd Mon Sep 17 00:00:00 2001 From: Manex Agirre Date: Tue, 19 Feb 2019 16:20:22 +0100 Subject: [PATCH 4/4] Fix error and compress --- textcounter.js | 6 +++--- textcounter.min.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/textcounter.js b/textcounter.js index 9f60726..eae8dcb 100644 --- a/textcounter.js +++ b/textcounter.js @@ -58,14 +58,14 @@ } if (base.options.max == 'auto' || base.options.max == 'autocustom' || base.options.automax) { - var attrName = (base.options.max == 'auto') ? 'maxlength' : base.options.autoCustomAttr: - var max = base.$el.attr(attrName); + 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: [' + attrName + '] attribute not set'); + base.$container.text('error: [' + base.options.autoCustomAttr + '] attribute not set'); } } diff --git a/textcounter.min.js b/textcounter.min.js index f687e91..868c0d8 100644 --- a/textcounter.min.js +++ b/textcounter.min.js @@ -5,4 +5,4 @@ * Copyright 2014 ractoon * Released under the MIT license */ -!function(f){f.textcounter=function(t,r){var C=this;C.$el=f(t),C.el=t,C.$el.data("textcounter",C),C.init=function(){C.options=f.extend({},f.textcounter.defaultOptions,r);var t=C.options.countDown?C.options.countDownText:C.options.counterText,o=C.options.countDown?C.options.max:0,n=f("
").addClass(C.options.textCountMessageClass).html(t.replace("%d",''+o+"")),e=f("
").addClass(C.options.countOverflowContainerClass);C.hideMessage(e),C.$container=f("<"+C.options.countContainerElement+"/>").addClass(C.options.countContainerClass).append(n).append(e),C.$text_counter=C.$container.find("span"),C.$el.after(C.$container),C.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",C.checkLimits).trigger("click.textcounter"),C.options.init(C.el)},C.checkLimits=function(t){var o=C.$el,n=(C.$container,o.val()),e=0,r=0,s=void 0!==t.originalEvent;if(f.isEmptyObject(n)||(e=C.textCount(n)),"auto"==C.options.max||C.options.automax)void 0!==(i=C.$el.attr("maxlength"))&&!1!==i?C.options.max=i:C.$container.text("error: [maxlength] attribute not set");else if("autocustom"==C.options.max){var i;void 0!==(i=C.$el.attr(C.options.autoCustomAttr))&&!1!==i?C.options.max=i:C.$container.text("error: ["+C.options.autoCustomAttr+"] attribute not set")}if(r=C.options.countDown?C.options.max-e:e,C.setCount(r),0=C.options.min&&(C.options.mincount(C.el),C.clearErrors("min"))),-1!==C.options.max)if(e===C.options.max&&0!==C.options.max)C.options.maxcount(C.el),C.clearErrors("max");else if(e>C.options.max&&0!==C.options.max)if(C.options.stopInputAtMaximum){var a="";if("word"==C.options.type)for(var u=n.split(/[^\S\n]/g),c=0;c=C.options.max);)void 0!==u[c]&&(a+=u[c]+" ",c++);else{var p=C.options.twoCharCarriageReturn?C.options.max-C.twoCharCarriageReturnCount(n):C.options.max;if(C.options.countSpaces)a=n.substring(0,p);else{var l=n.split(""),m=l.length,x=0;for(c=0;x=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.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 +!function(f){f.textcounter=function(t,r){var C=this;C.$el=f(t),C.el=t,C.$el.data("textcounter",C),C.init=function(){C.options=f.extend({},f.textcounter.defaultOptions,r);var t=C.options.countDown?C.options.countDownText:C.options.counterText,o=C.options.countDown?C.options.max:0,n=f("
").addClass(C.options.textCountMessageClass).html(t.replace("%d",''+o+"")),e=f("
").addClass(C.options.countOverflowContainerClass);C.hideMessage(e),C.$container=f("<"+C.options.countContainerElement+"/>").addClass(C.options.countContainerClass).append(n).append(e),C.$text_counter=C.$container.find("span"),C.$el.after(C.$container),C.$el.bind("keyup.textcounter click.textcounter blur.textcounter focus.textcounter change.textcounter paste.textcounter",C.checkLimits).trigger("click.textcounter"),C.options.init(C.el)},C.checkLimits=function(t){var o=C.$el,n=(C.$container,o.val()),e=0,r=0,s=void 0!==t.originalEvent;if(f.isEmptyObject(n)||(e=C.textCount(n)),"auto"==C.options.max||"autocustom"==C.options.max||C.options.automax){C.options.autoCustomAttr="auto"==C.options.max?"maxlength":C.options.autoCustomAttr;var i=C.$el.attr(C.options.autoCustomAttr);void 0!==i&&!1!==i?C.options.max=i:C.$container.text("error: ["+C.options.autoCustomAttr+"] attribute not set")}if(r=C.options.countDown?C.options.max-e:e,C.setCount(r),0=C.options.min&&(C.options.mincount(C.el),C.clearErrors("min"))),-1!==C.options.max)if(e===C.options.max&&0!==C.options.max)C.options.maxcount(C.el),C.clearErrors("max");else if(e>C.options.max&&0!==C.options.max)if(C.options.stopInputAtMaximum){var a="";if("word"==C.options.type)for(var u=n.split(/[^\S\n]/g),c=0;c=C.options.max);)void 0!==u[c]&&(a+=u[c]+" ",c++);else{var p=C.options.twoCharCarriageReturn?C.options.max-C.twoCharCarriageReturnCount(n):C.options.max;if(C.options.countSpaces)a=n.substring(0,p);else{var l=n.split(""),m=l.length,x=0;for(c=0;x=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.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