summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2015-11-12 16:06:34 (GMT)
committerAlex Pott2015-11-12 20:34:38 (GMT)
commit87ac540626363682b114735146689742cb0f4853 (patch)
tree81dd6650d4b21a2d7775f5dcaef26703d5b82ea2
parent7e3042e8166f6f8a57bd4e05d6f34ba0fbf9b01f (diff)
Issue #1824636 by DuaelFr, nod_, meenakshi.r, larowlan, mgifford, falcon03, hefox, alexdmccabe, Stuart Miller, Sam152: Do not move the cursor to the top of the page on ajax calls
-rw-r--r--core/lib/Drupal/Core/Render/Element/RenderElement.php5
-rw-r--r--core/misc/ajax.js27
-rw-r--r--core/modules/views_ui/src/ViewPreviewForm.php1
3 files changed, 33 insertions, 0 deletions
diff --git a/core/lib/Drupal/Core/Render/Element/RenderElement.php b/core/lib/Drupal/Core/Render/Element/RenderElement.php
index 47c8402..b6f95bc 100644
--- a/core/lib/Drupal/Core/Render/Element/RenderElement.php
+++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php
@@ -269,6 +269,11 @@ abstract class RenderElement extends PluginBase implements ElementInterface {
return $element;
}
+ // Add a data attribute to disable automatic refocus after ajax call.
+ if (!empty($element['#ajax']['disable-refocus'])) {
+ $element['#attributes']['data-disable-refocus'] = "true";
+ }
+
// Add a reasonable default event handler if none was specified.
if (isset($element['#ajax']) && !isset($element['#ajax']['event'])) {
switch ($element['#type']) {
diff --git a/core/misc/ajax.js b/core/misc/ajax.js
index 2a6616d..b63bc03 100644
--- a/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -735,9 +735,36 @@
}
$(this.element).prop('disabled', false);
+ // Save element's ancestors tree so if the element is removed from the dom
+ // we can try to refocus one of its parents. Using addBack reverse the
+ // result array, meaning that index 0 is the highest parent in the hierarchy
+ // in this situation it is usually a <form> element.
+ var elementParents = $(this.element).parents('[data-drupal-selector]').addBack().toArray();
+
+ // Track if any command is altering the focus so we can avoid changing the
+ // focus set by the Ajax command.
+ var focusChanged = false;
for (var i in response) {
if (response.hasOwnProperty(i) && response[i].command && this.commands[response[i].command]) {
this.commands[response[i].command](this, response[i], status);
+ if (response[i].command === 'invoke' && response[i].method === 'focus') {
+ focusChanged = true;
+ }
+ }
+ }
+
+ // If the focus hasn't be changed by the ajax commands, try to refocus the
+ // triggering element or one of its parents if that element does not exist
+ // anymore.
+ if (!focusChanged && this.element && !$(this.element).data('disable-refocus')) {
+ var target = false;
+
+ for (var n = elementParents.length - 1; !target && n > 0; n--) {
+ target = document.querySelector('[data-drupal-selector="' + elementParents[n].getAttribute('data-drupal-selector') + '"]');
+ }
+
+ if (target) {
+ $(target).trigger('focus');
}
}
diff --git a/core/modules/views_ui/src/ViewPreviewForm.php b/core/modules/views_ui/src/ViewPreviewForm.php
index 7ec8f4d..d87f593 100644
--- a/core/modules/views_ui/src/ViewPreviewForm.php
+++ b/core/modules/views_ui/src/ViewPreviewForm.php
@@ -94,6 +94,7 @@ class ViewPreviewForm extends ViewFormBase {
'event' => 'click',
'progress' => array('type' => 'fullscreen'),
'method' => 'replaceWith',
+ 'disable-refocus' => TRUE,
),
),
);