r86372 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r86371‎ | r86372 | r86373 >
Date:02:48, 19 April 2011
Author:neilk
Status:ok (Comments)
Tags:
Comment:
fix bug with confirm-close-window caused by jQuery upgrade, but also refactor and fix a bug that existed anyway
Modified paths:
  • /trunk/extensions/UploadWizard/UploadWizard.i18n.php (modified) (history)
  • /trunk/extensions/UploadWizard/UploadWizardHooks.php (modified) (history)
  • /trunk/extensions/UploadWizard/resources/mw.ConfirmCloseWindow.js (added) (history)
  • /trunk/extensions/UploadWizard/resources/mw.UploadWizard.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/UploadWizardHooks.php
@@ -50,6 +50,7 @@
5151 'resources/mw.Api.edit.js',
5252 'resources/mw.Title.js',
5353 'resources/mw.Feedback.js',
 54+ 'resources/mw.ConfirmCloseWindow.js',
5455
5556 // language menus
5657 'resources/mw.LanguageUpWiz.js',
@@ -234,6 +235,7 @@
235236 'mwe-upwiz-home',
236237 'mwe-upwiz-upload-another',
237238 'mwe-prevent-close',
 239+ 'mwe-upwiz-prevent-close',
238240 'mwe-upwiz-files-complete',
239241 'mwe-upwiz-tooltip-author',
240242 'mwe-upwiz-tooltip-source',
Index: trunk/extensions/UploadWizard/UploadWizard.i18n.php
@@ -166,8 +166,8 @@
167167 'mwe-upwiz-next-details' => 'Next',
168168 'mwe-upwiz-home' => 'Go to wiki home page',
169169 'mwe-upwiz-upload-another' => 'Upload more files',
170 - 'mwe-prevent-close' => 'Your files are still uploading.
171 -Are you sure you want to navigate away from this page?',
 170+ 'mwe-prevent-close' => 'Leaving this page may cause you to lose any changes you have made.',
 171+ 'mwe-upwiz-prevent-close' => 'You haven\'t finished uploading and publishing {{PLURAL:$1|this file|these files}} yet.',
172172 'mwe-upwiz-files-complete' => 'Your files finished uploading!',
173173 'mwe-upwiz-tooltip-author' => 'The name of the person who took the photo, or painted the picture, drew the drawing, etc.',
174174 'mwe-upwiz-tooltip-source' => 'Where this digital file came from — could be a URL, or a book or publication',
Index: trunk/extensions/UploadWizard/resources/mw.UploadWizard.js
@@ -689,6 +689,9 @@
690690 $j( '.mwe-upwiz-hint' ).each( function(i) { $j( this ).tipsy( 'hide' ); } ); // close tipsy help balloons
691691 if ( _this.detailsValid() ) {
692692 _this.detailsSubmit( function() {
 693+ if ( mw.isDefined( _this.allowCloseWindow ) ) {
 694+ _this.allowCloseWindow();
 695+ }
693696 _this.prefillThanksPage();
694697 _this.moveToStep( 'thanks' );
695698 } );
@@ -992,8 +995,9 @@
993996 }
994997 } );
995998
996 - var allowCloseWindow = $j().preventCloseWindow( {
997 - message: gM( 'mwe-prevent-close')
 999+ this.allowCloseWindow = mw.confirmCloseWindow( {
 1000+ message: function() { return gM( 'mwe-upwiz-prevent-close', _this.uploads.length ) },
 1001+ test: function() { return _this.uploads.length > 0 }
9981002 } );
9991003
10001004 $j( '#mwe-upwiz-progress' ).show();
@@ -1021,7 +1025,6 @@
10221026 upload.start();
10231027 },
10241028 function() {
1025 - allowCloseWindow();
10261029 $j().notify( gM( 'mwe-upwiz-files-complete' ) );
10271030 _this.showFileNext();
10281031 }
@@ -1296,38 +1299,8 @@
12971300
12981301 } )( jQuery );
12991302
1300 -( function ( $j ) {
1301 - /**
1302 - * Prevent the closing of a window with a confirm message (the onbeforeunload event seems to
1303 - * work in most browsers
1304 - * e.g.
1305 - * var allowCloseWindow = jQuery().preventCloseWindow( { message: "Don't go away!" } );
1306 - * // ... do stuff that can't be interrupted ...
1307 - * allowCloseWindow();
1308 - *
1309 - * @param options object which should have a message string, already internationalized
1310 - * @return closure execute this when you want to allow the user to close the window
1311 - */
1312 - $j.fn.preventCloseWindow = function( options ) {
1313 - if ( typeof options === 'undefined' ) {
1314 - options = {};
1315 - }
 1303+( function ( $j ) {
13161304
1317 - if ( typeof options.message === 'undefined' ) {
1318 - options.message = 'Are you sure you want to close this window?';
1319 - }
1320 -
1321 - $j( window ).unload( function() {
1322 - return options.message;
1323 - } );
1324 -
1325 - return function() {
1326 - $j( window ).removeAttr( 'unload' );
1327 - };
1328 -
1329 - };
1330 -
1331 -
13321305 $j.fn.notify = function ( message ) {
13331306 // could do something here with Chrome's in-browser growl-like notifications.
13341307 // play a sound?
Index: trunk/extensions/UploadWizard/resources/mw.ConfirmCloseWindow.js
@@ -0,0 +1,57 @@
 2+( function( mw, $ ) {
 3+ /**
 4+ * Prevent the closing of a window with a confirm message (the onbeforeunload event seems to
 5+ * work in most browsers.)
 6+ *
 7+ * This supersedes any previous onbeforeunload handler. If there was a handler before, it is
 8+ * restored when you execute the returned function.
 9+ * e.g.
 10+ *
 11+ * var allowCloseWindow = mw.confirmCloseWindow( { message: 'Dont close me!' } );
 12+ * // ... do stuff that can't be interrupted ...
 13+ * allowCloseWindow();
 14+ *
 15+ *
 16+ * @param options options - optional set of the following optional arguments:
 17+ * message: function returning string message to show.
 18+ * test: function returning boolean. If true, alert is shown. Defaults to always true.
 19+ * @return closure execute this when you want to allow the user to close the window
 20+ */
 21+ mw.confirmCloseWindow = function( options ) {
 22+ if ( ! mw.isDefined( options ) ) {
 23+ options = {};
 24+ }
 25+
 26+ var defaults = {
 27+ message: function() { return gM( 'mwe-prevent-close' ) },
 28+ test: function() { return true; }
 29+ };
 30+ options = $.extend( defaults, options );
 31+
 32+ var oldUnloadHandler = window.onbeforeunload;
 33+
 34+ window.onbeforeunload = function() {
 35+ if ( options.test() ) {
 36+ // remove the handler while the alert is showing - otherwise breaks caching in Firefox (3?).
 37+ // but if they continue working on this page, immediately re-register this handler
 38+ var thisFunction = arguments.callee;
 39+ window.onbeforeunload = null;
 40+ setTimeout( function() {
 41+ window.onbeforeunload = thisFunction;
 42+ } );
 43+
 44+ // show an alert with this message
 45+ return options.message();
 46+ }
 47+ };
 48+
 49+ // return the function they can use to stop this
 50+ return function() {
 51+ window.onbeforeunload = oldUnloadHandler;
 52+ };
 53+
 54+ };
 55+
 56+} )( window.mediaWiki, jQuery );
 57+
 58+
Property changes on: trunk/extensions/UploadWizard/resources/mw.ConfirmCloseWindow.js
___________________________________________________________________
Added: svn:eol-style
159 + native

Comments

#Comment by Catrope (talk | contribs)   11:10, 25 April 2011

Nice plugin. EditWarning implements the same thing right now, so we should move this one to core (its API is way better) and integrate the pageshow event binding from EditWarning's implementation.

#Comment by NeilK (talk | contribs)   16:42, 25 April 2011

EditWarning seems to do something similar and different, AFAICT. It registers an unload handler but then goes to great lengths to ensure that any *other* handler has priority. The impl above is simpler, just masking whatever behaviour was there previously.

#Comment by Trevor Parscal (WMF) (talk | contribs)   17:05, 25 April 2011

ArticleFeedback also uses pageshow and pagehide handling, which help prevent data loss in forms when using the back/forward buttons. This is probably not as relevant to your case however.

#Comment by Catrope (talk | contribs)   17:12, 25 April 2011

Hmm, well the distinction is small, right? Could be an option, as well as the pageshow/pagehide handling.

Status & tagging log