Deepali has uploaded a new change for review.

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

Change subject: Warn user that changes will not be saved if s/he navigates away 
from JSON editor
......................................................................

Warn user that changes will not be saved if s/he navigates away from JSON editor

ext.BookManagerv2.editWarning.js displays editwarning message on beforeunload 
if the input field values of the JSON editor form have been changed and user 
has enabled edit warning option in preferences.

Bug: 53449
Change-Id: I2e9137f98876d307c4ef62e74e7a3d579fedcdda
---
M BookManagerv2.php
M JsonEditor.php
A modules/ext.BookManagerv2.editWarning.js
3 files changed, 65 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BookManagerv2 
refs/changes/45/125945/1

diff --git a/BookManagerv2.php b/BookManagerv2.php
index e75e84d..1398a40 100644
--- a/BookManagerv2.php
+++ b/BookManagerv2.php
@@ -176,6 +176,11 @@
                'bookmanagerv2-rename'
        )
 );
+$wgResourceModules['ext.BookManagerv2.editWarning'] = array(
+       'scripts' => 'ext.BookManagerv2.editWarning.js',
+       'localBasePath' => __DIR__ . '/modules',
+       'remoteExtPath' => 'BookManagerv2/modules',
+);
 
 // User configuration
 
diff --git a/JsonEditor.php b/JsonEditor.php
index 59b6b6f..8ecc354 100644
--- a/JsonEditor.php
+++ b/JsonEditor.php
@@ -237,7 +237,7 @@
        }
 
        protected function showContentForm() {
-               global $wgOut;
+               global $wgOut, $wgUser;
                $schema = self::getSchema();
                $originalContent = $this->getContent();
                if ( $originalContent !== '' ) {
@@ -452,6 +452,9 @@
                $wgOut->addHTML( $html );
                $wgOut->addModuleStyles( 'ext.BookManagerv2.editor' );
                $wgOut->addModules( 'ext.BookManagerv2.editorjs' );
+               if ( $wgUser->getOption( 'useeditwarning', false ) ) {
+                       $wgOut->addModules( 'ext.BookManagerv2.editWarning' );
+               }
        }
 
        /**
diff --git a/modules/ext.BookManagerv2.editWarning.js 
b/modules/ext.BookManagerv2.editWarning.js
new file mode 100644
index 0000000..cd9441c
--- /dev/null
+++ b/modules/ext.BookManagerv2.editWarning.js
@@ -0,0 +1,56 @@
+/*
+ * Javascript for module editWarning
+ */
+( function ( mw, $ ) {
+       $( function () {
+               // Get the original values of form elements
+               $( '#editform input' ).each( function () {
+                       $( this ).data( 'origtext', $( this ).val() );
+               } );
+               var savedWindowOnBeforeUnload;
+               $( window )
+                       .on( 'beforeunload.editwarning', function () {
+                               var retval;
+
+                               // Check if the current values of some form 
elements are the same as
+                               // the original values
+                               $( "#editform" ).data( "changed", false );
+                               $( '#editform input' ).each( function() {
+                                       if ( $( this ).val() !== $( this 
).data( 'origtext' ) ) {
+                                           $( "#editform" ).data( "changed", 
true );
+                                       }
+                               });
+                               if (
+                                       mw.config.get( 'wgAction' ) === 
'submit' || $( "#editform" ).data( "changed" )
+                               ) {
+                                       // Return our message
+                                       retval = mw.msg( 'editwarning-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.editwarning', function () {
+                               // Re-add onbeforeunload handler
+                               if ( !window.onbeforeunload ) {
+                                       window.onbeforeunload = 
savedWindowOnBeforeUnload;
+                               }
+                       } );
+
+               // Add form submission handler
+               $( '#editform' ).submit( function () {
+                       // Unbind our handlers
+                       $( window ).off( '.editwarning' );
+               } );
+       } );
+
+}( mediaWiki, jQuery ) );
+

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2e9137f98876d307c4ef62e74e7a3d579fedcdda
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BookManagerv2
Gerrit-Branch: master
Gerrit-Owner: Deepali <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to