3 * https://www.mediawiki.org/wiki/OOjs_UI
5 * Copyright 2011–2016 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
9 * Date: 2016-05-31T21:50:52Z
16 * Toolbars are complex interface components that permit users to easily access a variety
17 * of {@link OO.ui.Tool tools} (e.g., formatting commands) and actions, which are additional commands that are
18 * part of the toolbar, but not configured as tools.
20 * Individual tools are customized and then registered with a {@link OO.ui.ToolFactory tool factory}, which creates
21 * the tools on demand. Each tool has a symbolic name (used when registering the tool), a title (e.g., ‘Insert
22 * image’), and an icon.
24 * Individual tools are organized in {@link OO.ui.ToolGroup toolgroups}, which can be {@link OO.ui.MenuToolGroup menus}
25 * of tools, {@link OO.ui.ListToolGroup lists} of tools, or a single {@link OO.ui.BarToolGroup bar} of tools.
26 * The arrangement and order of the toolgroups is customized when the toolbar is set up. Tools can be presented in
27 * any order, but each can only appear once in the toolbar.
29 * The toolbar can be synchronized with the state of the external "application", like a text
30 * editor's editing area, marking tools as active/inactive (e.g. a 'bold' tool would be shown as
31 * active when the text cursor was inside bolded text) or enabled/disabled (e.g. a table caption
32 * tool would be disabled while the user is not editing a table). A state change is signalled by
33 * emitting the {@link #event-updateState 'updateState' event}, which calls Tools'
34 * {@link OO.ui.Tool#onUpdateState onUpdateState method}.
36 * The following is an example of a basic toolbar.
39 * // Example of a toolbar
40 * // Create the toolbar
41 * var toolFactory = new OO.ui.ToolFactory();
42 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
43 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
45 * // We will be placing status text in this element when tools are used
46 * var $area = $( '<p>' ).text( 'Toolbar example' );
48 * // Define the tools that we're going to place in our toolbar
50 * // Create a class inheriting from OO.ui.Tool
51 * function SearchTool() {
52 * SearchTool.parent.apply( this, arguments );
54 * OO.inheritClass( SearchTool, OO.ui.Tool );
55 * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
56 * // of 'icon' and 'title' (displayed icon and text).
57 * SearchTool.static.name = 'search';
58 * SearchTool.static.icon = 'search';
59 * SearchTool.static.title = 'Search...';
60 * // Defines the action that will happen when this tool is selected (clicked).
61 * SearchTool.prototype.onSelect = function () {
62 * $area.text( 'Search tool clicked!' );
63 * // Never display this tool as "active" (selected).
64 * this.setActive( false );
66 * SearchTool.prototype.onUpdateState = function () {};
67 * // Make this tool available in our toolFactory and thus our toolbar
68 * toolFactory.register( SearchTool );
70 * // Register two more tools, nothing interesting here
71 * function SettingsTool() {
72 * SettingsTool.parent.apply( this, arguments );
74 * OO.inheritClass( SettingsTool, OO.ui.Tool );
75 * SettingsTool.static.name = 'settings';
76 * SettingsTool.static.icon = 'settings';
77 * SettingsTool.static.title = 'Change settings';
78 * SettingsTool.prototype.onSelect = function () {
79 * $area.text( 'Settings tool clicked!' );
80 * this.setActive( false );
82 * SettingsTool.prototype.onUpdateState = function () {};
83 * toolFactory.register( SettingsTool );
85 * // Register two more tools, nothing interesting here
86 * function StuffTool() {
87 * StuffTool.parent.apply( this, arguments );
89 * OO.inheritClass( StuffTool, OO.ui.Tool );
90 * StuffTool.static.name = 'stuff';
91 * StuffTool.static.icon = 'ellipsis';
92 * StuffTool.static.title = 'More stuff';
93 * StuffTool.prototype.onSelect = function () {
94 * $area.text( 'More stuff tool clicked!' );
95 * this.setActive( false );
97 * StuffTool.prototype.onUpdateState = function () {};
98 * toolFactory.register( StuffTool );
100 * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
101 * // little popup window (a PopupWidget).
102 * function HelpTool( toolGroup, config ) {
103 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
108 * this.popup.$body.append( '<p>I am helpful!</p>' );
110 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
111 * HelpTool.static.name = 'help';
112 * HelpTool.static.icon = 'help';
113 * HelpTool.static.title = 'Help';
114 * toolFactory.register( HelpTool );
116 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
117 * // used once (but not all defined tools must be used).
120 * // 'bar' tool groups display tools' icons only, side-by-side.
122 * include: [ 'search', 'help' ]
125 * // 'list' tool groups display both the titles and icons, in a dropdown list.
129 * include: [ 'settings', 'stuff' ]
131 * // Note how the tools themselves are toolgroup-agnostic - the same tool can be displayed
132 * // either in a 'list' or a 'bar'. There is a 'menu' tool group too, not showcased here,
133 * // since it's more complicated to use. (See the next example snippet on this page.)
136 * // Create some UI around the toolbar and place it in the document
137 * var frame = new OO.ui.PanelLayout( {
141 * var contentFrame = new OO.ui.PanelLayout( {
145 * frame.$element.append(
147 * contentFrame.$element.append( $area )
149 * $( 'body' ).append( frame.$element );
151 * // Here is where the toolbar is actually built. This must be done after inserting it into the
153 * toolbar.initialize();
154 * toolbar.emit( 'updateState' );
156 * The following example extends the previous one to illustrate 'menu' toolgroups and the usage of
157 * {@link #event-updateState 'updateState' event}.
160 * // Create the toolbar
161 * var toolFactory = new OO.ui.ToolFactory();
162 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
163 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
165 * // We will be placing status text in this element when tools are used
166 * var $area = $( '<p>' ).text( 'Toolbar example' );
168 * // Define the tools that we're going to place in our toolbar
170 * // Create a class inheriting from OO.ui.Tool
171 * function SearchTool() {
172 * SearchTool.parent.apply( this, arguments );
174 * OO.inheritClass( SearchTool, OO.ui.Tool );
175 * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
176 * // of 'icon' and 'title' (displayed icon and text).
177 * SearchTool.static.name = 'search';
178 * SearchTool.static.icon = 'search';
179 * SearchTool.static.title = 'Search...';
180 * // Defines the action that will happen when this tool is selected (clicked).
181 * SearchTool.prototype.onSelect = function () {
182 * $area.text( 'Search tool clicked!' );
183 * // Never display this tool as "active" (selected).
184 * this.setActive( false );
186 * SearchTool.prototype.onUpdateState = function () {};
187 * // Make this tool available in our toolFactory and thus our toolbar
188 * toolFactory.register( SearchTool );
190 * // Register two more tools, nothing interesting here
191 * function SettingsTool() {
192 * SettingsTool.parent.apply( this, arguments );
193 * this.reallyActive = false;
195 * OO.inheritClass( SettingsTool, OO.ui.Tool );
196 * SettingsTool.static.name = 'settings';
197 * SettingsTool.static.icon = 'settings';
198 * SettingsTool.static.title = 'Change settings';
199 * SettingsTool.prototype.onSelect = function () {
200 * $area.text( 'Settings tool clicked!' );
201 * // Toggle the active state on each click
202 * this.reallyActive = !this.reallyActive;
203 * this.setActive( this.reallyActive );
204 * // To update the menu label
205 * this.toolbar.emit( 'updateState' );
207 * SettingsTool.prototype.onUpdateState = function () {};
208 * toolFactory.register( SettingsTool );
210 * // Register two more tools, nothing interesting here
211 * function StuffTool() {
212 * StuffTool.parent.apply( this, arguments );
213 * this.reallyActive = false;
215 * OO.inheritClass( StuffTool, OO.ui.Tool );
216 * StuffTool.static.name = 'stuff';
217 * StuffTool.static.icon = 'ellipsis';
218 * StuffTool.static.title = 'More stuff';
219 * StuffTool.prototype.onSelect = function () {
220 * $area.text( 'More stuff tool clicked!' );
221 * // Toggle the active state on each click
222 * this.reallyActive = !this.reallyActive;
223 * this.setActive( this.reallyActive );
224 * // To update the menu label
225 * this.toolbar.emit( 'updateState' );
227 * StuffTool.prototype.onUpdateState = function () {};
228 * toolFactory.register( StuffTool );
230 * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
231 * // little popup window (a PopupWidget). 'onUpdateState' is also already implemented.
232 * function HelpTool( toolGroup, config ) {
233 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
238 * this.popup.$body.append( '<p>I am helpful!</p>' );
240 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
241 * HelpTool.static.name = 'help';
242 * HelpTool.static.icon = 'help';
243 * HelpTool.static.title = 'Help';
244 * toolFactory.register( HelpTool );
246 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
247 * // used once (but not all defined tools must be used).
250 * // 'bar' tool groups display tools' icons only, side-by-side.
252 * include: [ 'search', 'help' ]
255 * // 'menu' tool groups display both the titles and icons, in a dropdown menu.
256 * // Menu label indicates which items are selected.
259 * include: [ 'settings', 'stuff' ]
263 * // Create some UI around the toolbar and place it in the document
264 * var frame = new OO.ui.PanelLayout( {
268 * var contentFrame = new OO.ui.PanelLayout( {
272 * frame.$element.append(
274 * contentFrame.$element.append( $area )
276 * $( 'body' ).append( frame.$element );
278 * // Here is where the toolbar is actually built. This must be done after inserting it into the
280 * toolbar.initialize();
281 * toolbar.emit( 'updateState' );
284 * @extends OO.ui.Element
285 * @mixins OO.EventEmitter
286 * @mixins OO.ui.mixin.GroupElement
289 * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
290 * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating toolgroups
291 * @param {Object} [config] Configuration options
292 * @cfg {boolean} [actions] Add an actions section to the toolbar. Actions are commands that are included
293 * in the toolbar, but are not configured as tools. By default, actions are displayed on the right side of
295 * @cfg {boolean} [shadow] Add a shadow below the toolbar.
297 OO
.ui
.Toolbar
= function OoUiToolbar( toolFactory
, toolGroupFactory
, config
) {
298 // Allow passing positional parameters inside the config object
299 if ( OO
.isPlainObject( toolFactory
) && config
=== undefined ) {
300 config
= toolFactory
;
301 toolFactory
= config
.toolFactory
;
302 toolGroupFactory
= config
.toolGroupFactory
;
305 // Configuration initialization
306 config
= config
|| {};
308 // Parent constructor
309 OO
.ui
.Toolbar
.parent
.call( this, config
);
311 // Mixin constructors
312 OO
.EventEmitter
.call( this );
313 OO
.ui
.mixin
.GroupElement
.call( this, config
);
316 this.toolFactory
= toolFactory
;
317 this.toolGroupFactory
= toolGroupFactory
;
320 this.$bar
= $( '<div>' );
321 this.$actions
= $( '<div>' );
322 this.initialized
= false;
323 this.onWindowResizeHandler
= this.onWindowResize
.bind( this );
327 .add( this.$bar
).add( this.$group
).add( this.$actions
)
328 .on( 'mousedown keydown', this.onPointerDown
.bind( this ) );
331 this.$group
.addClass( 'oo-ui-toolbar-tools' );
332 if ( config
.actions
) {
333 this.$bar
.append( this.$actions
.addClass( 'oo-ui-toolbar-actions' ) );
336 .addClass( 'oo-ui-toolbar-bar' )
337 .append( this.$group
, '<div style="clear:both"></div>' );
338 if ( config
.shadow
) {
339 this.$bar
.append( '<div class="oo-ui-toolbar-shadow"></div>' );
341 this.$element
.addClass( 'oo-ui-toolbar' ).append( this.$bar
);
346 OO
.inheritClass( OO
.ui
.Toolbar
, OO
.ui
.Element
);
347 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.EventEmitter
);
348 OO
.mixinClass( OO
.ui
.Toolbar
, OO
.ui
.mixin
.GroupElement
);
355 * An 'updateState' event must be emitted on the Toolbar (by calling `toolbar.emit( 'updateState' )`)
356 * every time the state of the application using the toolbar changes, and an update to the state of
359 * @param {...Mixed} data Application-defined parameters
365 * Get the tool factory.
367 * @return {OO.ui.ToolFactory} Tool factory
369 OO
.ui
.Toolbar
.prototype.getToolFactory = function () {
370 return this.toolFactory
;
374 * Get the toolgroup factory.
376 * @return {OO.Factory} Toolgroup factory
378 OO
.ui
.Toolbar
.prototype.getToolGroupFactory = function () {
379 return this.toolGroupFactory
;
383 * Handles mouse down events.
386 * @param {jQuery.Event} e Mouse down event
388 OO
.ui
.Toolbar
.prototype.onPointerDown = function ( e
) {
389 var $closestWidgetToEvent
= $( e
.target
).closest( '.oo-ui-widget' ),
390 $closestWidgetToToolbar
= this.$element
.closest( '.oo-ui-widget' );
391 if ( !$closestWidgetToEvent
.length
|| $closestWidgetToEvent
[ 0 ] === $closestWidgetToToolbar
[ 0 ] ) {
397 * Handle window resize event.
400 * @param {jQuery.Event} e Window resize event
402 OO
.ui
.Toolbar
.prototype.onWindowResize = function () {
403 this.$element
.toggleClass(
404 'oo-ui-toolbar-narrow',
405 this.$bar
.width() <= this.narrowThreshold
410 * Sets up handles and preloads required information for the toolbar to work.
411 * This must be called after it is attached to a visible document and before doing anything else.
413 OO
.ui
.Toolbar
.prototype.initialize = function () {
414 if ( !this.initialized
) {
415 this.initialized
= true;
416 this.narrowThreshold
= this.$group
.width() + this.$actions
.width();
417 $( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler
);
418 this.onWindowResize();
423 * Set up the toolbar.
425 * The toolbar is set up with a list of toolgroup configurations that specify the type of
426 * toolgroup ({@link OO.ui.BarToolGroup bar}, {@link OO.ui.MenuToolGroup menu}, or {@link OO.ui.ListToolGroup list})
427 * to add and which tools to include, exclude, promote, or demote within that toolgroup. Please
428 * see {@link OO.ui.ToolGroup toolgroups} for more information about including tools in toolgroups.
430 * @param {Object.<string,Array>} groups List of toolgroup configurations
431 * @param {Array|string} [groups.include] Tools to include in the toolgroup
432 * @param {Array|string} [groups.exclude] Tools to exclude from the toolgroup
433 * @param {Array|string} [groups.promote] Tools to promote to the beginning of the toolgroup
434 * @param {Array|string} [groups.demote] Tools to demote to the end of the toolgroup
436 OO
.ui
.Toolbar
.prototype.setup = function ( groups
) {
437 var i
, len
, type
, group
,
441 // Cleanup previous groups
444 // Build out new groups
445 for ( i
= 0, len
= groups
.length
; i
< len
; i
++ ) {
447 if ( group
.include
=== '*' ) {
448 // Apply defaults to catch-all groups
449 if ( group
.type
=== undefined ) {
452 if ( group
.label
=== undefined ) {
453 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
456 // Check type has been registered
457 type
= this.getToolGroupFactory().lookup( group
.type
) ? group
.type
: defaultType
;
459 this.getToolGroupFactory().create( type
, this, group
)
462 this.addItems( items
);
466 * Remove all tools and toolgroups from the toolbar.
468 OO
.ui
.Toolbar
.prototype.reset = function () {
473 for ( i
= 0, len
= this.items
.length
; i
< len
; i
++ ) {
474 this.items
[ i
].destroy();
480 * Destroy the toolbar.
482 * Destroying the toolbar removes all event handlers and DOM elements that constitute the toolbar. Call
483 * this method whenever you are done using a toolbar.
485 OO
.ui
.Toolbar
.prototype.destroy = function () {
486 $( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler
);
488 this.$element
.remove();
492 * Check if the tool is available.
494 * Available tools are ones that have not yet been added to the toolbar.
496 * @param {string} name Symbolic name of tool
497 * @return {boolean} Tool is available
499 OO
.ui
.Toolbar
.prototype.isToolAvailable = function ( name
) {
500 return !this.tools
[ name
];
504 * Prevent tool from being used again.
506 * @param {OO.ui.Tool} tool Tool to reserve
508 OO
.ui
.Toolbar
.prototype.reserveTool = function ( tool
) {
509 this.tools
[ tool
.getName() ] = tool
;
513 * Allow tool to be used again.
515 * @param {OO.ui.Tool} tool Tool to release
517 OO
.ui
.Toolbar
.prototype.releaseTool = function ( tool
) {
518 delete this.tools
[ tool
.getName() ];
522 * Get accelerator label for tool.
524 * The OOjs UI library does not contain an accelerator system, but this is the hook for one. To
525 * use an accelerator system, subclass the toolbar and override this method, which is meant to return a label
526 * that describes the accelerator keys for the tool passed (by symbolic name) to the method.
528 * @param {string} name Symbolic name of tool
529 * @return {string|undefined} Tool accelerator label if available
531 OO
.ui
.Toolbar
.prototype.getToolAccelerator = function () {
536 * Tools, together with {@link OO.ui.ToolGroup toolgroups}, constitute {@link OO.ui.Toolbar toolbars}.
537 * Each tool is configured with a static name, title, and icon and is customized with the command to carry
538 * out when the tool is selected. Tools must also be registered with a {@link OO.ui.ToolFactory tool factory},
539 * which creates the tools on demand.
541 * Every Tool subclass must implement two methods:
543 * - {@link #onUpdateState}
544 * - {@link #onSelect}
546 * Tools are added to toolgroups ({@link OO.ui.ListToolGroup ListToolGroup},
547 * {@link OO.ui.BarToolGroup BarToolGroup}, or {@link OO.ui.MenuToolGroup MenuToolGroup}), which determine how
548 * the tool is displayed in the toolbar. See {@link OO.ui.Toolbar toolbars} for an example.
550 * For more information, please see the [OOjs UI documentation on MediaWiki][1].
551 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
555 * @extends OO.ui.Widget
556 * @mixins OO.ui.mixin.IconElement
557 * @mixins OO.ui.mixin.FlaggedElement
558 * @mixins OO.ui.mixin.TabIndexedElement
561 * @param {OO.ui.ToolGroup} toolGroup
562 * @param {Object} [config] Configuration options
563 * @cfg {string|Function} [title] Title text or a function that returns text. If this config is omitted, the value of
564 * the {@link #static-title static title} property is used.
566 * The title is used in different ways depending on the type of toolgroup that contains the tool. The
567 * title is used as a tooltip if the tool is part of a {@link OO.ui.BarToolGroup bar} toolgroup, or as the label text if the tool is
568 * part of a {@link OO.ui.ListToolGroup list} or {@link OO.ui.MenuToolGroup menu} toolgroup.
570 * For bar toolgroups, a description of the accelerator key is appended to the title if an accelerator key
571 * is associated with an action by the same name as the tool and accelerator functionality has been added to the application.
572 * To add accelerator key functionality, you must subclass OO.ui.Toolbar and override the {@link OO.ui.Toolbar#getToolAccelerator getToolAccelerator} method.
574 OO
.ui
.Tool
= function OoUiTool( toolGroup
, config
) {
575 // Allow passing positional parameters inside the config object
576 if ( OO
.isPlainObject( toolGroup
) && config
=== undefined ) {
578 toolGroup
= config
.toolGroup
;
581 // Configuration initialization
582 config
= config
|| {};
584 // Parent constructor
585 OO
.ui
.Tool
.parent
.call( this, config
);
588 this.toolGroup
= toolGroup
;
589 this.toolbar
= this.toolGroup
.getToolbar();
591 this.$title
= $( '<span>' );
592 this.$accel
= $( '<span>' );
593 this.$link
= $( '<a>' );
596 // Mixin constructors
597 OO
.ui
.mixin
.IconElement
.call( this, config
);
598 OO
.ui
.mixin
.FlaggedElement
.call( this, config
);
599 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$link
} ) );
602 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
605 this.$title
.addClass( 'oo-ui-tool-title' );
607 .addClass( 'oo-ui-tool-accel' )
609 // This may need to be changed if the key names are ever localized,
610 // but for now they are essentially written in English
615 .addClass( 'oo-ui-tool-link' )
616 .append( this.$icon
, this.$title
, this.$accel
)
617 .attr( 'role', 'button' );
619 .data( 'oo-ui-tool', this )
621 'oo-ui-tool ' + 'oo-ui-tool-name-' +
622 this.constructor.static.name
.replace( /^([^\/]+)\/([^\/]+).*$/, '$1-$2' )
624 .toggleClass( 'oo-ui-tool-with-label', this.constructor.static.displayBothIconAndLabel
)
625 .append( this.$link
);
626 this.setTitle( config
.title
|| this.constructor.static.title
);
631 OO
.inheritClass( OO
.ui
.Tool
, OO
.ui
.Widget
);
632 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.mixin
.IconElement
);
633 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.mixin
.FlaggedElement
);
634 OO
.mixinClass( OO
.ui
.Tool
, OO
.ui
.mixin
.TabIndexedElement
);
636 /* Static Properties */
642 OO
.ui
.Tool
.static.tagName
= 'span';
645 * Symbolic name of tool.
647 * The symbolic name is used internally to register the tool with a {@link OO.ui.ToolFactory ToolFactory}. It can
648 * also be used when adding tools to toolgroups.
655 OO
.ui
.Tool
.static.name
= '';
658 * Symbolic name of the group.
660 * The group name is used to associate tools with each other so that they can be selected later by
661 * a {@link OO.ui.ToolGroup toolgroup}.
668 OO
.ui
.Tool
.static.group
= '';
671 * Tool title text or a function that returns title text. The value of the static property is overridden if the #title config option is used.
676 * @property {string|Function}
678 OO
.ui
.Tool
.static.title
= '';
681 * Display both icon and label when the tool is used in a {@link OO.ui.BarToolGroup bar} toolgroup.
682 * Normally only the icon is displayed, or only the label if no icon is given.
686 * @property {boolean}
688 OO
.ui
.Tool
.static.displayBothIconAndLabel
= false;
691 * Add tool to catch-all groups automatically.
693 * A catch-all group, which contains all tools that do not currently belong to a toolgroup,
694 * can be included in a toolgroup using the wildcard selector, an asterisk (*).
698 * @property {boolean}
700 OO
.ui
.Tool
.static.autoAddToCatchall
= true;
703 * Add tool to named groups automatically.
705 * By default, tools that are configured with a static ‘group’ property are added
706 * to that group and will be selected when the symbolic name of the group is specified (e.g., when
707 * toolgroups include tools by group name).
710 * @property {boolean}
713 OO
.ui
.Tool
.static.autoAddToGroup
= true;
716 * Check if this tool is compatible with given data.
718 * This is a stub that can be overridden to provide support for filtering tools based on an
719 * arbitrary piece of information (e.g., where the cursor is in a document). The implementation
720 * must also call this method so that the compatibility check can be performed.
724 * @param {Mixed} data Data to check
725 * @return {boolean} Tool can be used with data
727 OO
.ui
.Tool
.static.isCompatibleWith = function () {
734 * Handle the toolbar state being updated. This method is called when the
735 * {@link OO.ui.Toolbar#event-updateState 'updateState' event} is emitted on the
736 * {@link OO.ui.Toolbar Toolbar} that uses this tool, and should set the state of this tool
737 * depending on application state (usually by calling #setDisabled to enable or disable the tool,
738 * or #setActive to mark is as currently in-use or not).
740 * This is an abstract method that must be overridden in a concrete subclass.
746 OO
.ui
.Tool
.prototype.onUpdateState
= null;
749 * Handle the tool being selected. This method is called when the user triggers this tool,
750 * usually by clicking on its label/icon.
752 * This is an abstract method that must be overridden in a concrete subclass.
758 OO
.ui
.Tool
.prototype.onSelect
= null;
761 * Check if the tool is active.
763 * Tools become active when their #onSelect or #onUpdateState handlers change them to appear pressed
764 * with the #setActive method. Additional CSS is applied to the tool to reflect the active state.
766 * @return {boolean} Tool is active
768 OO
.ui
.Tool
.prototype.isActive = function () {
773 * Make the tool appear active or inactive.
775 * This method should be called within #onSelect or #onUpdateState event handlers to make the tool
776 * appear pressed or not.
778 * @param {boolean} state Make tool appear active
780 OO
.ui
.Tool
.prototype.setActive = function ( state
) {
781 this.active
= !!state
;
783 this.$element
.addClass( 'oo-ui-tool-active' );
785 this.$element
.removeClass( 'oo-ui-tool-active' );
790 * Set the tool #title.
792 * @param {string|Function} title Title text or a function that returns text
795 OO
.ui
.Tool
.prototype.setTitle = function ( title
) {
796 this.title
= OO
.ui
.resolveMsg( title
);
802 * Get the tool #title.
804 * @return {string} Title text
806 OO
.ui
.Tool
.prototype.getTitle = function () {
811 * Get the tool's symbolic name.
813 * @return {string} Symbolic name of tool
815 OO
.ui
.Tool
.prototype.getName = function () {
816 return this.constructor.static.name
;
822 OO
.ui
.Tool
.prototype.updateTitle = function () {
823 var titleTooltips
= this.toolGroup
.constructor.static.titleTooltips
,
824 accelTooltips
= this.toolGroup
.constructor.static.accelTooltips
,
825 accel
= this.toolbar
.getToolAccelerator( this.constructor.static.name
),
828 this.$title
.text( this.title
);
829 this.$accel
.text( accel
);
831 if ( titleTooltips
&& typeof this.title
=== 'string' && this.title
.length
) {
832 tooltipParts
.push( this.title
);
834 if ( accelTooltips
&& typeof accel
=== 'string' && accel
.length
) {
835 tooltipParts
.push( accel
);
837 if ( tooltipParts
.length
) {
838 this.$link
.attr( 'title', tooltipParts
.join( ' ' ) );
840 this.$link
.removeAttr( 'title' );
847 * Destroying the tool removes all event handlers and the tool’s DOM elements.
848 * Call this method whenever you are done using a tool.
850 OO
.ui
.Tool
.prototype.destroy = function () {
851 this.toolbar
.disconnect( this );
852 this.$element
.remove();
856 * ToolGroups are collections of {@link OO.ui.Tool tools} that are used in a {@link OO.ui.Toolbar toolbar}.
857 * The type of toolgroup ({@link OO.ui.ListToolGroup list}, {@link OO.ui.BarToolGroup bar}, or {@link OO.ui.MenuToolGroup menu})
858 * to which a tool belongs determines how the tool is arranged and displayed in the toolbar. Toolgroups
859 * themselves are created on demand with a {@link OO.ui.ToolGroupFactory toolgroup factory}.
861 * Toolgroups can contain individual tools, groups of tools, or all available tools, as specified
862 * using the `include` config option. See OO.ui.ToolFactory#extract on documentation of the format.
863 * The options `exclude`, `promote`, and `demote` support the same formats.
865 * See {@link OO.ui.Toolbar toolbars} for a full example. For more information about toolbars in general,
866 * please see the [OOjs UI documentation on MediaWiki][1].
868 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
872 * @extends OO.ui.Widget
873 * @mixins OO.ui.mixin.GroupElement
876 * @param {OO.ui.Toolbar} toolbar
877 * @param {Object} [config] Configuration options
878 * @cfg {Array|string} [include] List of tools to include in the toolgroup, see above.
879 * @cfg {Array|string} [exclude] List of tools to exclude from the toolgroup, see above.
880 * @cfg {Array|string} [promote] List of tools to promote to the beginning of the toolgroup, see above.
881 * @cfg {Array|string} [demote] List of tools to demote to the end of the toolgroup, see above.
882 * This setting is particularly useful when tools have been added to the toolgroup
883 * en masse (e.g., via the catch-all selector).
885 OO
.ui
.ToolGroup
= function OoUiToolGroup( toolbar
, config
) {
886 // Allow passing positional parameters inside the config object
887 if ( OO
.isPlainObject( toolbar
) && config
=== undefined ) {
889 toolbar
= config
.toolbar
;
892 // Configuration initialization
893 config
= config
|| {};
895 // Parent constructor
896 OO
.ui
.ToolGroup
.parent
.call( this, config
);
898 // Mixin constructors
899 OO
.ui
.mixin
.GroupElement
.call( this, config
);
902 this.toolbar
= toolbar
;
905 this.autoDisabled
= false;
906 this.include
= config
.include
|| [];
907 this.exclude
= config
.exclude
|| [];
908 this.promote
= config
.promote
|| [];
909 this.demote
= config
.demote
|| [];
910 this.onCapturedMouseKeyUpHandler
= this.onCapturedMouseKeyUp
.bind( this );
914 mousedown
: this.onMouseKeyDown
.bind( this ),
915 mouseup
: this.onMouseKeyUp
.bind( this ),
916 keydown
: this.onMouseKeyDown
.bind( this ),
917 keyup
: this.onMouseKeyUp
.bind( this ),
918 focus
: this.onMouseOverFocus
.bind( this ),
919 blur
: this.onMouseOutBlur
.bind( this ),
920 mouseover
: this.onMouseOverFocus
.bind( this ),
921 mouseout
: this.onMouseOutBlur
.bind( this )
923 this.toolbar
.getToolFactory().connect( this, { register
: 'onToolFactoryRegister' } );
924 this.aggregate( { disable
: 'itemDisable' } );
925 this.connect( this, { itemDisable
: 'updateDisabled' } );
928 this.$group
.addClass( 'oo-ui-toolGroup-tools' );
930 .addClass( 'oo-ui-toolGroup' )
931 .append( this.$group
);
937 OO
.inheritClass( OO
.ui
.ToolGroup
, OO
.ui
.Widget
);
938 OO
.mixinClass( OO
.ui
.ToolGroup
, OO
.ui
.mixin
.GroupElement
);
946 /* Static Properties */
949 * Show labels in tooltips.
953 * @property {boolean}
955 OO
.ui
.ToolGroup
.static.titleTooltips
= false;
958 * Show acceleration labels in tooltips.
960 * Note: The OOjs UI library does not include an accelerator system, but does contain
961 * a hook for one. To use an accelerator system, subclass the {@link OO.ui.Toolbar toolbar} and
962 * override the {@link OO.ui.Toolbar#getToolAccelerator getToolAccelerator} method, which is
963 * meant to return a label that describes the accelerator keys for a given tool (e.g., 'Ctrl + M').
967 * @property {boolean}
969 OO
.ui
.ToolGroup
.static.accelTooltips
= false;
972 * Automatically disable the toolgroup when all tools are disabled
976 * @property {boolean}
978 OO
.ui
.ToolGroup
.static.autoDisable
= true;
985 OO
.ui
.ToolGroup
.prototype.isDisabled = function () {
986 return this.autoDisabled
|| OO
.ui
.ToolGroup
.parent
.prototype.isDisabled
.apply( this, arguments
);
992 OO
.ui
.ToolGroup
.prototype.updateDisabled = function () {
993 var i
, item
, allDisabled
= true;
995 if ( this.constructor.static.autoDisable
) {
996 for ( i
= this.items
.length
- 1; i
>= 0; i
-- ) {
997 item
= this.items
[ i
];
998 if ( !item
.isDisabled() ) {
1003 this.autoDisabled
= allDisabled
;
1005 OO
.ui
.ToolGroup
.parent
.prototype.updateDisabled
.apply( this, arguments
);
1009 * Handle mouse down and key down events.
1012 * @param {jQuery.Event} e Mouse down or key down event
1014 OO
.ui
.ToolGroup
.prototype.onMouseKeyDown = function ( e
) {
1016 !this.isDisabled() &&
1017 ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
|| e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
1019 this.pressed
= this.getTargetTool( e
);
1020 if ( this.pressed
) {
1021 this.pressed
.setActive( true );
1022 this.getElementDocument().addEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler
, true );
1023 this.getElementDocument().addEventListener( 'keyup', this.onCapturedMouseKeyUpHandler
, true );
1030 * Handle captured mouse up and key up events.
1033 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1035 OO
.ui
.ToolGroup
.prototype.onCapturedMouseKeyUp = function ( e
) {
1036 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler
, true );
1037 this.getElementDocument().removeEventListener( 'keyup', this.onCapturedMouseKeyUpHandler
, true );
1038 // onMouseKeyUp may be called a second time, depending on where the mouse is when the button is
1039 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
1040 this.onMouseKeyUp( e
);
1044 * Handle mouse up and key up events.
1047 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1049 OO
.ui
.ToolGroup
.prototype.onMouseKeyUp = function ( e
) {
1050 var tool
= this.getTargetTool( e
);
1053 !this.isDisabled() && this.pressed
&& this.pressed
=== tool
&&
1054 ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
|| e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
1056 this.pressed
.onSelect();
1057 this.pressed
= null;
1059 e
.stopPropagation();
1062 this.pressed
= null;
1066 * Handle mouse over and focus events.
1069 * @param {jQuery.Event} e Mouse over or focus event
1071 OO
.ui
.ToolGroup
.prototype.onMouseOverFocus = function ( e
) {
1072 var tool
= this.getTargetTool( e
);
1074 if ( this.pressed
&& this.pressed
=== tool
) {
1075 this.pressed
.setActive( true );
1080 * Handle mouse out and blur events.
1083 * @param {jQuery.Event} e Mouse out or blur event
1085 OO
.ui
.ToolGroup
.prototype.onMouseOutBlur = function ( e
) {
1086 var tool
= this.getTargetTool( e
);
1088 if ( this.pressed
&& this.pressed
=== tool
) {
1089 this.pressed
.setActive( false );
1094 * Get the closest tool to a jQuery.Event.
1096 * Only tool links are considered, which prevents other elements in the tool such as popups from
1097 * triggering tool group interactions.
1100 * @param {jQuery.Event} e
1101 * @return {OO.ui.Tool|null} Tool, `null` if none was found
1103 OO
.ui
.ToolGroup
.prototype.getTargetTool = function ( e
) {
1105 $item
= $( e
.target
).closest( '.oo-ui-tool-link' );
1107 if ( $item
.length
) {
1108 tool
= $item
.parent().data( 'oo-ui-tool' );
1111 return tool
&& !tool
.isDisabled() ? tool
: null;
1115 * Handle tool registry register events.
1117 * If a tool is registered after the group is created, we must repopulate the list to account for:
1119 * - a tool being added that may be included
1120 * - a tool already included being overridden
1123 * @param {string} name Symbolic name of tool
1125 OO
.ui
.ToolGroup
.prototype.onToolFactoryRegister = function () {
1130 * Get the toolbar that contains the toolgroup.
1132 * @return {OO.ui.Toolbar} Toolbar that contains the toolgroup
1134 OO
.ui
.ToolGroup
.prototype.getToolbar = function () {
1135 return this.toolbar
;
1139 * Add and remove tools based on configuration.
1141 OO
.ui
.ToolGroup
.prototype.populate = function () {
1142 var i
, len
, name
, tool
,
1143 toolFactory
= this.toolbar
.getToolFactory(),
1147 list
= this.toolbar
.getToolFactory().getTools(
1148 this.include
, this.exclude
, this.promote
, this.demote
1151 // Build a list of needed tools
1152 for ( i
= 0, len
= list
.length
; i
< len
; i
++ ) {
1156 toolFactory
.lookup( name
) &&
1157 // Tool is available or is already in this group
1158 ( this.toolbar
.isToolAvailable( name
) || this.tools
[ name
] )
1160 // Hack to prevent infinite recursion via ToolGroupTool. We need to reserve the tool before
1161 // creating it, but we can't call reserveTool() yet because we haven't created the tool.
1162 this.toolbar
.tools
[ name
] = true;
1163 tool
= this.tools
[ name
];
1165 // Auto-initialize tools on first use
1166 this.tools
[ name
] = tool
= toolFactory
.create( name
, this );
1169 this.toolbar
.reserveTool( tool
);
1171 names
[ name
] = true;
1174 // Remove tools that are no longer needed
1175 for ( name
in this.tools
) {
1176 if ( !names
[ name
] ) {
1177 this.tools
[ name
].destroy();
1178 this.toolbar
.releaseTool( this.tools
[ name
] );
1179 remove
.push( this.tools
[ name
] );
1180 delete this.tools
[ name
];
1183 if ( remove
.length
) {
1184 this.removeItems( remove
);
1186 // Update emptiness state
1188 this.$element
.removeClass( 'oo-ui-toolGroup-empty' );
1190 this.$element
.addClass( 'oo-ui-toolGroup-empty' );
1192 // Re-add tools (moving existing ones to new locations)
1193 this.addItems( add
);
1194 // Disabled state may depend on items
1195 this.updateDisabled();
1199 * Destroy toolgroup.
1201 OO
.ui
.ToolGroup
.prototype.destroy = function () {
1205 this.toolbar
.getToolFactory().disconnect( this );
1206 for ( name
in this.tools
) {
1207 this.toolbar
.releaseTool( this.tools
[ name
] );
1208 this.tools
[ name
].disconnect( this ).destroy();
1209 delete this.tools
[ name
];
1211 this.$element
.remove();
1215 * A ToolFactory creates tools on demand. All tools ({@link OO.ui.Tool Tools}, {@link OO.ui.PopupTool PopupTools},
1216 * and {@link OO.ui.ToolGroupTool ToolGroupTools}) must be registered with a tool factory. Tools are
1217 * registered by their symbolic name. See {@link OO.ui.Toolbar toolbars} for an example.
1219 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
1221 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1224 * @extends OO.Factory
1227 OO
.ui
.ToolFactory
= function OoUiToolFactory() {
1228 // Parent constructor
1229 OO
.ui
.ToolFactory
.parent
.call( this );
1234 OO
.inheritClass( OO
.ui
.ToolFactory
, OO
.Factory
);
1239 * Get tools from the factory
1241 * @param {Array|string} [include] Included tools, see #extract for format
1242 * @param {Array|string} [exclude] Excluded tools, see #extract for format
1243 * @param {Array|string} [promote] Promoted tools, see #extract for format
1244 * @param {Array|string} [demote] Demoted tools, see #extract for format
1245 * @return {string[]} List of tools
1247 OO
.ui
.ToolFactory
.prototype.getTools = function ( include
, exclude
, promote
, demote
) {
1248 var i
, len
, included
, promoted
, demoted
,
1252 // Collect included and not excluded tools
1253 included
= OO
.simpleArrayDifference( this.extract( include
), this.extract( exclude
) );
1256 promoted
= this.extract( promote
, used
);
1257 demoted
= this.extract( demote
, used
);
1260 for ( i
= 0, len
= included
.length
; i
< len
; i
++ ) {
1261 if ( !used
[ included
[ i
] ] ) {
1262 auto
.push( included
[ i
] );
1266 return promoted
.concat( auto
).concat( demoted
);
1270 * Get a flat list of names from a list of names or groups.
1272 * Normally, `collection` is an array of tool specifications. Tools can be specified in the
1275 * - To include an individual tool, use the symbolic name: `{ name: 'tool-name' }` or `'tool-name'`.
1276 * - To include all tools in a group, use the group name: `{ group: 'group-name' }`. (To assign the
1277 * tool to a group, use OO.ui.Tool.static.group.)
1279 * Alternatively, to include all tools that are not yet assigned to any other toolgroup, use the
1280 * catch-all selector `'*'`.
1282 * If `used` is passed, tool names that appear as properties in this object will be considered
1283 * already assigned, and will not be returned even if specified otherwise. The tool names extracted
1284 * by this function call will be added as new properties in the object.
1287 * @param {Array|string} collection List of tools, see above
1288 * @param {Object} [used] Object containing information about used tools, see above
1289 * @return {string[]} List of extracted tool names
1291 OO
.ui
.ToolFactory
.prototype.extract = function ( collection
, used
) {
1292 var i
, len
, item
, name
, tool
,
1295 if ( collection
=== '*' ) {
1296 for ( name
in this.registry
) {
1297 tool
= this.registry
[ name
];
1299 // Only add tools by group name when auto-add is enabled
1300 tool
.static.autoAddToCatchall
&&
1301 // Exclude already used tools
1302 ( !used
|| !used
[ name
] )
1306 used
[ name
] = true;
1310 } else if ( Array
.isArray( collection
) ) {
1311 for ( i
= 0, len
= collection
.length
; i
< len
; i
++ ) {
1312 item
= collection
[ i
];
1313 // Allow plain strings as shorthand for named tools
1314 if ( typeof item
=== 'string' ) {
1315 item
= { name
: item
};
1317 if ( OO
.isPlainObject( item
) ) {
1319 for ( name
in this.registry
) {
1320 tool
= this.registry
[ name
];
1322 // Include tools with matching group
1323 tool
.static.group
=== item
.group
&&
1324 // Only add tools by group name when auto-add is enabled
1325 tool
.static.autoAddToGroup
&&
1326 // Exclude already used tools
1327 ( !used
|| !used
[ name
] )
1331 used
[ name
] = true;
1335 // Include tools with matching name and exclude already used tools
1336 } else if ( item
.name
&& ( !used
|| !used
[ item
.name
] ) ) {
1337 names
.push( item
.name
);
1339 used
[ item
.name
] = true;
1349 * ToolGroupFactories create {@link OO.ui.ToolGroup toolgroups} on demand. The toolgroup classes must
1350 * specify a symbolic name and be registered with the factory. The following classes are registered by
1353 * - {@link OO.ui.BarToolGroup BarToolGroups} (‘bar’)
1354 * - {@link OO.ui.MenuToolGroup MenuToolGroups} (‘menu’)
1355 * - {@link OO.ui.ListToolGroup ListToolGroups} (‘list’)
1357 * See {@link OO.ui.Toolbar toolbars} for an example.
1359 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
1361 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1364 * @extends OO.Factory
1367 OO
.ui
.ToolGroupFactory
= function OoUiToolGroupFactory() {
1368 var i
, l
, defaultClasses
;
1369 // Parent constructor
1370 OO
.Factory
.call( this );
1372 defaultClasses
= this.constructor.static.getDefaultClasses();
1374 // Register default toolgroups
1375 for ( i
= 0, l
= defaultClasses
.length
; i
< l
; i
++ ) {
1376 this.register( defaultClasses
[ i
] );
1382 OO
.inheritClass( OO
.ui
.ToolGroupFactory
, OO
.Factory
);
1384 /* Static Methods */
1387 * Get a default set of classes to be registered on construction.
1389 * @return {Function[]} Default classes
1391 OO
.ui
.ToolGroupFactory
.static.getDefaultClasses = function () {
1394 OO
.ui
.ListToolGroup
,
1400 * Popup tools open a popup window when they are selected from the {@link OO.ui.Toolbar toolbar}. Each popup tool is configured
1401 * with a static name, title, and icon, as well with as any popup configurations. Unlike other tools, popup tools do not require that developers specify
1402 * an #onSelect or #onUpdateState method, as these methods have been implemented already.
1404 * // Example of a popup tool. When selected, a popup tool displays
1405 * // a popup window.
1406 * function HelpTool( toolGroup, config ) {
1407 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
1412 * this.popup.$body.append( '<p>I am helpful!</p>' );
1414 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
1415 * HelpTool.static.name = 'help';
1416 * HelpTool.static.icon = 'help';
1417 * HelpTool.static.title = 'Help';
1418 * toolFactory.register( HelpTool );
1420 * For an example of a toolbar that contains a popup tool, see {@link OO.ui.Toolbar toolbars}. For more information about
1421 * toolbars in genreral, please see the [OOjs UI documentation on MediaWiki][1].
1423 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1427 * @extends OO.ui.Tool
1428 * @mixins OO.ui.mixin.PopupElement
1431 * @param {OO.ui.ToolGroup} toolGroup
1432 * @param {Object} [config] Configuration options
1434 OO
.ui
.PopupTool
= function OoUiPopupTool( toolGroup
, config
) {
1435 // Allow passing positional parameters inside the config object
1436 if ( OO
.isPlainObject( toolGroup
) && config
=== undefined ) {
1438 toolGroup
= config
.toolGroup
;
1441 // Parent constructor
1442 OO
.ui
.PopupTool
.parent
.call( this, toolGroup
, config
);
1444 // Mixin constructors
1445 OO
.ui
.mixin
.PopupElement
.call( this, config
);
1449 .addClass( 'oo-ui-popupTool' )
1450 .append( this.popup
.$element
);
1455 OO
.inheritClass( OO
.ui
.PopupTool
, OO
.ui
.Tool
);
1456 OO
.mixinClass( OO
.ui
.PopupTool
, OO
.ui
.mixin
.PopupElement
);
1461 * Handle the tool being selected.
1465 OO
.ui
.PopupTool
.prototype.onSelect = function () {
1466 if ( !this.isDisabled() ) {
1467 this.popup
.toggle();
1469 this.setActive( false );
1474 * Handle the toolbar state being updated.
1478 OO
.ui
.PopupTool
.prototype.onUpdateState = function () {
1479 this.setActive( false );
1483 * A ToolGroupTool is a special sort of tool that can contain other {@link OO.ui.Tool tools}
1484 * and {@link OO.ui.ToolGroup toolgroups}. The ToolGroupTool was specifically designed to be used
1485 * inside a {@link OO.ui.BarToolGroup bar} toolgroup to provide access to additional tools from
1486 * the bar item. Included tools will be displayed in a dropdown {@link OO.ui.ListToolGroup list}
1487 * when the ToolGroupTool is selected.
1489 * // Example: ToolGroupTool with two nested tools, 'setting1' and 'setting2', defined elsewhere.
1491 * function SettingsTool() {
1492 * SettingsTool.parent.apply( this, arguments );
1494 * OO.inheritClass( SettingsTool, OO.ui.ToolGroupTool );
1495 * SettingsTool.static.name = 'settings';
1496 * SettingsTool.static.title = 'Change settings';
1497 * SettingsTool.static.groupConfig = {
1499 * label: 'ToolGroupTool',
1500 * include: [ 'setting1', 'setting2' ]
1502 * toolFactory.register( SettingsTool );
1504 * For more information, please see the [OOjs UI documentation on MediaWiki][1].
1506 * Please note that this implementation is subject to change per [T74159] [2].
1508 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars#ToolGroupTool
1509 * [2]: https://phabricator.wikimedia.org/T74159
1513 * @extends OO.ui.Tool
1516 * @param {OO.ui.ToolGroup} toolGroup
1517 * @param {Object} [config] Configuration options
1519 OO
.ui
.ToolGroupTool
= function OoUiToolGroupTool( toolGroup
, config
) {
1520 // Allow passing positional parameters inside the config object
1521 if ( OO
.isPlainObject( toolGroup
) && config
=== undefined ) {
1523 toolGroup
= config
.toolGroup
;
1526 // Parent constructor
1527 OO
.ui
.ToolGroupTool
.parent
.call( this, toolGroup
, config
);
1530 this.innerToolGroup
= this.createGroup( this.constructor.static.groupConfig
);
1533 this.innerToolGroup
.connect( this, { disable
: 'onToolGroupDisable' } );
1536 this.$link
.remove();
1538 .addClass( 'oo-ui-toolGroupTool' )
1539 .append( this.innerToolGroup
.$element
);
1544 OO
.inheritClass( OO
.ui
.ToolGroupTool
, OO
.ui
.Tool
);
1546 /* Static Properties */
1549 * Toolgroup configuration.
1551 * The toolgroup configuration consists of the tools to include, as well as an icon and label
1552 * to use for the bar item. Tools can be included by symbolic name, group, or with the
1553 * wildcard selector. Please see {@link OO.ui.ToolGroup toolgroup} for more information.
1555 * @property {Object.<string,Array>}
1557 OO
.ui
.ToolGroupTool
.static.groupConfig
= {};
1562 * Handle the tool being selected.
1566 OO
.ui
.ToolGroupTool
.prototype.onSelect = function () {
1567 this.innerToolGroup
.setActive( !this.innerToolGroup
.active
);
1572 * Synchronize disabledness state of the tool with the inner toolgroup.
1575 * @param {boolean} disabled Element is disabled
1577 OO
.ui
.ToolGroupTool
.prototype.onToolGroupDisable = function ( disabled
) {
1578 this.setDisabled( disabled
);
1582 * Handle the toolbar state being updated.
1586 OO
.ui
.ToolGroupTool
.prototype.onUpdateState = function () {
1587 this.setActive( false );
1591 * Build a {@link OO.ui.ToolGroup toolgroup} from the specified configuration.
1593 * @param {Object.<string,Array>} group Toolgroup configuration. Please see {@link OO.ui.ToolGroup toolgroup} for
1595 * @return {OO.ui.ListToolGroup}
1597 OO
.ui
.ToolGroupTool
.prototype.createGroup = function ( group
) {
1598 if ( group
.include
=== '*' ) {
1599 // Apply defaults to catch-all groups
1600 if ( group
.label
=== undefined ) {
1601 group
.label
= OO
.ui
.msg( 'ooui-toolbar-more' );
1605 return this.toolbar
.getToolGroupFactory().create( 'list', this.toolbar
, group
);
1609 * BarToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
1610 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
1611 * and {@link OO.ui.ListToolGroup ListToolGroup}). The {@link OO.ui.Tool tools} in a BarToolGroup are
1612 * displayed by icon in a single row. The title of the tool is displayed when users move the mouse over
1615 * BarToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar is
1619 * // Example of a BarToolGroup with two tools
1620 * var toolFactory = new OO.ui.ToolFactory();
1621 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
1622 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
1624 * // We will be placing status text in this element when tools are used
1625 * var $area = $( '<p>' ).text( 'Example of a BarToolGroup with two tools.' );
1627 * // Define the tools that we're going to place in our toolbar
1629 * // Create a class inheriting from OO.ui.Tool
1630 * function SearchTool() {
1631 * SearchTool.parent.apply( this, arguments );
1633 * OO.inheritClass( SearchTool, OO.ui.Tool );
1634 * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
1635 * // of 'icon' and 'title' (displayed icon and text).
1636 * SearchTool.static.name = 'search';
1637 * SearchTool.static.icon = 'search';
1638 * SearchTool.static.title = 'Search...';
1639 * // Defines the action that will happen when this tool is selected (clicked).
1640 * SearchTool.prototype.onSelect = function () {
1641 * $area.text( 'Search tool clicked!' );
1642 * // Never display this tool as "active" (selected).
1643 * this.setActive( false );
1645 * SearchTool.prototype.onUpdateState = function () {};
1646 * // Make this tool available in our toolFactory and thus our toolbar
1647 * toolFactory.register( SearchTool );
1649 * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
1650 * // little popup window (a PopupWidget).
1651 * function HelpTool( toolGroup, config ) {
1652 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
1657 * this.popup.$body.append( '<p>I am helpful!</p>' );
1659 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
1660 * HelpTool.static.name = 'help';
1661 * HelpTool.static.icon = 'help';
1662 * HelpTool.static.title = 'Help';
1663 * toolFactory.register( HelpTool );
1665 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
1666 * // used once (but not all defined tools must be used).
1669 * // 'bar' tool groups display tools by icon only
1671 * include: [ 'search', 'help' ]
1675 * // Create some UI around the toolbar and place it in the document
1676 * var frame = new OO.ui.PanelLayout( {
1680 * var contentFrame = new OO.ui.PanelLayout( {
1684 * frame.$element.append(
1686 * contentFrame.$element.append( $area )
1688 * $( 'body' ).append( frame.$element );
1690 * // Here is where the toolbar is actually built. This must be done after inserting it into the
1692 * toolbar.initialize();
1694 * For more information about how to add tools to a bar tool group, please see {@link OO.ui.ToolGroup toolgroup}.
1695 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
1697 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1700 * @extends OO.ui.ToolGroup
1703 * @param {OO.ui.Toolbar} toolbar
1704 * @param {Object} [config] Configuration options
1706 OO
.ui
.BarToolGroup
= function OoUiBarToolGroup( toolbar
, config
) {
1707 // Allow passing positional parameters inside the config object
1708 if ( OO
.isPlainObject( toolbar
) && config
=== undefined ) {
1710 toolbar
= config
.toolbar
;
1713 // Parent constructor
1714 OO
.ui
.BarToolGroup
.parent
.call( this, toolbar
, config
);
1717 this.$element
.addClass( 'oo-ui-barToolGroup' );
1722 OO
.inheritClass( OO
.ui
.BarToolGroup
, OO
.ui
.ToolGroup
);
1724 /* Static Properties */
1726 OO
.ui
.BarToolGroup
.static.titleTooltips
= true;
1728 OO
.ui
.BarToolGroup
.static.accelTooltips
= true;
1730 OO
.ui
.BarToolGroup
.static.name
= 'bar';
1733 * PopupToolGroup is an abstract base class used by both {@link OO.ui.MenuToolGroup MenuToolGroup}
1734 * and {@link OO.ui.ListToolGroup ListToolGroup} to provide a popup--an overlaid menu or list of tools with an
1735 * optional icon and label. This class can be used for other base classes that also use this functionality.
1739 * @extends OO.ui.ToolGroup
1740 * @mixins OO.ui.mixin.IconElement
1741 * @mixins OO.ui.mixin.IndicatorElement
1742 * @mixins OO.ui.mixin.LabelElement
1743 * @mixins OO.ui.mixin.TitledElement
1744 * @mixins OO.ui.mixin.ClippableElement
1745 * @mixins OO.ui.mixin.TabIndexedElement
1748 * @param {OO.ui.Toolbar} toolbar
1749 * @param {Object} [config] Configuration options
1750 * @cfg {string} [header] Text to display at the top of the popup
1752 OO
.ui
.PopupToolGroup
= function OoUiPopupToolGroup( toolbar
, config
) {
1753 // Allow passing positional parameters inside the config object
1754 if ( OO
.isPlainObject( toolbar
) && config
=== undefined ) {
1756 toolbar
= config
.toolbar
;
1759 // Configuration initialization
1760 config
= config
|| {};
1762 // Parent constructor
1763 OO
.ui
.PopupToolGroup
.parent
.call( this, toolbar
, config
);
1766 this.active
= false;
1767 this.dragging
= false;
1768 this.onBlurHandler
= this.onBlur
.bind( this );
1769 this.$handle
= $( '<span>' );
1771 // Mixin constructors
1772 OO
.ui
.mixin
.IconElement
.call( this, config
);
1773 OO
.ui
.mixin
.IndicatorElement
.call( this, config
);
1774 OO
.ui
.mixin
.LabelElement
.call( this, config
);
1775 OO
.ui
.mixin
.TitledElement
.call( this, config
);
1776 OO
.ui
.mixin
.ClippableElement
.call( this, $.extend( {}, config
, { $clippable
: this.$group
} ) );
1777 OO
.ui
.mixin
.TabIndexedElement
.call( this, $.extend( {}, config
, { $tabIndexed
: this.$handle
} ) );
1781 keydown
: this.onHandleMouseKeyDown
.bind( this ),
1782 keyup
: this.onHandleMouseKeyUp
.bind( this ),
1783 mousedown
: this.onHandleMouseKeyDown
.bind( this ),
1784 mouseup
: this.onHandleMouseKeyUp
.bind( this )
1789 .addClass( 'oo-ui-popupToolGroup-handle' )
1790 .append( this.$icon
, this.$label
, this.$indicator
);
1791 // If the pop-up should have a header, add it to the top of the toolGroup.
1792 // Note: If this feature is useful for other widgets, we could abstract it into an
1793 // OO.ui.HeaderedElement mixin constructor.
1794 if ( config
.header
!== undefined ) {
1796 .prepend( $( '<span>' )
1797 .addClass( 'oo-ui-popupToolGroup-header' )
1798 .text( config
.header
)
1802 .addClass( 'oo-ui-popupToolGroup' )
1803 .prepend( this.$handle
);
1808 OO
.inheritClass( OO
.ui
.PopupToolGroup
, OO
.ui
.ToolGroup
);
1809 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.mixin
.IconElement
);
1810 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.mixin
.IndicatorElement
);
1811 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.mixin
.LabelElement
);
1812 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.mixin
.TitledElement
);
1813 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.mixin
.ClippableElement
);
1814 OO
.mixinClass( OO
.ui
.PopupToolGroup
, OO
.ui
.mixin
.TabIndexedElement
);
1821 OO
.ui
.PopupToolGroup
.prototype.setDisabled = function () {
1823 OO
.ui
.PopupToolGroup
.parent
.prototype.setDisabled
.apply( this, arguments
);
1825 if ( this.isDisabled() && this.isElementAttached() ) {
1826 this.setActive( false );
1831 * Handle focus being lost.
1833 * The event is actually generated from a mouseup/keyup, so it is not a normal blur event object.
1836 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1838 OO
.ui
.PopupToolGroup
.prototype.onBlur = function ( e
) {
1839 // Only deactivate when clicking outside the dropdown element
1840 if ( $( e
.target
).closest( '.oo-ui-popupToolGroup' )[ 0 ] !== this.$element
[ 0 ] ) {
1841 this.setActive( false );
1848 OO
.ui
.PopupToolGroup
.prototype.onMouseKeyUp = function ( e
) {
1849 // Only close toolgroup when a tool was actually selected
1851 !this.isDisabled() && this.pressed
&& this.pressed
=== this.getTargetTool( e
) &&
1852 ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
|| e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
1854 this.setActive( false );
1856 return OO
.ui
.PopupToolGroup
.parent
.prototype.onMouseKeyUp
.call( this, e
);
1860 * Handle mouse up and key up events.
1863 * @param {jQuery.Event} e Mouse up or key up event
1865 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseKeyUp = function ( e
) {
1867 !this.isDisabled() &&
1868 ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
|| e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
1875 * Handle mouse down and key down events.
1878 * @param {jQuery.Event} e Mouse down or key down event
1880 OO
.ui
.PopupToolGroup
.prototype.onHandleMouseKeyDown = function ( e
) {
1882 !this.isDisabled() &&
1883 ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
|| e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
1885 this.setActive( !this.active
);
1891 * Switch into 'active' mode.
1893 * When active, the popup is visible. A mouseup event anywhere in the document will trigger
1896 OO
.ui
.PopupToolGroup
.prototype.setActive = function ( value
) {
1897 var containerWidth
, containerLeft
;
1899 if ( this.active
!== value
) {
1900 this.active
= value
;
1902 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler
, true );
1903 this.getElementDocument().addEventListener( 'keyup', this.onBlurHandler
, true );
1905 this.$clippable
.css( 'left', '' );
1906 // Try anchoring the popup to the left first
1907 this.$element
.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
1908 this.toggleClipping( true );
1909 if ( this.isClippedHorizontally() ) {
1910 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
1911 this.toggleClipping( false );
1913 .removeClass( 'oo-ui-popupToolGroup-left' )
1914 .addClass( 'oo-ui-popupToolGroup-right' );
1915 this.toggleClipping( true );
1917 if ( this.isClippedHorizontally() ) {
1918 // Anchoring to the right also caused the popup to clip, so just make it fill the container
1919 containerWidth
= this.$clippableScrollableContainer
.width();
1920 containerLeft
= this.$clippableScrollableContainer
.offset().left
;
1922 this.toggleClipping( false );
1923 this.$element
.removeClass( 'oo-ui-popupToolGroup-right' );
1925 this.$clippable
.css( {
1926 left
: -( this.$element
.offset().left
- containerLeft
),
1927 width
: containerWidth
1931 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler
, true );
1932 this.getElementDocument().removeEventListener( 'keyup', this.onBlurHandler
, true );
1933 this.$element
.removeClass(
1934 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
1936 this.toggleClipping( false );
1942 * ListToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
1943 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
1944 * and {@link OO.ui.BarToolGroup BarToolGroup}). The {@link OO.ui.Tool tools} in a ListToolGroup are displayed
1945 * by label in a dropdown menu. The title of the tool is used as the label text. The menu itself can be configured
1946 * with a label, icon, indicator, header, and title.
1948 * ListToolGroups can be configured to be expanded and collapsed. Collapsed lists will have a ‘More’ option that
1949 * users can select to see the full list of tools. If a collapsed toolgroup is expanded, a ‘Fewer’ option permits
1950 * users to collapse the list again.
1952 * ListToolGroups are created by a {@link OO.ui.ToolGroupFactory toolgroup factory} when the toolbar is set up. The factory
1953 * requires the ListToolGroup's symbolic name, 'list', which is specified along with the other configurations. For more
1954 * information about how to add tools to a ListToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
1957 * // Example of a ListToolGroup
1958 * var toolFactory = new OO.ui.ToolFactory();
1959 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
1960 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
1962 * // Configure and register two tools
1963 * function SettingsTool() {
1964 * SettingsTool.parent.apply( this, arguments );
1966 * OO.inheritClass( SettingsTool, OO.ui.Tool );
1967 * SettingsTool.static.name = 'settings';
1968 * SettingsTool.static.icon = 'settings';
1969 * SettingsTool.static.title = 'Change settings';
1970 * SettingsTool.prototype.onSelect = function () {
1971 * this.setActive( false );
1973 * SettingsTool.prototype.onUpdateState = function () {};
1974 * toolFactory.register( SettingsTool );
1975 * // Register two more tools, nothing interesting here
1976 * function StuffTool() {
1977 * StuffTool.parent.apply( this, arguments );
1979 * OO.inheritClass( StuffTool, OO.ui.Tool );
1980 * StuffTool.static.name = 'stuff';
1981 * StuffTool.static.icon = 'search';
1982 * StuffTool.static.title = 'Change the world';
1983 * StuffTool.prototype.onSelect = function () {
1984 * this.setActive( false );
1986 * StuffTool.prototype.onUpdateState = function () {};
1987 * toolFactory.register( StuffTool );
1990 * // Configurations for list toolgroup.
1992 * label: 'ListToolGroup',
1993 * indicator: 'down',
1995 * title: 'This is the title, displayed when user moves the mouse over the list toolgroup',
1996 * header: 'This is the header',
1997 * include: [ 'settings', 'stuff' ],
1998 * allowCollapse: ['stuff']
2002 * // Create some UI around the toolbar and place it in the document
2003 * var frame = new OO.ui.PanelLayout( {
2007 * frame.$element.append(
2010 * $( 'body' ).append( frame.$element );
2011 * // Build the toolbar. This must be done after the toolbar has been appended to the document.
2012 * toolbar.initialize();
2014 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
2016 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
2019 * @extends OO.ui.PopupToolGroup
2022 * @param {OO.ui.Toolbar} toolbar
2023 * @param {Object} [config] Configuration options
2024 * @cfg {Array} [allowCollapse] Allow the specified tools to be collapsed. By default, collapsible tools
2025 * will only be displayed if users click the ‘More’ option displayed at the bottom of the list. If
2026 * the list is expanded, a ‘Fewer’ option permits users to collapse the list again. Any tools that
2027 * are included in the toolgroup, but are not designated as collapsible, will always be displayed.
2028 * To open a collapsible list in its expanded state, set #expanded to 'true'.
2029 * @cfg {Array} [forceExpand] Expand the specified tools. All other tools will be designated as collapsible.
2030 * Unless #expanded is set to true, the collapsible tools will be collapsed when the list is first opened.
2031 * @cfg {boolean} [expanded=false] Expand collapsible tools. This config is only relevant if tools have
2032 * been designated as collapsible. When expanded is set to true, all tools in the group will be displayed
2033 * when the list is first opened. Users can collapse the list with a ‘Fewer’ option at the bottom.
2035 OO
.ui
.ListToolGroup
= function OoUiListToolGroup( toolbar
, config
) {
2036 // Allow passing positional parameters inside the config object
2037 if ( OO
.isPlainObject( toolbar
) && config
=== undefined ) {
2039 toolbar
= config
.toolbar
;
2042 // Configuration initialization
2043 config
= config
|| {};
2045 // Properties (must be set before parent constructor, which calls #populate)
2046 this.allowCollapse
= config
.allowCollapse
;
2047 this.forceExpand
= config
.forceExpand
;
2048 this.expanded
= config
.expanded
!== undefined ? config
.expanded
: false;
2049 this.collapsibleTools
= [];
2051 // Parent constructor
2052 OO
.ui
.ListToolGroup
.parent
.call( this, toolbar
, config
);
2055 this.$element
.addClass( 'oo-ui-listToolGroup' );
2060 OO
.inheritClass( OO
.ui
.ListToolGroup
, OO
.ui
.PopupToolGroup
);
2062 /* Static Properties */
2064 OO
.ui
.ListToolGroup
.static.name
= 'list';
2071 OO
.ui
.ListToolGroup
.prototype.populate = function () {
2072 var i
, len
, allowCollapse
= [];
2074 OO
.ui
.ListToolGroup
.parent
.prototype.populate
.call( this );
2076 // Update the list of collapsible tools
2077 if ( this.allowCollapse
!== undefined ) {
2078 allowCollapse
= this.allowCollapse
;
2079 } else if ( this.forceExpand
!== undefined ) {
2080 allowCollapse
= OO
.simpleArrayDifference( Object
.keys( this.tools
), this.forceExpand
);
2083 this.collapsibleTools
= [];
2084 for ( i
= 0, len
= allowCollapse
.length
; i
< len
; i
++ ) {
2085 if ( this.tools
[ allowCollapse
[ i
] ] !== undefined ) {
2086 this.collapsibleTools
.push( this.tools
[ allowCollapse
[ i
] ] );
2090 // Keep at the end, even when tools are added
2091 this.$group
.append( this.getExpandCollapseTool().$element
);
2093 this.getExpandCollapseTool().toggle( this.collapsibleTools
.length
!== 0 );
2094 this.updateCollapsibleState();
2098 * Get the expand/collapse tool for this group
2100 * @return {OO.ui.Tool} Expand collapse tool
2102 OO
.ui
.ListToolGroup
.prototype.getExpandCollapseTool = function () {
2103 var ExpandCollapseTool
;
2104 if ( this.expandCollapseTool
=== undefined ) {
2105 ExpandCollapseTool = function () {
2106 ExpandCollapseTool
.parent
.apply( this, arguments
);
2109 OO
.inheritClass( ExpandCollapseTool
, OO
.ui
.Tool
);
2111 ExpandCollapseTool
.prototype.onSelect = function () {
2112 this.toolGroup
.expanded
= !this.toolGroup
.expanded
;
2113 this.toolGroup
.updateCollapsibleState();
2114 this.setActive( false );
2116 ExpandCollapseTool
.prototype.onUpdateState = function () {
2117 // Do nothing. Tool interface requires an implementation of this function.
2120 ExpandCollapseTool
.static.name
= 'more-fewer';
2122 this.expandCollapseTool
= new ExpandCollapseTool( this );
2124 return this.expandCollapseTool
;
2130 OO
.ui
.ListToolGroup
.prototype.onMouseKeyUp = function ( e
) {
2131 // Do not close the popup when the user wants to show more/fewer tools
2133 $( e
.target
).closest( '.oo-ui-tool-name-more-fewer' ).length
&&
2134 ( e
.which
=== OO
.ui
.MouseButtons
.LEFT
|| e
.which
=== OO
.ui
.Keys
.SPACE
|| e
.which
=== OO
.ui
.Keys
.ENTER
)
2136 // HACK: Prevent the popup list from being hidden. Skip the PopupToolGroup implementation (which
2137 // hides the popup list when a tool is selected) and call ToolGroup's implementation directly.
2138 return OO
.ui
.ListToolGroup
.parent
.parent
.prototype.onMouseKeyUp
.call( this, e
);
2140 return OO
.ui
.ListToolGroup
.parent
.prototype.onMouseKeyUp
.call( this, e
);
2144 OO
.ui
.ListToolGroup
.prototype.updateCollapsibleState = function () {
2147 this.getExpandCollapseTool()
2148 .setIcon( this.expanded
? 'collapse' : 'expand' )
2149 .setTitle( OO
.ui
.msg( this.expanded
? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
2151 for ( i
= 0, len
= this.collapsibleTools
.length
; i
< len
; i
++ ) {
2152 this.collapsibleTools
[ i
].toggle( this.expanded
);
2157 * MenuToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
2158 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.BarToolGroup BarToolGroup}
2159 * and {@link OO.ui.ListToolGroup ListToolGroup}). MenuToolGroups contain selectable {@link OO.ui.Tool tools},
2160 * which are displayed by label in a dropdown menu. The tool's title is used as the label text, and the
2161 * menu label is updated to reflect which tool or tools are currently selected. If no tools are selected,
2162 * the menu label is empty. The menu can be configured with an indicator, icon, title, and/or header.
2164 * MenuToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar
2168 * // Example of a MenuToolGroup
2169 * var toolFactory = new OO.ui.ToolFactory();
2170 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
2171 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
2173 * // We will be placing status text in this element when tools are used
2174 * var $area = $( '<p>' ).text( 'An example of a MenuToolGroup. Select a tool from the dropdown menu.' );
2176 * // Define the tools that we're going to place in our toolbar
2178 * function SettingsTool() {
2179 * SettingsTool.parent.apply( this, arguments );
2180 * this.reallyActive = false;
2182 * OO.inheritClass( SettingsTool, OO.ui.Tool );
2183 * SettingsTool.static.name = 'settings';
2184 * SettingsTool.static.icon = 'settings';
2185 * SettingsTool.static.title = 'Change settings';
2186 * SettingsTool.prototype.onSelect = function () {
2187 * $area.text( 'Settings tool clicked!' );
2188 * // Toggle the active state on each click
2189 * this.reallyActive = !this.reallyActive;
2190 * this.setActive( this.reallyActive );
2191 * // To update the menu label
2192 * this.toolbar.emit( 'updateState' );
2194 * SettingsTool.prototype.onUpdateState = function () {};
2195 * toolFactory.register( SettingsTool );
2197 * function StuffTool() {
2198 * StuffTool.parent.apply( this, arguments );
2199 * this.reallyActive = false;
2201 * OO.inheritClass( StuffTool, OO.ui.Tool );
2202 * StuffTool.static.name = 'stuff';
2203 * StuffTool.static.icon = 'ellipsis';
2204 * StuffTool.static.title = 'More stuff';
2205 * StuffTool.prototype.onSelect = function () {
2206 * $area.text( 'More stuff tool clicked!' );
2207 * // Toggle the active state on each click
2208 * this.reallyActive = !this.reallyActive;
2209 * this.setActive( this.reallyActive );
2210 * // To update the menu label
2211 * this.toolbar.emit( 'updateState' );
2213 * StuffTool.prototype.onUpdateState = function () {};
2214 * toolFactory.register( StuffTool );
2216 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
2217 * // used once (but not all defined tools must be used).
2221 * header: 'This is the (optional) header',
2222 * title: 'This is the (optional) title',
2223 * indicator: 'down',
2224 * include: [ 'settings', 'stuff' ]
2228 * // Create some UI around the toolbar and place it in the document
2229 * var frame = new OO.ui.PanelLayout( {
2233 * var contentFrame = new OO.ui.PanelLayout( {
2237 * frame.$element.append(
2239 * contentFrame.$element.append( $area )
2241 * $( 'body' ).append( frame.$element );
2243 * // Here is where the toolbar is actually built. This must be done after inserting it into the
2245 * toolbar.initialize();
2246 * toolbar.emit( 'updateState' );
2248 * For more information about how to add tools to a MenuToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
2249 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki] [1].
2251 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
2254 * @extends OO.ui.PopupToolGroup
2257 * @param {OO.ui.Toolbar} toolbar
2258 * @param {Object} [config] Configuration options
2260 OO
.ui
.MenuToolGroup
= function OoUiMenuToolGroup( toolbar
, config
) {
2261 // Allow passing positional parameters inside the config object
2262 if ( OO
.isPlainObject( toolbar
) && config
=== undefined ) {
2264 toolbar
= config
.toolbar
;
2267 // Configuration initialization
2268 config
= config
|| {};
2270 // Parent constructor
2271 OO
.ui
.MenuToolGroup
.parent
.call( this, toolbar
, config
);
2274 this.toolbar
.connect( this, { updateState
: 'onUpdateState' } );
2277 this.$element
.addClass( 'oo-ui-menuToolGroup' );
2282 OO
.inheritClass( OO
.ui
.MenuToolGroup
, OO
.ui
.PopupToolGroup
);
2284 /* Static Properties */
2286 OO
.ui
.MenuToolGroup
.static.name
= 'menu';
2291 * Handle the toolbar state being updated.
2293 * When the state changes, the title of each active item in the menu will be joined together and
2294 * used as a label for the group. The label will be empty if none of the items are active.
2298 OO
.ui
.MenuToolGroup
.prototype.onUpdateState = function () {
2302 for ( name
in this.tools
) {
2303 if ( this.tools
[ name
].isActive() ) {
2304 labelTexts
.push( this.tools
[ name
].getTitle() );
2308 this.setLabel( labelTexts
.join( ', ' ) || ' ' );