From 42c7ae372f7f4f26ba928926f22f1b9f872f15da Mon Sep 17 00:00:00 2001
From: Harshal Dhumal <harshal.dhumal@enterprisedb.com>
Date: Thu, 24 Jan 2019 17:15:12 +0530
Subject: [PATCH] Fixed dialog tab navigation for Backup and Restore dialog.
 Fixes regression of #3862

---
 .../static/js/alertify/dialog_wrapper.js       | 18 ++++++++++++++----
 web/pgadmin/static/js/utils.js                 |  2 +-
 .../backup/static/js/backup_dialog_wrapper.js  |  2 +-
 .../static/js/restore_dialog_wrapper.js        |  2 +-
 4 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/web/pgadmin/static/js/alertify/dialog_wrapper.js b/web/pgadmin/static/js/alertify/dialog_wrapper.js
index 75452f2bf..77663805d 100644
--- a/web/pgadmin/static/js/alertify/dialog_wrapper.js
+++ b/web/pgadmin/static/js/alertify/dialog_wrapper.js
@@ -8,6 +8,7 @@
 //////////////////////////////////////////////////////////////
 
 import * as commonUtils from '../utils';
+import $ from 'jquery';
 
 export class DialogWrapper {
   constructor(
@@ -53,11 +54,20 @@ export class DialogWrapper {
     return undefined;
   }
 
-  focusOnDialog(dialog) {
-    dialog.$el.attr('tabindex', -1);
-    this.pgBrowser.keyboardNavigation.getDialogTabNavigator(dialog);
-    const container = dialog.$el.find('.tab-content:first > .tab-pane.active:first');
+  focusOnDialog(alertifyDialog) {
+    let backform_tab = $(alertifyDialog.elements.body).find('.backform-tab');
+    backform_tab.attr('tabindex', -1);
+    this.pgBrowser.keyboardNavigation.getDialogTabNavigator($(alertifyDialog.elements.dialog));
+    const container = backform_tab.find('.tab-content:first > .tab-pane.active:first');
     commonUtils.findAndSetFocus(container);
+
+    $(alertifyDialog.elements.footer).on('keydown', 'button', function(event) {
+      if (event.keyCode == 9 && $(this).nextAll('button:not([disabled])').length == 0) {
+        // set focus back to first editable input element of current active tab once we cycle through all enabled buttons.
+        commonUtils.findAndSetFocus($(alertifyDialog.elements.body).find('.tab-content div.active'));
+        return false;
+      }
+    });
   }
 
   isNodeSelected(selectedTreeNode) {
diff --git a/web/pgadmin/static/js/utils.js b/web/pgadmin/static/js/utils.js
index 86727a19e..27b999f7a 100644
--- a/web/pgadmin/static/js/utils.js
+++ b/web/pgadmin/static/js/utils.js
@@ -28,7 +28,7 @@ export function findAndSetFocus(container) {
 
     if (first_el.length == 0) {
       first_el = container
-        .find('.pgadmin-controls:first>input:enabled,.CodeMirror-scroll');
+        .find('.pgadmin-controls:first input:enabled,.CodeMirror-scroll');
     }
 
     if(first_el.length > 0) {
diff --git a/web/pgadmin/tools/backup/static/js/backup_dialog_wrapper.js b/web/pgadmin/tools/backup/static/js/backup_dialog_wrapper.js
index 931d0a025..a9a01e5d9 100644
--- a/web/pgadmin/tools/backup/static/js/backup_dialog_wrapper.js
+++ b/web/pgadmin/tools/backup/static/js/backup_dialog_wrapper.js
@@ -99,7 +99,7 @@ export class BackupDialogWrapper extends DialogWrapper {
 
     this.elements.content.appendChild($container.get(0));
 
-    this.focusOnDialog(dialog);
+    this.focusOnDialog(this);
     this.setListenersForFilenameChanges();
   }
 
diff --git a/web/pgadmin/tools/restore/static/js/restore_dialog_wrapper.js b/web/pgadmin/tools/restore/static/js/restore_dialog_wrapper.js
index 0d211aecc..aba30480f 100644
--- a/web/pgadmin/tools/restore/static/js/restore_dialog_wrapper.js
+++ b/web/pgadmin/tools/restore/static/js/restore_dialog_wrapper.js
@@ -99,7 +99,7 @@ export class RestoreDialogWrapper extends DialogWrapper {
 
     this.elements.content.appendChild($container.get(0));
 
-    this.focusOnDialog(dialog);
+    this.focusOnDialog(this);
     this.setListenersForFilenameChanges();
   }