This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
drupalcampbristol/web/modules/contrib/webform/js/webform.element.counter.js
2018-11-23 12:29:20 +00:00

60 lines
1.9 KiB
JavaScript

/**
* @file
* JavaScript behaviors for jQuery Text Counter integration.
*/
(function ($, Drupal) {
'use strict';
// @see https://github.com/ractoon/jQuery-Text-Counter#options
Drupal.webform = Drupal.webform || {};
Drupal.webform.counter = Drupal.webform.counter || {};
Drupal.webform.counter.options = Drupal.webform.counter.options || {};
/**
* Initialize text field and textarea word and character counter.
*
* @type {Drupal~behavior}
*/
Drupal.behaviors.webformCounter = {
attach: function (context) {
if (!$.fn.textcounter) {
return;
}
$(context).find('.js-webform-counter').once('webform-counter').each(function () {
var options = {
type: $(this).data('counter-type'),
max: $(this).data('counter-maximum'),
min: $(this).data('counter-minimum') || 0,
counterText: $(this).data('counter-minimum-message'),
countDownText: $(this).data('counter-maximum-message'),
inputErrorClass: 'webform-counter-warning',
counterErrorClass: 'webform-counter-warning',
countSpaces: true,
stopInputAtMaximum: false,
// Don't display min/max message since server-side validation will
// display these messages.
minimumErrorText: '',
maximumErrorText: ''
};
options.countDown = (options.max) ? true : false;
if (!options.counterText) {
options.counterText = (options.type === 'word') ? Drupal.t('%d word(s) entered') : Drupal.t('%d characters(s) entered');
}
if (!options.countDownText) {
options.countDownText = (options.type === 'word') ? Drupal.t('%d word(s) remaining') : Drupal.t('%d characters(s) remaining');
}
options = $.extend(options, Drupal.webform.counter.options);
$(this).textcounter(options);
});
}
};
})(jQuery, Drupal);