jenkins-bot has submitted this change and it was merged.

Change subject: Remove items from DTH factory indexes when unregistering
......................................................................


Remove items from DTH factory indexes when unregistering

Bug: T143954
Change-Id: I4a5a5de4dbd234f752f0111b086f2e63dec43ba2
---
M src/ui/ve.ui.DataTransferHandlerFactory.js
1 file changed, 50 insertions(+), 12 deletions(-)

Approvals:
  DLynch: Looks good to me, approved
  Jforrester: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/src/ui/ve.ui.DataTransferHandlerFactory.js 
b/src/ui/ve.ui.DataTransferHandlerFactory.js
index 1b649e4..a64302b 100644
--- a/src/ui/ve.ui.DataTransferHandlerFactory.js
+++ b/src/ui/ve.ui.DataTransferHandlerFactory.js
@@ -33,14 +33,33 @@
  * @inheritdoc
  */
 ve.ui.DataTransferHandlerFactory.prototype.register = function ( constructor ) 
{
-       var i, j, ilen, jlen, kinds, types, extensions;
-
        // Parent method
-       ve.ui.DataTransferHandlerFactory.super.prototype.register.call( this, 
constructor );
+       ve.ui.DataTransferHandlerFactory.super.prototype.register.apply( this, 
arguments );
 
-       kinds = constructor.static.kinds;
-       types = constructor.static.types;
-       extensions = constructor.static.extensions;
+       this.updateIndexes( constructor, true );
+};
+
+/**
+ * @inheritdoc
+ */
+ve.ui.DataTransferHandlerFactory.prototype.unregister = function ( constructor 
) {
+       // Parent method
+       ve.ui.DataTransferHandlerFactory.super.prototype.unregister.apply( 
this, arguments );
+
+       this.updateIndexes( constructor, false );
+};
+
+/**
+ * Update indexes used for handler loopup
+ *
+ * @param {Function} constructor Handler's constructor to insert/remove
+ * @param {boolean} insert Insert the handler into the indexes, remove 
otherwise
+ */
+ve.ui.DataTransferHandlerFactory.prototype.updateIndexes = function ( 
constructor, insert ) {
+       var i, j, ilen, jlen,
+               kinds = constructor.static.kinds,
+               types = constructor.static.types,
+               extensions = constructor.static.extensions;
 
        function ensureArray( obj, prop ) {
                if ( obj[ prop ] === undefined ) {
@@ -56,23 +75,42 @@
                return obj[ prop ];
        }
 
+       function remove( arr, item ) {
+               var index;
+               if ( ( index = arr.indexOf( item ) ) !== -1 ) {
+                       arr.splice( index, 1 );
+               }
+       }
+
        if ( !kinds ) {
                for ( j = 0, jlen = types.length; j < jlen; j++ ) {
-                       ensureArray( this.handlerNamesByType, types[ j ] 
).unshift( constructor.static.name );
+                       if ( insert ) {
+                               ensureArray( this.handlerNamesByType, types[ j 
] ).unshift( constructor.static.name );
+                       } else {
+                               remove( this.handlerNamesByType[ types[ j ] ], 
constructor.static.name );
+                       }
                }
        } else {
                for ( i = 0, ilen = kinds.length; i < ilen; i++ ) {
                        for ( j = 0, jlen = types.length; j < jlen; j++ ) {
-                               ensureArray(
-                                       ensureMap( 
this.handlerNamesByKindAndType, kinds[ i ] ),
-                                       types[ j ]
-                               ).unshift( constructor.static.name );
+                               if ( insert ) {
+                                       ensureArray(
+                                               ensureMap( 
this.handlerNamesByKindAndType, kinds[ i ] ),
+                                               types[ j ]
+                                       ).unshift( constructor.static.name );
+                               } else {
+                                       remove( this.handlerNamesByKindAndType[ 
kinds[ i ] ][ types[ j ] ], constructor.static.name );
+                               }
                        }
                }
        }
        if ( constructor.prototype instanceof ve.ui.FileTransferHandler ) {
                for ( i = 0, ilen = extensions.length; i < ilen; i++ ) {
-                       ensureArray( this.handlerNamesByExtension, extensions[ 
i ] ).unshift( constructor.static.name );
+                       if ( insert ) {
+                               ensureArray( this.handlerNamesByExtension, 
extensions[ i ] ).unshift( constructor.static.name );
+                       } else {
+                               remove( this.handlerNamesByExtension[ 
extensions[ i ] ], constructor.static.name );
+                       }
                }
        }
 };

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4a5a5de4dbd234f752f0111b086f2e63dec43ba2
Gerrit-PatchSet: 4
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Esanders <esand...@wikimedia.org>
Gerrit-Reviewer: DLynch <dly...@wikimedia.org>
Gerrit-Reviewer: Jforrester <jforres...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to