Santhosh has uploaded a new change for review.

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

Change subject: WIP: Inline template editor
......................................................................

WIP: Inline template editor

Bug: T144004
Change-Id: I574df83798975fd0c9ae3b9186c0bbfd92e210e0
---
M modules/tools/ext.cx.tools.template.card.js
M modules/tools/ext.cx.tools.template.js
2 files changed, 72 insertions(+), 10 deletions(-)


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

diff --git a/modules/tools/ext.cx.tools.template.card.js 
b/modules/tools/ext.cx.tools.template.card.js
index 5bad039..4fdf9c8 100644
--- a/modules/tools/ext.cx.tools.template.card.js
+++ b/modules/tools/ext.cx.tools.template.card.js
@@ -170,19 +170,18 @@
         * @param {jQuery|string} $section section or section identifier
         */
        TemplateControlCard.prototype.start = function ( $section ) {
-               var $sourceSection, $targetTemplate, $sourceTemplate, 
storedState = false;
+               var sourceId, $targetTemplate, $sourceTemplate, storedState = 
false;
 
                if ( $section instanceof jQuery ) {
-                       this.sourceId = $section.data( 'source' );
+                       $targetTemplate = $section.filter( 
'[typeof~="mw:Transclusion"]' );
                } else {
-                       this.sourceId = $section;
+                       sourceId = $section;
+                       $targetTemplate = mw.cx.getTranslationSection( sourceId 
);
                }
 
-               $sourceSection = mw.cx.getSourceSection( this.sourceId );
-               $targetTemplate = mw.cx.getTranslationSection( this.sourceId );
                // Source section and source template wont be same since the 
template
                // with template definition may be another element with same 
about attribute.
-               $sourceTemplate = mw.cx.Template.static.getTemplateDef( 
$sourceSection );
+               $sourceTemplate = mw.cx.Template.static.getTemplateDef( 
$targetTemplate );
 
                if ( !$sourceTemplate.length ) {
                        this.stop();
@@ -293,8 +292,8 @@
                var $new = $newTemplate
                        .clone()
                        .attr( {
-                               id: 'cx' + this.sourceId,
-                               'data-source': this.sourceId,
+                               id: 'cx' + this.sourceTemplate.$template.prop( 
'id' ),
+                               'data-source': 
this.sourceTemplate.$template.prop( 'id' ),
                                'data-template-state': state,
                                typeof: $newTemplate.attr( 'typeof' ),
                                about: this.sourceTemplate.$template.attr( 
'about' ),
@@ -354,8 +353,7 @@
        TemplateControlCard.prototype.getTriggerEvents = function () {
                return [
                        'mw.cx.translation.template.focus',
-                       'mw.cx.translation.add',
-                       'mw.cx.translation.postMT'
+                       'mw.cx.translation.add'
                ];
        };
 
diff --git a/modules/tools/ext.cx.tools.template.js 
b/modules/tools/ext.cx.tools.template.js
index 747d1b0..a5dbb0f 100644
--- a/modules/tools/ext.cx.tools.template.js
+++ b/modules/tools/ext.cx.tools.template.js
@@ -140,6 +140,7 @@
                if ( !$template ) {
                        return $sourceTemplate;
                }
+
                aboutAttr = $template.attr( 'about' ) ||
                        mw.cx.getSourceSection( $template.data( 'source' ) 
).attr( 'about' );
 
@@ -724,6 +725,66 @@
                return targetParams;
        };
 
+       TemplateTool.prototype.getEditor = function () {
+               return new mw.cx.TemplateEditor(
+                       this.sourceTemplate,
+                       this.targetTemplate, {
+                               onUpdate: $.proxy( this.updateTargetTemplate, 
this ),
+                               onFail: function () {
+                                       mw.hook( 'mw.cx.error' ).fire( 
'Template editing failed' );
+                               }
+                       }
+               );
+       };
+
+       /**
+        * Get inline templates in a section
+        *
+        * @param  {jQuery} $section Target section
+        * @return {Object[]} Array of source-target pairs of the found inline 
templates
+        */
+       TemplateTool.static.getInlineTemplates = function ( $section ) {
+               var $targetTemplates, inlineTemplates = [];
+
+               $targetTemplates = $section.find( '[typeof~="mw:Transclusion"]' 
);
+               $targetTemplates.each( function () {
+                       var inlineTemplate, $targetTemplate = $( this );
+
+                       inlineTemplate = {
+                               source: mw.cx.Template.static.getTemplateDef( 
$targetTemplate ),
+                               target: $section.find( '[about="' + 
$targetTemplate.attr( 'about' ) + '"]' )
+                       };
+
+                       if ( inlineTemplate.source.length ) {
+                               inlineTemplates.push( inlineTemplate );
+                       }
+               } );
+
+               return inlineTemplates;
+       };
+
+       TemplateTool.static.processInlineTemplates = function ( $section ) {
+               var inlineTemplates, i, inlineTemplate, sourceTemplate, 
targetTemplate, templateTool;
+               inlineTemplates = mw.cx.TemplateTool.static.getInlineTemplates( 
$section );
+
+               for ( i = 0; i < inlineTemplates.length; i++ ) {
+                       inlineTemplate = inlineTemplates[ i ];
+                       sourceTemplate = new mw.cx.Template( 
inlineTemplate.source, {
+                               language: mw.cx.sourceLanguage,
+                               siteMapper: mw.cx.siteMapper,
+                               inline: true
+                       } );
+                       targetTemplate = new mw.cx.Template( 
inlineTemplate.target, {
+                               language: mw.cx.targetLanguage,
+                               siteMapper: mw.cx.siteMapper,
+                               inline: true
+                       } );
+                       templateTool = new mw.cx.TemplateTool( sourceTemplate, 
targetTemplate );
+//                     inlineTemplate.source.data( 'cxtemplate', templateTool 
);
+                       templateTool.adapt();
+               }
+       };
+
        mw.cx.TemplateTool = TemplateTool;
 
        mw.cx.TemplateTool.defaults = {
@@ -732,4 +793,7 @@
                targetLanguage: mw.cx.targetLanguage
        };
 
+       $( function () {
+               mw.hook( 'mw.cx.translation.postMT' ).add( 
mw.cx.TemplateTool.static.processInlineTemplates );
+       } );
 }( jQuery, mediaWiki ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I574df83798975fd0c9ae3b9186c0bbfd92e210e0
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

Reply via email to