Florianschmidtwelzow has uploaded a new change for review. https://gerrit.wikimedia.org/r/204538
Change subject: Extend mediaWiki.confirmCloseWindow with a custom event ...................................................................... Extend mediaWiki.confirmCloseWindow with a custom event Some extensions and/or functions doesn't reload the page to destroy forms, that can contain a user input. To use confirmCloseWindow to cover these forms, too, we need to listen on a custom event (which can be set dynamically by the developer using this module) and show the warning to the user, if this event is triggered. Bug: T94194 Change-Id: I897330bc62b9ef3bfd04f61f5c5d0744c8ec61d3 --- M resources/src/mediawiki/mediawiki.confirmCloseWindow.js 1 file changed, 42 insertions(+), 7 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/38/204538/1 diff --git a/resources/src/mediawiki/mediawiki.confirmCloseWindow.js b/resources/src/mediawiki/mediawiki.confirmCloseWindow.js index 7fc5c42..983f694 100644 --- a/resources/src/mediawiki/mediawiki.confirmCloseWindow.js +++ b/resources/src/mediawiki/mediawiki.confirmCloseWindow.js @@ -13,6 +13,21 @@ * // ... do stuff that can't be interrupted ... * allowCloseWindow(); * + * You can define a custom event name, which, when triggered on the window object, also shows a confirm + * window to the user, if your test function returns true. The confirmation window looks slightly different + * from the one created by onbeforeunload event, but has the same message. + * Usage example: + * + * var allowCloseWindow = mw.confirmCloseWindow( { + * test: function () { + * return true; // example + * }, + * customEventName: 'customCloseEvent' + * } ); + * // .. other things + * // will be true, if the test function returns false or if the user confirms the warning window + * var canClosed = $( window ).triggerHandler( 'customCloseEvent' ); + * * @param {Object} [options] * @param {string} [options.namespace] Namespace for the event registration * @param {string} [options.message] @@ -24,7 +39,8 @@ mw.confirmCloseWindow = function ( options ) { var savedUnloadHandler, mainEventName = 'beforeunload', - showEventName = 'pageshow'; + showEventName = 'pageshow', + customEventName = options.customEventName || ''; options = $.extend( { message: mw.message( 'mwe-prevent-close' ).text(), @@ -34,6 +50,21 @@ if ( options.namespace ) { mainEventName += '.' + options.namespace; showEventName += '.' + options.namespace; + if ( customEventName ) { + customEventName += '.' + options.namespace; + } + } + + /** + * Returns the message string to show to the user. + * @return string + */ + function getMessage() { + if ( $.isFunction( options.message ) ) { + return options.message(); + } else { + return options.message; + } } $( window ).on( mainEventName, function () { @@ -47,22 +78,26 @@ }, 1 ); // show an alert with this message - if ( $.isFunction( options.message ) ) { - return options.message(); - } else { - return options.message; - } + return getMessage(); } } ).on( showEventName, function () { // Re-add onbeforeunload handler if ( !window.onbeforeunload && savedUnloadHandler ) { window.onbeforeunload = savedUnloadHandler; } + } ).on( customEventName, function () { + // use window.confirm to show the message to the user (if options.text() is true) + if ( options.test() && !this.confirm( getMessage() ) ) { + // the user want to keep the actual page + return false; + } + // otherwise return true + return true; } ); // return the function they can use to stop this return function () { - $( window ).off( mainEventName + ' ' + showEventName ); + $( window ).off( mainEventName + ' ' + showEventName + ' ' + customEventName ); }; }; } )( mediaWiki, jQuery ); -- To view, visit https://gerrit.wikimedia.org/r/204538 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I897330bc62b9ef3bfd04f61f5c5d0744c8ec61d3 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.wel...@t-online.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits