JGonera has uploaded a new change for review.

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


Change subject: Prevent preview and editor being shown at the same time
......................................................................

Prevent preview and editor being shown at the same time

Bug: 58945
Change-Id: I7b2510f252c990779205db63b49caaa2cb92be1d
---
M javascripts/modules/editor/EditorApi.js
M javascripts/modules/editor/EditorOverlay.js
M javascripts/modules/editorNew/EditorOverlay.js
M tests/javascripts/modules/editor/test_EditorApi.js
4 files changed, 60 insertions(+), 64 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/49/104749/1

diff --git a/javascripts/modules/editor/EditorApi.js 
b/javascripts/modules/editor/EditorApi.js
index a400626..5a18714 100644
--- a/javascripts/modules/editor/EditorApi.js
+++ b/javascripts/modules/editor/EditorApi.js
@@ -151,6 +151,37 @@
                        this.getToken().done( saveContent ).fail( $.proxy( 
result, 'reject' ) );
 
                        return result;
+               },
+
+               getPreview: function( options ) {
+                       var result = $.Deferred();
+
+                       $.extend( options, {
+                               action: 'parse',
+                               // Enable section preview mode to avoid errors 
(bug 49218)
+                               sectionpreview: true,
+                               // needed for pre-save transform to work (bug 
53692)
+                               pst: true,
+                               // Output mobile HTML (bug 54243)
+                               mobileformat: true,
+                               title: this.title,
+                               prop: 'text'
+                       } );
+
+                       this.post( options ).done( function( resp ) {
+                               if ( resp && resp.parse && resp.parse.text ) {
+                                       // FIXME: hacky
+                                       var $tmp = $( '<div>' ).html( 
resp.parse.text['*'] );
+                                       // remove heading from the parsed output
+                                       $tmp.find( 'h1, h2' ).eq( 0 ).remove();
+
+                                       result.resolve( $tmp.html() );
+                               } else {
+                                       result.reject();
+                               }
+                       } ).fail( $.proxy( result, 'reject' ) );
+
+                       return result;
                }
        } );
 
diff --git a/javascripts/modules/editor/EditorOverlay.js 
b/javascripts/modules/editor/EditorOverlay.js
index efaf5bf..e4615c6 100644
--- a/javascripts/modules/editor/EditorOverlay.js
+++ b/javascripts/modules/editor/EditorOverlay.js
@@ -5,7 +5,6 @@
                Page = M.require( 'Page' ),
                schema = M.require( 'loggingSchemas/mobileWebEditing' ),
                popup = M.require( 'toast' ),
-               api = M.require( 'api' ),
                inBetaOrAlpha = M.isBetaGroupMember(),
                inCampaign = M.query.campaign ? true : false,
                inKeepGoingCampaign = M.query.campaign === 'mobile-keepgoing',
@@ -107,18 +106,7 @@
                },
 
                _showPreview: function() {
-                       var self = this, params = {
-                               action: 'parse',
-                               // Enable section preview mode to avoid errors 
(bug 49218)
-                               sectionpreview: true,
-                               // needed for pre-save transform to work (bug 
53692)
-                               pst: true,
-                               // Output mobile HTML (bug 54243)
-                               mobileformat: true,
-                               title: self.options.title,
-                               text: self.$content.val(),
-                               prop: 'text'
-                       };
+                       var self = this, params = { text: this.$content.val() };
 
                        // log save button click
                        this.log( 'save' );
@@ -138,34 +126,16 @@
                        if ( mw.config.get( 'wgIsMainPage' ) ) {
                                params.mainpage = 1; // Setting it to 0 will 
have the same effect
                        }
-                       api.post( params ).then( function( resp ) {
-                               var html;
-                               if ( resp && resp.parse && resp.parse.text ) {
-                                       html = resp.parse.text['*'];
-                                       return $.Deferred().resolve( html );
-                               } else {
-                                       return $.Deferred().reject();
-                               }
-                       } ).done( function( parsedText ) {
-                               // FIXME: hacky
-                               var $tmp = $( '<div>' ).html( parsedText ), 
heading;
-                               // Extract the first heading
-                               heading = $tmp.find( 'h1 span, h2 span' ).eq( 0 
).text();
-                               // remove heading from the parsed output
-                               $tmp.find( 'h1,h2' ).eq( 0 ).remove();
-
+                       this.api.getPreview( params ).done( function( 
parsedText ) {
                                new Section( {
                                        el: self.$preview.find( '.content' ),
-                                       index: 'preview',
-                                       // doesn't account for headings with 
html inside
-                                       heading: heading,
-                                       content: $tmp.html()
+                                       content: parsedText
                                // bug 49218: stop links from being clickable 
(note user can still hold down to navigate to them)
                                } ).$( 'a' ).on( 'click', false );
                                // Emit event so we can perform enhancements to 
page
                                M.emit( 'edit-preview', self );
                        } ).fail( function() {
-                               self.$preview.addClass( 'error' ).text( mw.msg( 
'mobile-frontend-editor-error-preview' ) );
+                               self.$preview.addClass( 'error' ).find( 
'.content' ).text( mw.msg( 'mobile-frontend-editor-error-preview' ) );
                        } ).always( function() {
                                self.$spinner.hide();
                                self.$preview.show();
@@ -173,7 +143,9 @@
                },
 
                _hidePreview: function() {
-                       this.$preview.hide();
+                       this.api.abort();
+                       this.$spinner.hide();
+                       this.$preview.removeClass( 'error' ).hide();
                        this.$content.show();
                        window.scrollTo( 0, this.scrollTop );
                        this._showBar( '.initial-bar' );
diff --git a/javascripts/modules/editorNew/EditorOverlay.js 
b/javascripts/modules/editorNew/EditorOverlay.js
index b83e872..6d85bf2 100644
--- a/javascripts/modules/editorNew/EditorOverlay.js
+++ b/javascripts/modules/editorNew/EditorOverlay.js
@@ -4,7 +4,6 @@
                user = M.require( 'user' ),
                Page = M.require( 'Page' ),
                popup = M.require( 'toast' ),
-               api = M.require( 'api' ),
                inBetaOrAlpha = M.isBetaGroupMember(),
                inKeepGoingCampaign = M.query.campaign === 'mobile-keepgoing',
                Section = M.require( 'Section' ),
@@ -93,19 +92,9 @@
                                return false;
                        }
                },
+
                _showPreview: function() {
-                       var self = this, params = {
-                               action: 'parse',
-                               // Enable section preview mode to avoid errors 
(bug 49218)
-                               sectionpreview: true,
-                               // needed for pre-save transform to work (bug 
53692)
-                               pst: true,
-                               // Output mobile HTML (bug 54243)
-                               mobileformat: true,
-                               title: self.options.title,
-                               text: self.$content.val(),
-                               prop: 'text'
-                       };
+                       var self = this, params = { text: this.$content.val() };
 
                        // log save button click
                        this.log( 'save' );
@@ -124,23 +113,10 @@
                        if ( mw.config.get( 'wgIsMainPage' ) ) {
                                params.mainpage = 1; // Setting it to 0 will 
have the same effect
                        }
-                       api.post( params ).then( function( resp ) {
-                               var html;
-                               if ( resp && resp.parse && resp.parse.text ) {
-                                       html = resp.parse.text['*'];
-                                       return $.Deferred().resolve( html );
-                               } else {
-                                       return $.Deferred().reject();
-                               }
-                       } ).done( function( parsedText ) {
-                               // FIXME: hacky
-                               var $tmp = $( '<div>' ).html( parsedText );
-                               // remove heading from the parsed output
-                               $tmp.find( 'h1, h2' ).eq( 0 ).remove();
-
+                       this.api.getPreview( params ).done( function( 
parsedText ) {
                                new Section( {
                                        el: self.$preview,
-                                       content: $tmp.html()
+                                       content: parsedText
                                // bug 49218: stop links from being clickable 
(note user can still hold down to navigate to them)
                                } ).$( 'a' ).on( 'click', false );
                                // Emit event so we can perform enhancements to 
page
@@ -154,7 +130,9 @@
                },
 
                _hidePreview: function() {
-                       this.$preview.hide();
+                       this.api.abort();
+                       this.$spinner.hide();
+                       this.$preview.removeClass( 'error' ).hide();
                        this.$content.show();
                        window.scrollTo( 0, this.scrollTop );
                        this._showHidden( '.initial-header' );
diff --git a/tests/javascripts/modules/editor/test_EditorApi.js 
b/tests/javascripts/modules/editor/test_EditorApi.js
index 959a0e4..e99ebc7 100644
--- a/tests/javascripts/modules/editor/test_EditorApi.js
+++ b/tests/javascripts/modules/editor/test_EditorApi.js
@@ -338,4 +338,19 @@
                assert.ok( !editorApi.getToken.called, "don't get the token" );
        } );
 
+       QUnit.test( '#getPreview', 2, function( assert ) {
+               var editorApi = new EditorApi( { title: 'Test', sectionId: 1 } 
), doneSpy = sinon.spy();
+               sinon.stub( editorApi, 'post' ).returns( $.Deferred().resolve( {
+                       "parse": {
+                               "title": "test",
+                               "text": { "*": "<h1>Heading 1</h1><h2>Heading 
2</h2><p>test content</p>" }
+                       }
+               } ) );
+
+               editorApi.getPreview( { text: "test content" } ).done( doneSpy 
);
+
+               assert.ok( editorApi.post.calledWithMatch( { text: "test 
content" } ) );
+               assert.ok( doneSpy.calledWith( '<h2>Heading 2</h2><p>test 
content</p>' ) );
+       } );
+
 }( mw.mobileFrontend, jQuery ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7b2510f252c990779205db63b49caaa2cb92be1d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: JGonera <jgon...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to