From: Alexander Ebert Date: Sat, 10 Mar 2018 13:44:04 +0000 (+0100) Subject: Callback-override for legacy Dialog-API calls X-Git-Tag: 3.0.13~4 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=23cffb2d624b054fb89719164a18c727988c3f74;p=GitHub%2FWoltLab%2FWCF.git Callback-override for legacy Dialog-API calls --- diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index a726112146..33709be8b3 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -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 { diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Dialog.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Dialog.js index 4e589232c4..7d0a26ea01 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Dialog.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Dialog.js @@ -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. *