form.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal, debounce) {
  8. $.fn.drupalGetSummary = function () {
  9. var callback = this.data('summaryCallback');
  10. return this[0] && callback ? $.trim(callback(this[0])) : '';
  11. };
  12. $.fn.drupalSetSummary = function (callback) {
  13. var self = this;
  14. if (typeof callback !== 'function') {
  15. var val = callback;
  16. callback = function callback() {
  17. return val;
  18. };
  19. }
  20. return this.data('summaryCallback', callback).off('formUpdated.summary').on('formUpdated.summary', function () {
  21. self.trigger('summaryUpdated');
  22. }).trigger('summaryUpdated');
  23. };
  24. Drupal.behaviors.formSingleSubmit = {
  25. attach: function attach() {
  26. function onFormSubmit(e) {
  27. var $form = $(e.currentTarget);
  28. var formValues = $form.serialize();
  29. var previousValues = $form.attr('data-drupal-form-submit-last');
  30. if (previousValues === formValues) {
  31. e.preventDefault();
  32. } else {
  33. $form.attr('data-drupal-form-submit-last', formValues);
  34. }
  35. }
  36. $('body').once('form-single-submit').on('submit.singleSubmit', 'form:not([method~="GET"])', onFormSubmit);
  37. }
  38. };
  39. function triggerFormUpdated(element) {
  40. $(element).trigger('formUpdated');
  41. }
  42. function fieldsList(form) {
  43. var $fieldList = $(form).find('[name]').map(function (index, element) {
  44. return element.getAttribute('id');
  45. });
  46. return $.makeArray($fieldList);
  47. }
  48. Drupal.behaviors.formUpdated = {
  49. attach: function attach(context) {
  50. var $context = $(context);
  51. var contextIsForm = $context.is('form');
  52. var $forms = (contextIsForm ? $context : $context.find('form')).once('form-updated');
  53. var formFields = void 0;
  54. if ($forms.length) {
  55. $.makeArray($forms).forEach(function (form) {
  56. var events = 'change.formUpdated input.formUpdated ';
  57. var eventHandler = debounce(function (event) {
  58. triggerFormUpdated(event.target);
  59. }, 300);
  60. formFields = fieldsList(form).join(',');
  61. form.setAttribute('data-drupal-form-fields', formFields);
  62. $(form).on(events, eventHandler);
  63. });
  64. }
  65. if (contextIsForm) {
  66. formFields = fieldsList(context).join(',');
  67. var currentFields = $(context).attr('data-drupal-form-fields');
  68. if (formFields !== currentFields) {
  69. triggerFormUpdated(context);
  70. }
  71. }
  72. },
  73. detach: function detach(context, settings, trigger) {
  74. var $context = $(context);
  75. var contextIsForm = $context.is('form');
  76. if (trigger === 'unload') {
  77. var $forms = (contextIsForm ? $context : $context.find('form')).removeOnce('form-updated');
  78. if ($forms.length) {
  79. $.makeArray($forms).forEach(function (form) {
  80. form.removeAttribute('data-drupal-form-fields');
  81. $(form).off('.formUpdated');
  82. });
  83. }
  84. }
  85. }
  86. };
  87. Drupal.behaviors.fillUserInfoFromBrowser = {
  88. attach: function attach(context, settings) {
  89. var userInfo = ['name', 'mail', 'homepage'];
  90. var $forms = $('[data-user-info-from-browser]').once('user-info-from-browser');
  91. if ($forms.length) {
  92. userInfo.forEach(function (info) {
  93. var $element = $forms.find('[name=' + info + ']');
  94. var browserData = localStorage.getItem('Drupal.visitor.' + info);
  95. var emptyOrDefault = $element.val() === '' || $element.attr('data-drupal-default-value') === $element.val();
  96. if ($element.length && emptyOrDefault && browserData) {
  97. $element.val(browserData);
  98. }
  99. });
  100. }
  101. $forms.on('submit', function () {
  102. userInfo.forEach(function (info) {
  103. var $element = $forms.find('[name=' + info + ']');
  104. if ($element.length) {
  105. localStorage.setItem('Drupal.visitor.' + info, $element.val());
  106. }
  107. });
  108. });
  109. }
  110. };
  111. var handleFragmentLinkClickOrHashChange = function handleFragmentLinkClickOrHashChange(e) {
  112. var url = void 0;
  113. if (e.type === 'click') {
  114. url = e.currentTarget.location ? e.currentTarget.location : e.currentTarget;
  115. } else {
  116. url = window.location;
  117. }
  118. var hash = url.hash.substr(1);
  119. if (hash) {
  120. var $target = $('#' + hash);
  121. $('body').trigger('formFragmentLinkClickOrHashChange', [$target]);
  122. setTimeout(function () {
  123. return $target.trigger('focus');
  124. }, 300);
  125. }
  126. };
  127. var debouncedHandleFragmentLinkClickOrHashChange = debounce(handleFragmentLinkClickOrHashChange, 300, true);
  128. $(window).on('hashchange.form-fragment', debouncedHandleFragmentLinkClickOrHashChange);
  129. $(document).on('click.form-fragment', 'a[href*="#"]', debouncedHandleFragmentLinkClickOrHashChange);
  130. })(jQuery, Drupal, Drupal.debounce);