Archive: Namaste PHP AMQP framework v1.0 (2017-2020)
952 days continuous production uptime, 40k+ tp/s single node. Original corpo Bitbucket history not included — clean archive commit.
This commit is contained in:
55
html/js/modules (optional)/character-counter.js
Executable file
55
html/js/modules (optional)/character-counter.js
Executable file
@@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
/* CHARACTER COUNTER */
|
||||
|
||||
(function ($) {
|
||||
|
||||
$.fn.characterCounter = function () {
|
||||
return this.each(function () {
|
||||
|
||||
var itHasLengthAttribute = $(this).attr('length') !== undefined;
|
||||
|
||||
if (itHasLengthAttribute) {
|
||||
$(this).on('input', updateCounter);
|
||||
$(this).on('focus', updateCounter);
|
||||
$(this).on('blur', removeCounterElement);
|
||||
|
||||
addCounterElement($(this));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
function updateCounter() {
|
||||
var maxLength = Number($(this).attr('length'));
|
||||
var actualLength = Number($(this).val().length);
|
||||
var isValidLength = actualLength <= maxLength;
|
||||
|
||||
$(this).parent().find('span[class="character-counter"]').html(actualLength + '/' + maxLength);
|
||||
|
||||
addInputStyle(isValidLength, $(this));
|
||||
}
|
||||
|
||||
function addCounterElement($input) {
|
||||
var $counterElement = $('<span/>').addClass('character-counter').css('float', 'right').css('font-size', '12px').css('height', 1);
|
||||
|
||||
$input.parent().append($counterElement);
|
||||
}
|
||||
|
||||
function removeCounterElement() {
|
||||
$(this).parent().find('span[class="character-counter"]').html('');
|
||||
}
|
||||
|
||||
function addInputStyle(isValidLength, $input) {
|
||||
var inputHasInvalidClass = $input.hasClass('invalid');
|
||||
if (isValidLength && inputHasInvalidClass) {
|
||||
$input.removeClass('invalid');
|
||||
} else if (!isValidLength && !inputHasInvalidClass) {
|
||||
$input.removeClass('valid');
|
||||
$input.addClass('invalid');
|
||||
}
|
||||
}
|
||||
|
||||
$(document).ready(function () {
|
||||
$('input, textarea').characterCounter();
|
||||
});
|
||||
})(jQuery);
|
||||
Reference in New Issue
Block a user