From a32b5fdd64f0c2e617b02103a50ffbe8ae8a8591 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Mon, 11 Oct 2021 11:16:33 +1100
Subject: [PATCH 1/3] Adds setting to close forms using escape key

- Configurable per user
- Default = false (to preserve behavior)
---
 InvenTree/common/models.py                             | 7 +++++++
 InvenTree/templates/InvenTree/settings/user_forms.html | 1 +
 InvenTree/templates/js/translated/modals.js            | 4 ++--
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py
index 9a9373d5c3..49d8ada561 100644
--- a/InvenTree/common/models.py
+++ b/InvenTree/common/models.py
@@ -981,6 +981,13 @@ class InvenTreeUserSetting(BaseInvenTreeSetting):
             'default': True,
             'validator': bool,
         },
+
+        'FORMS_CLOSE_USING_ESCAPE': {
+            'name': _('Escape Key Closes Forms'),
+            'description': _('Use the escape key to close modal forms'),
+            'default': False,
+            'validator': bool,
+        }
     }
 
     class Meta:
diff --git a/InvenTree/templates/InvenTree/settings/user_forms.html b/InvenTree/templates/InvenTree/settings/user_forms.html
index 121a0c7143..f5eecc574b 100644
--- a/InvenTree/templates/InvenTree/settings/user_forms.html
+++ b/InvenTree/templates/InvenTree/settings/user_forms.html
@@ -14,6 +14,7 @@
 <div class='row'>
     <table class='table table-striped table-condensed'>
         <tbody>
+            {% include "InvenTree/settings/setting.html" with key="FORMS_CLOSE_USING_ESCAPE" icon="fa-window-close" user_setting=True %}
             {% include "InvenTree/settings/setting.html" with key="PART_SHOW_QUANTITY_IN_FORMS" icon="fa-hashtag" user_setting=True %}
         </tbody>
     </table>
diff --git a/InvenTree/templates/js/translated/modals.js b/InvenTree/templates/js/translated/modals.js
index 96e41fd6ec..698a929390 100644
--- a/InvenTree/templates/js/translated/modals.js
+++ b/InvenTree/templates/js/translated/modals.js
@@ -43,7 +43,7 @@ function createNewModal(options={}) {
     });
 
     var html = `
-    <div class='modal fade modal-fixed-footer modal-primary inventree-modal' role='dialog' id='modal-form-${id}'>
+    <div class='modal fade modal-fixed-footer modal-primary inventree-modal' role='dialog' id='modal-form-${id}' tabindex='-1'>
         <div class='modal-dialog'>
             <div class='modal-content'>
                 <div class="modal-header">
@@ -111,7 +111,7 @@ function createNewModal(options={}) {
 
     $(modal_name).modal({
         backdrop: 'static',
-        keyboard: false,
+        keyboard: user_settings.FORMS_CLOSE_USING_ESCAPE,
     });
 
     // Set labels based on supplied options

From 25031e09e4ec844e3632daefbc3642b90908f5f2 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Mon, 11 Oct 2021 11:22:17 +1100
Subject: [PATCH 2/3] Enable setting for legacy launchModalForm function

---
 InvenTree/templates/js/translated/modals.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/InvenTree/templates/js/translated/modals.js b/InvenTree/templates/js/translated/modals.js
index 698a929390..f9658df559 100644
--- a/InvenTree/templates/js/translated/modals.js
+++ b/InvenTree/templates/js/translated/modals.js
@@ -681,7 +681,7 @@ function openModal(options) {
 
     $(modal).modal({
         backdrop: 'static',
-        keyboard: false,
+        keyboard: user_settings.FORMS_CLOSE_USING_ESCAPE,
     });
 
     // Disable the form

From f978924c4e1c82e25c7f51f1a53fe2b9a755436a Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Mon, 11 Oct 2021 11:23:35 +1100
Subject: [PATCH 3/3] Adds required tabindex

---
 InvenTree/templates/modals.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/InvenTree/templates/modals.html b/InvenTree/templates/modals.html
index ac26a5fa9a..68d574fe89 100644
--- a/InvenTree/templates/modals.html
+++ b/InvenTree/templates/modals.html
@@ -7,7 +7,7 @@
 
 </div>
 
-<div class='modal fade modal-fixed-footer modal-primary' role='dialog' id='modal-form'>
+<div class='modal fade modal-fixed-footer modal-primary' role='dialog' id='modal-form' tabindex='-1'>
     <div class='modal-dialog'>
         <div class='modal-content'>
             <div class="modal-header">