diff --git a/core/misc/form.js b/core/misc/form.js index c0c5f08..766b159 100644 --- a/core/misc/form.js +++ b/core/misc/form.js @@ -1,43 +1,21 @@ -(function ($) { +(function ($, Drupal, debounce) { "use strict"; -/** - * Retrieves the summary for the first element. - */ -$.fn.drupalGetSummary = function () { - var callback = this.data('summaryCallback'); - return (this[0] && callback) ? $.trim(callback(this[0])) : ''; -}; - -/** - * Sets the summary for all matched elements. - * - * @param callback - * Either a function that will be called each time the summary is - * retrieved or a string (which is returned each time). - */ -$.fn.drupalSetSummary = function (callback) { - var self = this; - - // To facilitate things, the callback should always be a function. If it's - // not, we wrap it into an anonymous function which just returns the value. - if (typeof callback !== 'function') { - var val = callback; - callback = function () { return val; }; +Drupal.form = { + serialize: function (form) { + return $(form).serialize(); + }, + changed: function (form) { + var $form = $(form); + var previousValues = $form.attr('data-drupal-form-values'); + var currentValues = Drupal.form.serialize(form); + $form.attr('data-drupal-form-values', currentValues); + if (!previousValues) { + return false; + } + return previousValues !== currentValues; } - - return this - .data('summaryCallback', callback) - // To prevent duplicate events, the handlers are first removed and then - // (re-)added. - .off('formUpdated.summary') - .on('formUpdated.summary', function () { - self.trigger('summaryUpdated'); - }) - // The actual summaryUpdated handler doesn't fire when the callback is - // changed, so we have to do this manually. - .trigger('summaryUpdated'); }; /** @@ -45,17 +23,19 @@ $.fn.drupalSetSummary = function (callback) { */ Drupal.behaviors.formUpdated = { attach: function (context) { - // These events are namespaced so that we can remove them later. - var events = 'change.formUpdated click.formUpdated blur.formUpdated keyup.formUpdated'; - $(context) - // Since context could be an input element itself, it's added back to - // the jQuery object and filtered again. - .find(':input').addBack().filter(':input') - // To prevent duplicate events, the handlers are first removed and then - // (re-)added. - .off(events).on(events, function () { - $(this).trigger('formUpdated'); - }); + // Binds to forms and not the whole page to avoid too much messing around. + + function checkFormUpdated (e) { + var $form = $(e.currentTarget); + if (Drupal.form.changed(e.currentTarget)) { + $form.trigger('formUpdated'); + } + } + + var events = 'change.formUpdated keyup.formUpdated load.formUpdated'; + + $('body').once('form-updated').on(events, 'form', debounce(checkFormUpdated, 500)); + } }; @@ -99,15 +79,17 @@ Drupal.behaviors.formUpdated = { */ Drupal.behaviors.formSingleSubmit = { attach: function (context) { - function onFormSubmit(e) { + + function onFormSubmit (e) { var $form = $(e.currentTarget); - var formValues = $form.serialize(); + var currentValues = Drupal.form.serialize(e.currentTarget); var previousValues = $form.attr('data-drupal-form-submit-last'); - if (previousValues === formValues) { + + if (currentValues === previousValues) { e.preventDefault(); } else { - $form.attr('data-drupal-form-submit-last', formValues); + $form.attr('data-drupal-form-submit-last', currentValues); } } @@ -115,24 +97,4 @@ Drupal.behaviors.formSingleSubmit = { } }; -/** - * Prepopulate form fields with information from the visitor cookie. - */ -Drupal.behaviors.fillUserInfoFromCookie = { - attach: function (context, settings) { - var userInfo = ['name', 'mail', 'homepage']; - $('form.user-info-from-cookie').once('user-info-from-cookie', function () { - var $formContext = $(this); - var i, il, $element, cookie; - for (i = 0, il = userInfo.length; i < il; i += 1) { - $element = $formContext.find('[name=' + userInfo[i] + ']'); - cookie = $.cookie('Drupal.visitor.' + userInfo[i]); - if ($element.length && cookie) { - $element.val(cookie); - } - } - }); - } -}; - -})(jQuery); +})(jQuery, Drupal, Drupal.debounce); diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 9500adf..10c97d4 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -973,11 +973,12 @@ function system_library_info() { 'version' => \Drupal::VERSION, 'js' => array( 'core/misc/form.js' => array('group' => JS_LIBRARY, 'weight' => 1), + 'core/misc/form.summary.js' => array('group' => JS_LIBRARY, 'weight' => 1), ), 'dependencies' => array( array('system', 'jquery'), array('system', 'drupal'), - array('system', 'jquery.cookie'), + array('system', 'drupal.debounce'), array('system', 'jquery.once'), ), ); diff --git a/core/modules/user/user.module b/core/modules/user/user.module index 6744e9e..d95d088 100644 --- a/core/modules/user/user.module +++ b/core/modules/user/user.module @@ -1925,6 +1925,19 @@ function user_library_info() { ), ); + $libraries['drupal.user.info'] = array( + 'title' => 'User', + 'version' => \Drupal::VERSION, + 'js' => array( + $path . '/user.infos.js' => array(), + ), + 'dependencies' => array( + array('system', 'jquery'), + array('system', 'drupal'), + array('system', 'jquery.cookie'), + ), + ); + $libraries['drupal.user.permissions'] = array( 'title' => 'User permissions', 'version' => \Drupal::VERSION,