diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 4dba53e..0028676 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -735,9 +735,43 @@ } $(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. + var element_tree = $(this.element).parents().toArray(); + element_tree.unshift(this.element); + + var changed_focus = 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') { + changed_focus = true; + } + } + } + + // If the focus han'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 (!changed_focus) { + var n = 0; + var m = element_tree.length; + var target; + var element; + + do { + element = element_tree[n]; + if (element.id) { + target = document.getElementById(element.id); + } + if (!target && element.name) { + target = document.getElementsByName(element.name)[0]; + } + } + while (!target && (n++ < m)); + + if (target) { + $(target).focus(); } }