summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/system/js/system.date.js
blob: 8de2e65a5e872540f634a6fbf4de06b18b7b9b1f (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) {
  var dateFormats = drupalSettings.dateFormats;
  Drupal.behaviors.dateFormat = {
    attach: function attach(context) {
      var $context = $(context);
      var $source = $context.find('[data-drupal-date-formatter="source"]').once('dateFormat');
      var $target = $context.find('[data-drupal-date-formatter="preview"]').once('dateFormat');
      var $preview = $target.find('em');

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

      function dateFormatHandler(e) {
        var baseValue = $(e.target).val() || '';
        var dateString = baseValue.replace(/\\?(.?)/gi, function (key, value) {
          return 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);