1 line
4.7 KiB
JavaScript
1 line
4.7 KiB
JavaScript
!function(a){"use strict";a.event.special.destroyed||(a.event.special.destroyed={remove:function(a){a.handler&&a.handler()}}),a.fn.extend({maxlength:function(b,c){function f(a){var c=a.val();c=b.twoCharLinebreak?c.replace(/\r(?!\n)|\n(?!\r)/g,"\r\n"):c.replace(new RegExp("\r?\n","g"),"\n");var d=0;return d=b.utf8?h(c):c.length}function g(a,c){var d=a.val(),e=0;b.twoCharLinebreak&&(d=d.replace(/\r(?!\n)|\n(?!\r)/g,"\r\n"),"\n"===d.substr(d.length-1)&&d.length%2===1&&(e=1)),a.val(d.substr(0,c-e))}function h(a){for(var b=0,c=0;c<a.length;c++){var d=a.charCodeAt(c);128>d?b++:b+=d>127&&2048>d?2:3}return b}function i(a,c,d){var e=!0;return!b.alwaysShow&&d-f(a)>c&&(e=!1),e}function j(a,b){var c=b-f(a);return c}function k(a,b){b.css({display:"block"}),a.trigger("maxlength.shown")}function l(a,c){b.alwaysShow||(c.css({display:"none"}),a.trigger("maxlength.hidden"))}function m(a,c,d){var e="";return b.message?e="function"==typeof b.message?b.message(a,c):b.message.replace("%charsTyped%",d).replace("%charsRemaining%",c-d).replace("%charsTotal%",c):(b.preText&&(e+=b.preText),e+=b.showCharsTyped?d:c-d,b.showMaxLength&&(e+=b.separator+c),b.postText&&(e+=b.postText)),e}function n(a,c,d,e){e&&(e.html(m(c.val(),d,d-a)),a>0?i(c,b.threshold,d)?k(c,e.removeClass(b.limitReachedClass).addClass(b.warningClass)):l(c,e):k(c,e.removeClass(b.warningClass).addClass(b.limitReachedClass))),b.customMaxAttribute&&(0>a?c.addClass("overmax"):c.removeClass("overmax"))}function o(b){var c=b[0];return a.extend({},"function"==typeof c.getBoundingClientRect?c.getBoundingClientRect():{width:c.offsetWidth,height:c.offsetHeight},b.offset())}function p(c,d){var e=o(c);if("function"===a.type(b.placement))return void b.placement(c,d,e);if(a.isPlainObject(b.placement))return void q(b.placement,d);var f=c.outerWidth(),g=d.outerWidth(),h=d.width(),i=d.height();switch(b.appendToParent&&(e.top-=c.parent().offset().top,e.left-=c.parent().offset().left),b.placement){case"bottom":d.css({top:e.top+e.height,left:e.left+e.width/2-h/2});break;case"top":d.css({top:e.top-i,left:e.left+e.width/2-h/2});break;case"left":d.css({top:e.top+e.height/2-i/2,left:e.left-h});break;case"right":d.css({top:e.top+e.height/2-i/2,left:e.left+e.width});break;case"bottom-right":d.css({top:e.top+e.height,left:e.left+e.width});break;case"top-right":d.css({top:e.top-i,left:e.left+f});break;case"top-left":d.css({top:e.top-i,left:e.left-g});break;case"bottom-left":d.css({top:e.top+c.outerHeight(),left:e.left-g});break;case"centered-right":d.css({top:e.top+i/2,left:e.left+f-g-3});break;case"bottom-right-inside":d.css({top:e.top+e.height,left:e.left+e.width-g});break;case"top-right-inside":d.css({top:e.top-i,left:e.left+f-g});break;case"top-left-inside":d.css({top:e.top-i,left:e.left});break;case"bottom-left-inside":d.css({top:e.top+c.outerHeight(),left:e.left})}}function q(c,d){if(c&&d){var e=["top","bottom","left","right","position"],f={};a.each(e,function(a,c){var d=b.placement[c];"undefined"!=typeof d&&(f[c]=d)}),d.css(f)}}function r(){return"bottom-right-inside"===b.placement||"top-right-inside"===b.placement||"function"==typeof b.placement||b.message&&"function"==typeof b.message}function s(a){var c=a.attr("maxlength")||b.customMaxAttribute;if(b.customMaxAttribute&&!b.allowOverMax){var d=a.attr(b.customMaxAttribute);(!c||c>d)&&(c=d)}return c||(c=a.attr("size")),c}var d=a("body"),e={showOnReady:!1,alwaysShow:!1,threshold:10,warningClass:"label label-success",limitReachedClass:"label label-important label-danger",separator:" / ",preText:"",postText:"",showMaxLength:!0,placement:"bottom",message:null,showCharsTyped:!0,validate:!1,utf8:!1,appendToParent:!1,twoCharLinebreak:!0,customMaxAttribute:null,allowOverMax:!1};return a.isFunction(b)&&!c&&(c=b,b={}),b=a.extend(e,b),this.each(function(){function h(){var g=m(c.val(),e,"0");e=s(c),f||(f=a('<span class="bootstrap-maxlength"></span>').css({display:"none",position:"absolute",whiteSpace:"nowrap",zIndex:1099}).html(g)),c.is("textarea")&&(c.data("maxlenghtsizex",c.outerWidth()),c.data("maxlenghtsizey",c.outerHeight()),c.mouseup(function(){(c.outerWidth()!==c.data("maxlenghtsizex")||c.outerHeight()!==c.data("maxlenghtsizey"))&&p(c,f),c.data("maxlenghtsizex",c.outerWidth()),c.data("maxlenghtsizey",c.outerHeight())})),b.appendToParent?(c.parent().append(f),c.parent().css("position","relative")):d.append(f);var h=j(c,s(c));n(h,c,e,f),p(c,f)}var e,f,c=a(this);a(window).resize(function(){f&&p(c,f)}),b.showOnReady?c.ready(function(){h()}):c.focus(function(){h()}),c.on("maxlength.reposition",function(){p(c,f)}),c.on("destroyed",function(){f&&f.remove()}),c.on("blur",function(){!f||b.showOnReady||b.alwaysShow||f.remove()}),c.on("input",function(){var a=s(c),d=j(c,a),h=!0;return b.validate&&0>d?(g(c,a),h=!1):n(d,c,e,f),r()&&p(c,f),h})})}})}(jQuery); |