Daniel Werner has submitted this change and it was merged.

Change subject: (bug 44745) Toolbar controller widget
......................................................................


(bug 44745) Toolbar controller widget

Initialized in the repo's entityViewInit.js, the toolbar controller widget 
initializes
and manages all "add" and "edit" toolbars registered to the controller. This 
ensures
a clean separation between the toolbars and the actual widgets they interact 
with.

- patch set 2: Moved registry for toolbarcontroller definitions

Change-Id: If9537051448bc263ce749c0b56bba2a001c4b74f
---
M lib/resources/Resources.php
M lib/resources/jquery.wikibase/jquery.wikibase.addtoolbar.js
M lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
M lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
M lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
M lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
M lib/resources/jquery.wikibase/jquery.wikibase.listview.js
M lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
M lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
A 
lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.definitions.js
A 
lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.js
M repo/resources/Resources.php
M repo/resources/wikibase.ui.entityViewInit.js
13 files changed, 336 insertions(+), 66 deletions(-)

Approvals:
  Daniel Werner: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/resources/Resources.php b/lib/resources/Resources.php
index 7971739..9cd155f 100644
--- a/lib/resources/Resources.php
+++ b/lib/resources/Resources.php
@@ -318,6 +318,17 @@
                        )
                ),
 
+               'jquery.wikibase.toolbarcontroller' => $moduleTemplate + array(
+                       'scripts' => array(
+                               
'jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.js',
+                               
'jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.definitions.js',
+                       ),
+                       'dependencies' => array(
+                               'jquery.wikibase.addtoolbar',
+                               'jquery.wikibase.edittoolbar',
+                       )
+               ),
+
                'jquery.wikibase.addtoolbar' => $moduleTemplate + array(
                        'scripts' => array(
                                'jquery.wikibase/jquery.wikibase.addtoolbar.js',
@@ -390,7 +401,7 @@
                        ),
                        'dependencies' => array(
                                'jquery.ui.TemplatedWidget',
-                               'jquery.wikibase.edittoolbar',
+                               'jquery.wikibase.toolbarcontroller',
                        )
                ),
 
@@ -451,6 +462,7 @@
                        ),
                        'dependencies' => array(
                                'jquery.wikibase.claimview',
+                               'jquery.wikibase.toolbarcontroller',
                        )
                ),
 
@@ -459,10 +471,10 @@
                                
'jquery.wikibase/jquery.wikibase.statementview.js',
                        ),
                        'dependencies' => array(
-                               'jquery.wikibase.addtoolbar',
                                'jquery.wikibase.claimview',
                                'jquery.wikibase.listview',
                                'jquery.wikibase.referenceview',
+                               'jquery.wikibase.toolbarcontroller',
                                'wikibase.utilities',
                        ),
                        'messages' => array(
@@ -478,7 +490,7 @@
                        ),
                        'dependencies' => array(
                                'jquery.wikibase.claimview',
-                               'jquery.wikibase.edittoolbar',
+                               'jquery.wikibase.toolbarcontroller',
                                'wikibase.templates',
                        ),
                        'messages' => array(
@@ -491,9 +503,9 @@
                                'jquery.wikibase/jquery.wikibase.entityview.js'
                        ),
                        'dependencies' => array(
-                               'jquery.wikibase.addtoolbar',
                                'jquery.wikibase.statementview',
                                'jquery.wikibase.claimlistview',
+                               'jquery.wikibase.toolbarcontroller',
                                'wikibase.templates'
                        ),
                        'messages' => array(
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.addtoolbar.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.addtoolbar.js
index 9cec3fe..9e9a555 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.addtoolbar.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.addtoolbar.js
@@ -16,6 +16,9 @@
         * The widget the toolbar shall interact with has to have implemented 
certain methods listed in
         * the _requiredMethods attribute.
         *
+        * @option toolbarParentSelector {string} (REQUIRED) jQuery selector to 
find the node the actual
+        *         toolbar buttons shall be appended to.
+        *
         * @option interactionWidgetName {string} Name of the widget the 
toolbar shall interact with.
         *         (That widget needs to be initialized on the same DOM node 
this toolbar is initialized
         *         on.) If the interactionWidgetName option is omitted, the 
toolbar will be initialized
@@ -23,12 +26,12 @@
         *         When omitting the interactionWidgetName option, the "action" 
option should be set.
         *         Default value: null (no interaction widget)
         *
-        * @option toolbarParentSelector {string} (required) jQuery selector to 
find the node the actual
-        *         toolbar buttons shall be appended to.
-        *
-        * @option action {function} Custom action the add button shall 
trigger. The action
+        * @option customAction {function} Custom action the add button shall 
trigger. The action
         *         will be triggered only when no interaction widget via the 
interactionWidgetName
         *         option is set.
+        *         The function receives the following parameters:
+        *         (1) {jQuery.Event} "Add" button's action event
+        *         (2) {jQuery} The action toolbar's DOM node
         *         Default value: null (no custom action)
         *
         * @option eventPrefix {string} Custom event prefix the events the 
toolbar will listen to will
@@ -41,7 +44,6 @@
         *         Default value: mw.msg( 'wikibase-add' )
         */
        $.widget( 'wikibase.addtoolbar', {
-               widgetName: 'wikibase-addtoolbar',
                widgetBaseClass: 'wb-addtoolbar',
 
                /**
@@ -49,9 +51,9 @@
                 * @type {Object}
                 */
                options: {
-                       interactionWidgetName: null,
                        toolbarParentSelector: null,
-                       action: null,
+                       interactionWidgetName: null,
+                       customAction: null,
                        eventPrefix: '',
                        addButtonLabel: mw.msg( 'wikibase-add' )
                },
@@ -152,8 +154,8 @@
                        $( this.toolbar.btnAdd ).on( 'action', function( event 
) {
                                if ( self._interactionWidget ) {
                                        self._interactionWidget.enterNewItem();
-                               } else if ( self.options.action ) {
-                                       self.options.action();
+                               } else if ( self.options.customAction ) {
+                                       self.options.customAction( event, 
self.element );
                                }
                        } );
 
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
index 48d9901..509d201 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
@@ -30,7 +30,6 @@
  *        (1) {jQuery.Event}
  */
 $.widget( 'wikibase.claimlistview', PARENT, {
-       widgetName: 'wikibase-claimlistview',
        widgetBaseClass: 'wb-claimlistview',
 
        /**
@@ -198,16 +197,20 @@
         */
        _addClaim: function( claim ) {
                var propertyId = claim.getMainSnak().getPropertyId(),
-                       $newClaim = this._lmwInstantiate( $( '<div/>' ), {
-                               value: claim,
-                               locked: {
-                                       mainSnak: {
-                                               property: true
-                                       }
-                               }
-                       } ).element;
+                       $newClaim = $( '<div/>' );
 
                this._insertClaimRow( propertyId, $newClaim );
+
+               // Instantiate list member widget after adding its element node 
to the DOM to be able to
+               // listen to widget "create" events.
+               this._lmwInstantiate( $newClaim, {
+                       value: claim,
+                       locked: {
+                               mainSnak: {
+                                       property: true
+                               }
+                       }
+               } );
        },
 
        /**
@@ -220,13 +223,6 @@
        _insertClaimRow: function( claimSectionPropertyId, $content ) {
                // get the section we want to insert stuff in:
                var $claimRows = this._serveClaimSection( 
claimSectionPropertyId );
-
-               if ( this._lmwInstance( $content ) ) {
-                       $content.edittoolbar( {
-                               interactionWidgetName: this._lmwInstance( 
$content ).widgetName,
-                               toolbarParentSelector: '.wb-statement-claim 
.wb-claim-toolbar'
-                       } );
-               }
 
                // insert before last child node in that list (there is at 
least one node always, holding the 'add)
                $claimRows.children( '.wb-claim-container' ).last().before( 
$content );
@@ -278,13 +274,11 @@
                                mainSnakPropertyId, // main Snak's property ID
                                wb.utilities.ui.buildEntityLink( property ), // 
property name
                                $addClaim // claim
-                       ).appendTo( this.$claims );
+                       )
+                       .data( 'wb-propertyId', mainSnakPropertyId )
+                       .appendTo( this.$claims );
 
-                       $section.addtoolbar( {
-                               toolbarParentSelector: '.wb-claim-add 
.wb-claim-toolbar',
-                               action: function() { 
self.enterNewClaimInSection( mainSnakPropertyId ) },
-                               eventPrefix: this.widgetEventPrefix
-                       } );
+                       $section.trigger( 'claimsectioncreate' );
                }
 
                return $section;
@@ -343,12 +337,6 @@
 
                // initialize view after node is in DOM, so the 'startediting' 
event can bubble
                this._lmwInstantiate( $newClaim, options ).element.addClass( 
'wb-claim-new' );
-
-               $newClaim.edittoolbar( {
-                       interactionWidgetName: this._lmwInstance( $newClaim 
).widgetName,
-                       toolbarParentSelector: '.wb-statement-claim 
.wb-claim-toolbar',
-                       enableRemove: false
-               } );
 
                this._lmwInstance( $newClaim ).startEditing();
 
@@ -458,4 +446,34 @@
 
 } );
 
+// Register toolbars:
+var widgetPrototype = $.wikibase.claimlistview.prototype;
+
+$.wikibase.toolbarcontroller.definition( 'addtoolbar', {
+       widget: {
+               name: 'wikibase.claimlistview',
+               prototype: widgetPrototype
+       },
+       options: {
+               interactionWidgetName: widgetPrototype.widgetName,
+               toolbarParentSelector: '.wb-claims-toolbar'
+       }
+} );
+
+$.wikibase.toolbarcontroller.definition( 'addtoolbar', {
+       id: 'claimsection',
+       selector: '.wb-claim-section',
+       eventPrefix: 'claimsection',
+       baseClass: widgetPrototype.widgetBaseClass,
+       options: {
+               toolbarParentSelector: '.wb-claim-add .wb-claim-toolbar',
+               customAction: function( event, $parent ) {
+                       $parent.closest( '.wb-claimlistview' ).data( 
'claimlistview' ).enterNewClaimInSection(
+                               $parent.data( 'wb-propertyId' )
+                       );
+               },
+               eventPrefix: widgetPrototype.widgetEventPrefix
+       }
+} );
+
 }( mediaWiki, wikibase, jQuery ) );
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
index 4e23194..a8e3446 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
@@ -437,11 +437,8 @@
         * @see $.widget.destroy
         */
        destroy: function() {
-               this.element.removeClass( this.widgetBaseClass );
-
                this.$mainSnak.snakview( 'destroy' );
-
-               $.Widget.prototype.destroy.call( this );
+               PARENT.prototype.destroy.call( this );
        },
 
        /**
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
index 6a1f7cc..eacb37c 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
@@ -35,7 +35,6 @@
         *         Default value: true
         */
        $.widget( 'wikibase.edittoolbar', {
-               widgetName: 'wikibase-edittoolbar',
                widgetBaseClass: 'wb-edittoolbar',
 
                /**
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
index a7818ae..a4d9538 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.entityview.js
@@ -49,11 +49,6 @@
                        listMembersWidget: $.wikibase.statementview
                } );
 
-               this.$claims.addtoolbar( {
-                       interactionWidgetName: 'claimlistview',
-                       toolbarParentSelector: '.wb-claims-toolbar'
-               } );
-
                // append all the stuff:
                // NOTE: doing this here will prevent events from bubbling 
during widget initializations!
                //       Shouldn't harm and will increase performance because 
DOM needs to render once only.
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.listview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.listview.js
index 256cb94..6e1cce4 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.listview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.listview.js
@@ -50,6 +50,8 @@
  *        (1) {jQuery.Event}
  */
 $.widget( 'wikibase.listview', PARENT, {
+       widgetBaseClass: 'wb-listview',
+
        /**
         * Section node containing the list items
         * @type jQuery
@@ -199,12 +201,6 @@
                        this.$listItems.append( $newLi );
                        this._lia.newListItem( $newLi, value );
 
-                       $newLi.edittoolbar( {
-                               interactionWidgetName: this._lia.liInstance( 
$newLi ).widgetName,
-                               toolbarParentSelector: '.wb-claim-toolbar',
-                               enableRemove: !!value
-                       } );
-
                        this._trigger( 'itemadded', null, [ value, $newLi ] );
                }
        } ),
@@ -331,4 +327,17 @@
 
 } );
 
+// Register toolbar:
+$.wikibase.toolbarcontroller.definition( 'addtoolbar', {
+       widget: {
+               name: 'wikibase.listview',
+               prototype: $.wikibase.listview.prototype
+       },
+       options: {
+               interactionWidgetName: $.wikibase.listview.prototype.widgetName,
+               toolbarParentSelector: '.wb-listview-toolbar',
+               addButtonLabel: mw.msg( 'wikibase-addreference' )
+       }
+} );
+
 }( mediaWiki, wikibase, jQuery ) );
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
index fe48573..713bd6d 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
@@ -17,7 +17,6 @@
  * @extends jQuery.wikibase.claimview
  */
 $.widget( 'wikibase.referenceview', PARENT, {
-       widgetName: 'wikibase-referenceview',
        widgetBaseClass: 'wb-referenceview',
 
        options: {
@@ -52,6 +51,9 @@
 
                this.element.removeClassByRegex( /wb-reference-.+/ );
                this.element.addClass( 'wb-reference-' + refHash );
+
+               this.element.removeClassByRegex( new RegExp( 
this.widgetBaseClass ) + '-.+' );
+               this.element.addClass( this.widgetBaseClass + '-' + refHash );
        },
 
        /**
@@ -63,7 +65,7 @@
         */
        value: function() {
                // since we inherit from claimview, internal _claim will hold 
what we got in 'value' option.
-               if( this._claim instanceof wb.Claim) {
+               if( this._claim instanceof wb.Claim ) {
                        // in claimview._create there is an event listening to 
the mainsnak's snakview, when it
                        // drops out of edit mode the first time, a new claim 
is created. In the referenceview
                        // we don't want the claim, so use this hack to get a 
reference from it!
@@ -150,4 +152,16 @@
        }
 } );
 
+// Register toolbar:
+$.wikibase.toolbarcontroller.definition( 'edittoolbar', {
+       widget: {
+               name: 'wikibase.referenceview',
+               prototype: $.wikibase.referenceview.prototype
+       },
+       options: {
+               interactionWidgetName: 
$.wikibase.referenceview.prototype.widgetName,
+               toolbarParentSelector: '.wb-claim-toolbar'
+       }
+} );
+
 }( mediaWiki, wikibase, jQuery ) );
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
index fc25b13..30db435 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
@@ -17,7 +17,6 @@
  * @extends jQuery.wikibase.claimview
  */
 $.widget( 'wikibase.statementview', PARENT, {
-       widgetName: 'wikibase-statementview',
        widgetBaseClass: 'wb-statementview',
 
        options: {
@@ -93,12 +92,6 @@
                                self.drawReferencesCounter();
                        } );
 
-                       this.$references.addtoolbar( {
-                               interactionWidgetName: 'listview',
-                               toolbarParentSelector: '.wb-listview-toolbar',
-                               addButtonLabel: mw.msg( 'wikibase-addreference' 
)
-                       } );
-
                        // Collapse references if there is at least one.
                        if ( this.$references.data( 'listview' ).items().length 
> 0 ) {
                                this.$references.css( 'display', 'none' );
@@ -115,6 +108,8 @@
 
                        // replace heading with nice counter:
                        this.drawReferencesCounter();
+               } else {
+                       this.element.addClass( this.widgetBaseClass + '-new' );
                }
        },
 
@@ -122,6 +117,7 @@
         * @see $.widget.destroy
         */
        destroy: function() {
+               this.element.removeClass( this.widgetBaseClass + '-new' );
                this.element.removeClass( 'wb-claimview' );
                PARENT.prototype.destroy.call( this );
        },
@@ -168,4 +164,16 @@
        }
 } );
 
+// Register toolbar:
+$.wikibase.toolbarcontroller.definition( 'edittoolbar', {
+       widget: {
+               name: 'wikibase.statementview',
+               prototype: $.wikibase.statementview.prototype
+       },
+       options: {
+               interactionWidgetName: 
$.wikibase.statementview.prototype.widgetName,
+               toolbarParentSelector: '.wb-statement-claim .wb-claim-toolbar'
+       }
+} );
+
 }( mediaWiki, wikibase, jQuery ) );
diff --git 
a/lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.definitions.js
 
b/lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.definitions.js
new file mode 100644
index 0000000..61d3f69
--- /dev/null
+++ 
b/lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.definitions.js
@@ -0,0 +1,96 @@
+/**
+ * @file
+ * @ingroup WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author H. Snater < mediaw...@snater.com >
+ */
+( function( mw, wb, $ ) {
+       'use strict';
+
+       var MODULE = $.wikibase.toolbarcontroller;
+
+       /**
+        * Toolbar definitions
+        * @type {Object}
+        */
+       var toolbarDefinitions = {};
+
+       /**
+        * Registers/Gets a toolbar definition.
+        *
+        * The simplest way to specify a toolbar definition is to reference a 
widget that interfaces
+        * a certain toolbar by having implemented the methods required by the 
toolbar type:
+        * $.wikibase.toolbarcontroller.definition(
+        *   'addtoolbar', // the toolbar type
+        *   {
+        *     widget: { // the referenced widget that needs to be able to 
interface to the toolbar type
+        *       name: 'wikibase.claimlistview', // <namespace>.<name> of the 
widget
+        *       prototype: $.wikibase.claimlistview.prototype
+        *     },
+        *     options: { // options passed to the toolbar
+        *       interactionWidgetName: 
$.wikibase.claimlistview.prototype.widgetName,
+        *       toolbarParentSelector: '.wb-claims-toolbar'
+        *     }
+        *   }
+        * );
+        * A toolbar may also be defined on a plain jQuery node which requires 
specifying some
+        * information that would have been extracted from the widget:
+        * $.wikibase.toolbarcontroller.definition(
+        *   'addtoolbar',
+        *   {
+        *     id: 'claimsection',
+        *     selector: '.wb-claim-section', // selector to access the node 
from the toolbar
+        *                                    // controller's node
+        *     eventPrefix: 'claimsection',
+        *     baseClass: widgetPrototype.widgetBaseClass,
+        *     options: { // options passed to the toolbar
+        *       toolbarParentSelector: '.wb-claim-add .wb-claim-toolbar',
+        *       customAction: function( event, $parent ) {
+        *         $parent.closest( '.wb-claimlistview' ).data( 'claimlistview' 
)
+        *         .enterNewClaimInSection( $parent.data( 'wb-propertyId' ) );
+        *       },
+        *       eventPrefix: widgetPrototype.widgetEventPrefix
+        *     }
+        *   }
+        * );
+        *
+        * @since 0.4
+        *
+        * @param {string} type The toolbar type (see options for available 
types)
+        * @param {Object} toolbarDefinitionOrId Object defining a toolbar that 
should be set or a
+        *        toolbar id/widget name to get a registered toolbar definition.
+        * @return {Object} Toolbar definition
+        */
+       MODULE.definition = function( type, toolbarDefinitionOrId ) {
+               if ( typeof toolbarDefinitionOrId === 'string' ) {
+                       // GET existing definition
+                       return toolbarDefinitions[type][toolbarDefinitionOrId];
+               }
+               // SET new definition
+               var toolbarDefinition = toolbarDefinitionOrId,
+                       id = toolbarDefinition.id || 
toolbarDefinition.widget.name;
+
+               if ( !id ) {
+                       throw new Error( 'jquery.wikibase.toolbarcontroller: 
Either an id or a widget ' +
+                               'name is necessary to register a toolbar' );
+               }
+
+               if ( toolbarDefinition.widget ) {
+                       var widget = toolbarDefinition.widget;
+                       widget.namespace = widget.name.split( '.' )[ 0 ];
+                       widget.name = widget.name.split( '.' )[ 1 ];
+                       widget.fullName = widget.namespace + '-' + widget.name;
+                       id = widget.prototype.widgetName;
+               }
+
+               if ( !toolbarDefinitions[type] ) {
+                       toolbarDefinitions[type] = {};
+               }
+
+               toolbarDefinitions[type][id] = toolbarDefinition;
+
+               return toolbarDefinition;
+       };
+
+}( mediaWiki, wikibase, jQuery ) );
diff --git 
a/lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.js
 
b/lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.js
new file mode 100644
index 0000000..4eefe60
--- /dev/null
+++ 
b/lib/resources/jquery.wikibase/jquery.wikibase.toolbarcontroller/toolbarcontroller.js
@@ -0,0 +1,113 @@
+/**
+ * @file
+ * @ingroup WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author H. Snater < mediaw...@snater.com >
+ */
+( function( mw, wb, $ ) {
+       'use strict';
+
+       /**
+        * The toolbar types we have.
+        * TODO: create a registry for allowing adding additional toolbar types
+        *
+        * @type {string[]}
+        */
+       var TOOLBAR_TYPES = ['addtoolbar', 'edittoolbar'];
+
+       /**
+        * Toolbar controller widget
+        *
+        * The toolbar controller initializes and manages toolbar widgets. 
Toolbar definitions are
+        * registered via the jQuery.toolbarcontroller.definition() method. 
When initializing the
+        * toolbar controller, the ids or widget names of the registered 
toolbar definitions that the
+        * controller shall initialize are passed as options.
+        *
+        * @since 0.4
+        *
+        * @option addtoolbar {string[]} List of toolbar definition ids/widget 
names that are registered
+        *         as "addtoolbars" and shall be initialized.
+        *         Default: []
+        *
+        * @option edittoolbar {string[]} List of toolbar definition ids/widget 
names that are
+        *         registered as "edittoolbars" and shall be initialized.
+        *         Default: []
+        */
+       $.widget( 'wikibase.toolbarcontroller', {
+               /**
+                * Options
+                * @type {Object}
+                */
+               options: {
+                       addtoolbar: [],
+                       edittoolbar: []
+               },
+
+               /**
+                * @see jQuery.Widget._create
+                */
+               _create: function() {
+                       this.initToolbars();
+               },
+
+               /**
+                * Initializes the toolbars for the nodes that are descendants 
of the node the toolbar
+                * controller is initialized on.
+                * @since 0.4
+                */
+               initToolbars: function() {
+                       var self = this;
+
+                       $.each( TOOLBAR_TYPES, function( i, type ) {
+                               $.each( self.options[type], function( j, id ) {
+                                       var def = 
$.wikibase.toolbarcontroller.definition( type, id );
+                                       self.element
+                                       .find( def.selector || ':' + 
def.widget.fullName )[type]( def.options );
+                               } );
+                       } );
+
+                       this.initEventListeners();
+               },
+
+               /**
+                * Initializes event listeners for all toolbars defined in the 
options. This will make sure
+                * that when a new widget toolbars are defined for is 
initialized, its toolbar(s) will
+                * be initialized as well.
+                * @since 0.4
+                */
+               initEventListeners: function() {
+                       var self = this;
+
+                       this.element.off( '.' + this.widgetName );
+
+                       $.each( TOOLBAR_TYPES, function( i, type ) {
+                               $.each( self.options[type], function( j, 
definitionId ) {
+                                       var def = 
$.wikibase.toolbarcontroller.definition( type, definitionId ),
+                                               eventPrefix = def.eventPrefix 
|| def.widget.prototype.widgetEventPrefix,
+                                               baseClass = def.baseClass || 
def.widget.prototype.widgetBaseClass;
+
+                                       // Listen to widget's native "create" 
event in order to initialize toolbars
+                                       // corresponding to the widget just 
instantiated.
+                                       self.element.on( eventPrefix + 
'create.' + self.widgetName, function( event ) {
+                                               if ( type === 'addtoolbar' ) {
+                                                       self.initToolbars();
+                                               } else if ( type === 
'edittoolbar' ) {
+                                                       var $target = $( 
event.target ),
+                                                               isPending = 
$target.hasClass( baseClass + '-new' )
+                                                                       || 
$target.find( baseClass + '-new' ).length > 0;
+
+                                                       $( event.target 
).edittoolbar(
+                                                               $.extend( {}, 
def.options, { enableRemove: !isPending } )
+                                                       );
+                                               }
+                                       } );
+
+                               } );
+                       } );
+
+               }
+
+       } );
+
+}( mediaWiki, wikibase, jQuery ) );
diff --git a/repo/resources/Resources.php b/repo/resources/Resources.php
index dac0f07..153920a 100644
--- a/repo/resources/Resources.php
+++ b/repo/resources/Resources.php
@@ -45,6 +45,7 @@
                                'mediawiki.user',
                                'wikibase.ui.PropertyEditTool',
                                'jquery.wikibase.entityview',
+                               'jquery.wikibase.toolbarcontroller',
                                'wikibase.datamodel',
                                'jquery.json',
                                'jquery.cookie',
diff --git a/repo/resources/wikibase.ui.entityViewInit.js 
b/repo/resources/wikibase.ui.entityViewInit.js
index 4bd2836..6f57c85 100644
--- a/repo/resources/wikibase.ui.entityViewInit.js
+++ b/repo/resources/wikibase.ui.entityViewInit.js
@@ -126,6 +126,12 @@
                                // actual initialization
                                new wb.ui.SiteLinksEditTool( $( this ) );
                        } );
+
+                       // BUILD TOOLBARS
+                       $( '.wb-entity' ).toolbarcontroller( {
+                               addtoolbar: ['claimlistview', 'listview', 
'claimsection'],
+                               edittoolbar: ['statementview', 'referenceview']
+                       } );
                }
 
                // handle edit restrictions

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If9537051448bc263ce749c0b56bba2a001c4b74f
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de>
Gerrit-Reviewer: Daniel Werner <daniel.wer...@wikimedia.de>
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