Anomie has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/92677


Change subject: Add "unsaved changes" warning to Special:Preferences
......................................................................

Add "unsaved changes" warning to Special:Preferences

Much like the similar warning on the edit page, it has been requested
that we display a warning if the user has made changes on
Special:Preferences and attempts to leave without saving.

This adapts the code from
resources/mediawiki.action/mediawiki.action.edit.editWarning.js to do
so.

Bug: 55966
Change-Id: Idb00f50ad8148cd80bd0af81b4cd06a0eb217d96
---
M includes/Preferences.php
M languages/messages/MessagesEn.php
M languages/messages/MessagesQqq.php
M maintenance/language/messages.inc
M resources/Resources.php
M resources/mediawiki.special/mediawiki.special.preferences.js
6 files changed, 53 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/77/92677/1

diff --git a/includes/Preferences.php b/includes/Preferences.php
index c9caf4f..466ed9c 100644
--- a/includes/Preferences.php
+++ b/includes/Preferences.php
@@ -1597,7 +1597,9 @@
                if ( $this->getModifiedUser()->isAllowed( 'editmyoptions' ) ) {
                        $t = SpecialPage::getTitleFor( 'Preferences', 'reset' );
 
-                       $html .= "\n" . Linker::link( $t, $this->msg( 
'restoreprefs' )->escaped() );
+                       $html .= "\n" . Linker::link( $t, $this->msg( 
'restoreprefs' )->escaped(), array(
+                               'id' => 'mw-prefs-restoreprefs',
+                       ) );
 
                        $html = Xml::tags( 'div', array( 'class' => 
'mw-prefs-buttons' ), $html );
                }
diff --git a/languages/messages/MessagesEn.php 
b/languages/messages/MessagesEn.php
index 7c5b1b9..da49bf9 100644
--- a/languages/messages/MessagesEn.php
+++ b/languages/messages/MessagesEn.php
@@ -2019,6 +2019,8 @@
 'prefs-tokenwatchlist'          => 'Token',
 'prefs-diffs'                   => 'Diffs',
 'prefs-help-prefershttps'       => 'This preference will take effect on your 
next login.',
+'prefswarning-warning'          => "You've made changes to your preferences 
that have not been saved yet.
+If you leave this page without clicking Save your preferences will not be 
updated.",
 
 # User preference: email validation using jQuery
 'email-address-validity-valid'   => 'Email address appears valid',
diff --git a/languages/messages/MessagesQqq.php 
b/languages/messages/MessagesQqq.php
index 9d58538..e321826 100644
--- a/languages/messages/MessagesQqq.php
+++ b/languages/messages/MessagesQqq.php
@@ -3229,6 +3229,9 @@
 The checkbox has the label {{msg-mw|Tog-prefershttps}}.
 
 See example: [[mw:Special:Preferences]].',
+'prefswarning-warning' => "{{doc-important|Do ''not'' use 
<nowiki>{{int:saveprefs}}</nowiki> for \"Save\". It is forbidden in this 
message, see [[mwr:68405]].}}
+
+but you can see the text of that button here: {{msg-mw|saveprefs}}",
 
 # User preference: email validation using jQuery
 'email-address-validity-valid' => 'Used as hint for 
{{msg-mw|changeemail-newemail}} field in [[Special:ChangeEmail]], when the 
provided E-mail address is valid.',
diff --git a/maintenance/language/messages.inc 
b/maintenance/language/messages.inc
index 1ac9500..69e364b 100644
--- a/maintenance/language/messages.inc
+++ b/maintenance/language/messages.inc
@@ -1143,6 +1143,8 @@
                'prefs-tokenwatchlist',
                'prefs-diffs',
                'prefs-help-prefershttps',
+
+               'prefswarning-warning',
        ),
        'preferences-email' => array(
                'email-address-validity-valid',
diff --git a/resources/Resources.php b/resources/Resources.php
index 8809227..fe5f5e6 100644
--- a/resources/Resources.php
+++ b/resources/Resources.php
@@ -1014,6 +1014,9 @@
                'skinStyles' => array(
                        'vector' => 'skins/vector/special.preferences.less',
                ),
+               'messages' => array(
+                       'prefswarning-warning',
+               ),
        ),
        'mediawiki.special.recentchanges' => array(
                'scripts' => 
'resources/mediawiki.special/mediawiki.special.recentchanges.js',
diff --git a/resources/mediawiki.special/mediawiki.special.preferences.js 
b/resources/mediawiki.special/mediawiki.special.preferences.js
index 03d93d0..ff870bb 100644
--- a/resources/mediawiki.special/mediawiki.special.preferences.js
+++ b/resources/mediawiki.special/mediawiki.special.preferences.js
@@ -196,4 +196,44 @@
                        sessionStorage.setItem( 'mediawikiPreferencesTab', 
storageData );
                } );
        }
+
+       // 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() );
+       var savedWindowOnBeforeUnload;
+       $( window )
+               .on( 'beforeunload.prefswarning', function () {
+                       var retval;
+
+                       // Check if anything changed
+                       if ( $( '#mw-prefs-form' ).serialize() !== $( 
'#mw-prefs-form' ).data( 'origdata' ) ) {
+                               // Return our message
+                               retval = mw.msg( 'prefswarning-warning' );
+                       }
+
+                       // Unset the onbeforeunload handler so we don't break 
page caching in Firefox
+                       savedWindowOnBeforeUnload = window.onbeforeunload;
+                       window.onbeforeunload = null;
+                       if ( retval !== undefined ) {
+                               // ...but if the user chooses not to leave the 
page, we need to rebind it
+                               setTimeout( function () {
+                                       window.onbeforeunload = 
savedWindowOnBeforeUnload;
+                               }, 1 );
+                               return retval;
+                       }
+               } )
+               .on( 'pageshow.prefswarning', function () {
+                       // Re-add onbeforeunload handler
+                       if ( !window.onbeforeunload ) {
+                               window.onbeforeunload = 
savedWindowOnBeforeUnload;
+                       }
+               } );
+       $( '#mw-prefs-form' ).submit( function () {
+               // Unbind our beforeunload handler
+               $( window ).off( '.prefswarning' );
+       } );
+       $( '#mw-prefs-restoreprefs' ).click( function () {
+               // Unbind our beforeunload handler
+               $( window ).off( '.prefswarning' );
+       } );
 } );

-- 
To view, visit https://gerrit.wikimedia.org/r/92677
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idb00f50ad8148cd80bd0af81b4cd06a0eb217d96
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Anomie <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to