luci-base: ui.js: modal dialog tweaks
authorJo-Philipp Wich <jo@mein.io>
Sun, 5 Feb 2023 18:41:20 +0000 (19:41 +0100)
committerJo-Philipp Wich <jo@mein.io>
Mon, 3 Apr 2023 11:47:26 +0000 (13:47 +0200)
 - Automatically focus modal dialog frame
 - Close modal dialog on pressing escape key

Fixes: #4609, #6205
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit c9939a83e72beb32dd1e11b07139714e2ae91623)

modules/luci-base/htdocs/luci-static/resources/ui.js

index 450cb655c6792d9d0c040b7b37451758c71b15ea..51b6c0902de56536aa514d36a1a957138ddf6a68 100644 (file)
@@ -3486,8 +3486,17 @@ var UITable = baseclass.extend(/** @lends LuCI.ui.table.prototype */ {
 var UI = baseclass.extend(/** @lends LuCI.ui.prototype */ {
        __init__: function() {
                modalDiv = document.body.appendChild(
-                       dom.create('div', { id: 'modal_overlay' },
-                               dom.create('div', { class: 'modal', role: 'dialog', 'aria-modal': true })));
+                       dom.create('div', {
+                               id: 'modal_overlay',
+                               tabindex: -1,
+                               keydown: this.cancelModal
+                       }, [
+                               dom.create('div', {
+                                       class: 'modal',
+                                       role: 'dialog',
+                                       'aria-modal': true
+                               })
+                       ]));
 
                tooltipDiv = document.body.appendChild(
                        dom.create('div', { class: 'cbi-tooltip' }));
@@ -3551,6 +3560,7 @@ var UI = baseclass.extend(/** @lends LuCI.ui.prototype */ {
 
                document.body.classList.add('modal-overlay-active');
                modalDiv.scrollTop = 0;
+               modalDiv.focus();
 
                return dlg;
        },
@@ -3567,6 +3577,17 @@ var UI = baseclass.extend(/** @lends LuCI.ui.prototype */ {
         */
        hideModal: function() {
                document.body.classList.remove('modal-overlay-active');
+               modalDiv.blur();
+       },
+
+       /** @private */
+       cancelModal: function(ev) {
+               if (ev.key == 'Escape') {
+                       var btn = modalDiv.querySelector('.right > button, .right > .btn');
+
+                       if (btn)
+                               btn.click();
+               }
        },
 
        /** @private */