Fix form builder dialog cancel button after form validation error
authorMatthias Schmidt <gravatronics@live.com>
Thu, 20 Jun 2019 15:09:46 +0000 (17:09 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Thu, 20 Jun 2019 15:09:46 +0000 (17:09 +0200)
See #2509

wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Dialog.js

index 6bf2009b8c2e1b9e45c9ae9163376832107953ef..96528008a948b85808734083e2cbbeb76e610d44 100644 (file)
@@ -35,8 +35,7 @@ define(['Ajax', 'Core', './Manager', 'Ui/Dialog'], function(Ajax, Core, FormBuil
                                usesDboAction: this._className.match(/\w+\\data\\/)
                        }, options);
                        this._options.dialog = Core.extend(this._options.dialog || {}, {
-                               onClose: this._dialogOnClose.bind(this),
-                               onSetup: this._dialogOnSetup.bind(this)
+                               onClose: this._dialogOnClose.bind(this)
                        });
                        
                        this._formId = '';
@@ -86,12 +85,7 @@ define(['Ajax', 'Core', './Manager', 'Ui/Dialog'], function(Ajax, Core, FormBuil
                                                throw new Error("Missing form id in return data.");
                                        }
                                        
-                                       this.destroy(true);
-                                       
-                                       this._formId = data.returnValues.formId;
-                                       this._dialogContent = data.returnValues.dialog;
-                                       
-                                       UiDialog.open(this, this._dialogContent);
+                                       this._openDialogContent(data.returnValues.formId, data.returnValues.dialog);
                                        
                                        break;
                                        
@@ -102,11 +96,7 @@ define(['Ajax', 'Core', './Manager', 'Ui/Dialog'], function(Ajax, Core, FormBuil
                                                        throw new Error("Mismatch between form ids: expected '" + this._formId + "' but got '" + data.returnValues.formId + "'.");
                                                }
                                                
-                                               this.destroy(true);
-                                               
-                                               this._dialogContent = data.returnValues.dialog;
-                                               
-                                               UiDialog.open(this, this._dialogContent);
+                                               this._openDialogContent(data.returnValues.formId, data.returnValues.dialog);
                                        }
                                        else {
                                                this.destroy();
@@ -130,18 +120,6 @@ define(['Ajax', 'Core', './Manager', 'Ui/Dialog'], function(Ajax, Core, FormBuil
                        UiDialog.close(this);
                },
                
-               /**
-                * Is called when the dialog is set up.
-                * 
-                * @param       {HTMLElement}   content         dialog's content element
-                */
-               _dialogOnSetup: function(content) {
-                       var cancelButton = elBySel('button[data-type=cancel]', content);
-                       if (cancelButton !== null) {
-                               cancelButton.addEventListener('click', this._closeDialog.bind(this));
-                       }
-               },
-               
                /**
                 * Is called by the dialog API when the dialog is closed.
                 */
@@ -171,6 +149,27 @@ define(['Ajax', 'Core', './Manager', 'Ui/Dialog'], function(Ajax, Core, FormBuil
                        this.getData().then(this._submitForm.bind(this));
                },
                
+               /**
+                * Opens the form dialog with the given form content.
+                * 
+                * @param       {string}        formId
+                * @param       {string}        dialogContent
+                */
+               _openDialogContent: function(formId, dialogContent) {
+                       this.destroy(true);
+                       
+                       this._formId = formId;
+                       this._dialogContent = dialogContent;
+                       
+                       var dialogData = UiDialog.open(this, this._dialogContent);
+                       
+                       var cancelButton = elBySel('button[data-type=cancel]', dialogData.content);
+                       if (cancelButton !== null && !elDataBool(cancelButton, 'has-event-listener')) {
+                               cancelButton.addEventListener('click', this._closeDialog.bind(this));
+                               elData(cancelButton, 'has-event-listener', 1);
+                       }
+               },
+               
                /**
                 * Submits the form with the given form data.
                 *