From d1bd824b1a98652b25e76efc0f61f101147237b1 Mon Sep 17 00:00:00 2001
From: Andreas Fernandez <a.fernandez@scripting-base.de>
Date: Sun, 8 Mar 2020 17:44:47 +0100
Subject: [PATCH] [BUGFIX] Disable FormEngine's save button on submit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

To avoid issues with slow internet connections in combination with the
"Refresh required" modal the save button of the FormEngine is now
disabled on submit.

Resolves: #88637
Releases: master, 9.5
Change-Id: Ib89753350992eb135276cac21995acd81a64f1ae
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63623
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Christian Eßl <indy.essl@gmail.com>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Christian Eßl <indy.essl@gmail.com>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
---
 .../Resources/Public/JavaScript/FormEngine.js | 35 ++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/backend/Resources/Public/JavaScript/FormEngine.js b/typo3/sysext/backend/Resources/Public/JavaScript/FormEngine.js
index c933cd1fb73b..dfbe420353bb 100644
--- a/typo3/sysext/backend/Resources/Public/JavaScript/FormEngine.js
+++ b/typo3/sysext/backend/Resources/Public/JavaScript/FormEngine.js
@@ -33,12 +33,13 @@ var setFormValueOpenBrowser, // @deprecated
 define(['jquery',
   'TYPO3/CMS/Backend/FormEngineValidation',
   'TYPO3/CMS/Backend/DocumentSaveActions',
+  'TYPO3/CMS/Backend/Icons',
   'TYPO3/CMS/Backend/Modal',
   'TYPO3/CMS/Backend/Utility/MessageUtility',
   'TYPO3/CMS/Backend/Severity',
   'TYPO3/CMS/Backend/BackendException',
   'TYPO3/CMS/Backend/Event/InteractionRequestMap'
-], function($, FormEngineValidation, DocumentSaveActions, Modal, MessageUtility, Severity, BackendException, InteractionRequestMap) {
+], function($, FormEngineValidation, DocumentSaveActions, Icons, Modal, MessageUtility, Severity, BackendException, InteractionRequestMap) {
 
   /**
    * @param {InteractionRequest} interactionRequest
@@ -598,6 +599,25 @@ define(['jquery',
       FormEngine.openPopupWindow(mode, params);
     });
 
+    document.editform.addEventListener('submit', function () {
+      const elements = [
+        'button[form]',
+        'button[name^="_save"]',
+        'a[data-name^="_save"]',
+        'button[name="CMD"][value^="save"]',
+        'a[data-name="CMD"][data-value^="save"]',
+      ].join(',');
+
+      const button = document.querySelector(elements);
+      if (button !== null) {
+        button.disabled = true;
+
+        Icons.getIcon('spinner-circle-dark', Icons.sizes.small).then(function (markup) {
+          button.querySelector('.t3js-icon').outerHTML = markup;
+        });
+      }
+    });
+
     window.addEventListener('message', FormEngine.handlePostMessage);
   };
 
@@ -1291,14 +1311,27 @@ define(['jquery',
    */
   FormEngine.closeDocument = function() {
     document.editform.closeDoc.value = 1;
+
+    FormEngine.dispatchSubmitEvent();
     document.editform.submit();
   };
 
   FormEngine.saveDocument = function() {
     document.editform.doSave.value = 1;
+
+    FormEngine.dispatchSubmitEvent();
     document.editform.submit();
   };
 
+  /**
+   * Dispatches the "submit" event to the form. This is necessary if .submit() is called directly.
+   */
+  FormEngine.dispatchSubmitEvent = function() {
+    const submitEvent = document.createEvent('Event');
+    submitEvent.initEvent('submit', false, true);
+    document.editform.dispatchEvent(submitEvent);
+  };
+
   /**
    * Main init function called from outside
    *
-- 
GitLab