From: Alexander Ebert Date: Thu, 2 Feb 2012 13:57:00 +0000 (+0100) Subject: Added confirmation dialog and improved dialog X-Git-Tag: 2.0.0_Beta_1~1360^2~40 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=34e158d9662c81d9f26bb5a883fce84086254f09;p=GitHub%2FWoltLab%2FWCF.git Added confirmation dialog and improved dialog --- diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index ec41edc29e..fd6f6bd26e 100644 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -3531,6 +3531,109 @@ WCF.System.Notification = Class.extend({ } }); +/** + * Provides dialog-based confirmations. + */ +WCF.System.Confirmation = { + /** + * notification callback + * @var object + */ + _callback: null, + + /** + * confirmation dialog + * @var jQuery + */ + _dialog: null, + + /** + * dialog visibility + * @var boolean + */ + _visible: false, + + /** + * Displays a confirmation dialog. + * + * @param string message + * @param object callback + */ + show: function(message, callback) { + if (this._visible) { + console.debug('[WCF.System.Confirmation] Confirmation dialog is already open, refusing action.'); + return; + } + + if (!$.isFunction(callback)) { + console.debug('[WCF.System.Confirmation] Given callback is invalid, aborting.'); + return; + } + + this._callback = callback; + if (this._dialog === null) { + this._createDialog(); + } + + this._dialog.find('p').html(message); + this._dialog.wcfDialog({ + onClose: $.proxy(this._close, this), + onShow: $.proxy(this._show, this), + title: WCF.Language.get('wcf.global.confirmation.title') + }); + + this._visible = true; + }, + + /** + * Creates the confirmation dialog on first use. + */ + _createDialog: function() { + this._dialog = $('

').hide().appendTo(document.body); + var $formButtons = $('
').appendTo(this._dialog); + + $('').data('action', 'confirm').click($.proxy(this._click, this)).appendTo($formButtons); + $('').data('action', 'cancel').click($.proxy(this._click, this)).appendTo($formButtons); + }, + + /** + * Handles button clicks. + * + * @param object event + */ + _click: function(event) { + this._notify($(event.currentTarget).data('action')); + }, + + /** + * Handles dialog being closed. + */ + _close: function() { + if (this._visible) { + this._notify('cancel'); + } + }, + + /** + * Notifies callback upon user's decision. + * + * @param string action + */ + _notify: function(action) { + this._visible = false; + this._dialog.wcfDialog('close'); + + this._callback(action); + }, + + /** + * Tries to set focus on confirm button. + */ + _show: function() { + this._dialog.find('button:eq(0)').blur().focus(); + } +}; + /** * Default implementation for inline editors. * @@ -4016,7 +4119,11 @@ $.widget('ui.wcfDialog', { showLoadingOverlay: true, success: null, type: 'POST', - url: 'index.php/AJAXProxy/?t=' + SECURITY_TOKEN + SID_ARG_2ND + url: 'index.php/AJAXProxy/?t=' + SECURITY_TOKEN + SID_ARG_2ND, + + // event callbacks + onClose: null, + onShow: null }, /** @@ -4192,6 +4299,10 @@ $.widget('ui.wcfDialog', { if (this._overlay !== null) { this._overlay.hide(); } + + if (this.options.onClose !== null) { + this.options.onClose(); + } }, /** @@ -4325,6 +4436,10 @@ $.widget('ui.wcfDialog', { this._isRendering = false; })); } + + if (this.options.onShow !== null) { + this.options.onShow(); + } this._isRendering = true; },