var lastFocusedControlId = ""; function focusHandler(e) { document.activeElement = e.originalTarget; } function appInit() { if (typeof (window.addEventListener) !== "undefined") { window.addEventListener("focus", focusHandler, true); } Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler); Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler); } function pageLoadingHandler(sender, args) { lastFocusedControlId = typeof (document.activeElement) === "undefined" ? "" : document.activeElement.id; } function focusControl(targetControl) { if (Sys.Browser.agent === Sys.Browser.InternetExplorer) { var focusTarget = targetControl; if (focusTarget && (typeof (focusTarget.contentEditable) !== "undefined")) { oldContentEditableSetting = focusTarget.contentEditable; focusTarget.contentEditable = false; } else { focusTarget = null; } targetControl.focus(); if (focusTarget) { focusTarget.contentEditable = oldContentEditableSetting; } } else { targetControl.focus(); } } function pageLoadedHandler(sender, args) { if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") { var newFocused = $get(lastFocusedControlId); if (newFocused) { focusControl(newFocused); } } } Sys.Application.add_init(appInit); /* * FixFocus.js actualmente se carga antes de bootstrap.min.js. * * $(function () {}) se ejecuta cuando el DOM terminó de cargar; * para entonces bootstrap.min.js ya fue procesado. */ (function (window, document, $) { if (!$) { return; } function fixBootstrapModalSelect2Focus() { /* * FixFocus.js puede cargarse antes que bootstrap.min.js, * por eso primero verificamos que Bootstrap Modal ya exista. */ if ( !$.fn.modal || !$.fn.modal.Constructor ) { return false; } /* * Evita modificar el prototype más de una vez. */ if ($.fn.modal.Constructor.prototype._sigSelect2FocusFixed) { return true; } $.fn.modal.Constructor.prototype.enforceFocus = function () { var modal = this; $(document) .off('focusin.bs.modal') .on('focusin.bs.modal', function (e) { var $target = $(e.target); /* * El propio modal tiene el focus. */ if (modal.$element[0] === e.target) { return; } /* * El elemento está realmente dentro del modal. */ if (modal.$element.has(e.target).length > 0) { return; } /* * SELECT2 * * Aunque estos elementos pueden estar físicamente * colocados bajo , pertenecen funcionalmente * al Select2 abierto desde el modal. * * Bootstrap NO debe quitarles el focus. */ if ( $target.hasClass('select2-search__field') || $target.closest('.select2-container').length > 0 || $target.closest('.select2-dropdown').length > 0 || $target.closest('.select2-search').length > 0 || $target.closest('.select2-results').length > 0 ) { return; } /* * Para todo lo demás se conserva el comportamiento * normal de Bootstrap: el focus debe permanecer * dentro del modal. */ modal.$element.trigger('focus'); }); }; $.fn.modal.Constructor.prototype._sigSelect2FocusFixed = true; return true; } /* * FixFocus.js actualmente se carga antes de bootstrap.min.js. * * $(function () {}) se ejecuta cuando el DOM terminó de cargar; * para entonces bootstrap.min.js ya fue procesado. */ $(function () { if (fixBootstrapModalSelect2Focus()) { return; } /* * Respaldo por si algún layout carga Bootstrap todavía * más tarde. */ var intentos = 0; var interval = window.setInterval(function () { intentos++; if ( fixBootstrapModalSelect2Focus() || intentos >= 20 ) { window.clearInterval(interval); } }, 100); }); }(window, document, window.jQuery));