Callback-override for legacy Dialog-API calls
authorAlexander Ebert <ebert@woltlab.com>
Sat, 10 Mar 2018 13:44:04 +0000 (14:44 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 10 Mar 2018 13:44:04 +0000 (14:44 +0100)
wcfsetup/install/files/js/WCF.js
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Dialog.js

index a726112146a643cd787ccbb4636a9b060b350b3a..33709be8b3cdc10207897c12acad8516fae096c4 100755 (executable)
@@ -7069,8 +7069,16 @@ jQuery.fn.extend({
                                UiDialog.rebuild(id);
                        }
                        else if (method === 'option') {
-                               if (args.length === 3 && args[1] === 'title' && typeof args[2] === 'string') {
-                                       UiDialog.setTitle(id, args[2]);
+                               if (args.length === 3) {
+                                       if (args[1] === 'title' && typeof args[2] === 'string') {
+                                               UiDialog.setTitle(id, args[2]);
+                                       }
+                                       else if (args[1].indexOf('on') === 0) {
+                                               UiDialog.setCallback(id, args[1], args[2]);
+                                       }
+                                       else if (args[1] === 'closeConfirmMessage' && args[2] === null) {
+                                               UiDialog.setCallback(id, 'onBeforeClose', null);
+                                       }
                                }
                        }
                        else {
index 4e589232c4b468117d067cdbb6e047858f018a14..7d0a26ea01713768cb4a97b39029cb1543063af9 100644 (file)
@@ -27,6 +27,7 @@ define(
        var _dialogFullHeight = false;
        var _keyupListener = null;
        var _staticDialogs = elByClass('jsStaticDialog');
+       var _validCallbacks = ['onBeforeClose', 'onClose', 'onShow'];
        
        /**
         * @exports     WoltLabSuite/Core/Ui/Dialog
@@ -288,6 +289,37 @@ define(
                        }
                },
                
+               /**
+                * Sets a callback function on runtime.
+                * 
+                * @param       {(string|object)}       id              element id
+                * @param       {string}                key             callback identifier
+                * @param       {?function}             value           callback function or `null`
+                */
+               setCallback: function(id, key, value) {
+                       if (typeof id === 'object') {
+                               var dialogData = _dialogObjects.get(id);
+                               if (dialogData !== undefined) {
+                                       id = dialogData.id;
+                               }
+                       }
+                       
+                       var data = _dialogs.get(id);
+                       if (data === undefined) {
+                               throw new Error("Expected a valid dialog id, '" + id + "' does not match any active dialog.");
+                       }
+                       
+                       if (_validCallbacks.indexOf(key) === -1) {
+                               throw new Error("Invalid callback identifier, '" + key + "' is not recognized.");
+                       }
+                       
+                       if (typeof value !== 'function' && value !== null) {
+                               throw new Error("Only functions or the 'null' value are acceptable callback values ('" + typeof value+ "' given).");
+                       }
+                       
+                       data[key] = value;
+               },
+               
                /**
                 * Creates the DOM for a new dialog and opens it.
                 *