Santhosh has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/357356 )
Change subject: CX2: Move title and namespace handler to TranslationController
......................................................................
CX2: Move title and namespace handler to TranslationController
These handlers were moved from TranslationView to TranslationController.
A notable change is mw.cx.TargetArticle no longer has targetTitle
property. Its value changes whenever translator edit it. So reading
from a property will not get current value. Introduced getTargetTitle
method, that will read the current value from mw.cx.dm.Translation
instance. That value is guaranteed to be up to date.
Also moved the title, namespace processing code to mw.cx.util
Bug: T166768
Change-Id: Id7cf56db088750e2bdc611a3958b8c75a5d8819e
---
M extension.json
M modules/mw.cx.TargetArticle.js
M modules/mw.cx.TranslationController.js
M modules/ui/mw.cx.ui.TranslationView.js
M modules/util/mw.cx.util.js
5 files changed, 77 insertions(+), 72 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation
refs/changes/56/357356/1
diff --git a/extension.json b/extension.json
index 689b19f..2e4d3aa 100644
--- a/extension.json
+++ b/extension.json
@@ -1478,7 +1478,8 @@
"mw.cx.ui.Columns",
"mw.cx.ui.Header",
"mw.cx.ui.TranslationUnits",
- "mw.cx.ui.PublishSettingsWidget"
+ "mw.cx.ui.PublishSettingsWidget",
+ "mw.cx.util"
]
},
"mw.cx.ui.TranslationView.legacy": {
diff --git a/modules/mw.cx.TargetArticle.js b/modules/mw.cx.TargetArticle.js
index 7ba79c5..6537987 100644
--- a/modules/mw.cx.TargetArticle.js
+++ b/modules/mw.cx.TargetArticle.js
@@ -10,7 +10,6 @@
this.config = config;
this.siteMapper = config.siteMapper;
this.sourceTitle = config.sourceTitle;
- this.targetTitle = config.targetTitle;
this.sourceLanguage = config.sourceLanguage;
this.targetLanguage = config.targetLanguage;
this.sourceRevision = config.sourceRevision;
@@ -44,7 +43,7 @@
this.publishDeferred = $.Deferred();
// Check for title conflicts
- this.checkTargetTitle( this.targetTitle ).then( function ( title ) {
+ this.checkTargetTitle( this.getTargetTitle() ).then( function ( title )
{
apiParams.title = title;
// Post the content to publish.
return new mw.Api().postWithToken( 'csrf', apiParams, {
@@ -404,12 +403,13 @@
}.bind( this ) );
};
+/**
+ * Get current target title from translation data model.
+ * Not the translation title can be changed by translator at any point of
translation.
+ * @return {string} target title
+ */
mw.cx.TargetArticle.prototype.getTargetTitle = function () {
- return this.targetTitle;
-};
-
-mw.cx.TargetArticle.prototype.setTargetTitle = function ( title ) {
- this.targetTitle = title;
+ return this.translation.getTargetTitle();
};
/**
@@ -548,9 +548,9 @@
* and show a warning if needed.
*/
mw.cx.TargetArticle.prototype.validateTargetTitle = function () {
- var viewTargetUrl = this.siteMapper.getPageUrl( this.targetLanguage,
this.targetTitle );
+ var viewTargetUrl = this.siteMapper.getPageUrl( this.targetLanguage,
this.getTargetTitle() );
- this.isTitleExistInLanguage( this.targetLanguage, this.targetTitle )
+ this.isTitleExistInLanguage( this.targetLanguage, this.getTargetTitle()
)
.then( function ( pageExist ) {
// If page doesn't exist, it's OK
if ( !pageExist ) {
@@ -560,11 +560,11 @@
mw.hook( 'mw.cx.warning' ).fire( mw.message(
'cx-translation-target-page-exists',
viewTargetUrl,
- this.targetTitle
+ this.getTargetTitle()
) );
} );
};
mw.cx.TargetArticle.prototype.getTargetURL = function () {
- return mw.util.getUrl( this.targetTitle );
+ return mw.util.getUrl( this.getTargetTitle() );
};
diff --git a/modules/mw.cx.TranslationController.js
b/modules/mw.cx.TranslationController.js
index 869b091..29fed94 100644
--- a/modules/mw.cx.TranslationController.js
+++ b/modules/mw.cx.TranslationController.js
@@ -37,7 +37,8 @@
} );
this.view.connect( this, {
- publish: 'publish'
+ publish: 'publish',
+ targetTitleChange: 'onTranslationTitleChange'
} );
// Save when CTRL+S is pressed.
@@ -318,3 +319,28 @@
} );
return this.targetArticle;
};
+
+/**
+ * Translation title change handler
+ * @param {string} newTitle The new title
+ */
+mw.cx.TranslationController.prototype.onTranslationTitleChange = function (
newTitle ) {
+ var currentTitleObj, newTitleObj;
+
+ if ( this.translation.getTargetTitle() === newTitle ) {
+ // Nothing really changed.
+ return;
+ }
+
+ newTitleObj = mw.Title.newFromText( newTitle );
+ if ( !newTitleObj ) {
+ mw.log.error( '[CX] Invalid target title' );
+ return;
+ }
+ currentTitleObj = mw.Title.newFromText(
this.translation.getTargetTitle() );
+ this.translation.setTargetTitle( newTitle );
+
+ if ( currentTitleObj !== newTitleObj.getNamespaceId() ) {
+ this.view.changeNamespace( newTitleObj.getNamespaceId() );
+ }
+};
diff --git a/modules/ui/mw.cx.ui.TranslationView.js
b/modules/ui/mw.cx.ui.TranslationView.js
index 9ca69c4..f1b845d 100644
--- a/modules/ui/mw.cx.ui.TranslationView.js
+++ b/modules/ui/mw.cx.ui.TranslationView.js
@@ -198,33 +198,12 @@
* @param {number} namespaceId
*/
mw.cx.ui.TranslationView.prototype.onPublishNamespaceChange = function (
namespaceId ) {
- var currentTitleObj, title, newTitle, currentNamespace, username;
+ var newTitle;
- currentTitleObj = new mw.Title( this.translation.getTargetTitle() );
- currentNamespace = currentTitleObj.getNamespaceId();
- if ( namespaceId === currentNamespace ) {
- // No change.
- return;
- }
-
- // Get the current title string
- title = currentTitleObj.getMainText();
- if ( currentNamespace === mw.config.get( 'wgNamespaceIds' ).user ) {
- // User namespace. Get the title part alone after removing
User:username/ part
- title = title.substr( title.indexOf( '/' ) + 1 );
- }
-
- if ( namespaceId === mw.config.get( 'wgNamespaceIds' ).user ) {
- username = mw.user.getName();
- title = mw.Title.newFromText( username + '/' + title,
namespaceId ).toText();
- }
- newTitle = mw.Title.newFromText( title, namespaceId ).toText();
-
- this.getTargetArticle().setTargetTitle( newTitle );
+ newTitle = mw.cx.getTitleForNamespace(
this.translation.getTargetTitle(), namespaceId );
this.columns.translationColumn.setTargetTitle( newTitle );
+ // This will take care of necessary event firing for title change.
mw.log( '[CX] Target title changed to ' + newTitle );
- // Namespace changed. Enable the publish button
- this.publishButton.setDisabled( false );
};
/**
@@ -247,51 +226,20 @@
};
/**
- * @return {mw.cx.TargetArticle} targetArticle instance
- */
-mw.cx.ui.TranslationView.prototype.getTargetArticle = function () {
- if ( this.targetArticle ) {
- return this.targetArticle;
- }
- this.targetArticle = new mw.cx.TargetArticle( this.translation, this,
this.config );
- this.getTargetArticle().connect( this, {
- publishSuccess: 'onPublishSuccess'
- } );
- return this.targetArticle;
-};
-
-/**
* Translation title change handler
* @param {string} changedTitle The new title
*/
mw.cx.ui.TranslationView.prototype.onTranslationTitleChange = function (
changedTitle ) {
- var currentTitleObj, currentNamespace;
-
- if ( !this.translation ) {
- mw.log( '[CX] Translation not ready yet' );
- return;
- }
- this.translation.setTargetTitle( changedTitle );
- // TODO: Ideally, target article should update the title by listening to
- // change in this.translation
- this.getTargetArticle().setTargetTitle( changedTitle );
+ this.emit( 'targetTitleChange', changedTitle );
// Align translation titles when it get changed/being edited
mw.cx.alignSections(
this.columns.sourceColumn.titleWidget.$element,
this.columns.translationColumn.titleWidget.$element
);
+};
- // Restore the namespace choice
- currentTitleObj = mw.Title.newFromText( changedTitle );
- if ( !currentTitleObj ) {
- mw.log.error( '[CX] Invalid target title' );
- return;
- }
- currentNamespace = currentTitleObj.getNamespaceId();
- this.publishSettings.setDestinationNamespace( currentNamespace );
-
- // Translation title change is a change trigger for translation.
- this.onChange();
+mw.cx.ui.TranslationView.prototype.changeNamespace = function ( namespaceId ) {
+ this.publishSettings.setDestinationNamespace( namespaceId );
};
mw.cx.ui.TranslationView.prototype.onPublishSuccess = function () {
diff --git a/modules/util/mw.cx.util.js b/modules/util/mw.cx.util.js
index 3d3d87b..ec2e61d 100644
--- a/modules/util/mw.cx.util.js
+++ b/modules/util/mw.cx.util.js
@@ -37,3 +37,33 @@
}
}
};
+
+/**
+ * Get the title after changing its namespace to new one.
+ * @param {string} currentTitle Original title string
+ * @param {number} newNamespaceId New namespace id
+ * @return {string} New title with changed namespace
+ */
+mw.cx.getTitleForNamespace = function ( currentTitle, newNamespaceId ) {
+ var currentTitleObj, currentNamespace, username;
+
+ currentTitleObj = new mw.Title( currentTitle );
+ currentNamespace = currentTitleObj.getNamespaceId();
+ if ( newNamespaceId === currentNamespace ) {
+ // No change.
+ return currentTitle;
+ }
+
+ // Get the current title string
+ currentTitle = currentTitleObj.getMainText();
+ if ( currentNamespace === mw.config.get( 'wgNamespaceIds' ).user ) {
+ // User namespace. Get the title part alone after removing
User:username/ part
+ currentTitle = currentTitle.substr( currentTitle.indexOf( '/' )
+ 1 );
+ }
+
+ if ( newNamespaceId === mw.config.get( 'wgNamespaceIds' ).user ) {
+ username = mw.user.getName();
+ currentTitle = mw.Title.newFromText( username + '/' +
currentTitle, newNamespaceId ).toText();
+ }
+ return mw.Title.newFromText( currentTitle, newNamespaceId ).toText();
+};
--
To view, visit https://gerrit.wikimedia.org/r/357356
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id7cf56db088750e2bdc611a3958b8c75a5d8819e
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