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.find('[data-drupal-date-formatter="source"]').once('dateFormat');
21 const $target = $context.find('[data-drupal-date-formatter="preview"]').once('dateFormat');
22 const $preview = $target.find('em');
24 // All elements have to exist.
25 if (!$source.length || !$target.length) {
30 * Event handler that replaces date characters with value.
32 * @param {jQuery.Event} e
33 * The jQuery event triggered.
35 function dateFormatHandler(e) {
36 const baseValue = $(e.target).val() || '';
37 const dateString = baseValue.replace(/\\?(.?)/gi, (key, value) => dateFormats[key] ? dateFormats[key] : value);
39 $preview.html(dateString);
40 $target.toggleClass('js-hide', !dateString.length);
44 * On given event triggers the date character replacement.
46 $source.on('keyup.dateFormat change.dateFormat input.dateFormat', dateFormatHandler)
47 // Initialize preview.
51 }(jQuery, Drupal, drupalSettings));