src/form/remaining-chars-count/remaining-chars-count.directive.js
angular.module('patternfly.form').directive('pfRemainingCharsCount', function ($timeout) {
'use strict';
return {
restrict: 'A',
require: 'ngModel',
scope: {
ngModel: "="
},
link: function ($scope, $element, $attributes) {
var charsMaxLimit = $attributes.charsMaxLimit;
var charsWarnRemaining = $attributes.charsWarnRemaining;
var countRemainingFld = angular.element(document.getElementById($attributes.countFld));
var blockInputAtMaxLimit = ($attributes.blockInputAtMaxLimit === 'true');
var checkCharactersRemaining = function () {
var charsLength = $scope.ngModel.length;
var remainingChars = charsMaxLimit - charsLength;
// trim if blockInputAtMaxLimit and over limit
if (blockInputAtMaxLimit && charsLength > charsMaxLimit) {
$scope.ngModel = $scope.ngModel.substring(0, charsMaxLimit);
charsLength = $scope.ngModel.length;
remainingChars = charsMaxLimit - charsLength;
}
// creating scope vars for unit testing
$scope.remainingChars = remainingChars;
$scope.remainingCharsWarning = (remainingChars <= charsWarnRemaining);
countRemainingFld.text(remainingChars);
countRemainingFld.toggleClass('chars-warn-remaining-pf', remainingChars <= charsWarnRemaining);
if (remainingChars < 0) {
$scope.$emit('overCharsMaxLimit', $attributes.id);
} else {
$scope.$emit('underCharsMaxLimit', $attributes.id);
}
};
$scope.$watch('ngModel', function () {
checkCharactersRemaining();
});
$element.on('keypress', function (event) {
// Once the charsMaxLimit has been met or exceeded, prevent all keypresses from working
if (blockInputAtMaxLimit && $element.val().length >= charsMaxLimit) {
// Except backspace
if (event.keyCode !== 8) {
event.preventDefault();
}
}
});
}
};
});