3 * Provides date format preview feature.
6 (function($, Drupal, drupalSettings) {
7 const dateFormats = drupalSettings.dateFormats;
10 * Display the preview for date format entered.
12 * @type {Drupal~behavior}
14 * @prop {Drupal~behaviorAttach} attach
15 * Attach behavior for previewing date formats on input elements.
17 Drupal.behaviors.dateFormat = {
19 const $context = $(context);
20 const $source = $context
21 .find('[data-drupal-date-formatter="source"]')
23 const $target = $context
24 .find('[data-drupal-date-formatter="preview"]')
26 const $preview = $target.find('em');
28 // All elements have to exist.
29 if (!$source.length || !$target.length) {
34 * Event handler that replaces date characters with value.
36 * @param {jQuery.Event} e
37 * The jQuery event triggered.
39 function dateFormatHandler(e) {
40 const baseValue = $(e.target).val() || '';
41 const dateString = baseValue.replace(
43 (key, value) => (dateFormats[key] ? dateFormats[key] : value),
46 $preview.html(dateString);
47 $target.toggleClass('js-hide', !dateString.length);
51 * On given event triggers the date character replacement.
55 'keyup.dateFormat change.dateFormat input.dateFormat',
58 // Initialize preview.
62 })(jQuery, Drupal, drupalSettings);