Drupal investigation

system.date.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * @file
  3. * Provides date format preview feature.
  4. */
  5. (function ($, Drupal, drupalSettings) {
  6. 'use strict';
  7. var dateFormats = drupalSettings.dateFormats;
  8. /**
  9. * Display the preview for date format entered.
  10. *
  11. * @type {Drupal~behavior}
  12. *
  13. * @prop {Drupal~behaviorAttach} attach
  14. * Attach behavior for previewing date formats on input elements.
  15. */
  16. Drupal.behaviors.dateFormat = {
  17. attach: function (context) {
  18. var $context = $(context);
  19. var $source = $context.find('[data-drupal-date-formatter="source"]').once('dateFormat');
  20. var $target = $context.find('[data-drupal-date-formatter="preview"]').once('dateFormat');
  21. var $preview = $target.find('em');
  22. // All elements have to exist.
  23. if (!$source.length || !$target.length) {
  24. return;
  25. }
  26. /**
  27. * Event handler that replaces date characters with value.
  28. *
  29. * @param {jQuery.Event} e
  30. * The jQuery event triggered.
  31. */
  32. function dateFormatHandler(e) {
  33. var baseValue = $(e.target).val() || '';
  34. var dateString = baseValue.replace(/\\?(.?)/gi, function (key, value) {
  35. return dateFormats[key] ? dateFormats[key] : value;
  36. });
  37. $preview.html(dateString);
  38. $target.toggleClass('js-hide', !dateString.length);
  39. }
  40. /**
  41. * On given event triggers the date character replacement.
  42. */
  43. $source.on('keyup.dateFormat change.dateFormat input.dateFormat', dateFormatHandler)
  44. // Initialize preview.
  45. .trigger('keyup');
  46. }
  47. };
  48. })(jQuery, Drupal, drupalSettings);