summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/system/js/system.date.js
blob: e742395ea2399fcf72dfdeb6a45bc7f060476884 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* DO NOT EDIT THIS FILE.
* See the following change record for more information,
* https://www.drupal.org/node/2815083
* @preserve
**/

(function ($, Drupal, drupalSettings) {
  const dateFormats = drupalSettings.dateFormats;
  Drupal.behaviors.dateFormat = {
    attach(context) {
      const source = once('dateFormat', '[data-drupal-date-formatter="source"]', context);
      const target = once('dateFormat', '[data-drupal-date-formatter="preview"]', context);

      if (!source.length || !target.length) {
        return;
      }

      const $target = $(target);
      const $preview = $target.find('em');

      function dateFormatHandler(e) {
        const baseValue = e.target.value || '';
        const dateString = baseValue.replace(/\\?(.?)/gi, (key, value) => dateFormats[key] ? dateFormats[key] : value);
        $preview.text(dateString);
        $target.toggleClass('js-hide', !dateString.length);
      }

      $(source).on('keyup.dateFormat change.dateFormat input.dateFormat', dateFormatHandler).trigger('keyup');
    }

  };
})(jQuery, Drupal, drupalSettings);