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

Change subject: toolbarcontroller: Detach event handlers before re-attaching.
......................................................................


toolbarcontroller: Detach event handlers before re-attaching.

Change-Id: Iaa854768c658f173ca19061e29e7792bf765efda
---
M lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
1 file changed, 44 insertions(+), 28 deletions(-)

Approvals:
  Tobias Gritschacher: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js 
b/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
index 9833b97..810f9d1 100644
--- a/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
+++ b/lib/resources/jquery.wikibase/toolbar/toolbarcontroller.js
@@ -9,7 +9,7 @@
        'use strict';
 
        /**
-        * The toolbar types we have.
+        * Available toolbar types.
         * TODO: create a registry for allowing adding additional toolbar types
         *
         * @type {string[]}
@@ -65,6 +65,7 @@
                 *        (re-)initialization is in a pending state.
                 *
                 * @throws {Error} in case a given toolbar ID is not registered 
for the toolbar type given.
+                * @throws {Error} if the callback provided in an event 
definition is not a function.
                 */
                initToolbars: function( isPending ) {
                        var self = this;
@@ -88,12 +89,34 @@
                                                // definition:
                                                var defaultCallbacks = 
createDefaultEventHandlers( type, options );
 
-                                               // Toolbars that shall be 
created upon certain events.
+                                               // Detach all event handlers 
first in order to not end up with having the
+                                               // handler attached multiple 
times. This cannot be done along with
+                                               // re-attaching the handlers 
since multiple event handlers may be registered
+                                               // for the same event.
+                                               $.each( def.events, function( 
eventNames, callbackOrKeyword ) {
+                                                       var namespacedEvents = 
assignNamespaces(
+                                                               eventNames, 
[self.widgetName, self.widgetName + type]
+                                                       );
+                                                       $initNode.off( 
namespacedEvents );
+                                               } );
+
+                                               // Attach event handlers for 
toolbars that shall be created upon certain
+                                               // events:
                                                $.each( def.events, function( 
eventNames, callbackOrKeyword ) {
                                                        var callback =
-                                                               
defaultCallbacks[ callbackOrKeyword ] || callbackOrKeyword;
+                                                               
defaultCallbacks[ callbackOrKeyword ] || callbackOrKeyword,
+                                                               
namespacedEvents = assignNamespaces(
+                                                                       
eventNames, [self.widgetName, self.widgetName + type]
+                                                               );
 
-                                                       
self._attachToolbarEventListener( $initNode, eventNames, callback );
+                                                       if( !$.isFunction( 
callback ) ) {
+                                                               throw new 
Error( 'No callback or known default action given for '
+                                                                       + 
'event "' + eventNames + '"' );
+                                                       }
+
+                                                       $initNode.on( 
namespacedEvents, function( event ) {
+                                                               callback( 
event, $( event.target ) );
+                                                       } );
                                                } );
                                        }
 
@@ -104,30 +127,6 @@
                        } );
 
                        this.initEventListeners();
-               },
-
-               /**
-                * Attaches an event listener defined by the toolbar definition 
to the node the toolbar is
-                * initialized on.
-                * @since 0.4
-                *
-                * @param {jQuery} $toolbar
-                * @param {string[]} eventNames
-                * @param {Function} callback
-                *
-                * @throws {Error} if the provided callback is not a function.
-                */
-               _attachToolbarEventListener: function( $toolbar, eventNames, 
callback ) {
-                       if( !$.isFunction( callback ) ) {
-                               throw new Error( 'No callback or known default 
action given for event "' +
-                                       eventNames + '"' );
-                       }
-                       var namespacedEventNames = ( eventNames + ' ' )
-                               .split( ' ' ).join( '.' + this.widgetName + ' ' 
);
-
-                       $toolbar.on( namespacedEventNames, function( event ) {
-                               callback( event, $( event.target ) );
-                       } );
                },
 
                /**
@@ -203,4 +202,21 @@
                };
        }
 
+       /**
+        * Assigns namespaces to event names passed in as a string.
+        * @since 0.4
+        *
+        * @param {string} eventNames
+        * @param {string[]} namespaces
+        * @return {string}
+        */
+       function assignNamespaces( eventNames, namespaces ) {
+               eventNames = eventNames.split( ' ' );
+
+               // Add an empty string to assign namespaces to the last 
non-empty event name via join():
+               eventNames.push( '' );
+
+               return eventNames.join( '.' + namespaces.join( '.' ) + ' ' );
+       }
+
 }( mediaWiki, wikibase, jQuery ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Iaa854768c658f173ca19061e29e7792bf765efda
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to