Esanders has uploaded a new change for review.

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

Change subject: Update tab and history state when switching between NWE & VE
......................................................................

Update tab and history state when switching between NWE & VE

Bug: T143577
Change-Id: I729c0f4a8131f54f81be2d776bddf3020a21e704
---
M modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
M modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
M modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js
M modules/ve-mw/init/ve.init.mw.ArticleTarget.js
4 files changed, 76 insertions(+), 39 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/98/306798/1

diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js 
b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
index 8f577f6..d9d4be1 100644
--- a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
+++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.init.js
@@ -143,18 +143,6 @@
                                        target = 
ve.init.mw.targetFactory.create(
                                                conf.contentModels[ 
mw.config.get( 'wgPageContentModel' ) ]
                                        );
-                                       target.connect( this, {
-                                               transformPage: function () {
-                                                       if ( onlyTabIsVE ) {
-                                                               $( '#ca-edit' 
).addClass( 'selected' );
-                                                       }
-                                               },
-                                               restorePage: function () {
-                                                       if ( onlyTabIsVE ) {
-                                                               $( '#ca-edit' 
).removeClass( 'selected' );
-                                                       }
-                                               }
-                                       } );
                                        target.setContainer( $( '#content' ) );
                                        targetLoaded = true;
                                        return target;
diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js 
b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
index e54a941..4805049 100644
--- a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
+++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
@@ -1173,12 +1173,9 @@
  * Page modifications for switching to edit mode.
  */
 ve.init.mw.DesktopArticleTarget.prototype.transformPage = function () {
-       var uri, $content;
+       var $content;
 
-       // Deselect current mode (e.g. "view" or "history"). In skins like 
monobook that don't have
-       // separate tab sections for content actions and namespaces the below 
is a no-op.
-       $( '#p-views' ).find( 'li.selected' ).removeClass( 'selected' );
-       $( '#ca-ve-edit' ).addClass( 'selected' );
+       this.updateTabs( true );
        this.emit( 'transformPage' );
 
        mw.hook( 've.activate' ).fire();
@@ -1196,13 +1193,23 @@
        // Support IE9: Disable links
        $( '.ve-init-mw-desktopArticleTarget-uneditableContent' ).on( 
'click.ve-target', function () { return false; } );
 
-       // Push veaction=edit url in history (if not already. If we got here by 
a veaction=edit
+       this.updateHistoryState();
+       this.actFromPopState = false;
+};
+
+/**
+ * Update the history state based on the editor mode
+ */
+ve.init.mw.DesktopArticleTarget.prototype.updateHistoryState = function () {
+       var uri,
+               veaction = this.mode === 'visual' ? 'edit' : 'editsource';
+
+       // Push veaction=edit(source) url in history (if not already. If we got 
here by a veaction=edit(source)
        // permalink then it will be there already and the constructor called 
#activate)
        if (
                !this.actFromPopState &&
                history.pushState &&
-               this.currentUri.query.veaction !== 'edit' &&
-               this.currentUri.query.veaction !== 'editsource' &&
+               this.currentUri.query.veaction !== veaction &&
                this.currentUri.query.action !== 'edit'
        ) {
                // Set the current URL
@@ -1215,7 +1222,7 @@
                        uri.query.action = 'edit';
                        mw.config.set( 'wgAction', 'edit' );
                } else {
-                       uri.query.veaction = 'edit';
+                       uri.query.veaction = veaction;
                        delete uri.query.action;
                        mw.config.set( 'wgAction', 'view' );
                }
@@ -1234,8 +1241,7 @@
        // Skins like monobook don't have a tab for view mode and instead just 
have the namespace tab
        // selected. We didn't deselect the namespace tab, so we're ready after 
deselecting #ca-ve-edit.
        // In skins having #ca-view (like Vector), select that.
-       $( '#ca-ve-edit' ).removeClass( 'selected' );
-       $( '#ca-view' ).addClass( 'selected' );
+       this.updateTabs( false );
 
        // Remove any VE-added redirectMsg
        $( '.mw-body-content > .ve-redirect-header' ).remove();
@@ -1271,14 +1277,13 @@
                        history.pushState( this.popState, document.title, uri );
                }
        }
-       this.actFromPopState = false;
 };
 
 /**
  * @param {Event} e Native event object
  */
 ve.init.mw.DesktopArticleTarget.prototype.onWindowPopState = function ( e ) {
-       var newUri;
+       var newUri, veaction;
 
        if ( !this.verifyPopState( e.state ) ) {
                // Ignore popstate events fired for states not created by us
@@ -1287,12 +1292,13 @@
        }
 
        newUri = this.currentUri = new mw.Uri( location.href );
+       veaction = newUri.query.veaction;
 
-       if ( !this.active && newUri.query.veaction === 'edit' ) {
+       if ( !this.active && ( veaction === 'edit' || veaction === 'editsource' 
) ) {
                this.actFromPopState = true;
                this.activate();
        }
-       if ( this.active && newUri.query.veaction !== 'edit' ) {
+       if ( this.active && veaction !== 'edit' && veaction !== 'editsource' ) {
                this.actFromPopState = true;
                this.deactivate( false, 'navigate-back' );
        }
diff --git 
a/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js 
b/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js
index 1eb0839..6ec10d8 100644
--- a/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js
+++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopWikitextArticleTarget.js
@@ -43,16 +43,6 @@
 /**
  * @inheritdoc
  */
-ve.init.mw.DesktopWikitextArticleTarget.prototype.restorePage = function () {
-       $( '#ca-edit' ).removeClass( 'selected' );
-
-       // Parent method
-       
ve.init.mw.DesktopWikitextArticleTarget.super.prototype.restorePage.apply( 
this, arguments );
-};
-
-/**
- * @inheritdoc
- */
 ve.init.mw.DesktopWikitextArticleTarget.prototype.switchToWikitextEditor = 
function ( discardChanges, modified ) {
        var dataPromise,
                target = this;
@@ -95,6 +85,33 @@
 };
 
 /**
+ * @inheritdoc
+ */
+ve.init.mw.DesktopWikitextArticleTarget.prototype.onWindowPopState = function 
( e ) {
+       var veaction, mode;
+
+       if ( !this.verifyPopState( e.state ) ) {
+               return;
+       }
+
+       // Parent method
+       
ve.init.mw.DesktopWikitextArticleTarget.super.prototype.onWindowPopState.apply( 
this, arguments );
+
+       veaction = this.currentUri.query.veaction;
+       mode = veaction === 'editsource' ? 'source' : 'visual';
+
+       if ( this.active ) {
+               if ( veaction === 'editsource' && this.mode === 'visual' ) {
+                       this.actFromPopState = true;
+                       this.switchToWikitextEditor();
+               } else if ( veaction === 'edit' && this.mode === 'source' ) {
+                       this.actFromPopState = true;
+                       this.switchToVisualEditor();
+               }
+       }
+};
+
+/**
  * Reload the target surface in the new editor mode
  */
 ve.init.mw.DesktopWikitextArticleTarget.prototype.reloadSurface = function ( 
dataPromise ) {
@@ -105,6 +122,7 @@
        this.activatingDeferred = $.Deferred();
        this.load( dataPromise );
        this.activatingDeferred.done( function () {
+               target.updateHistoryState();
                target.afterActivate();
                target.setupTriggerListeners();
        } );
diff --git a/modules/ve-mw/init/ve.init.mw.ArticleTarget.js 
b/modules/ve-mw/init/ve.init.mw.ArticleTarget.js
index 896226e..5282609 100644
--- a/modules/ve-mw/init/ve.init.mw.ArticleTarget.js
+++ b/modules/ve-mw/init/ve.init.mw.ArticleTarget.js
@@ -194,13 +194,38 @@
  */
 ve.init.mw.ArticleTarget.prototype.setMode = function ( mode ) {
        if ( mode !== this.mode ) {
-               $( '#ca-ve-edit' ).toggleClass( 'selected', mode === 'visual' );
-               $( '#ca-edit' ).toggleClass( 'selected', mode === 'source' );
                this.mode = mode;
+               this.updateTabs( true );
        }
 };
 
 /**
+ * Update state of editing tabs
+ */
+ve.init.mw.ArticleTarget.prototype.updateTabs = function ( editing ) {
+       var selectVe = false,
+               selectEdit = false;
+
+       // Deselect current mode (e.g. "view" or "history"). In skins like 
monobook that don't have
+       // separate tab sections for content actions and namespaces the below 
is a no-op.
+       $( '#p-views' ).find( 'li.selected' ).removeClass( 'selected' );
+
+       if ( editing ) {
+               if ( $( '#ca-ve-edit' ).length ) {
+                       selectVe = this.mode === 'visual';
+                       selectEdit = this.mode === 'source';
+               } else {
+                       // Single edit tab
+                       selectEdit = true;
+               }
+       } else {
+               $( '#ca-view' ).addClass( 'selected' );
+       }
+       $( '#ca-ve-edit' ).toggleClass( 'selected', selectVe );
+       $( '#ca-edit' ).toggleClass( 'selected', selectEdit );
+};
+
+/**
  * Handle response to a successful load request.
  *
  * This method is called within the context of a target instance. If 
successful the DOM from the

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I729c0f4a8131f54f81be2d776bddf3020a21e704
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <[email protected]>

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

Reply via email to