Extend mediaWiki.confirmCloseWindow to trigger it manually
authorFlorian <florian.schmidt.welzow@t-online.de>
Thu, 16 Apr 2015 16:29:02 +0000 (18:29 +0200)
committerJdlrobson <jrobson@wikimedia.org>
Tue, 12 May 2015 11:22:16 +0000 (11:22 +0000)
Some extensions and/or functions doesn't reload the page to destroy a form
that can contain some user input. To use confirmCloseWindow to cover these forms,
too, we need a way to manually trigger this module and show the warning
to the user.

Bug: T94194
Change-Id: I897330bc62b9ef3bfd04f61f5c5d0744c8ec61d3

RELEASE-NOTES-1.26
resources/src/mediawiki.action/mediawiki.action.edit.editWarning.js
resources/src/mediawiki.special/mediawiki.special.preferences.js
resources/src/mediawiki.special/mediawiki.special.upload.js
resources/src/mediawiki/mediawiki.confirmCloseWindow.js

index 8a08f02..8cb7503 100644 (file)
@@ -50,6 +50,9 @@ by default for the ContentHandler base class and true for TextContentHandler
 and it's derivative classes (everything in core). For Content types that
 do not support direct editing, an alternative mechanism should be provided
 for editing, such as action overrides or specific api modules.
+* mediaWiki.confirmCloseWindow now returns an object of functions, instead of
+one function. The callback can't be called directly any more. The callback function
+is replaced with confirmCloseWindow.release().
 
 == Compatibility ==
 
index 6b33012..56dba70 100644 (file)
@@ -35,7 +35,7 @@
 
                // Add form submission handler
                $( '#editform' ).submit( function () {
-                       allowCloseWindow();
+                       allowCloseWindow.release();
                } );
        } );
 
index 4bd747b..8617f7c 100644 (file)
@@ -5,7 +5,7 @@ jQuery( function ( $ ) {
        var $preftoc, $preferences, $fieldsets, $legends,
                hash, labelFunc,
                $tzSelect, $tzTextbox, $localtimeHolder, servertime,
-               $checkBoxes, allowCloseWindowFn;
+               $checkBoxes, allowCloseWindow;
 
        labelFunc = function () {
                return this.id.replace( /^mw-prefsection/g, 'preftab' );
@@ -266,7 +266,7 @@ jQuery( function ( $ ) {
        // Set up a message to notify users if they try to leave the page without
        // saving.
        $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
-       allowCloseWindowFn = mediaWiki.confirmCloseWindow( {
+       allowCloseWindow = mediaWiki.confirmCloseWindow( {
                test: function () {
                        return $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' );
                },
@@ -274,6 +274,6 @@ jQuery( function ( $ ) {
                message: mediaWiki.msg( 'prefswarning-warning', mediaWiki.msg( 'saveprefs' ) ),
                namespace: 'prefswarning'
        } );
-       $( '#mw-prefs-form' ).submit( allowCloseWindowFn );
-       $( '#mw-prefs-restoreprefs' ).click( allowCloseWindowFn );
+       $( '#mw-prefs-form' ).submit( allowCloseWindow.release() );
+       $( '#mw-prefs-restoreprefs' ).click( allowCloseWindow.release() );
 } );
index eeccda5..d0dfb28 100644 (file)
                } );
 
                $uploadForm.submit( function () {
-                       allowCloseWindow();
+                       allowCloseWindow.release();
                } );
        } );
 }( mediaWiki, jQuery ) );
index 7fc5c42..8d1faa6 100644 (file)
@@ -1,3 +1,4 @@
+/* jshint devel: true */
 ( function ( mw, $ ) {
        /**
         * @method confirmCloseWindow
@@ -7,11 +8,22 @@
         * work in most browsers.)
         *
         * This supersedes any previous onbeforeunload handler. If there was a handler before, it is
-        * restored when you execute the returned function.
+        * restored when you execute the returned release() function.
         *
         *     var allowCloseWindow = mw.confirmCloseWindow();
         *     // ... do stuff that can't be interrupted ...
-        *     allowCloseWindow();
+        *     allowCloseWindow.release();
+        *
+        * The second function returned is a trigger function to trigger the check and an alert
+        * window manually, e.g.:
+        *
+        *     var allowCloseWindow = mw.confirmCloseWindow();
+        *     // ... do stuff that can't be interrupted ...
+        *     if ( allowCloseWindow.trigger() ) {
+        *         // don't do anything (e.g. destroy the input field)
+        *     } else {
+        *         // do whatever you wanted to do
+        *     }
         *
         * @param {Object} [options]
         * @param {string} [options.namespace] Namespace for the event registration
         * @param {string} options.message.return The string message to show in the confirm dialog.
         * @param {Function} [options.test]
         * @param {boolean} [options.test.return=true] Whether to show the dialog to the user.
-        * @return {Function} Execute this when you want to allow the user to close the window
+        * @return {Object} An object of functions to work with this module
         */
        mw.confirmCloseWindow = function ( options ) {
                var savedUnloadHandler,
                        mainEventName = 'beforeunload',
-                       showEventName = 'pageshow';
+                       showEventName = 'pageshow',
+                       message;
 
                options = $.extend( {
                        message: mw.message( 'mwe-prevent-close' ).text(),
                        showEventName += '.' + options.namespace;
                }
 
+               if ( $.isFunction( options.message ) ) {
+                       message = options.message();
+               } else {
+                       message = options.message;
+               }
+
                $( window ).on( mainEventName, function () {
                        if ( options.test() ) {
                                // remove the handler while the alert is showing - otherwise breaks caching in Firefox (3?).
                                }, 1 );
 
                                // show an alert with this message
-                               if ( $.isFunction( options.message ) ) {
-                                       return options.message();
-                               } else {
-                                       return options.message;
-                               }
+                               return message;
                        }
                } ).on( showEventName, function () {
                        // Re-add onbeforeunload handler
                        }
                } );
 
-               // return the function they can use to stop this
-               return function () {
-                       $( window ).off( mainEventName + ' ' + showEventName );
+               /**
+                * Return the object with functions to release and manually trigger the confirm alert
+                * @ignore
+                */
+               return {
+                       /**
+                        * Remove all event listeners and don't show an alert anymore, if the user wants to leave
+                        * the page.
+                        * @ignore
+                        */
+                       release: function () {
+                               $( window ).off( mainEventName + ' ' + showEventName );
+                       },
+                       /**
+                        * Trigger the module's function manually: Check, if options.test() returns true and show
+                        * an alert to the user if he/she want to leave this page. Returns false, if options.test() returns
+                        * false or the user cancelled the alert window (~don't leave the page), true otherwise.
+                        * @ignore
+                        * @return boolean
+                        */
+                       trigger: function () {
+                               // use confirm to show the message to the user (if options.text() is true)
+                               if ( options.test() && !confirm( message ) ) {
+                                       // the user want to keep the actual page
+                                       return false;
+                               }
+                               // otherwise return true
+                               return true;
+                       }
                };
        };
 } )( mediaWiki, jQuery );