Santhosh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/357348 )

Change subject: CX2: Move publish, success and error handler from view to 
controller
......................................................................

CX2: Move publish, success and error handler from view to controller

Move publish, success and error handlers to TranslationController
from TranslationView.

Bug: T166768
Change-Id: I4ef6f55cf77080cb815d6d613caba177f6280290
---
M modules/mw.cx.TargetArticle.js
M modules/mw.cx.TranslationController.js
M modules/ui/mw.cx.ui.TranslationView.js
3 files changed, 84 insertions(+), 13 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/48/357348/1

diff --git a/modules/mw.cx.TargetArticle.js b/modules/mw.cx.TargetArticle.js
index 677b414..7ba79c5 100644
--- a/modules/mw.cx.TargetArticle.js
+++ b/modules/mw.cx.TargetArticle.js
@@ -564,3 +564,7 @@
                        ) );
                } );
 };
+
+mw.cx.TargetArticle.prototype.getTargetURL = function () {
+       return mw.util.getUrl( this.targetTitle );
+};
diff --git a/modules/mw.cx.TranslationController.js 
b/modules/mw.cx.TranslationController.js
index d5c1a31..869b091 100644
--- a/modules/mw.cx.TranslationController.js
+++ b/modules/mw.cx.TranslationController.js
@@ -36,6 +36,10 @@
                change: 'save'
        } );
 
+       this.view.connect( this, {
+               publish: 'publish'
+       } );
+
        // Save when CTRL+S is pressed.
        document.onkeydown = function ( e ) {
                // See 
https://medium.com/medium-eng/the-curious-case-of-disappearing-polish-s-fa398313d4df
@@ -253,3 +257,64 @@
        }
        // TODO: Find out orphan translation units and handle them.
 };
+
+/**
+ * Publish the translation
+ */
+mw.cx.TranslationController.prototype.publish = function () {
+       this.emit( 'publishing' );
+       mw.log( '[CX] Publishing translation...' );
+       this.getTargetArticle().publish();
+};
+
+/**
+ * Publish success handler
+ */
+mw.cx.TranslationController.prototype.onPublishSuccess = function () {
+       mw.log( '[CX] Publishing finished successfully' );
+       this.view.emit( 'publishSuccess', 
this.getTargetArticle().getTargetURL() );
+       // Event logging
+       mw.hook( 'mw.cx.translation.published' ).fire(
+               this.translation.sourceLanguage,
+               this.translation.targetLanguage,
+               this.translation.sourceTitle,
+               this.translation.targetTitle
+       );
+};
+
+/**
+ * Publish error handler
+ * @param {string} errorCode
+ * @param {Object} details
+ */
+mw.cx.TranslationController.prototype.onPublishFailure = function ( errorCode, 
details ) {
+       mw.log( '[CX] Publishing failed ' + errorCode );
+       if ( details.exception instanceof Error ) {
+               details.exception = details.exception.toString();
+       }
+       this.view.emit( 'publishFailure', errorCode );
+
+       // Event logging
+       mw.hook( 'mw.cx.translation.publish.error' ).fire(
+               mw.cx.sourceLanguage,
+               mw.cx.targetLanguage,
+               mw.cx.sourceTitle,
+               this.targetTitle,
+               JSON.stringify( details )
+       );
+};
+
+/**
+ * @return {mw.cx.TargetArticle} targetArticle instance
+ */
+mw.cx.TranslationController.prototype.getTargetArticle = function () {
+       if ( this.targetArticle ) {
+               return this.targetArticle;
+       }
+       this.targetArticle = new mw.cx.TargetArticle( this.translation, 
this.view, this.config );
+       this.getTargetArticle().connect( this, {
+               publishSuccess: 'onPublishSuccess',
+               publishError: 'onPublishFailure'
+       } );
+       return this.targetArticle;
+};
diff --git a/modules/ui/mw.cx.ui.TranslationView.js 
b/modules/ui/mw.cx.ui.TranslationView.js
index d8c7c89..9ca69c4 100644
--- a/modules/ui/mw.cx.ui.TranslationView.js
+++ b/modules/ui/mw.cx.ui.TranslationView.js
@@ -61,7 +61,9 @@
        this.connect( this, {
                change: 'onChange',
                translationRestored: 'onTranslationRestore',
-               titleChange: 'onTranslationTitleChange'
+               titleChange: 'onTranslationTitleChange',
+               publishSuccess: 'onPublishSuccess',
+               publishFailure: 'onPublishFailure'
        } );
        this.columns.translationColumn.connect( this, {
                titleChange: 'onTranslationTitleChange'
@@ -169,7 +171,7 @@
                choose: 'onPublishNamespaceChange'
        } );
        this.publishButton.connect( this, {
-               click: 'publish'
+               click: 'onPublishButtonClick'
        } );
        mw.hook( 'mw.cx.progress' ).add( function ( weights ) {
                self.publishButton.setDisabled( weights.any === 0 );
@@ -236,14 +238,12 @@
 };
 
 /**
- * Publish the translation
+ * Publish button click handler
  */
-mw.cx.ui.TranslationView.prototype.publish = function () {
+mw.cx.ui.TranslationView.prototype.onPublishButtonClick = function () {
        // Disable the trigger button
        this.publishButton.setDisabled( true ).setLabel( mw.msg( 
'cx-publish-button-publishing' ) );
-       this.getTargetArticle().publish().always( function () {
-               this.publishButton.setDisabled( true ).setLabel( mw.msg( 
'cx-publish-button' ) );
-       }.bind( this ) );
+       this.emit( 'publish' );
 };
 
 /**
@@ -303,13 +303,15 @@
                        target: '_blank'
                } ).text( this.translation.targetTitle )[ 0 ].outerHTML
        ) );
-       // Event logging
-       mw.hook( 'mw.cx.translation.published' ).fire(
-               this.translation.sourceLanguage,
-               this.translation.targetLanguage,
-               this.translation.sourceTitle,
-               this.translation.targetTitle
+       this.publishButton.setDisabled( false ).setLabel( mw.msg( 
'cx-publish-button' ) );
+};
+
+mw.cx.ui.TranslationView.prototype.onPublishFailure = function ( error ) {
+       this.showMessage(
+               'error',
+               mw.msg( 'cx-publish-page-error', error )
        );
+       this.publishButton.setDisabled( false ).setLabel( mw.msg( 
'cx-publish-button' ) );
 };
 
 /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4ef6f55cf77080cb815d6d613caba177f6280290
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Santhosh <[email protected]>

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

Reply via email to