Santhosh has uploaded a new change for review. https://gerrit.wikimedia.org/r/305489
Change subject: WIP: Template card ...................................................................... WIP: Template card Change-Id: Iae750e63e00db991386964b99fd51250bd9ca747 --- M extension.json M i18n/en.json M modules/tools/ext.cx.tools.template.js 3 files changed, 205 insertions(+), 18 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation refs/changes/89/305489/1 diff --git a/extension.json b/extension.json index ebcbc53..9fd8604 100644 --- a/extension.json +++ b/extension.json @@ -685,6 +685,12 @@ ], "dependencies": [ "mediawiki.RegExp" + ], + "messages": [ + "cx-tools-template-title", + "cx-template-action-adapt", + "cx-template-action-keep-original", + "cx-template-action-skip" ] }, "ext.cx.tools.images": { diff --git a/i18n/en.json b/i18n/en.json index 45bf7b2..f5f1c73 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -211,5 +211,9 @@ "cx-page-old-revision-loaded": "This translation is based on an older version of the content. The source page may have [$1 changed significantly]. You can continue this translation or start it again to use the updated content.", "cx-tools-mt-new-provider": "new", "cx-tools-mt-new-providers-available": "with new options", - "cx-continue-translation": "Continue translation" + "cx-continue-translation": "Continue translation", + "cx-tools-template-title": "Template", + "cx-template-action-adapt": "Use equivalent template", + "cx-template-action-keep-original": "Keep original template", + "cx-template-action-skip": "Skip template" } diff --git a/modules/tools/ext.cx.tools.template.js b/modules/tools/ext.cx.tools.template.js index 9a5b7b1..1c542ce 100644 --- a/modules/tools/ext.cx.tools.template.js +++ b/modules/tools/ext.cx.tools.template.js @@ -32,6 +32,7 @@ this.siteMapper = this.options.siteMapper; this.sourceLanguage = this.options.sourceLanguage; this.targetLanguage = this.options.targetLanguage; + this.action = 'adapt'; } /** @@ -39,16 +40,12 @@ */ TemplateTool.prototype.deconstruct = function () { mw.log( '[CX] Deconstructing template ' + this.templateTitle ); - this.$template.removeAttr( 'typeof about data-mw data-parsoid' ); + // this.$template.removeAttr( 'typeof about data-mw data-parsoid' ); }; - /** - * Get the template data from source section. Target section - * might not have it always. - */ - TemplateTool.prototype.getSourceTemplateData = function () { - var templateData, - aboutAttr = this.$template.attr( 'about' ); + function getSourceTemplate( $template ) { + var $sourceTemplate = $( [] ), + aboutAttr = $template.attr( 'about' ); $( '[about="' + aboutAttr + '"]' ).each( function ( index, fragment ) { var $fragment = $( fragment ); @@ -59,13 +56,20 @@ $fragment.is( '[typeof*="mw:Transclusion"]' ) && $fragment.attr( 'data-mw' ) ) { - templateData = $fragment.data( 'mw' ); - // Exit. + $sourceTemplate = $fragment; return false; } } ); - return templateData; + return $sourceTemplate; + } + + /** + * Get the template data from source section. Target section + * might not have it always. + */ + TemplateTool.prototype.getSourceTemplateData = function () { + return getSourceTemplate( this.$template ).data( 'mw' ); }; /** @@ -392,9 +396,6 @@ return $.Deferred().resolve().promise(); } - // Remove the typeof attribute for now. We will set it once we successfully adapt the template - // Otherwise, parsoid will fail fatally while parsing. - this.$template.removeAttr( 'typeof' ); this.templateTitle = this.templateData.parts[ 0 ].template.target.wt; // Normalize the name title = mw.Title.newFromText( self.templateTitle ); @@ -491,7 +492,183 @@ } } - $( function () { - mw.hook( 'mw.cx.translation.postMT' ).add( processTemplates ); - } ); + /** + * Template Control card + */ + function TemplateControlCard() { + this.mt = null; + this.$targetSection = null; + this.$sourceSection = null; + this.$card = null; + this.$translations = null; + this.$providersMenu = null; + this.actions = [ 'adapt', 'keep-original', 'skip' ]; + } + + /** + * Prepare and return the card template. + * + * @return {jQuery} + */ + TemplateControlCard.prototype.getCard = function () { + var $titleRow, $title; + + this.$card = $( '<div>' ) + .addClass( 'card template' ); + + $title = $( '<div>' ) + .addClass( 'card__title' ) + .text( mw.msg( 'cx-tools-template-title' ) ); + + $titleRow = $( '<div>' ) + .addClass( 'card__title-row' ) + .append( $title ); + + this.$actionSelectorTrigger = $( '<div>' ) + .addClass( 'card__trigger' ); + + this.$card.append( + $titleRow, + this.$actionSelectorTrigger + ); + + this.$actionsMenu = $( [] ); + + this.listen(); + + return this.$card; + }; + + /** + * Select a given action id for the current section. + * + * @param {string} actionId Action id + */ + TemplateControlCard.prototype.onProviderSelect = function ( actionId ) { + // Hide the menu + this.$actionsMenu.hide(); + + // Set the main label + this.$actionSelectorTrigger.text( this.getActionMenuItemLabel( actionId ) ); + + // Apply the selected provider to the current section. + // this.mt.changeAction actionId ); + // this.mt.translate(); + }; + + TemplateControlCard.prototype.listen = function () { + var self = this; + + this.$actionSelectorTrigger + .on( 'click', function ( e ) { + self.$actionsMenu.toggle(); + e.stopPropagation(); + } ); + + // Hide the dropdown on clicking outside of it + $( 'html' ).on( 'click', function ( e ) { + if ( !e.isDefaultPrevented() ) { + self.$actionsMenu.hide(); + } + } ); + }; + + TemplateControlCard.prototype.buildActionsMenu = function ( actions ) { + var i, + self = this; + + this.$actionsMenu = $( '<ul>' ) + .addClass( 'card__template-options-menu' ) + .hide(); + + for ( i = 0; i < actions.length; i++ ) { + this.$actionsMenu.append( this.getActionMenuItem( actions[ i ] ) ); + } + + // Set the main label + this.$actionSelectorTrigger + .text( this.getActionMenuItemLabel( this.template.action ) ) + .after( this.$actionsMenu ); + + this.$actionsMenu.find( '.card__template-actions-item' ) + .on( 'click', function () { + self.onActionSelect( $( this ).data( 'template-action' ) ); + } ); + }; + + TemplateControlCard.prototype.getActionMenuItemLabel = function ( actionId ) { + return mw.msg( 'cx-template-action-' + actionId ); + }; + + /** + * Get a menu item for the actions list. + * + * @param {string} actionId Action id. + * @return {jQuery} + */ + TemplateControlCard.prototype.getActionMenuItem = function ( actionId ) { + var $label, + + selected = ''; + + $label = $( '<span>' ) + .text( this.getActionMenuItemLabel( actionId ) ); + + // Mark the selected item + if ( actionId === this.template.action ) { + selected = 'selected'; + } + + return $( '<li>' ) + .addClass( [ + 'card__template-action-item', selected, actionId + ].join( ' ' ) ) + .attr( 'data-template-action', actionId ) + .append( $label ); + }; + + TemplateControlCard.prototype.start = function ( $section ) { + var sourceId; + + this.$targetTemplate = $section; + sourceId = $section.data( 'source' ); + this.$sourceSection = mw.cx.getSourceSection( sourceId ); + this.$sourceTemplate = getSourceTemplate( $section ); + // Source section and source template wont be same since the template + // with template definition may be another element with same about attribute. + if ( !this.$sourceTemplate.is( '[typeof~="mw:Transclusion"]' ) ) { + this.stop(); + return; + } + + this.template = this.$sourceTemplate.data( 'cxtemplate' ); + if ( !this.template ) { + this.template = new mw.cx.TemplateTool( this.$sourceTemplate[ 0 ] ); + this.$sourceTemplate.data( 'cxmt', this.template ); + this.template.process(); + } + this.buildActionsMenu( this.actions ); + this.$card.show(); + this.onShow(); + }; + + TemplateControlCard.prototype.stop = function () { + this.$card.remove(); + mw.hook( 'mw.cx.tools.shown' ).fire( true ); + }; + + TemplateControlCard.prototype.onShow = function () { + mw.hook( 'mw.cx.tools.shown' ).fire( true ); + }; + + TemplateControlCard.prototype.getTriggerEvents = function () { + return [ + 'mw.cx.translation.focus', + 'mw.cx.translation.change', + 'mw.cx.translation.add', + 'mw.cx.translation.postMT' + ]; + }; + + mw.cx.tools.template = TemplateControlCard; }( jQuery, mediaWiki ) ); -- To view, visit https://gerrit.wikimedia.org/r/305489 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iae750e63e00db991386964b99fd51250bd9ca747 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