forked from mimo84/bootstrap-maxlength
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-maxlength.min.js
More file actions
9 lines (9 loc) · 2.92 KB
/
bootstrap-maxlength.min.js
File metadata and controls
9 lines (9 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
/* ==========================================================
* bootstrap-maxlength.js v1.4.0
*
* Copyright (c) 2013 Maurizio Napoleoni;
*
* Licensed under the terms of the MIT license.
* See: https://github.com/mimo84/bootstrap-maxlength/blob/master/LICENSE
* ========================================================== */
(function(a){a.fn.extend({maxlength:function(o,m){var h=a("body"),c={alwaysShow:false,threshold:10,warningClass:"badge badge-success",limitReachedClass:"badge badge-important",separator:" / ",preText:"",postText:"",showMaxLength:true,placement:"bottom",showCharsTyped:true,validate:false};if(a.isFunction(o)&&!m){m=o;o={}}o=a.extend(c,o);function d(p){var s=p.val(),r=s.match(/\n/g),q=r?r.length:0;return p.val().length+q}function g(q,s,r){var p=true;if(!o.alwaysShow&&(r-d(q)>s)){p=false}return p}function i(p,q){var r=q-d(p);return r}function f(p){p.css({display:"block"})}function k(p){p.css({display:"none"})}function l(q,r){var p="";if(o.preText){p+=o.preText}if(!o.showCharsTyped){p+=i(r,q)}else{p+=r}if(o.showMaxLength){p+=o.separator+q}if(o.postText){p+=o.postText}return p}function e(q,p,r,s){s.html(l(r,(r-q)));if(q>0){if(g(p,o.threshold,r)){f(s.removeClass(o.limitReachedClass).addClass(o.warningClass))}else{k(s)}}else{f(s.removeClass(o.warningClass).addClass(o.limitReachedClass))}}function n(p){var q=p[0];return a.extend({},(typeof q.getBoundingClientRect==="function")?q.getBoundingClientRect():{width:q.offsetWidth,height:q.offsetHeight},p.offset())}function b(p,t){var v=n(p),s=p.outerWidth(),r=t.outerWidth(),q=t.width(),u=t.height();switch(o.placement){case"bottom":t.css({top:v.top+v.height,left:v.left+v.width/2-q/2});break;case"top":t.css({top:v.top-u,left:v.left+v.width/2-q/2});break;case"left":t.css({top:v.top+v.height/2-u/2,left:v.left-q});break;case"right":t.css({top:v.top+v.height/2-u/2,left:v.left+v.width});break;case"bottom-right":t.css({top:v.top+v.height,left:v.left+v.width});break;case"top-right":t.css({top:v.top-u,left:v.left+s});break;case"top-left":t.css({top:v.top-u,left:v.left-r});break;case"bottom-left":t.css({top:v.top+p.outerHeight(),left:v.left-r});break;case"centered-right":t.css({top:v.top+(u/2),left:v.left+s-r-3});break}}function j(p){return p.attr("maxlength")||p.attr("size")}return this.each(function(){var p=a(this),q=j(p),r=a("<span></span>").css({display:"none",position:"absolute",whiteSpace:"nowrap",zIndex:999}).html(l(q,"0"));if(p.is("textarea")){p.data("maxlenghtsizex",p.outerWidth());p.data("maxlenghtsizey",p.outerHeight());p.mouseup(function(){if(p.outerWidth()!==p.data("maxlenghtsizex")||p.outerHeight()!==p.data("maxlenghtsizey")){b(p,r)}p.data("maxlenghtsizex",p.outerWidth());p.data("maxlenghtsizey",p.outerHeight())})}h.append(r);p.focus(function(){var s=i(p,j(p));e(s,p,q,r);b(p,r)});a(window).resize(function(){b(p,r)});p.blur(function(){r.css("display","none")});p.keyup(function(){var t=i(p,j(p)),s=true;if(o.validate&&t<0){s=false}else{e(t,p,q,r)}return s})})}})}(jQuery));