Santhosh has uploaded a new change for review. https://gerrit.wikimedia.org/r/308515
Change subject: Split the MT card from ext.cx.tools.mt module ...................................................................... Split the MT card from ext.cx.tools.mt module Change-Id: I557a6e69537099102ac3839beb0216839f3048f5 --- M extension.json A modules/tools/ext.cx.tools.mt.card.js M modules/tools/ext.cx.tools.mt.js R modules/tools/styles/ext.cx.tools.mt.card.less 4 files changed, 270 insertions(+), 246 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation refs/changes/15/308515/1 diff --git a/extension.json b/extension.json index 257b9c1..66b839a 100644 --- a/extension.json +++ b/extension.json @@ -498,6 +498,7 @@ "ext.cx.tools.linter", "ext.cx.tools.manager", "ext.cx.tools.mt", + "ext.cx.tools.mt.card", "ext.cx.tools.mtabuse", "ext.cx.tools.reference", "ext.cx.tools.template", @@ -638,8 +639,18 @@ "scripts": [ "tools/ext.cx.tools.mt.js" ], + "dependencies": [ + "ext.cx.model", + "ext.cx.util", + "mediawiki.storage" + ] + }, + "ext.cx.tools.mt.card": { + "scripts": [ + "tools/ext.cx.tools.mt.card.js" + ], "styles": [ - "tools/styles/ext.cx.tools.mt.less" + "tools/styles/ext.cx.tools.mt.card.less" ], "messages": [ "cx-tools-mt-title", @@ -653,13 +664,9 @@ ], "dependencies": [ "ext.cx.model", - "ext.cx.source", "ext.cx.tools.card", "ext.cx.tools.manager", - "ext.cx.translation", - "ext.cx.translationview", - "ext.cx.util", - "mediawiki.storage" + "ext.cx.tools.mt" ] }, "ext.cx.tools.reference": { diff --git a/modules/tools/ext.cx.tools.mt.card.js b/modules/tools/ext.cx.tools.mt.card.js new file mode 100644 index 0000000..463ada5 --- /dev/null +++ b/modules/tools/ext.cx.tools.mt.card.js @@ -0,0 +1,257 @@ +/*! + * ContentTranslation Tools + * A tool that allows editors to translate pages from one language + * to another with the help of machine translation and other translation tools + * + * @ingroup Extensions + * @copyright See AUTHORS.txt + * @license GPL-2.0+ + */ +( function ( $, mw ) { + 'use strict'; + + var noMT = 'no-mt', + resetMT = 'reset-mt', + sourceMT = 'source-mt'; + + /** + * MT Control card + */ + function MTControlCard() { + this.mt = null; + this.$targetSection = null; + this.$sourceSection = null; + this.$card = null; + this.$translations = null; + this.$providersMenu = null; + } + + /** + * Prepare and return the card template. + * + * @return {jQuery} + */ + MTControlCard.prototype.getCard = function () { + var $titleRow, $title, $controlButtonsBlock; + + this.$card = $( '<div>' ) + .addClass( 'card mt' ); + + $title = $( '<div>' ) + .addClass( 'card__title' ) + .text( mw.msg( 'cx-tools-mt-title' ) ); + + $titleRow = $( '<div>' ) + .addClass( 'card__title-row' ) + .append( $title ); + + this.$providerSelectorTrigger = $( '<div>' ) + .addClass( 'card__trigger' ); + + this.$keepDefault = $( '<div>' ) + .addClass( 'card__control-button cx-mt-set-default' ) + .text( mw.msg( 'cx-tools-mt-set-default' ) ) + .hide(); + + $controlButtonsBlock = $( '<div>' ) + .addClass( 'card__button-block' ) + .append( this.$keepDefault ); + + this.$card.append( + $titleRow, + this.$providerSelectorTrigger, + $controlButtonsBlock + ); + this.$providersMenu = $( [] ); + + this.listen(); + + return this.$card; + }; + + /** + * Select a given provider id for the current section. + * + * @param {string} providerId Provider id + */ + MTControlCard.prototype.onProviderSelect = function ( providerId ) { + // Hide the menu + this.$providersMenu.hide(); + + // Set the main label + this.$providerSelectorTrigger.text( this.getProviderLabel( providerId ) ); + + // Apply the selected provider to the current section. + this.mt.changeProvider( providerId ); + this.mt.translate(); + }; + + MTControlCard.prototype.listen = function () { + var self = this; + + this.$providerSelectorTrigger + .on( 'click', function ( e ) { + self.$providersMenu.toggle(); + e.stopPropagation(); + } ); + + // Hide the dropdown on clicking outside of it + $( 'html' ).on( 'click', function ( e ) { + if ( !e.isDefaultPrevented() ) { + self.$providersMenu.hide(); + } + } ); + + this.$keepDefault.on( 'click', function () { + self.mt.setDefaultProvider( self.mt.provider ); + self.$keepDefault.hide(); + } ); + }; + + MTControlCard.prototype.buildProvidersMenu = function ( providers ) { + var i, self = this, + nonDefaultMT = false, + newProvider = false; + + this.$providersMenu = $( '<ul>' ) + .addClass( 'card__providers-menu' ) + .hide(); + + if ( this.mt.translationOptions.indexOf( this.mt.providers[ 0 ] ) === 0 && + this.mt.translationOptions.indexOf( this.mt.provider ) >= 0 + ) { + nonDefaultMT = true; + this.$card.find( '.card__title-row' ) + .addClass( 'card--new' ) + .append( $( '<div>' ) + .text( mw.msg( 'cx-tools-mt-new-providers-available' ) ) + .addClass( 'card__new-providers' ) + ); + } + + if ( !this.$targetSection.data( 'cx-state' ) ) { + providers.unshift( resetMT ); + } + + // Add available machine translation engines to the menu + for ( i = 0; i < providers.length; i++ ) { + newProvider = nonDefaultMT && this.mt.translationOptions.indexOf( providers[ i ] ) < 0; + this.$providersMenu.append( + this.getProviderMenuItem( providers[ i ], newProvider ) + ); + } + + // Set the main label + this.$providerSelectorTrigger.text( this.getProviderLabel( this.mt.provider ) ); + + if ( this.mt.provider !== this.mt.getPreferredProvider() ) { + this.$keepDefault.show(); + } else { + this.$keepDefault.hide(); + } + + this.$providerSelectorTrigger.after( this.$providersMenu ); + + this.$providersMenu.find( '.card__providers-menu-item' ) + .on( 'click', function () { + self.onProviderSelect( $( this ).data( 'provider' ) ); + } ); + }; + + /** + * Get the text for the menu item in the providers list. + * + * @param {string} providerId Provider id. + * @return {string} + */ + MTControlCard.prototype.getProviderLabel = function ( providerId ) { + var providerLabels = { + Yandex: mw.msg( 'cx-tools-mt-provider-title', 'Yandex.Translate' ), + 'no-mt': mw.msg( 'cx-tools-mt-dont-use' ), + 'source-mt': mw.msg( 'cx-tools-mt-use-source' ), + 'reset-mt': mw.msg( 'cx-tools-mt-reset' ) + }; + + return providerLabels[ providerId ] || mw.msg( 'cx-tools-mt-provider-title', providerId ); + }; + + /** + * Get a menu item for the providers list. + * + * @param {string} providerId Provider id. + * @return {jQuery} + */ + MTControlCard.prototype.getProviderMenuItem = function ( providerId, newProvider ) { + var $label, + providerIdPrefix = 'cx-provider-', + selected = '', + $new = $( [] ); + + $label = $( '<span>' ) + .text( this.getProviderLabel( providerId ) ); + + if ( newProvider ) { + $new = $( '<span>' ) + .text( mw.msg( 'cx-tools-mt-new-provider' ) ) + .addClass( 'card__providers-menu-item--new' ); + } + + // Mark the selected item + if ( providerId === this.mt.provider ) { + selected = 'selected'; + } + + return $( '<li>' ) + .addClass( [ + 'card__providers-menu-item', selected, providerIdPrefix + providerId + ].join( ' ' ) ) + .attr( 'data-provider', providerId ) + .append( $label, $new ); + }; + + MTControlCard.prototype.start = function ( $section ) { + var self = this, + sourceId; + + this.$targetSection = $section; + sourceId = this.$targetSection.data( 'source' ); + this.$sourceSection = mw.cx.getSourceSection( sourceId ); + this.mt = this.$sourceSection.data( 'cxmt' ); + + if ( !this.mt ) { + this.mt = new mw.cx.MachineTranslation( sourceId ); + this.$sourceSection.data( 'cxmt', this.mt ); + } + + this.mt.init().then( function () { + var menuItems = self.mt.providers.concat( + self.mt.translationOptions.filter( function ( item ) { + return self.mt.providers.indexOf( item ) < 0; + } ) ); + + self.buildProvidersMenu( menuItems ); + } ); + + this.$card.show(); + this.onShow(); + }; + + MTControlCard.prototype.stop = function () { + this.$card.remove(); + mw.hook( 'mw.cx.tools.shown' ).fire( true ); + }; + + MTControlCard.prototype.onShow = function () { + mw.hook( 'mw.cx.tools.shown' ).fire( true ); + }; + + MTControlCard.prototype.getTriggerEvents = function () { + return [ + 'mw.cx.translation.focus', + 'mw.cx.translation.change' + ]; + }; + + mw.cx.tools.mt = MTControlCard; + +}( jQuery, mediaWiki ) ); diff --git a/modules/tools/ext.cx.tools.mt.js b/modules/tools/ext.cx.tools.mt.js index a4c8d8c..b7e98c8 100644 --- a/modules/tools/ext.cx.tools.mt.js +++ b/modules/tools/ext.cx.tools.mt.js @@ -439,246 +439,6 @@ targetLanguage: mw.cx.targetLanguage }; - /** - * MT Control card - */ - function MTControlCard() { - this.mt = null; - this.$targetSection = null; - this.$sourceSection = null; - this.$card = null; - this.$translations = null; - this.$providersMenu = null; - } - - /** - * Prepare and return the card template. - * - * @return {jQuery} - */ - MTControlCard.prototype.getCard = function () { - var $titleRow, $title, $controlButtonsBlock; - - this.$card = $( '<div>' ) - .addClass( 'card mt' ); - - $title = $( '<div>' ) - .addClass( 'card__title' ) - .text( mw.msg( 'cx-tools-mt-title' ) ); - - $titleRow = $( '<div>' ) - .addClass( 'card__title-row' ) - .append( $title ); - - this.$providerSelectorTrigger = $( '<div>' ) - .addClass( 'card__trigger' ); - - this.$keepDefault = $( '<div>' ) - .addClass( 'card__control-button cx-mt-set-default' ) - .text( mw.msg( 'cx-tools-mt-set-default' ) ) - .hide(); - - $controlButtonsBlock = $( '<div>' ) - .addClass( 'card__button-block' ) - .append( this.$keepDefault ); - - this.$card.append( - $titleRow, - this.$providerSelectorTrigger, - $controlButtonsBlock - ); - this.$providersMenu = $( [] ); - - this.listen(); - - return this.$card; - }; - - /** - * Select a given provider id for the current section. - * - * @param {string} providerId Provider id - */ - MTControlCard.prototype.onProviderSelect = function ( providerId ) { - // Hide the menu - this.$providersMenu.hide(); - - // Set the main label - this.$providerSelectorTrigger.text( this.getProviderLabel( providerId ) ); - - // Apply the selected provider to the current section. - this.mt.changeProvider( providerId ); - this.mt.translate(); - }; - - MTControlCard.prototype.listen = function () { - var self = this; - - this.$providerSelectorTrigger - .on( 'click', function ( e ) { - self.$providersMenu.toggle(); - e.stopPropagation(); - } ); - - // Hide the dropdown on clicking outside of it - $( 'html' ).on( 'click', function ( e ) { - if ( !e.isDefaultPrevented() ) { - self.$providersMenu.hide(); - } - } ); - - this.$keepDefault.on( 'click', function () { - self.mt.setDefaultProvider( self.mt.provider ); - self.$keepDefault.hide(); - } ); - }; - - MTControlCard.prototype.buildProvidersMenu = function ( providers ) { - var i, self = this, - nonDefaultMT = false, - newProvider = false; - - this.$providersMenu = $( '<ul>' ) - .addClass( 'card__providers-menu' ) - .hide(); - - if ( this.mt.translationOptions.indexOf( this.mt.providers[ 0 ] ) === 0 && - this.mt.translationOptions.indexOf( this.mt.provider ) >= 0 - ) { - nonDefaultMT = true; - this.$card.find( '.card__title-row' ) - .addClass( 'card--new' ) - .append( $( '<div>' ) - .text( mw.msg( 'cx-tools-mt-new-providers-available' ) ) - .addClass( 'card__new-providers' ) - ); - } - - if ( !this.$targetSection.data( 'cx-state' ) ) { - providers.unshift( resetMT ); - } - - // Add available machine translation engines to the menu - for ( i = 0; i < providers.length; i++ ) { - newProvider = nonDefaultMT && this.mt.translationOptions.indexOf( providers[ i ] ) < 0; - this.$providersMenu.append( - this.getProviderMenuItem( providers[ i ], newProvider ) - ); - } - - // Set the main label - this.$providerSelectorTrigger.text( this.getProviderLabel( this.mt.provider ) ); - - if ( this.mt.provider !== this.mt.getPreferredProvider() ) { - this.$keepDefault.show(); - } else { - this.$keepDefault.hide(); - } - - this.$providerSelectorTrigger.after( this.$providersMenu ); - - this.$providersMenu.find( '.card__providers-menu-item' ) - .on( 'click', function () { - self.onProviderSelect( $( this ).data( 'provider' ) ); - } ); - }; - - /** - * Get the text for the menu item in the providers list. - * - * @param {string} providerId Provider id. - * @return {string} - */ - MTControlCard.prototype.getProviderLabel = function ( providerId ) { - var providerLabels = { - Yandex: mw.msg( 'cx-tools-mt-provider-title', 'Yandex.Translate' ), - 'no-mt': mw.msg( 'cx-tools-mt-dont-use' ), - 'source-mt': mw.msg( 'cx-tools-mt-use-source' ), - 'reset-mt': mw.msg( 'cx-tools-mt-reset' ) - }; - - return providerLabels[ providerId ] || mw.msg( 'cx-tools-mt-provider-title', providerId ); - }; - - /** - * Get a menu item for the providers list. - * - * @param {string} providerId Provider id. - * @return {jQuery} - */ - MTControlCard.prototype.getProviderMenuItem = function ( providerId, newProvider ) { - var $label, - providerIdPrefix = 'cx-provider-', - selected = '', - $new = $( [] ); - - $label = $( '<span>' ) - .text( this.getProviderLabel( providerId ) ); - - if ( newProvider ) { - $new = $( '<span>' ) - .text( mw.msg( 'cx-tools-mt-new-provider' ) ) - .addClass( 'card__providers-menu-item--new' ); - } - - // Mark the selected item - if ( providerId === this.mt.provider ) { - selected = 'selected'; - } - - return $( '<li>' ) - .addClass( [ - 'card__providers-menu-item', selected, providerIdPrefix + providerId - ].join( ' ' ) ) - .attr( 'data-provider', providerId ) - .append( $label, $new ); - }; - - MTControlCard.prototype.start = function ( $section ) { - var self = this, - sourceId; - - this.$targetSection = $section; - sourceId = this.$targetSection.data( 'source' ); - this.$sourceSection = mw.cx.getSourceSection( sourceId ); - this.mt = this.$sourceSection.data( 'cxmt' ); - - if ( !this.mt ) { - this.mt = new MachineTranslation( sourceId ); - this.$sourceSection.data( 'cxmt', this.mt ); - } - - this.mt.init().then( function () { - var menuItems = self.mt.providers.concat( - self.mt.translationOptions.filter( function ( item ) { - return self.mt.providers.indexOf( item ) < 0; - } ) ); - - self.buildProvidersMenu( menuItems ); - } ); - - this.$card.show(); - this.onShow(); - }; - - MTControlCard.prototype.stop = function () { - this.$card.remove(); - mw.hook( 'mw.cx.tools.shown' ).fire( true ); - }; - - MTControlCard.prototype.onShow = function () { - mw.hook( 'mw.cx.tools.shown' ).fire( true ); - }; - - MTControlCard.prototype.getTriggerEvents = function () { - return [ - 'mw.cx.translation.focus', - 'mw.cx.translation.change' - ]; - }; - - mw.cx.tools.mt = MTControlCard; - function initTranslationRequest( sourceSectionId ) { var mt; mt = new MachineTranslation( sourceSectionId ); diff --git a/modules/tools/styles/ext.cx.tools.mt.less b/modules/tools/styles/ext.cx.tools.mt.card.less similarity index 100% rename from modules/tools/styles/ext.cx.tools.mt.less rename to modules/tools/styles/ext.cx.tools.mt.card.less -- To view, visit https://gerrit.wikimedia.org/r/308515 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I557a6e69537099102ac3839beb0216839f3048f5 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/ContentTranslation Gerrit-Branch: master Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits