-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.limit_text.min.js
5 lines (5 loc) · 2.46 KB
/
jquery.limit_text.min.js
1
2
3
4
5
(function(b){b.fn.limit_text=function(c){this.each(function(){c=b.extend({},b.text_limiter.defaults,c);var a=new b.text_limiter(b(this),c);if(c.show_immediately){a.restore_old_state();a.update_description()}b(this).keyup(function(){a.restore_old_state();a.update_description()});b(this).keypress(function(){a.restore_old_state();a.update_description()});b(this).keydown(function(){a.store_old_state()})});return this};b.text_limiter=function(c,a){this.options=a;this.text_jquery=c;this.description_id=
this.text_jquery[0].id+"_limit_description";this.last_good_text=this.text_jquery.val();if(jQuery("#"+this.description_id).length==0){c='<div id="'+this.description_id+'"></div>';if(a.placement=="after")this.text_jquery.after(c);else a.placement=="before"?this.text_jquery.before(c):alert("limit_text: Invalid placement")}this.description_jquery=b("#"+this.description_id);this.store_old_state=function(){if(b.text_limiter.get_count(this.text_jquery.val(),this.options.type)<=this.options.max){this.last_good_text=
this.text_jquery.val();if(this.text_jquery[0].selectionStart&&this.text_jquery[0].selectionEnd){this.last_selection_start=this.text_jquery[0].selectionStart;this.last_selection_end=this.text_jquery[0].selectionEnd}}};this.restore_old_state=function(){if(this.options.strict)if(b.text_limiter.get_count(this.text_jquery.val(),this.options.type)>a.max){this.text_jquery.val(this.last_good_text);if(this.text_jquery[0].selectionStart&&this.text_jquery[0].selectionEnd){this.text_jquery[0].selectionStart=
this.last_selection_start;this.text_jquery[0].selectionEnd=this.last_selection_end}}};this.update_description=function(){var d;d=b.text_limiter.get_count(this.text_jquery.val(),this.options.type);if(d>a.max){description_class=a.error_class;d=d-a.max+" "+a.type+"s over the limit (max: "+a.max+")"}else{description_class=a.max-d<a.warning_max?a.warning_class:a.success_class;d=a.max-d+" "+a.type+"s remaining."}var e=this.description_jquery;b.each([this.options.error_class,this.options.warning_class,this.options.success_class],
function(g,f){e.removeClass(f)});e.addClass(description_class);e.html(d)}};b.text_limiter.get_count=function(c,a){if(a=="character")return c.length;else if(a=="word")return b.grep(c.split(/\s+/),function(d){return d!=""}).length;else alert("limit_text: Invalid type")};b.text_limiter.defaults={type:"character",max:255,warning_max:25,success_class:"success",warning_class:"warning",error_class:"error",placement:"after",show_immediately:true,strict:false}})(jQuery);