DLynch has uploaded a new change for review.

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

Change subject: DesktopArticleTarget: Rebuild the category links when they're 
edited
......................................................................

DesktopArticleTarget: Rebuild the category links when they're edited

This has to go to the API, because it's skinnable. Also, separates out a few
of the initial page setup functions so they can be re-applied to the new
content inserted.

Bug: T151651
Change-Id: I8d5a6504edc2e0486a0b4f016d8ee6d9a6228de9
---
M modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
1 file changed, 107 insertions(+), 47 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/86/324786/1

diff --git a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js 
b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
index 29c9e80..163de27 100644
--- a/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
+++ b/modules/ve-mw/init/targets/ve.init.mw.DesktopArticleTarget.js
@@ -733,45 +733,51 @@
  * @param {boolean} [loading=false] Whether VE is loading.
  */
 ve.init.mw.DesktopArticleTarget.prototype.onMetaItemInserted = function ( 
metaItem, loading ) {
-       var title, target, $link;
-       if ( metaItem.getType() === 'mwRedirect' ) {
-               target = this;
-               title = metaItem.getAttribute( 'title' );
-               $link = $( '<a>' )
-                       .attr( 'title', mw.msg( 
'visualeditor-redirect-description', title ) )
-                       .text( title );
-               ve.init.platform.linkCache.styleElement( title, $link );
+       var title, target, $link,
+               metaList = this.surface.getModel().getMetaList();
+       switch ( metaItem.getType() ) {
+               case 'mwRedirect':
+                       target = this;
+                       title = metaItem.getAttribute( 'title' );
+                       $link = $( '<a>' )
+                               .attr( 'title', mw.msg( 
'visualeditor-redirect-description', title ) )
+                               .text( title );
+                       ve.init.platform.linkCache.styleElement( title, $link );
 
-               if ( loading ) {
-                       this.$originalRedirectMsg = $( '.redirectMsg' ).clone();
-                       this.$originalRedirectSub = $( '#redirectsub, 
#redirectsub + br' ).clone();
-               }
-               // Add redirect target header
-               if ( !$( '#redirectsub' ).length ) {
-                       $( '#contentSub' ).append(
-                               $( '<span>' )
-                                       .text( mw.msg( 'redirectpagesub' ) )
-                                       .attr( 'id', 'redirectsub' ),
-                               $( '<br>' )
-                       );
-               }
-               $( '<div>' )
-                       // Bit of a hack: Make sure any redirect note is styled
-                       .addClass( 'redirectMsg mw-content-' + $( 'html' 
).attr( 'dir' ) )
+                       if ( loading ) {
+                               this.$originalRedirectMsg = $( '.redirectMsg' 
).clone();
+                               this.$originalRedirectSub = $( '#redirectsub, 
#redirectsub + br' ).clone();
+                       }
+                       // Add redirect target header
+                       if ( !$( '#redirectsub' ).length ) {
+                               $( '#contentSub' ).append(
+                                       $( '<span>' )
+                                               .text( mw.msg( 
'redirectpagesub' ) )
+                                               .attr( 'id', 'redirectsub' ),
+                                       $( '<br>' )
+                               );
+                       }
+                       $( '<div>' )
+                               // Bit of a hack: Make sure any redirect note 
is styled
+                               .addClass( 'redirectMsg mw-content-' + $( 
'html' ).attr( 'dir' ) )
 
-                       .addClass( 've-redirect-header' )
-                       .append(
-                               $( '<p>' ).text( mw.msg( 'redirectto' ) ),
-                               $( '<ul>' )
-                                       .addClass( 'redirectText' )
-                                       .append( $( '<li>' ).append( $link ) )
-                       )
-                       .click( function ( e ) {
-                               var windowAction = ve.ui.actionFactory.create( 
'window', target.getSurface() );
-                               windowAction.open( 'meta', { page: 'settings' } 
);
-                               e.preventDefault();
-                       } )
-                       .insertAfter( $( '.mw-jump' ) );
+                               .addClass( 've-redirect-header' )
+                               .append(
+                                       $( '<p>' ).text( mw.msg( 'redirectto' ) 
),
+                                       $( '<ul>' )
+                                               .addClass( 'redirectText' )
+                                               .append( $( '<li>' ).append( 
$link ) )
+                               )
+                               .click( function ( e ) {
+                                       var windowAction = 
ve.ui.actionFactory.create( 'window', target.getSurface() );
+                                       windowAction.open( 'meta', { page: 
'settings' } );
+                                       e.preventDefault();
+                               } )
+                               .insertAfter( $( '.mw-jump' ) );
+                       break;
+               case 'mwCategory':
+                       this.rebuildCategories( metaList.getItemsInGroup( 
'mwCategory' ) );
+                       break;
        }
 };
 
@@ -783,10 +789,46 @@
  * @param {number} index Index within that offset the item was at
  */
 ve.init.mw.DesktopArticleTarget.prototype.onMetaItemRemoved = function ( 
metaItem ) {
-       if ( metaItem.getType() === 'mwRedirect' ) {
-               this.$originalContent.find( '.redirectMsg' ).remove();
-               $( '#contentSub #redirectsub, #contentSub #redirectsub + br' 
).remove();
+       var metaList = this.surface.getModel().getMetaList();
+       switch ( metaItem.getType() ) {
+               case 'mwRedirect':
+                       this.$originalContent.find( '.redirectMsg' ).remove();
+                       $( '#contentSub #redirectsub, #contentSub #redirectsub 
+ br' ).remove();
+                       break;
+               case 'mwCategory':
+                       this.rebuildCategories( metaList.getItemsInGroup( 
'mwCategory' ) );
+                       break;
        }
+};
+
+/**
+ * Redisplay the category list on the page
+ *
+ * @param  {ve.dm.MetaItem[]} categoryItems Array of category metaitems to 
display
+ */
+ve.init.mw.DesktopArticleTarget.prototype.rebuildCategories = function ( 
categoryItems ) {
+       var target = this;
+       // We need to fetch this from the API because the category list is skin-
+       // dependent, so the HTML output could be absolutely anything.
+       new mw.Api().post( {
+               formatversion: 2,
+               action: 'parse',
+               contentmodel: 'wikitext',
+               text: categoryItems.map( function ( categoryItem ) {
+                       if ( categoryItem.getAttribute( 'sortkey' ) ) {
+                               return '[[' + categoryItem.getAttribute( 
'category' ) + '|' + categoryItem.getAttribute( 'sortkey' ) + ']]';
+                       }
+                       return '[[' + categoryItem.getAttribute( 'category' ) + 
']]';
+               } ).join( '\n' ),
+               prop: 'categorieshtml'
+       } ).then( function ( response ) {
+               if ( !response || !response.parse || 
!response.parse.categorieshtml ) {
+                       return;
+               }
+               $( '#catlinks' ).replaceWith( response.parse.categorieshtml );
+               target.transformCategoryLinks( $( '#catlinks' ) );
+               target.disableUneditableContent( $( '#catlinks' ) );
+       } );
 };
 
 /**
@@ -1117,8 +1159,7 @@
  * Page modifications for switching to edit mode.
  */
 ve.init.mw.DesktopArticleTarget.prototype.transformPage = function () {
-       var target = this,
-               $content;
+       var $content;
 
        this.updateTabs( true );
        this.emit( 'transformPage' );
@@ -1136,22 +1177,41 @@
                $content = $content.parent();
        }
 
+       this.transformCategoryLinks( $( '.catlinks' ) );
+
+       this.disableUneditableContent();
+
+       this.updateHistoryState();
+};
+
+/**
+ * Category link section transformations for switching to edit mode. Broken out
+ * so it can be re-applied when displaying changes to the categories.
+ *
+ * @param  {jQuery} $catlinks Category links container element
+ */
+ve.init.mw.DesktopArticleTarget.prototype.transformCategoryLinks = function ( 
$catlinks ) {
+       var target = this;
        // Un-disable the catlinks wrapper, but not the links
-       $( '.catlinks' )
-               .removeClass( 
've-init-mw-desktopArticleTarget-uneditableContent' )
+       $catlinks.removeClass( 
've-init-mw-desktopArticleTarget-uneditableContent' )
                .on( 'click.ve-target', function () {
                        var windowAction = ve.ui.actionFactory.create( 
'window', target.getSurface() );
                        windowAction.open( 'meta', { page: 'categories' } );
                        return false;
                } )
                .find( 'a' ).addClass( 
've-init-mw-desktopArticleTarget-uneditableContent' );
+};
 
-       $( '.ve-init-mw-desktopArticleTarget-uneditableContent' ).on( 
'click.ve-target', function ( e ) {
+/**
+ * Disabling of non-editable content, in a given context
+ *
+ * @param  {jQuery|string|null} context Context to disable in
+ */
+ve.init.mw.DesktopArticleTarget.prototype.disableUneditableContent = function 
( context ) {
+       $( '.ve-init-mw-desktopArticleTarget-uneditableContent', context ).on( 
'click.ve-target', function ( e ) {
                // Support IE9: Prevent default, but don't stop propagation
                e.preventDefault();
        } );
-
-       this.updateHistoryState();
 };
 
 /**

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8d5a6504edc2e0486a0b4f016d8ee6d9a6228de9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: DLynch <dly...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to