Drupal investigation

form.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @file
  3. * Form features.
  4. */
  5. /**
  6. * Triggers when a value in the form changed.
  7. *
  8. * The event triggers when content is typed or pasted in a text field, before
  9. * the change event triggers.
  10. *
  11. * @event formUpdated
  12. */
  13. (function ($, Drupal, debounce) {
  14. 'use strict';
  15. /**
  16. * Retrieves the summary for the first element.
  17. *
  18. * @return {string}
  19. * The text of the summary.
  20. */
  21. $.fn.drupalGetSummary = function () {
  22. var callback = this.data('summaryCallback');
  23. return (this[0] && callback) ? $.trim(callback(this[0])) : '';
  24. };
  25. /**
  26. * Sets the summary for all matched elements.
  27. *
  28. * @param {function} callback
  29. * Either a function that will be called each time the summary is
  30. * retrieved or a string (which is returned each time).
  31. *
  32. * @return {jQuery}
  33. * jQuery collection of the current element.
  34. *
  35. * @fires event:summaryUpdated
  36. *
  37. * @listens event:formUpdated
  38. */
  39. $.fn.drupalSetSummary = function (callback) {
  40. var self = this;
  41. // To facilitate things, the callback should always be a function. If it's
  42. // not, we wrap it into an anonymous function which just returns the value.
  43. if (typeof callback !== 'function') {
  44. var val = callback;
  45. callback = function () { return val; };
  46. }
  47. return this
  48. .data('summaryCallback', callback)
  49. // To prevent duplicate events, the handlers are first removed and then
  50. // (re-)added.
  51. .off('formUpdated.summary')
  52. .on('formUpdated.summary', function () {
  53. self.trigger('summaryUpdated');
  54. })
  55. // The actual summaryUpdated handler doesn't fire when the callback is
  56. // changed, so we have to do this manually.
  57. .trigger('summaryUpdated');
  58. };
  59. /**
  60. * Prevents consecutive form submissions of identical form values.
  61. *
  62. * Repetitive form submissions that would submit the identical form values
  63. * are prevented, unless the form values are different to the previously
  64. * submitted values.
  65. *
  66. * This is a simplified re-implementation of a user-agent behavior that
  67. * should be natively supported by major web browsers, but at this time, only
  68. * Firefox has a built-in protection.
  69. *
  70. * A form value-based approach ensures that the constraint is triggered for
  71. * consecutive, identical form submissions only. Compared to that, a form
  72. * button-based approach would (1) rely on [visible] buttons to exist where
  73. * technically not required and (2) require more complex state management if
  74. * there are multiple buttons in a form.
  75. *
  76. * This implementation is based on form-level submit events only and relies
  77. * on jQuery's serialize() method to determine submitted form values. As such,
  78. * the following limitations exist:
  79. *
  80. * - Event handlers on form buttons that preventDefault() do not receive a
  81. * double-submit protection. That is deemed to be fine, since such button
  82. * events typically trigger reversible client-side or server-side
  83. * operations that are local to the context of a form only.
  84. * - Changed values in advanced form controls, such as file inputs, are not
  85. * part of the form values being compared between consecutive form submits
  86. * (due to limitations of jQuery.serialize()). That is deemed to be
  87. * acceptable, because if the user forgot to attach a file, then the size of
  88. * HTTP payload will most likely be small enough to be fully passed to the
  89. * server endpoint within (milli)seconds. If a user mistakenly attached a
  90. * wrong file and is technically versed enough to cancel the form submission
  91. * (and HTTP payload) in order to attach a different file, then that
  92. * edge-case is not supported here.
  93. *
  94. * Lastly, all forms submitted via HTTP GET are idempotent by definition of
  95. * HTTP standards, so excluded in this implementation.
  96. *
  97. * @type {Drupal~behavior}
  98. */
  99. Drupal.behaviors.formSingleSubmit = {
  100. attach: function () {
  101. function onFormSubmit(e) {
  102. var $form = $(e.currentTarget);
  103. var formValues = $form.serialize();
  104. var previousValues = $form.attr('data-drupal-form-submit-last');
  105. if (previousValues === formValues) {
  106. e.preventDefault();
  107. }
  108. else {
  109. $form.attr('data-drupal-form-submit-last', formValues);
  110. }
  111. }
  112. $('body').once('form-single-submit')
  113. .on('submit.singleSubmit', 'form:not([method~="GET"])', onFormSubmit);
  114. }
  115. };
  116. /**
  117. * Sends a 'formUpdated' event each time a form element is modified.
  118. *
  119. * @param {HTMLElement} element
  120. * The element to trigger a form updated event on.
  121. *
  122. * @fires event:formUpdated
  123. */
  124. function triggerFormUpdated(element) {
  125. $(element).trigger('formUpdated');
  126. }
  127. /**
  128. * Collects the IDs of all form fields in the given form.
  129. *
  130. * @param {HTMLFormElement} form
  131. * The form element to search.
  132. *
  133. * @return {Array}
  134. * Array of IDs for form fields.
  135. */
  136. function fieldsList(form) {
  137. var $fieldList = $(form).find('[name]').map(function (index, element) {
  138. // We use id to avoid name duplicates on radio fields and filter out
  139. // elements with a name but no id.
  140. return element.getAttribute('id');
  141. });
  142. // Return a true array.
  143. return $.makeArray($fieldList);
  144. }
  145. /**
  146. * Triggers the 'formUpdated' event on form elements when they are modified.
  147. *
  148. * @type {Drupal~behavior}
  149. *
  150. * @prop {Drupal~behaviorAttach} attach
  151. * Attaches formUpdated behaviors.
  152. * @prop {Drupal~behaviorDetach} detach
  153. * Detaches formUpdated behaviors.
  154. *
  155. * @fires event:formUpdated
  156. */
  157. Drupal.behaviors.formUpdated = {
  158. attach: function (context) {
  159. var $context = $(context);
  160. var contextIsForm = $context.is('form');
  161. var $forms = (contextIsForm ? $context : $context.find('form')).once('form-updated');
  162. var formFields;
  163. if ($forms.length) {
  164. // Initialize form behaviors, use $.makeArray to be able to use native
  165. // forEach array method and have the callback parameters in the right
  166. // order.
  167. $.makeArray($forms).forEach(function (form) {
  168. var events = 'change.formUpdated input.formUpdated ';
  169. var eventHandler = debounce(function (event) { triggerFormUpdated(event.target); }, 300);
  170. formFields = fieldsList(form).join(',');
  171. form.setAttribute('data-drupal-form-fields', formFields);
  172. $(form).on(events, eventHandler);
  173. });
  174. }
  175. // On ajax requests context is the form element.
  176. if (contextIsForm) {
  177. formFields = fieldsList(context).join(',');
  178. // @todo replace with form.getAttribute() when #1979468 is in.
  179. var currentFields = $(context).attr('data-drupal-form-fields');
  180. // If there has been a change in the fields or their order, trigger
  181. // formUpdated.
  182. if (formFields !== currentFields) {
  183. triggerFormUpdated(context);
  184. }
  185. }
  186. },
  187. detach: function (context, settings, trigger) {
  188. var $context = $(context);
  189. var contextIsForm = $context.is('form');
  190. if (trigger === 'unload') {
  191. var $forms = (contextIsForm ? $context : $context.find('form')).removeOnce('form-updated');
  192. if ($forms.length) {
  193. $.makeArray($forms).forEach(function (form) {
  194. form.removeAttribute('data-drupal-form-fields');
  195. $(form).off('.formUpdated');
  196. });
  197. }
  198. }
  199. }
  200. };
  201. /**
  202. * Prepopulate form fields with information from the visitor browser.
  203. *
  204. * @type {Drupal~behavior}
  205. *
  206. * @prop {Drupal~behaviorAttach} attach
  207. * Attaches the behavior for filling user info from browser.
  208. */
  209. Drupal.behaviors.fillUserInfoFromBrowser = {
  210. attach: function (context, settings) {
  211. var userInfo = ['name', 'mail', 'homepage'];
  212. var $forms = $('[data-user-info-from-browser]').once('user-info-from-browser');
  213. if ($forms.length) {
  214. userInfo.map(function (info) {
  215. var $element = $forms.find('[name=' + info + ']');
  216. var browserData = localStorage.getItem('Drupal.visitor.' + info);
  217. var emptyOrDefault = ($element.val() === '' || ($element.attr('data-drupal-default-value') === $element.val()));
  218. if ($element.length && emptyOrDefault && browserData) {
  219. $element.val(browserData);
  220. }
  221. });
  222. }
  223. $forms.on('submit', function () {
  224. userInfo.map(function (info) {
  225. var $element = $forms.find('[name=' + info + ']');
  226. if ($element.length) {
  227. localStorage.setItem('Drupal.visitor.' + info, $element.val());
  228. }
  229. });
  230. });
  231. }
  232. };
  233. })(jQuery, Drupal, Drupal.debounce);