﻿/* http://keith-wood.name/maxlength.html
Textarea Max Length for jQuery v1.0.2.
Written by Keith Wood (kwood{at}iinet.com.au) May 2009.
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 
MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 
Please attribute the author if you use it. */
(function ($) { var f = 'maxlength'; function MaxLength() { this._defaults = { max: 200, truncate: true, showFeedback: true, feedbackTarget: null, feedbackText: '{r} characters remaining ({m} maximum)', overflowText: '{o} characters too many ({m} maximum)'} } $.extend(MaxLength.prototype, { markerClassName: 'hasMaxLength', _feedbackClass: 'maxlength-feedback', _fullClass: 'maxlength-full', _overflowClass: 'maxlength-overflow', setDefaults: function (a) { $.extend(this._defaults, a || {}); return this }, _attachMaxLength: function (c, d) { c = $(c); if (c.hasClass(this.markerClassName)) { return } c.addClass(this.markerClassName).bind('keypress.maxlength', function (a) { if (!e.settings.truncate) { return true } var b = String.fromCharCode(a.charCode == undefined ? a.keyCode : a.charCode); return (a.ctrlKey || a.metaKey || b == '\u0000' || $(this).val().length < e.settings.max) }).bind('keyup.maxlength', function () { $.maxlength._checkLength($(this)) }); var e = { settings: $.extend({}, this._defaults), feedbackTarget: $([]) }; $.data(c[0], f, e); this._changeMaxLength(c, d) }, _changeMaxLength: function (a, b, c) { a = $(a); if (!a.hasClass(this.markerClassName)) { return } b = b || {}; if (typeof b == 'string') { var d = b; b = {}; b[d] = c } var e = $.data(a[0], f); $.extend(e.settings, b); if (e.feedbackTarget.length > 0) { if (e.hadFeedbackTarget) { e.feedbackTarget.empty().val('').removeClass(this._feedbackClass + ' ' + this._fullClass + ' ' + this._overflowClass) } else { e.feedbackTarget.remove() } e.feedbackTarget = $([]) } if (e.settings.showFeedback) { e.hadFeedbackTarget = !!e.feedbackTarget; if ($.isFunction(e.settings.feedbackTarget)) { e.feedbackTarget = e.settings.feedbackTarget.apply(a[0], []) } else if (e.settings.feedbackTarget) { e.feedbackTarget = $(e.settings.feedbackTarget) } else { e.feedbackTarget = $('<span></span>').insertAfter(a) } e.feedbackTarget.addClass(this._feedbackClass) } a.unbind('mouseover.maxlength focus.maxlength mouseout.maxlength blur.maxlength'); if (e.settings.showFeedback == 'active') { a.bind('mouseover.maxlength', function () { e.feedbackTarget.css('visibility', 'visible') }).bind('mouseout.maxlength', function () { if (!e.focussed) { e.feedbackTarget.css('visibility', 'hidden') } }).bind('focus.maxlength', function () { e.focussed = true; e.feedbackTarget.css('visibility', 'visible') }).bind('blur.maxlength', function () { e.focussed = false; e.feedbackTarget.css('visibility', 'hidden') }); e.feedbackTarget.css('visibility', 'hidden') } this._checkLength(a) }, _checkLength: function (a) { var b = $.data(a[0], f); var c = a.val(); a.toggleClass(this._fullClass, c.length >= b.settings.max).toggleClass(this._overflowClass, c.length > b.settings.max); if (c.length > b.settings.max && b.settings.truncate) { c = c.substring(0, b.settings.max); a.val(c) } var d = (c.length > b.settings.max ? b.settings.overflowText : b.settings.feedbackText).replace(/\{c\}/, c.length).replace(/\{m\}/, b.settings.max).replace(/\{r\}/, b.settings.max - c.length).replace(/\{o\}/, c.length - b.settings.max); b.feedbackTarget.toggleClass(this._fullClass, c.length >= b.settings.max).toggleClass(this._overflowClass, c.length > b.settings.max).text(d).val(d) }, _destroyMaxLength: function (a) { a = $(a); if (!a.hasClass(this.markerClassName)) { return } var b = $.data(a[0], f); if (b.feedbackTarget.length > 0) { if (b.hadFeedbackTarget) { b.feedbackTarget.empty().val('').css('visibility', 'visible').removeClass(this._feedbackClass + ' ' + this._fullClass + ' ' + this._overflowClass) } else { b.feedbackTarget.remove() } } a.removeClass(this.markerClassName).unbind('.maxlength'); $.removeData(a[0], f) }, _settingsMaxLength: function (a) { var b = $.data(a, f); return (b || {}).settings } }); var g = ['settings']; $.fn.maxlength = function (a) { var b = Array.prototype.slice.call(arguments, 1); if ($.inArray(a, g) > -1) { return $.maxlength['_' + a + 'MaxLength'].apply($.maxlength, [this[0]].concat(b)) } return this.each(function () { if (typeof a == 'string') { if (!$.maxlength['_' + a + 'MaxLength']) { throw 'Unknown command: ' + a; } $.maxlength['_' + a + 'MaxLength'].apply($.maxlength, [this].concat(b)) } else { $.maxlength._attachMaxLength(this, a || {}) } }) }; $.maxlength = new MaxLength() })(jQuery);
