Update OOUI to v0.26.0
[lhc/web/wiklou.git] / resources / lib / oojs-ui / oojs-ui-toolbars.js
1 /*!
2 * OOUI v0.26.0
3 * https://www.mediawiki.org/wiki/OOUI
4 *
5 * Copyright 2011–2018 OOUI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
8 *
9 * Date: 2018-03-21T00:00:53Z
10 */
11 ( function ( OO ) {
12
13 'use strict';
14
15 /**
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.
19 *
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.
23 *
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.
28 *
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}.
35 *
36 * The following is an example of a basic toolbar.
37 *
38 * @example
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 );
44 *
45 * // We will be placing status text in this element when tools are used
46 * var $area = $( '<p>' ).text( 'Toolbar example' );
47 *
48 * // Define the tools that we're going to place in our toolbar
49 *
50 * // Create a class inheriting from OO.ui.Tool
51 * function SearchTool() {
52 * SearchTool.parent.apply( this, arguments );
53 * }
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 );
65 * };
66 * SearchTool.prototype.onUpdateState = function () {};
67 * // Make this tool available in our toolFactory and thus our toolbar
68 * toolFactory.register( SearchTool );
69 *
70 * // Register two more tools, nothing interesting here
71 * function SettingsTool() {
72 * SettingsTool.parent.apply( this, arguments );
73 * }
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 );
81 * };
82 * SettingsTool.prototype.onUpdateState = function () {};
83 * toolFactory.register( SettingsTool );
84 *
85 * // Register two more tools, nothing interesting here
86 * function StuffTool() {
87 * StuffTool.parent.apply( this, arguments );
88 * }
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 );
96 * };
97 * StuffTool.prototype.onUpdateState = function () {};
98 * toolFactory.register( StuffTool );
99 *
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: {
104 * padded: true,
105 * label: 'Help',
106 * head: true
107 * } }, config ) );
108 * this.popup.$body.append( '<p>I am helpful!</p>' );
109 * }
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 );
115 *
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).
118 * toolbar.setup( [
119 * {
120 * // 'bar' tool groups display tools' icons only, side-by-side.
121 * type: 'bar',
122 * include: [ 'search', 'help' ]
123 * },
124 * {
125 * // 'list' tool groups display both the titles and icons, in a dropdown list.
126 * type: 'list',
127 * indicator: 'down',
128 * label: 'More',
129 * include: [ 'settings', 'stuff' ]
130 * }
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.)
134 * ] );
135 *
136 * // Create some UI around the toolbar and place it in the document
137 * var frame = new OO.ui.PanelLayout( {
138 * expanded: false,
139 * framed: true
140 * } );
141 * var contentFrame = new OO.ui.PanelLayout( {
142 * expanded: false,
143 * padded: true
144 * } );
145 * frame.$element.append(
146 * toolbar.$element,
147 * contentFrame.$element.append( $area )
148 * );
149 * $( 'body' ).append( frame.$element );
150 *
151 * // Here is where the toolbar is actually built. This must be done after inserting it into the
152 * // document.
153 * toolbar.initialize();
154 * toolbar.emit( 'updateState' );
155 *
156 * The following example extends the previous one to illustrate 'menu' toolgroups and the usage of
157 * {@link #event-updateState 'updateState' event}.
158 *
159 * @example
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 );
164 *
165 * // We will be placing status text in this element when tools are used
166 * var $area = $( '<p>' ).text( 'Toolbar example' );
167 *
168 * // Define the tools that we're going to place in our toolbar
169 *
170 * // Create a class inheriting from OO.ui.Tool
171 * function SearchTool() {
172 * SearchTool.parent.apply( this, arguments );
173 * }
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 );
185 * };
186 * SearchTool.prototype.onUpdateState = function () {};
187 * // Make this tool available in our toolFactory and thus our toolbar
188 * toolFactory.register( SearchTool );
189 *
190 * // Register two more tools, nothing interesting here
191 * function SettingsTool() {
192 * SettingsTool.parent.apply( this, arguments );
193 * this.reallyActive = false;
194 * }
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' );
206 * };
207 * SettingsTool.prototype.onUpdateState = function () {};
208 * toolFactory.register( SettingsTool );
209 *
210 * // Register two more tools, nothing interesting here
211 * function StuffTool() {
212 * StuffTool.parent.apply( this, arguments );
213 * this.reallyActive = false;
214 * }
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' );
226 * };
227 * StuffTool.prototype.onUpdateState = function () {};
228 * toolFactory.register( StuffTool );
229 *
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: {
234 * padded: true,
235 * label: 'Help',
236 * head: true
237 * } }, config ) );
238 * this.popup.$body.append( '<p>I am helpful!</p>' );
239 * }
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 );
245 *
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).
248 * toolbar.setup( [
249 * {
250 * // 'bar' tool groups display tools' icons only, side-by-side.
251 * type: 'bar',
252 * include: [ 'search', 'help' ]
253 * },
254 * {
255 * // 'menu' tool groups display both the titles and icons, in a dropdown menu.
256 * // Menu label indicates which items are selected.
257 * type: 'menu',
258 * indicator: 'down',
259 * include: [ 'settings', 'stuff' ]
260 * }
261 * ] );
262 *
263 * // Create some UI around the toolbar and place it in the document
264 * var frame = new OO.ui.PanelLayout( {
265 * expanded: false,
266 * framed: true
267 * } );
268 * var contentFrame = new OO.ui.PanelLayout( {
269 * expanded: false,
270 * padded: true
271 * } );
272 * frame.$element.append(
273 * toolbar.$element,
274 * contentFrame.$element.append( $area )
275 * );
276 * $( 'body' ).append( frame.$element );
277 *
278 * // Here is where the toolbar is actually built. This must be done after inserting it into the
279 * // document.
280 * toolbar.initialize();
281 * toolbar.emit( 'updateState' );
282 *
283 * @class
284 * @extends OO.ui.Element
285 * @mixins OO.EventEmitter
286 * @mixins OO.ui.mixin.GroupElement
287 *
288 * @constructor
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
294 * the toolbar.
295 * @cfg {string} [position='top'] Whether the toolbar is positioned above ('top') or below ('bottom') content.
296 */
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;
303 }
304
305 // Configuration initialization
306 config = config || {};
307
308 // Parent constructor
309 OO.ui.Toolbar.parent.call( this, config );
310
311 // Mixin constructors
312 OO.EventEmitter.call( this );
313 OO.ui.mixin.GroupElement.call( this, config );
314
315 // Properties
316 this.toolFactory = toolFactory;
317 this.toolGroupFactory = toolGroupFactory;
318 this.groups = [];
319 this.tools = {};
320 this.position = config.position || 'top';
321 this.$bar = $( '<div>' );
322 this.$actions = $( '<div>' );
323 this.initialized = false;
324 this.narrowThreshold = null;
325 this.onWindowResizeHandler = this.onWindowResize.bind( this );
326
327 // Events
328 this.$element
329 .add( this.$bar ).add( this.$group ).add( this.$actions )
330 .on( 'mousedown keydown', this.onPointerDown.bind( this ) );
331
332 // Initialization
333 this.$group.addClass( 'oo-ui-toolbar-tools' );
334 if ( config.actions ) {
335 this.$bar.append( this.$actions.addClass( 'oo-ui-toolbar-actions' ) );
336 }
337 this.$bar
338 .addClass( 'oo-ui-toolbar-bar' )
339 .append( this.$group, '<div style="clear:both"></div>' );
340 // Possible classes: oo-ui-toolbar-position-top, oo-ui-toolbar-position-bottom
341 this.$element.addClass( 'oo-ui-toolbar oo-ui-toolbar-position-' + this.position ).append( this.$bar );
342 };
343
344 /* Setup */
345
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 );
349
350 /* Events */
351
352 /**
353 * @event updateState
354 *
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
357 * tools is required.
358 *
359 * @param {...Mixed} data Application-defined parameters
360 */
361
362 /* Methods */
363
364 /**
365 * Get the tool factory.
366 *
367 * @return {OO.ui.ToolFactory} Tool factory
368 */
369 OO.ui.Toolbar.prototype.getToolFactory = function () {
370 return this.toolFactory;
371 };
372
373 /**
374 * Get the toolgroup factory.
375 *
376 * @return {OO.Factory} Toolgroup factory
377 */
378 OO.ui.Toolbar.prototype.getToolGroupFactory = function () {
379 return this.toolGroupFactory;
380 };
381
382 /**
383 * Handles mouse down events.
384 *
385 * @private
386 * @param {jQuery.Event} e Mouse down event
387 */
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 ] ) {
392 return false;
393 }
394 };
395
396 /**
397 * Handle window resize event.
398 *
399 * @private
400 * @param {jQuery.Event} e Window resize event
401 */
402 OO.ui.Toolbar.prototype.onWindowResize = function () {
403 this.$element.toggleClass(
404 'oo-ui-toolbar-narrow',
405 this.$bar[ 0 ].clientWidth <= this.getNarrowThreshold()
406 );
407 };
408
409 /**
410 * Get the (lazily-computed) width threshold for applying the oo-ui-toolbar-narrow
411 * class.
412 *
413 * @private
414 * @return {number} Width threshold in pixels
415 */
416 OO.ui.Toolbar.prototype.getNarrowThreshold = function () {
417 if ( this.narrowThreshold === null ) {
418 this.narrowThreshold = this.$group[ 0 ].offsetWidth + this.$actions[ 0 ].offsetWidth;
419 }
420 return this.narrowThreshold;
421 };
422
423 /**
424 * Sets up handles and preloads required information for the toolbar to work.
425 * This must be called after it is attached to a visible document and before doing anything else.
426 */
427 OO.ui.Toolbar.prototype.initialize = function () {
428 if ( !this.initialized ) {
429 this.initialized = true;
430 $( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler );
431 this.onWindowResize();
432 }
433 };
434
435 /**
436 * Set up the toolbar.
437 *
438 * The toolbar is set up with a list of toolgroup configurations that specify the type of
439 * toolgroup ({@link OO.ui.BarToolGroup bar}, {@link OO.ui.MenuToolGroup menu}, or {@link OO.ui.ListToolGroup list})
440 * to add and which tools to include, exclude, promote, or demote within that toolgroup. Please
441 * see {@link OO.ui.ToolGroup toolgroups} for more information about including tools in toolgroups.
442 *
443 * @param {Object.<string,Array>} groups List of toolgroup configurations
444 * @param {Array|string} [groups.include] Tools to include in the toolgroup
445 * @param {Array|string} [groups.exclude] Tools to exclude from the toolgroup
446 * @param {Array|string} [groups.promote] Tools to promote to the beginning of the toolgroup
447 * @param {Array|string} [groups.demote] Tools to demote to the end of the toolgroup
448 */
449 OO.ui.Toolbar.prototype.setup = function ( groups ) {
450 var i, len, type, group,
451 items = [],
452 defaultType = 'bar';
453
454 // Cleanup previous groups
455 this.reset();
456
457 // Build out new groups
458 for ( i = 0, len = groups.length; i < len; i++ ) {
459 group = groups[ i ];
460 if ( group.include === '*' ) {
461 // Apply defaults to catch-all groups
462 if ( group.type === undefined ) {
463 group.type = 'list';
464 }
465 if ( group.label === undefined ) {
466 group.label = OO.ui.msg( 'ooui-toolbar-more' );
467 }
468 }
469 // Check type has been registered
470 type = this.getToolGroupFactory().lookup( group.type ) ? group.type : defaultType;
471 items.push(
472 this.getToolGroupFactory().create( type, this, group )
473 );
474 }
475 this.addItems( items );
476 };
477
478 /**
479 * Remove all tools and toolgroups from the toolbar.
480 */
481 OO.ui.Toolbar.prototype.reset = function () {
482 var i, len;
483
484 this.groups = [];
485 this.tools = {};
486 for ( i = 0, len = this.items.length; i < len; i++ ) {
487 this.items[ i ].destroy();
488 }
489 this.clearItems();
490 };
491
492 /**
493 * Destroy the toolbar.
494 *
495 * Destroying the toolbar removes all event handlers and DOM elements that constitute the toolbar. Call
496 * this method whenever you are done using a toolbar.
497 */
498 OO.ui.Toolbar.prototype.destroy = function () {
499 $( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler );
500 this.reset();
501 this.$element.remove();
502 };
503
504 /**
505 * Check if the tool is available.
506 *
507 * Available tools are ones that have not yet been added to the toolbar.
508 *
509 * @param {string} name Symbolic name of tool
510 * @return {boolean} Tool is available
511 */
512 OO.ui.Toolbar.prototype.isToolAvailable = function ( name ) {
513 return !this.tools[ name ];
514 };
515
516 /**
517 * Prevent tool from being used again.
518 *
519 * @param {OO.ui.Tool} tool Tool to reserve
520 */
521 OO.ui.Toolbar.prototype.reserveTool = function ( tool ) {
522 this.tools[ tool.getName() ] = tool;
523 };
524
525 /**
526 * Allow tool to be used again.
527 *
528 * @param {OO.ui.Tool} tool Tool to release
529 */
530 OO.ui.Toolbar.prototype.releaseTool = function ( tool ) {
531 delete this.tools[ tool.getName() ];
532 };
533
534 /**
535 * Get accelerator label for tool.
536 *
537 * The OOUI library does not contain an accelerator system, but this is the hook for one. To
538 * use an accelerator system, subclass the toolbar and override this method, which is meant to return a label
539 * that describes the accelerator keys for the tool passed (by symbolic name) to the method.
540 *
541 * @param {string} name Symbolic name of tool
542 * @return {string|undefined} Tool accelerator label if available
543 */
544 OO.ui.Toolbar.prototype.getToolAccelerator = function () {
545 return undefined;
546 };
547
548 /**
549 * Tools, together with {@link OO.ui.ToolGroup toolgroups}, constitute {@link OO.ui.Toolbar toolbars}.
550 * Each tool is configured with a static name, title, and icon and is customized with the command to carry
551 * out when the tool is selected. Tools must also be registered with a {@link OO.ui.ToolFactory tool factory},
552 * which creates the tools on demand.
553 *
554 * Every Tool subclass must implement two methods:
555 *
556 * - {@link #onUpdateState}
557 * - {@link #onSelect}
558 *
559 * Tools are added to toolgroups ({@link OO.ui.ListToolGroup ListToolGroup},
560 * {@link OO.ui.BarToolGroup BarToolGroup}, or {@link OO.ui.MenuToolGroup MenuToolGroup}), which determine how
561 * the tool is displayed in the toolbar. See {@link OO.ui.Toolbar toolbars} for an example.
562 *
563 * For more information, please see the [OOUI documentation on MediaWiki][1].
564 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
565 *
566 * @abstract
567 * @class
568 * @extends OO.ui.Widget
569 * @mixins OO.ui.mixin.IconElement
570 * @mixins OO.ui.mixin.FlaggedElement
571 * @mixins OO.ui.mixin.TabIndexedElement
572 *
573 * @constructor
574 * @param {OO.ui.ToolGroup} toolGroup
575 * @param {Object} [config] Configuration options
576 * @cfg {string|Function} [title] Title text or a function that returns text. If this config is omitted, the value of
577 * the {@link #static-title static title} property is used.
578 *
579 * The title is used in different ways depending on the type of toolgroup that contains the tool. The
580 * 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
581 * part of a {@link OO.ui.ListToolGroup list} or {@link OO.ui.MenuToolGroup menu} toolgroup.
582 *
583 * For bar toolgroups, a description of the accelerator key is appended to the title if an accelerator key
584 * is associated with an action by the same name as the tool and accelerator functionality has been added to the application.
585 * To add accelerator key functionality, you must subclass OO.ui.Toolbar and override the {@link OO.ui.Toolbar#getToolAccelerator getToolAccelerator} method.
586 */
587 OO.ui.Tool = function OoUiTool( toolGroup, config ) {
588 // Allow passing positional parameters inside the config object
589 if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
590 config = toolGroup;
591 toolGroup = config.toolGroup;
592 }
593
594 // Configuration initialization
595 config = config || {};
596
597 // Parent constructor
598 OO.ui.Tool.parent.call( this, config );
599
600 // Properties
601 this.toolGroup = toolGroup;
602 this.toolbar = this.toolGroup.getToolbar();
603 this.active = false;
604 this.$title = $( '<span>' );
605 this.$accel = $( '<span>' );
606 this.$link = $( '<a>' );
607 this.title = null;
608
609 // Mixin constructors
610 OO.ui.mixin.IconElement.call( this, config );
611 OO.ui.mixin.FlaggedElement.call( this, config );
612 OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$link } ) );
613
614 // Events
615 this.toolbar.connect( this, { updateState: 'onUpdateState' } );
616
617 // Initialization
618 this.$title.addClass( 'oo-ui-tool-title' );
619 this.$accel
620 .addClass( 'oo-ui-tool-accel' )
621 .prop( {
622 // This may need to be changed if the key names are ever localized,
623 // but for now they are essentially written in English
624 dir: 'ltr',
625 lang: 'en'
626 } );
627 this.$link
628 .addClass( 'oo-ui-tool-link' )
629 .append( this.$icon, this.$title, this.$accel )
630 .attr( 'role', 'button' );
631 this.$element
632 .data( 'oo-ui-tool', this )
633 .addClass( 'oo-ui-tool' )
634 .addClass( 'oo-ui-tool-name-' + this.constructor.static.name.replace( /^([^/]+)\/([^/]+).*$/, '$1-$2' ) )
635 .toggleClass( 'oo-ui-tool-with-label', this.constructor.static.displayBothIconAndLabel )
636 .append( this.$link );
637 this.setTitle( config.title || this.constructor.static.title );
638 };
639
640 /* Setup */
641
642 OO.inheritClass( OO.ui.Tool, OO.ui.Widget );
643 OO.mixinClass( OO.ui.Tool, OO.ui.mixin.IconElement );
644 OO.mixinClass( OO.ui.Tool, OO.ui.mixin.FlaggedElement );
645 OO.mixinClass( OO.ui.Tool, OO.ui.mixin.TabIndexedElement );
646
647 /* Static Properties */
648
649 /**
650 * @static
651 * @inheritdoc
652 */
653 OO.ui.Tool.static.tagName = 'span';
654
655 /**
656 * Symbolic name of tool.
657 *
658 * The symbolic name is used internally to register the tool with a {@link OO.ui.ToolFactory ToolFactory}. It can
659 * also be used when adding tools to toolgroups.
660 *
661 * @abstract
662 * @static
663 * @inheritable
664 * @property {string}
665 */
666 OO.ui.Tool.static.name = '';
667
668 /**
669 * Symbolic name of the group.
670 *
671 * The group name is used to associate tools with each other so that they can be selected later by
672 * a {@link OO.ui.ToolGroup toolgroup}.
673 *
674 * @abstract
675 * @static
676 * @inheritable
677 * @property {string}
678 */
679 OO.ui.Tool.static.group = '';
680
681 /**
682 * 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.
683 *
684 * @abstract
685 * @static
686 * @inheritable
687 * @property {string|Function}
688 */
689 OO.ui.Tool.static.title = '';
690
691 /**
692 * Display both icon and label when the tool is used in a {@link OO.ui.BarToolGroup bar} toolgroup.
693 * Normally only the icon is displayed, or only the label if no icon is given.
694 *
695 * @static
696 * @inheritable
697 * @property {boolean}
698 */
699 OO.ui.Tool.static.displayBothIconAndLabel = false;
700
701 /**
702 * Add tool to catch-all groups automatically.
703 *
704 * A catch-all group, which contains all tools that do not currently belong to a toolgroup,
705 * can be included in a toolgroup using the wildcard selector, an asterisk (*).
706 *
707 * @static
708 * @inheritable
709 * @property {boolean}
710 */
711 OO.ui.Tool.static.autoAddToCatchall = true;
712
713 /**
714 * Add tool to named groups automatically.
715 *
716 * By default, tools that are configured with a static ‘group’ property are added
717 * to that group and will be selected when the symbolic name of the group is specified (e.g., when
718 * toolgroups include tools by group name).
719 *
720 * @static
721 * @property {boolean}
722 * @inheritable
723 */
724 OO.ui.Tool.static.autoAddToGroup = true;
725
726 /**
727 * Check if this tool is compatible with given data.
728 *
729 * This is a stub that can be overridden to provide support for filtering tools based on an
730 * arbitrary piece of information (e.g., where the cursor is in a document). The implementation
731 * must also call this method so that the compatibility check can be performed.
732 *
733 * @static
734 * @inheritable
735 * @param {Mixed} data Data to check
736 * @return {boolean} Tool can be used with data
737 */
738 OO.ui.Tool.static.isCompatibleWith = function () {
739 return false;
740 };
741
742 /* Methods */
743
744 /**
745 * Handle the toolbar state being updated. This method is called when the
746 * {@link OO.ui.Toolbar#event-updateState 'updateState' event} is emitted on the
747 * {@link OO.ui.Toolbar Toolbar} that uses this tool, and should set the state of this tool
748 * depending on application state (usually by calling #setDisabled to enable or disable the tool,
749 * or #setActive to mark is as currently in-use or not).
750 *
751 * This is an abstract method that must be overridden in a concrete subclass.
752 *
753 * @method
754 * @protected
755 * @abstract
756 */
757 OO.ui.Tool.prototype.onUpdateState = null;
758
759 /**
760 * Handle the tool being selected. This method is called when the user triggers this tool,
761 * usually by clicking on its label/icon.
762 *
763 * This is an abstract method that must be overridden in a concrete subclass.
764 *
765 * @method
766 * @protected
767 * @abstract
768 */
769 OO.ui.Tool.prototype.onSelect = null;
770
771 /**
772 * Check if the tool is active.
773 *
774 * Tools become active when their #onSelect or #onUpdateState handlers change them to appear pressed
775 * with the #setActive method. Additional CSS is applied to the tool to reflect the active state.
776 *
777 * @return {boolean} Tool is active
778 */
779 OO.ui.Tool.prototype.isActive = function () {
780 return this.active;
781 };
782
783 /**
784 * Make the tool appear active or inactive.
785 *
786 * This method should be called within #onSelect or #onUpdateState event handlers to make the tool
787 * appear pressed or not.
788 *
789 * @param {boolean} state Make tool appear active
790 */
791 OO.ui.Tool.prototype.setActive = function ( state ) {
792 this.active = !!state;
793 this.$element.toggleClass( 'oo-ui-tool-active', this.active );
794 this.updateThemeClasses();
795 };
796
797 /**
798 * Set the tool #title.
799 *
800 * @param {string|Function} title Title text or a function that returns text
801 * @chainable
802 */
803 OO.ui.Tool.prototype.setTitle = function ( title ) {
804 this.title = OO.ui.resolveMsg( title );
805 this.updateTitle();
806 return this;
807 };
808
809 /**
810 * Get the tool #title.
811 *
812 * @return {string} Title text
813 */
814 OO.ui.Tool.prototype.getTitle = function () {
815 return this.title;
816 };
817
818 /**
819 * Get the tool's symbolic name.
820 *
821 * @return {string} Symbolic name of tool
822 */
823 OO.ui.Tool.prototype.getName = function () {
824 return this.constructor.static.name;
825 };
826
827 /**
828 * Update the title.
829 */
830 OO.ui.Tool.prototype.updateTitle = function () {
831 var titleTooltips = this.toolGroup.constructor.static.titleTooltips,
832 accelTooltips = this.toolGroup.constructor.static.accelTooltips,
833 accel = this.toolbar.getToolAccelerator( this.constructor.static.name ),
834 tooltipParts = [];
835
836 this.$title.text( this.title );
837 this.$accel.text( accel );
838
839 if ( titleTooltips && typeof this.title === 'string' && this.title.length ) {
840 tooltipParts.push( this.title );
841 }
842 if ( accelTooltips && typeof accel === 'string' && accel.length ) {
843 tooltipParts.push( accel );
844 }
845 if ( tooltipParts.length ) {
846 this.$link.attr( 'title', tooltipParts.join( ' ' ) );
847 } else {
848 this.$link.removeAttr( 'title' );
849 }
850 };
851
852 /**
853 * Destroy tool.
854 *
855 * Destroying the tool removes all event handlers and the tool’s DOM elements.
856 * Call this method whenever you are done using a tool.
857 */
858 OO.ui.Tool.prototype.destroy = function () {
859 this.toolbar.disconnect( this );
860 this.$element.remove();
861 };
862
863 /**
864 * ToolGroups are collections of {@link OO.ui.Tool tools} that are used in a {@link OO.ui.Toolbar toolbar}.
865 * The type of toolgroup ({@link OO.ui.ListToolGroup list}, {@link OO.ui.BarToolGroup bar}, or {@link OO.ui.MenuToolGroup menu})
866 * to which a tool belongs determines how the tool is arranged and displayed in the toolbar. Toolgroups
867 * themselves are created on demand with a {@link OO.ui.ToolGroupFactory toolgroup factory}.
868 *
869 * Toolgroups can contain individual tools, groups of tools, or all available tools, as specified
870 * using the `include` config option. See OO.ui.ToolFactory#extract on documentation of the format.
871 * The options `exclude`, `promote`, and `demote` support the same formats.
872 *
873 * See {@link OO.ui.Toolbar toolbars} for a full example. For more information about toolbars in general,
874 * please see the [OOUI documentation on MediaWiki][1].
875 *
876 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
877 *
878 * @abstract
879 * @class
880 * @extends OO.ui.Widget
881 * @mixins OO.ui.mixin.GroupElement
882 *
883 * @constructor
884 * @param {OO.ui.Toolbar} toolbar
885 * @param {Object} [config] Configuration options
886 * @cfg {Array|string} [include] List of tools to include in the toolgroup, see above.
887 * @cfg {Array|string} [exclude] List of tools to exclude from the toolgroup, see above.
888 * @cfg {Array|string} [promote] List of tools to promote to the beginning of the toolgroup, see above.
889 * @cfg {Array|string} [demote] List of tools to demote to the end of the toolgroup, see above.
890 * This setting is particularly useful when tools have been added to the toolgroup
891 * en masse (e.g., via the catch-all selector).
892 */
893 OO.ui.ToolGroup = function OoUiToolGroup( toolbar, config ) {
894 // Allow passing positional parameters inside the config object
895 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
896 config = toolbar;
897 toolbar = config.toolbar;
898 }
899
900 // Configuration initialization
901 config = config || {};
902
903 // Parent constructor
904 OO.ui.ToolGroup.parent.call( this, config );
905
906 // Mixin constructors
907 OO.ui.mixin.GroupElement.call( this, config );
908
909 // Properties
910 this.toolbar = toolbar;
911 this.tools = {};
912 this.pressed = null;
913 this.autoDisabled = false;
914 this.include = config.include || [];
915 this.exclude = config.exclude || [];
916 this.promote = config.promote || [];
917 this.demote = config.demote || [];
918 this.onCapturedMouseKeyUpHandler = this.onCapturedMouseKeyUp.bind( this );
919
920 // Events
921 this.$element.on( {
922 mousedown: this.onMouseKeyDown.bind( this ),
923 mouseup: this.onMouseKeyUp.bind( this ),
924 keydown: this.onMouseKeyDown.bind( this ),
925 keyup: this.onMouseKeyUp.bind( this ),
926 focus: this.onMouseOverFocus.bind( this ),
927 blur: this.onMouseOutBlur.bind( this ),
928 mouseover: this.onMouseOverFocus.bind( this ),
929 mouseout: this.onMouseOutBlur.bind( this )
930 } );
931 this.toolbar.getToolFactory().connect( this, { register: 'onToolFactoryRegister' } );
932 this.aggregate( { disable: 'itemDisable' } );
933 this.connect( this, { itemDisable: 'updateDisabled' } );
934
935 // Initialization
936 this.$group.addClass( 'oo-ui-toolGroup-tools' );
937 this.$element
938 .addClass( 'oo-ui-toolGroup' )
939 .append( this.$group );
940 this.populate();
941 };
942
943 /* Setup */
944
945 OO.inheritClass( OO.ui.ToolGroup, OO.ui.Widget );
946 OO.mixinClass( OO.ui.ToolGroup, OO.ui.mixin.GroupElement );
947
948 /* Events */
949
950 /**
951 * @event update
952 */
953
954 /* Static Properties */
955
956 /**
957 * Show labels in tooltips.
958 *
959 * @static
960 * @inheritable
961 * @property {boolean}
962 */
963 OO.ui.ToolGroup.static.titleTooltips = false;
964
965 /**
966 * Show acceleration labels in tooltips.
967 *
968 * Note: The OOUI library does not include an accelerator system, but does contain
969 * a hook for one. To use an accelerator system, subclass the {@link OO.ui.Toolbar toolbar} and
970 * override the {@link OO.ui.Toolbar#getToolAccelerator getToolAccelerator} method, which is
971 * meant to return a label that describes the accelerator keys for a given tool (e.g., 'Ctrl + M').
972 *
973 * @static
974 * @inheritable
975 * @property {boolean}
976 */
977 OO.ui.ToolGroup.static.accelTooltips = false;
978
979 /**
980 * Automatically disable the toolgroup when all tools are disabled
981 *
982 * @static
983 * @inheritable
984 * @property {boolean}
985 */
986 OO.ui.ToolGroup.static.autoDisable = true;
987
988 /**
989 * @abstract
990 * @static
991 * @inheritable
992 * @property {string}
993 */
994 OO.ui.ToolGroup.static.name = null;
995
996 /* Methods */
997
998 /**
999 * @inheritdoc
1000 */
1001 OO.ui.ToolGroup.prototype.isDisabled = function () {
1002 return this.autoDisabled || OO.ui.ToolGroup.parent.prototype.isDisabled.apply( this, arguments );
1003 };
1004
1005 /**
1006 * @inheritdoc
1007 */
1008 OO.ui.ToolGroup.prototype.updateDisabled = function () {
1009 var i, item, allDisabled = true;
1010
1011 if ( this.constructor.static.autoDisable ) {
1012 for ( i = this.items.length - 1; i >= 0; i-- ) {
1013 item = this.items[ i ];
1014 if ( !item.isDisabled() ) {
1015 allDisabled = false;
1016 break;
1017 }
1018 }
1019 this.autoDisabled = allDisabled;
1020 }
1021 OO.ui.ToolGroup.parent.prototype.updateDisabled.apply( this, arguments );
1022 };
1023
1024 /**
1025 * Handle mouse down and key down events.
1026 *
1027 * @protected
1028 * @param {jQuery.Event} e Mouse down or key down event
1029 */
1030 OO.ui.ToolGroup.prototype.onMouseKeyDown = function ( e ) {
1031 if (
1032 !this.isDisabled() &&
1033 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1034 ) {
1035 this.pressed = this.findTargetTool( e );
1036 if ( this.pressed ) {
1037 this.pressed.setActive( true );
1038 this.getElementDocument().addEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler, true );
1039 this.getElementDocument().addEventListener( 'keyup', this.onCapturedMouseKeyUpHandler, true );
1040 return false;
1041 }
1042 }
1043 };
1044
1045 /**
1046 * Handle captured mouse up and key up events.
1047 *
1048 * @protected
1049 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1050 */
1051 OO.ui.ToolGroup.prototype.onCapturedMouseKeyUp = function ( e ) {
1052 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler, true );
1053 this.getElementDocument().removeEventListener( 'keyup', this.onCapturedMouseKeyUpHandler, true );
1054 // onMouseKeyUp may be called a second time, depending on where the mouse is when the button is
1055 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
1056 this.onMouseKeyUp( e );
1057 };
1058
1059 /**
1060 * Handle mouse up and key up events.
1061 *
1062 * @protected
1063 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1064 */
1065 OO.ui.ToolGroup.prototype.onMouseKeyUp = function ( e ) {
1066 var tool = this.findTargetTool( e );
1067
1068 if (
1069 !this.isDisabled() && this.pressed && this.pressed === tool &&
1070 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1071 ) {
1072 this.pressed.onSelect();
1073 this.pressed = null;
1074 e.preventDefault();
1075 e.stopPropagation();
1076 }
1077
1078 this.pressed = null;
1079 };
1080
1081 /**
1082 * Handle mouse over and focus events.
1083 *
1084 * @protected
1085 * @param {jQuery.Event} e Mouse over or focus event
1086 */
1087 OO.ui.ToolGroup.prototype.onMouseOverFocus = function ( e ) {
1088 var tool = this.findTargetTool( e );
1089
1090 if ( this.pressed && this.pressed === tool ) {
1091 this.pressed.setActive( true );
1092 }
1093 };
1094
1095 /**
1096 * Handle mouse out and blur events.
1097 *
1098 * @protected
1099 * @param {jQuery.Event} e Mouse out or blur event
1100 */
1101 OO.ui.ToolGroup.prototype.onMouseOutBlur = function ( e ) {
1102 var tool = this.findTargetTool( e );
1103
1104 if ( this.pressed && this.pressed === tool ) {
1105 this.pressed.setActive( false );
1106 }
1107 };
1108
1109 /**
1110 * Get the closest tool to a jQuery.Event.
1111 *
1112 * Only tool links are considered, which prevents other elements in the tool such as popups from
1113 * triggering tool group interactions.
1114 *
1115 * @private
1116 * @param {jQuery.Event} e
1117 * @return {OO.ui.Tool|null} Tool, `null` if none was found
1118 */
1119 OO.ui.ToolGroup.prototype.findTargetTool = function ( e ) {
1120 var tool,
1121 $item = $( e.target ).closest( '.oo-ui-tool-link' );
1122
1123 if ( $item.length ) {
1124 tool = $item.parent().data( 'oo-ui-tool' );
1125 }
1126
1127 return tool && !tool.isDisabled() ? tool : null;
1128 };
1129
1130 /**
1131 * Handle tool registry register events.
1132 *
1133 * If a tool is registered after the group is created, we must repopulate the list to account for:
1134 *
1135 * - a tool being added that may be included
1136 * - a tool already included being overridden
1137 *
1138 * @protected
1139 * @param {string} name Symbolic name of tool
1140 */
1141 OO.ui.ToolGroup.prototype.onToolFactoryRegister = function () {
1142 this.populate();
1143 };
1144
1145 /**
1146 * Get the toolbar that contains the toolgroup.
1147 *
1148 * @return {OO.ui.Toolbar} Toolbar that contains the toolgroup
1149 */
1150 OO.ui.ToolGroup.prototype.getToolbar = function () {
1151 return this.toolbar;
1152 };
1153
1154 /**
1155 * Add and remove tools based on configuration.
1156 */
1157 OO.ui.ToolGroup.prototype.populate = function () {
1158 var i, len, name, tool,
1159 toolFactory = this.toolbar.getToolFactory(),
1160 names = {},
1161 add = [],
1162 remove = [],
1163 list = this.toolbar.getToolFactory().getTools(
1164 this.include, this.exclude, this.promote, this.demote
1165 );
1166
1167 // Build a list of needed tools
1168 for ( i = 0, len = list.length; i < len; i++ ) {
1169 name = list[ i ];
1170 if (
1171 // Tool exists
1172 toolFactory.lookup( name ) &&
1173 // Tool is available or is already in this group
1174 ( this.toolbar.isToolAvailable( name ) || this.tools[ name ] )
1175 ) {
1176 // Hack to prevent infinite recursion via ToolGroupTool. We need to reserve the tool before
1177 // creating it, but we can't call reserveTool() yet because we haven't created the tool.
1178 this.toolbar.tools[ name ] = true;
1179 tool = this.tools[ name ];
1180 if ( !tool ) {
1181 // Auto-initialize tools on first use
1182 this.tools[ name ] = tool = toolFactory.create( name, this );
1183 tool.updateTitle();
1184 }
1185 this.toolbar.reserveTool( tool );
1186 add.push( tool );
1187 names[ name ] = true;
1188 }
1189 }
1190 // Remove tools that are no longer needed
1191 for ( name in this.tools ) {
1192 if ( !names[ name ] ) {
1193 this.tools[ name ].destroy();
1194 this.toolbar.releaseTool( this.tools[ name ] );
1195 remove.push( this.tools[ name ] );
1196 delete this.tools[ name ];
1197 }
1198 }
1199 if ( remove.length ) {
1200 this.removeItems( remove );
1201 }
1202 // Update emptiness state
1203 if ( add.length ) {
1204 this.$element.removeClass( 'oo-ui-toolGroup-empty' );
1205 } else {
1206 this.$element.addClass( 'oo-ui-toolGroup-empty' );
1207 }
1208 // Re-add tools (moving existing ones to new locations)
1209 this.addItems( add );
1210 // Disabled state may depend on items
1211 this.updateDisabled();
1212 };
1213
1214 /**
1215 * Destroy toolgroup.
1216 */
1217 OO.ui.ToolGroup.prototype.destroy = function () {
1218 var name;
1219
1220 this.clearItems();
1221 this.toolbar.getToolFactory().disconnect( this );
1222 for ( name in this.tools ) {
1223 this.toolbar.releaseTool( this.tools[ name ] );
1224 this.tools[ name ].disconnect( this ).destroy();
1225 delete this.tools[ name ];
1226 }
1227 this.$element.remove();
1228 };
1229
1230 /**
1231 * A ToolFactory creates tools on demand. All tools ({@link OO.ui.Tool Tools}, {@link OO.ui.PopupTool PopupTools},
1232 * and {@link OO.ui.ToolGroupTool ToolGroupTools}) must be registered with a tool factory. Tools are
1233 * registered by their symbolic name. See {@link OO.ui.Toolbar toolbars} for an example.
1234 *
1235 * For more information about toolbars in general, please see the [OOUI documentation on MediaWiki][1].
1236 *
1237 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
1238 *
1239 * @class
1240 * @extends OO.Factory
1241 * @constructor
1242 */
1243 OO.ui.ToolFactory = function OoUiToolFactory() {
1244 // Parent constructor
1245 OO.ui.ToolFactory.parent.call( this );
1246 };
1247
1248 /* Setup */
1249
1250 OO.inheritClass( OO.ui.ToolFactory, OO.Factory );
1251
1252 /* Methods */
1253
1254 /**
1255 * Get tools from the factory
1256 *
1257 * @param {Array|string} [include] Included tools, see #extract for format
1258 * @param {Array|string} [exclude] Excluded tools, see #extract for format
1259 * @param {Array|string} [promote] Promoted tools, see #extract for format
1260 * @param {Array|string} [demote] Demoted tools, see #extract for format
1261 * @return {string[]} List of tools
1262 */
1263 OO.ui.ToolFactory.prototype.getTools = function ( include, exclude, promote, demote ) {
1264 var i, len, included, promoted, demoted,
1265 auto = [],
1266 used = {};
1267
1268 // Collect included and not excluded tools
1269 included = OO.simpleArrayDifference( this.extract( include ), this.extract( exclude ) );
1270
1271 // Promotion
1272 promoted = this.extract( promote, used );
1273 demoted = this.extract( demote, used );
1274
1275 // Auto
1276 for ( i = 0, len = included.length; i < len; i++ ) {
1277 if ( !used[ included[ i ] ] ) {
1278 auto.push( included[ i ] );
1279 }
1280 }
1281
1282 return promoted.concat( auto ).concat( demoted );
1283 };
1284
1285 /**
1286 * Get a flat list of names from a list of names or groups.
1287 *
1288 * Normally, `collection` is an array of tool specifications. Tools can be specified in the
1289 * following ways:
1290 *
1291 * - To include an individual tool, use the symbolic name: `{ name: 'tool-name' }` or `'tool-name'`.
1292 * - To include all tools in a group, use the group name: `{ group: 'group-name' }`. (To assign the
1293 * tool to a group, use OO.ui.Tool.static.group.)
1294 *
1295 * Alternatively, to include all tools that are not yet assigned to any other toolgroup, use the
1296 * catch-all selector `'*'`.
1297 *
1298 * If `used` is passed, tool names that appear as properties in this object will be considered
1299 * already assigned, and will not be returned even if specified otherwise. The tool names extracted
1300 * by this function call will be added as new properties in the object.
1301 *
1302 * @private
1303 * @param {Array|string} collection List of tools, see above
1304 * @param {Object} [used] Object containing information about used tools, see above
1305 * @return {string[]} List of extracted tool names
1306 */
1307 OO.ui.ToolFactory.prototype.extract = function ( collection, used ) {
1308 var i, len, item, name, tool,
1309 names = [];
1310
1311 collection = !Array.isArray( collection ) ? [ collection ] : collection;
1312
1313 for ( i = 0, len = collection.length; i < len; i++ ) {
1314 item = collection[ i ];
1315 if ( item === '*' ) {
1316 for ( name in this.registry ) {
1317 tool = this.registry[ name ];
1318 if (
1319 // Only add tools by group name when auto-add is enabled
1320 tool.static.autoAddToCatchall &&
1321 // Exclude already used tools
1322 ( !used || !used[ name ] )
1323 ) {
1324 names.push( name );
1325 if ( used ) {
1326 used[ name ] = true;
1327 }
1328 }
1329 }
1330 } else {
1331 // Allow plain strings as shorthand for named tools
1332 if ( typeof item === 'string' ) {
1333 item = { name: item };
1334 }
1335 if ( OO.isPlainObject( item ) ) {
1336 if ( item.group ) {
1337 for ( name in this.registry ) {
1338 tool = this.registry[ name ];
1339 if (
1340 // Include tools with matching group
1341 tool.static.group === item.group &&
1342 // Only add tools by group name when auto-add is enabled
1343 tool.static.autoAddToGroup &&
1344 // Exclude already used tools
1345 ( !used || !used[ name ] )
1346 ) {
1347 names.push( name );
1348 if ( used ) {
1349 used[ name ] = true;
1350 }
1351 }
1352 }
1353 // Include tools with matching name and exclude already used tools
1354 } else if ( item.name && ( !used || !used[ item.name ] ) ) {
1355 names.push( item.name );
1356 if ( used ) {
1357 used[ item.name ] = true;
1358 }
1359 }
1360 }
1361 }
1362 }
1363 return names;
1364 };
1365
1366 /**
1367 * ToolGroupFactories create {@link OO.ui.ToolGroup toolgroups} on demand. The toolgroup classes must
1368 * specify a symbolic name and be registered with the factory. The following classes are registered by
1369 * default:
1370 *
1371 * - {@link OO.ui.BarToolGroup BarToolGroups} (‘bar’)
1372 * - {@link OO.ui.MenuToolGroup MenuToolGroups} (‘menu’)
1373 * - {@link OO.ui.ListToolGroup ListToolGroups} (‘list’)
1374 *
1375 * See {@link OO.ui.Toolbar toolbars} for an example.
1376 *
1377 * For more information about toolbars in general, please see the [OOUI documentation on MediaWiki][1].
1378 *
1379 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
1380 *
1381 * @class
1382 * @extends OO.Factory
1383 * @constructor
1384 */
1385 OO.ui.ToolGroupFactory = function OoUiToolGroupFactory() {
1386 var i, l, defaultClasses;
1387 // Parent constructor
1388 OO.Factory.call( this );
1389
1390 defaultClasses = this.constructor.static.getDefaultClasses();
1391
1392 // Register default toolgroups
1393 for ( i = 0, l = defaultClasses.length; i < l; i++ ) {
1394 this.register( defaultClasses[ i ] );
1395 }
1396 };
1397
1398 /* Setup */
1399
1400 OO.inheritClass( OO.ui.ToolGroupFactory, OO.Factory );
1401
1402 /* Static Methods */
1403
1404 /**
1405 * Get a default set of classes to be registered on construction.
1406 *
1407 * @return {Function[]} Default classes
1408 */
1409 OO.ui.ToolGroupFactory.static.getDefaultClasses = function () {
1410 return [
1411 OO.ui.BarToolGroup,
1412 OO.ui.ListToolGroup,
1413 OO.ui.MenuToolGroup
1414 ];
1415 };
1416
1417 /**
1418 * Popup tools open a popup window when they are selected from the {@link OO.ui.Toolbar toolbar}. Each popup tool is configured
1419 * 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
1420 * an #onSelect or #onUpdateState method, as these methods have been implemented already.
1421 *
1422 * // Example of a popup tool. When selected, a popup tool displays
1423 * // a popup window.
1424 * function HelpTool( toolGroup, config ) {
1425 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
1426 * padded: true,
1427 * label: 'Help',
1428 * head: true
1429 * } }, config ) );
1430 * this.popup.$body.append( '<p>I am helpful!</p>' );
1431 * };
1432 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
1433 * HelpTool.static.name = 'help';
1434 * HelpTool.static.icon = 'help';
1435 * HelpTool.static.title = 'Help';
1436 * toolFactory.register( HelpTool );
1437 *
1438 * For an example of a toolbar that contains a popup tool, see {@link OO.ui.Toolbar toolbars}. For more information about
1439 * toolbars in general, please see the [OOUI documentation on MediaWiki][1].
1440 *
1441 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
1442 *
1443 * @abstract
1444 * @class
1445 * @extends OO.ui.Tool
1446 * @mixins OO.ui.mixin.PopupElement
1447 *
1448 * @constructor
1449 * @param {OO.ui.ToolGroup} toolGroup
1450 * @param {Object} [config] Configuration options
1451 */
1452 OO.ui.PopupTool = function OoUiPopupTool( toolGroup, config ) {
1453 // Allow passing positional parameters inside the config object
1454 if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
1455 config = toolGroup;
1456 toolGroup = config.toolGroup;
1457 }
1458
1459 // Parent constructor
1460 OO.ui.PopupTool.parent.call( this, toolGroup, config );
1461
1462 // Mixin constructors
1463 OO.ui.mixin.PopupElement.call( this, config );
1464
1465 // Initialization
1466 this.popup.setPosition( toolGroup.getToolbar().position === 'bottom' ? 'above' : 'below' );
1467 this.$element
1468 .addClass( 'oo-ui-popupTool' )
1469 .append( this.popup.$element );
1470 };
1471
1472 /* Setup */
1473
1474 OO.inheritClass( OO.ui.PopupTool, OO.ui.Tool );
1475 OO.mixinClass( OO.ui.PopupTool, OO.ui.mixin.PopupElement );
1476
1477 /* Methods */
1478
1479 /**
1480 * Handle the tool being selected.
1481 *
1482 * @inheritdoc
1483 */
1484 OO.ui.PopupTool.prototype.onSelect = function () {
1485 if ( !this.isDisabled() ) {
1486 this.popup.toggle();
1487 }
1488 this.setActive( false );
1489 return false;
1490 };
1491
1492 /**
1493 * Handle the toolbar state being updated.
1494 *
1495 * @inheritdoc
1496 */
1497 OO.ui.PopupTool.prototype.onUpdateState = function () {
1498 this.setActive( false );
1499 };
1500
1501 /**
1502 * A ToolGroupTool is a special sort of tool that can contain other {@link OO.ui.Tool tools}
1503 * and {@link OO.ui.ToolGroup toolgroups}. The ToolGroupTool was specifically designed to be used
1504 * inside a {@link OO.ui.BarToolGroup bar} toolgroup to provide access to additional tools from
1505 * the bar item. Included tools will be displayed in a dropdown {@link OO.ui.ListToolGroup list}
1506 * when the ToolGroupTool is selected.
1507 *
1508 * // Example: ToolGroupTool with two nested tools, 'setting1' and 'setting2', defined elsewhere.
1509 *
1510 * function SettingsTool() {
1511 * SettingsTool.parent.apply( this, arguments );
1512 * };
1513 * OO.inheritClass( SettingsTool, OO.ui.ToolGroupTool );
1514 * SettingsTool.static.name = 'settings';
1515 * SettingsTool.static.title = 'Change settings';
1516 * SettingsTool.static.groupConfig = {
1517 * icon: 'settings',
1518 * label: 'ToolGroupTool',
1519 * include: [ 'setting1', 'setting2' ]
1520 * };
1521 * toolFactory.register( SettingsTool );
1522 *
1523 * For more information, please see the [OOUI documentation on MediaWiki][1].
1524 *
1525 * Please note that this implementation is subject to change per [T74159] [2].
1526 *
1527 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars#ToolGroupTool
1528 * [2]: https://phabricator.wikimedia.org/T74159
1529 *
1530 * @abstract
1531 * @class
1532 * @extends OO.ui.Tool
1533 *
1534 * @constructor
1535 * @param {OO.ui.ToolGroup} toolGroup
1536 * @param {Object} [config] Configuration options
1537 */
1538 OO.ui.ToolGroupTool = function OoUiToolGroupTool( toolGroup, config ) {
1539 // Allow passing positional parameters inside the config object
1540 if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
1541 config = toolGroup;
1542 toolGroup = config.toolGroup;
1543 }
1544
1545 // Parent constructor
1546 OO.ui.ToolGroupTool.parent.call( this, toolGroup, config );
1547
1548 // Properties
1549 this.innerToolGroup = this.createGroup( this.constructor.static.groupConfig );
1550
1551 // Events
1552 this.innerToolGroup.connect( this, { disable: 'onToolGroupDisable' } );
1553
1554 // Initialization
1555 this.$link.remove();
1556 this.$element
1557 .addClass( 'oo-ui-toolGroupTool' )
1558 .append( this.innerToolGroup.$element );
1559 };
1560
1561 /* Setup */
1562
1563 OO.inheritClass( OO.ui.ToolGroupTool, OO.ui.Tool );
1564
1565 /* Static Properties */
1566
1567 /**
1568 * Toolgroup configuration.
1569 *
1570 * The toolgroup configuration consists of the tools to include, as well as an icon and label
1571 * to use for the bar item. Tools can be included by symbolic name, group, or with the
1572 * wildcard selector. Please see {@link OO.ui.ToolGroup toolgroup} for more information.
1573 *
1574 * @property {Object.<string,Array>}
1575 */
1576 OO.ui.ToolGroupTool.static.groupConfig = {};
1577
1578 /* Methods */
1579
1580 /**
1581 * Handle the tool being selected.
1582 *
1583 * @inheritdoc
1584 */
1585 OO.ui.ToolGroupTool.prototype.onSelect = function () {
1586 this.innerToolGroup.setActive( !this.innerToolGroup.active );
1587 return false;
1588 };
1589
1590 /**
1591 * Synchronize disabledness state of the tool with the inner toolgroup.
1592 *
1593 * @private
1594 * @param {boolean} disabled Element is disabled
1595 */
1596 OO.ui.ToolGroupTool.prototype.onToolGroupDisable = function ( disabled ) {
1597 this.setDisabled( disabled );
1598 };
1599
1600 /**
1601 * Handle the toolbar state being updated.
1602 *
1603 * @inheritdoc
1604 */
1605 OO.ui.ToolGroupTool.prototype.onUpdateState = function () {
1606 this.setActive( false );
1607 };
1608
1609 /**
1610 * Build a {@link OO.ui.ToolGroup toolgroup} from the specified configuration.
1611 *
1612 * @param {Object.<string,Array>} group Toolgroup configuration. Please see {@link OO.ui.ToolGroup toolgroup} for
1613 * more information.
1614 * @return {OO.ui.ListToolGroup}
1615 */
1616 OO.ui.ToolGroupTool.prototype.createGroup = function ( group ) {
1617 if ( group.include === '*' ) {
1618 // Apply defaults to catch-all groups
1619 if ( group.label === undefined ) {
1620 group.label = OO.ui.msg( 'ooui-toolbar-more' );
1621 }
1622 }
1623
1624 return this.toolbar.getToolGroupFactory().create( 'list', this.toolbar, group );
1625 };
1626
1627 /**
1628 * BarToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
1629 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
1630 * and {@link OO.ui.ListToolGroup ListToolGroup}). The {@link OO.ui.Tool tools} in a BarToolGroup are
1631 * displayed by icon in a single row. The title of the tool is displayed when users move the mouse over
1632 * the tool.
1633 *
1634 * BarToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar is
1635 * set up.
1636 *
1637 * @example
1638 * // Example of a BarToolGroup with two tools
1639 * var toolFactory = new OO.ui.ToolFactory();
1640 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
1641 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
1642 *
1643 * // We will be placing status text in this element when tools are used
1644 * var $area = $( '<p>' ).text( 'Example of a BarToolGroup with two tools.' );
1645 *
1646 * // Define the tools that we're going to place in our toolbar
1647 *
1648 * // Create a class inheriting from OO.ui.Tool
1649 * function SearchTool() {
1650 * SearchTool.parent.apply( this, arguments );
1651 * }
1652 * OO.inheritClass( SearchTool, OO.ui.Tool );
1653 * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
1654 * // of 'icon' and 'title' (displayed icon and text).
1655 * SearchTool.static.name = 'search';
1656 * SearchTool.static.icon = 'search';
1657 * SearchTool.static.title = 'Search...';
1658 * // Defines the action that will happen when this tool is selected (clicked).
1659 * SearchTool.prototype.onSelect = function () {
1660 * $area.text( 'Search tool clicked!' );
1661 * // Never display this tool as "active" (selected).
1662 * this.setActive( false );
1663 * };
1664 * SearchTool.prototype.onUpdateState = function () {};
1665 * // Make this tool available in our toolFactory and thus our toolbar
1666 * toolFactory.register( SearchTool );
1667 *
1668 * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
1669 * // little popup window (a PopupWidget).
1670 * function HelpTool( toolGroup, config ) {
1671 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
1672 * padded: true,
1673 * label: 'Help',
1674 * head: true
1675 * } }, config ) );
1676 * this.popup.$body.append( '<p>I am helpful!</p>' );
1677 * }
1678 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
1679 * HelpTool.static.name = 'help';
1680 * HelpTool.static.icon = 'help';
1681 * HelpTool.static.title = 'Help';
1682 * toolFactory.register( HelpTool );
1683 *
1684 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
1685 * // used once (but not all defined tools must be used).
1686 * toolbar.setup( [
1687 * {
1688 * // 'bar' tool groups display tools by icon only
1689 * type: 'bar',
1690 * include: [ 'search', 'help' ]
1691 * }
1692 * ] );
1693 *
1694 * // Create some UI around the toolbar and place it in the document
1695 * var frame = new OO.ui.PanelLayout( {
1696 * expanded: false,
1697 * framed: true
1698 * } );
1699 * var contentFrame = new OO.ui.PanelLayout( {
1700 * expanded: false,
1701 * padded: true
1702 * } );
1703 * frame.$element.append(
1704 * toolbar.$element,
1705 * contentFrame.$element.append( $area )
1706 * );
1707 * $( 'body' ).append( frame.$element );
1708 *
1709 * // Here is where the toolbar is actually built. This must be done after inserting it into the
1710 * // document.
1711 * toolbar.initialize();
1712 *
1713 * For more information about how to add tools to a bar tool group, please see {@link OO.ui.ToolGroup toolgroup}.
1714 * For more information about toolbars in general, please see the [OOUI documentation on MediaWiki][1].
1715 *
1716 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
1717 *
1718 * @class
1719 * @extends OO.ui.ToolGroup
1720 *
1721 * @constructor
1722 * @param {OO.ui.Toolbar} toolbar
1723 * @param {Object} [config] Configuration options
1724 */
1725 OO.ui.BarToolGroup = function OoUiBarToolGroup( toolbar, config ) {
1726 // Allow passing positional parameters inside the config object
1727 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
1728 config = toolbar;
1729 toolbar = config.toolbar;
1730 }
1731
1732 // Parent constructor
1733 OO.ui.BarToolGroup.parent.call( this, toolbar, config );
1734
1735 // Initialization
1736 this.$element.addClass( 'oo-ui-barToolGroup' );
1737 };
1738
1739 /* Setup */
1740
1741 OO.inheritClass( OO.ui.BarToolGroup, OO.ui.ToolGroup );
1742
1743 /* Static Properties */
1744
1745 /**
1746 * @static
1747 * @inheritdoc
1748 */
1749 OO.ui.BarToolGroup.static.titleTooltips = true;
1750
1751 /**
1752 * @static
1753 * @inheritdoc
1754 */
1755 OO.ui.BarToolGroup.static.accelTooltips = true;
1756
1757 /**
1758 * @static
1759 * @inheritdoc
1760 */
1761 OO.ui.BarToolGroup.static.name = 'bar';
1762
1763 /**
1764 * PopupToolGroup is an abstract base class used by both {@link OO.ui.MenuToolGroup MenuToolGroup}
1765 * and {@link OO.ui.ListToolGroup ListToolGroup} to provide a popup--an overlaid menu or list of tools with an
1766 * optional icon and label. This class can be used for other base classes that also use this functionality.
1767 *
1768 * @abstract
1769 * @class
1770 * @extends OO.ui.ToolGroup
1771 * @mixins OO.ui.mixin.IconElement
1772 * @mixins OO.ui.mixin.IndicatorElement
1773 * @mixins OO.ui.mixin.LabelElement
1774 * @mixins OO.ui.mixin.TitledElement
1775 * @mixins OO.ui.mixin.FlaggedElement
1776 * @mixins OO.ui.mixin.ClippableElement
1777 * @mixins OO.ui.mixin.TabIndexedElement
1778 *
1779 * @constructor
1780 * @param {OO.ui.Toolbar} toolbar
1781 * @param {Object} [config] Configuration options
1782 * @cfg {string} [header] Text to display at the top of the popup
1783 */
1784 OO.ui.PopupToolGroup = function OoUiPopupToolGroup( toolbar, config ) {
1785 // Allow passing positional parameters inside the config object
1786 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
1787 config = toolbar;
1788 toolbar = config.toolbar;
1789 }
1790
1791 // Configuration initialization
1792 config = $.extend( {
1793 indicator: config.indicator === undefined ? ( toolbar.position === 'bottom' ? 'up' : 'down' ) : config.indicator
1794 }, config );
1795
1796 // Parent constructor
1797 OO.ui.PopupToolGroup.parent.call( this, toolbar, config );
1798
1799 // Properties
1800 this.active = false;
1801 this.dragging = false;
1802 this.onBlurHandler = this.onBlur.bind( this );
1803 this.$handle = $( '<span>' );
1804
1805 // Mixin constructors
1806 OO.ui.mixin.IconElement.call( this, config );
1807 OO.ui.mixin.IndicatorElement.call( this, config );
1808 OO.ui.mixin.LabelElement.call( this, config );
1809 OO.ui.mixin.TitledElement.call( this, config );
1810 OO.ui.mixin.FlaggedElement.call( this, config );
1811 OO.ui.mixin.ClippableElement.call( this, $.extend( {}, config, { $clippable: this.$group } ) );
1812 OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$handle } ) );
1813
1814 // Events
1815 this.$handle.on( {
1816 keydown: this.onHandleMouseKeyDown.bind( this ),
1817 keyup: this.onHandleMouseKeyUp.bind( this ),
1818 mousedown: this.onHandleMouseKeyDown.bind( this ),
1819 mouseup: this.onHandleMouseKeyUp.bind( this )
1820 } );
1821
1822 // Initialization
1823 this.$handle
1824 .addClass( 'oo-ui-popupToolGroup-handle' )
1825 .attr( 'role', 'button' )
1826 .append( this.$icon, this.$label, this.$indicator );
1827 // If the pop-up should have a header, add it to the top of the toolGroup.
1828 // Note: If this feature is useful for other widgets, we could abstract it into an
1829 // OO.ui.HeaderedElement mixin constructor.
1830 if ( config.header !== undefined ) {
1831 this.$group
1832 .prepend( $( '<span>' )
1833 .addClass( 'oo-ui-popupToolGroup-header' )
1834 .text( config.header )
1835 );
1836 }
1837 this.$element
1838 .addClass( 'oo-ui-popupToolGroup' )
1839 .prepend( this.$handle );
1840 };
1841
1842 /* Setup */
1843
1844 OO.inheritClass( OO.ui.PopupToolGroup, OO.ui.ToolGroup );
1845 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IconElement );
1846 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IndicatorElement );
1847 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.LabelElement );
1848 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TitledElement );
1849 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.FlaggedElement );
1850 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.ClippableElement );
1851 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TabIndexedElement );
1852
1853 /* Methods */
1854
1855 /**
1856 * @inheritdoc OO.ui.mixin.ClippableElement
1857 */
1858 OO.ui.PopupToolGroup.prototype.getHorizontalAnchorEdge = function () {
1859 var out;
1860 if ( this.$element.hasClass( 'oo-ui-popupToolGroup-right' ) ) {
1861 out = 'right';
1862 } else {
1863 out = 'left';
1864 }
1865 // Flip for RTL
1866 if ( this.$element.css( 'direction' ) === 'rtl' ) {
1867 out = ( out === 'left' ) ? 'right' : 'left';
1868 }
1869 return out;
1870 };
1871
1872 /**
1873 * @inheritdoc OO.ui.mixin.ClippableElement
1874 */
1875 OO.ui.PopupToolGroup.prototype.getVerticalAnchorEdge = function () {
1876 if ( this.toolbar.position === 'bottom' ) {
1877 return 'bottom';
1878 }
1879 return 'top';
1880 };
1881
1882 /**
1883 * @inheritdoc
1884 */
1885 OO.ui.PopupToolGroup.prototype.setDisabled = function () {
1886 // Parent method
1887 OO.ui.PopupToolGroup.parent.prototype.setDisabled.apply( this, arguments );
1888
1889 if ( this.isDisabled() && this.isElementAttached() ) {
1890 this.setActive( false );
1891 }
1892 };
1893
1894 /**
1895 * Handle focus being lost.
1896 *
1897 * The event is actually generated from a mouseup/keyup, so it is not a normal blur event object.
1898 *
1899 * @protected
1900 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1901 */
1902 OO.ui.PopupToolGroup.prototype.onBlur = function ( e ) {
1903 // Only deactivate when clicking outside the dropdown element
1904 if ( $( e.target ).closest( '.oo-ui-popupToolGroup' )[ 0 ] !== this.$element[ 0 ] ) {
1905 this.setActive( false );
1906 }
1907 };
1908
1909 /**
1910 * @inheritdoc
1911 */
1912 OO.ui.PopupToolGroup.prototype.onMouseKeyUp = function ( e ) {
1913 // Only close toolgroup when a tool was actually selected
1914 if (
1915 !this.isDisabled() && this.pressed && this.pressed === this.findTargetTool( e ) &&
1916 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1917 ) {
1918 this.setActive( false );
1919 }
1920 return OO.ui.PopupToolGroup.parent.prototype.onMouseKeyUp.call( this, e );
1921 };
1922
1923 /**
1924 * Handle mouse up and key up events.
1925 *
1926 * @protected
1927 * @param {jQuery.Event} e Mouse up or key up event
1928 */
1929 OO.ui.PopupToolGroup.prototype.onHandleMouseKeyUp = function ( e ) {
1930 if (
1931 !this.isDisabled() &&
1932 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1933 ) {
1934 return false;
1935 }
1936 };
1937
1938 /**
1939 * Handle mouse down and key down events.
1940 *
1941 * @protected
1942 * @param {jQuery.Event} e Mouse down or key down event
1943 */
1944 OO.ui.PopupToolGroup.prototype.onHandleMouseKeyDown = function ( e ) {
1945 if (
1946 !this.isDisabled() &&
1947 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1948 ) {
1949 this.setActive( !this.active );
1950 return false;
1951 }
1952 };
1953
1954 /**
1955 * Check if the tool group is active.
1956 *
1957 * @return {boolean} Tool group is active
1958 */
1959 OO.ui.PopupToolGroup.prototype.isActive = function () {
1960 return this.active;
1961 };
1962
1963 /**
1964 * Switch into 'active' mode.
1965 *
1966 * When active, the popup is visible. A mouseup event anywhere in the document will trigger
1967 * deactivation.
1968 *
1969 * @param {boolean} value The active state to set
1970 */
1971 OO.ui.PopupToolGroup.prototype.setActive = function ( value ) {
1972 var containerWidth, containerLeft;
1973 value = !!value;
1974 if ( this.active !== value ) {
1975 this.active = value;
1976 if ( value ) {
1977 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler, true );
1978 this.getElementDocument().addEventListener( 'keyup', this.onBlurHandler, true );
1979
1980 this.$clippable.css( 'left', '' );
1981 // Try anchoring the popup to the left first
1982 this.$element.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
1983 this.toggleClipping( true );
1984 if ( this.isClippedHorizontally() ) {
1985 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
1986 this.toggleClipping( false );
1987 this.$element
1988 .removeClass( 'oo-ui-popupToolGroup-left' )
1989 .addClass( 'oo-ui-popupToolGroup-right' );
1990 this.toggleClipping( true );
1991 }
1992 if ( this.isClippedHorizontally() ) {
1993 // Anchoring to the right also caused the popup to clip, so just make it fill the container
1994 containerWidth = this.$clippableScrollableContainer.width();
1995 containerLeft = this.$clippableScrollableContainer[ 0 ] === document.documentElement ?
1996 0 :
1997 this.$clippableScrollableContainer.offset().left;
1998
1999 this.toggleClipping( false );
2000 this.$element.removeClass( 'oo-ui-popupToolGroup-right' );
2001
2002 this.$clippable.css( {
2003 left: -( this.$element.offset().left - containerLeft ),
2004 width: containerWidth
2005 } );
2006 }
2007 } else {
2008 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler, true );
2009 this.getElementDocument().removeEventListener( 'keyup', this.onBlurHandler, true );
2010 this.$element.removeClass(
2011 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
2012 );
2013 this.toggleClipping( false );
2014 }
2015 this.updateThemeClasses();
2016 }
2017 };
2018
2019 /**
2020 * ListToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
2021 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
2022 * and {@link OO.ui.BarToolGroup BarToolGroup}). The {@link OO.ui.Tool tools} in a ListToolGroup are displayed
2023 * by label in a dropdown menu. The title of the tool is used as the label text. The menu itself can be configured
2024 * with a label, icon, indicator, header, and title.
2025 *
2026 * ListToolGroups can be configured to be expanded and collapsed. Collapsed lists will have a ‘More’ option that
2027 * users can select to see the full list of tools. If a collapsed toolgroup is expanded, a ‘Fewer’ option permits
2028 * users to collapse the list again.
2029 *
2030 * ListToolGroups are created by a {@link OO.ui.ToolGroupFactory toolgroup factory} when the toolbar is set up. The factory
2031 * requires the ListToolGroup's symbolic name, 'list', which is specified along with the other configurations. For more
2032 * information about how to add tools to a ListToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
2033 *
2034 * @example
2035 * // Example of a ListToolGroup
2036 * var toolFactory = new OO.ui.ToolFactory();
2037 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
2038 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
2039 *
2040 * // Configure and register two tools
2041 * function SettingsTool() {
2042 * SettingsTool.parent.apply( this, arguments );
2043 * }
2044 * OO.inheritClass( SettingsTool, OO.ui.Tool );
2045 * SettingsTool.static.name = 'settings';
2046 * SettingsTool.static.icon = 'settings';
2047 * SettingsTool.static.title = 'Change settings';
2048 * SettingsTool.prototype.onSelect = function () {
2049 * this.setActive( false );
2050 * };
2051 * SettingsTool.prototype.onUpdateState = function () {};
2052 * toolFactory.register( SettingsTool );
2053 * // Register two more tools, nothing interesting here
2054 * function StuffTool() {
2055 * StuffTool.parent.apply( this, arguments );
2056 * }
2057 * OO.inheritClass( StuffTool, OO.ui.Tool );
2058 * StuffTool.static.name = 'stuff';
2059 * StuffTool.static.icon = 'search';
2060 * StuffTool.static.title = 'Change the world';
2061 * StuffTool.prototype.onSelect = function () {
2062 * this.setActive( false );
2063 * };
2064 * StuffTool.prototype.onUpdateState = function () {};
2065 * toolFactory.register( StuffTool );
2066 * toolbar.setup( [
2067 * {
2068 * // Configurations for list toolgroup.
2069 * type: 'list',
2070 * label: 'ListToolGroup',
2071 * icon: 'ellipsis',
2072 * title: 'This is the title, displayed when user moves the mouse over the list toolgroup',
2073 * header: 'This is the header',
2074 * include: [ 'settings', 'stuff' ],
2075 * allowCollapse: ['stuff']
2076 * }
2077 * ] );
2078 *
2079 * // Create some UI around the toolbar and place it in the document
2080 * var frame = new OO.ui.PanelLayout( {
2081 * expanded: false,
2082 * framed: true
2083 * } );
2084 * frame.$element.append(
2085 * toolbar.$element
2086 * );
2087 * $( 'body' ).append( frame.$element );
2088 * // Build the toolbar. This must be done after the toolbar has been appended to the document.
2089 * toolbar.initialize();
2090 *
2091 * For more information about toolbars in general, please see the [OOUI documentation on MediaWiki][1].
2092 *
2093 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
2094 *
2095 * @class
2096 * @extends OO.ui.PopupToolGroup
2097 *
2098 * @constructor
2099 * @param {OO.ui.Toolbar} toolbar
2100 * @param {Object} [config] Configuration options
2101 * @cfg {Array} [allowCollapse] Allow the specified tools to be collapsed. By default, collapsible tools
2102 * will only be displayed if users click the ‘More’ option displayed at the bottom of the list. If
2103 * the list is expanded, a ‘Fewer’ option permits users to collapse the list again. Any tools that
2104 * are included in the toolgroup, but are not designated as collapsible, will always be displayed.
2105 * To open a collapsible list in its expanded state, set #expanded to 'true'.
2106 * @cfg {Array} [forceExpand] Expand the specified tools. All other tools will be designated as collapsible.
2107 * Unless #expanded is set to true, the collapsible tools will be collapsed when the list is first opened.
2108 * @cfg {boolean} [expanded=false] Expand collapsible tools. This config is only relevant if tools have
2109 * been designated as collapsible. When expanded is set to true, all tools in the group will be displayed
2110 * when the list is first opened. Users can collapse the list with a ‘Fewer’ option at the bottom.
2111 */
2112 OO.ui.ListToolGroup = function OoUiListToolGroup( toolbar, config ) {
2113 // Allow passing positional parameters inside the config object
2114 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
2115 config = toolbar;
2116 toolbar = config.toolbar;
2117 }
2118
2119 // Configuration initialization
2120 config = config || {};
2121
2122 // Properties (must be set before parent constructor, which calls #populate)
2123 this.allowCollapse = config.allowCollapse;
2124 this.forceExpand = config.forceExpand;
2125 this.expanded = config.expanded !== undefined ? config.expanded : false;
2126 this.collapsibleTools = [];
2127
2128 // Parent constructor
2129 OO.ui.ListToolGroup.parent.call( this, toolbar, config );
2130
2131 // Initialization
2132 this.$element.addClass( 'oo-ui-listToolGroup' );
2133 };
2134
2135 /* Setup */
2136
2137 OO.inheritClass( OO.ui.ListToolGroup, OO.ui.PopupToolGroup );
2138
2139 /* Static Properties */
2140
2141 /**
2142 * @static
2143 * @inheritdoc
2144 */
2145 OO.ui.ListToolGroup.static.name = 'list';
2146
2147 /* Methods */
2148
2149 /**
2150 * @inheritdoc
2151 */
2152 OO.ui.ListToolGroup.prototype.populate = function () {
2153 var i, len, allowCollapse = [];
2154
2155 OO.ui.ListToolGroup.parent.prototype.populate.call( this );
2156
2157 // Update the list of collapsible tools
2158 if ( this.allowCollapse !== undefined ) {
2159 allowCollapse = this.allowCollapse;
2160 } else if ( this.forceExpand !== undefined ) {
2161 allowCollapse = OO.simpleArrayDifference( Object.keys( this.tools ), this.forceExpand );
2162 }
2163
2164 this.collapsibleTools = [];
2165 for ( i = 0, len = allowCollapse.length; i < len; i++ ) {
2166 if ( this.tools[ allowCollapse[ i ] ] !== undefined ) {
2167 this.collapsibleTools.push( this.tools[ allowCollapse[ i ] ] );
2168 }
2169 }
2170
2171 // Keep at the end, even when tools are added
2172 this.$group.append( this.getExpandCollapseTool().$element );
2173
2174 this.getExpandCollapseTool().toggle( this.collapsibleTools.length !== 0 );
2175 this.updateCollapsibleState();
2176 };
2177
2178 /**
2179 * Get the expand/collapse tool for this group
2180 *
2181 * @return {OO.ui.Tool} Expand collapse tool
2182 */
2183 OO.ui.ListToolGroup.prototype.getExpandCollapseTool = function () {
2184 var ExpandCollapseTool;
2185 if ( this.expandCollapseTool === undefined ) {
2186 ExpandCollapseTool = function () {
2187 ExpandCollapseTool.parent.apply( this, arguments );
2188 };
2189
2190 OO.inheritClass( ExpandCollapseTool, OO.ui.Tool );
2191
2192 ExpandCollapseTool.prototype.onSelect = function () {
2193 this.toolGroup.expanded = !this.toolGroup.expanded;
2194 this.toolGroup.updateCollapsibleState();
2195 this.setActive( false );
2196 };
2197 ExpandCollapseTool.prototype.onUpdateState = function () {
2198 // Do nothing. Tool interface requires an implementation of this function.
2199 };
2200
2201 ExpandCollapseTool.static.name = 'more-fewer';
2202
2203 this.expandCollapseTool = new ExpandCollapseTool( this );
2204 }
2205 return this.expandCollapseTool;
2206 };
2207
2208 /**
2209 * @inheritdoc
2210 */
2211 OO.ui.ListToolGroup.prototype.onMouseKeyUp = function ( e ) {
2212 // Do not close the popup when the user wants to show more/fewer tools
2213 if (
2214 $( e.target ).closest( '.oo-ui-tool-name-more-fewer' ).length &&
2215 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
2216 ) {
2217 // HACK: Prevent the popup list from being hidden. Skip the PopupToolGroup implementation (which
2218 // hides the popup list when a tool is selected) and call ToolGroup's implementation directly.
2219 return OO.ui.ListToolGroup.parent.parent.prototype.onMouseKeyUp.call( this, e );
2220 } else {
2221 return OO.ui.ListToolGroup.parent.prototype.onMouseKeyUp.call( this, e );
2222 }
2223 };
2224
2225 OO.ui.ListToolGroup.prototype.updateCollapsibleState = function () {
2226 var i, icon, len;
2227
2228 if ( this.toolbar.position !== 'bottom' ) {
2229 icon = this.expanded ? 'collapse' : 'expand';
2230 } else {
2231 icon = this.expanded ? 'expand' : 'collapse';
2232 }
2233
2234 this.getExpandCollapseTool()
2235 .setIcon( icon )
2236 .setTitle( OO.ui.msg( this.expanded ? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
2237
2238 for ( i = 0, len = this.collapsibleTools.length; i < len; i++ ) {
2239 this.collapsibleTools[ i ].toggle( this.expanded );
2240 }
2241
2242 // Re-evaluate clipping, because our height has changed
2243 this.clip();
2244 };
2245
2246 /**
2247 * MenuToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
2248 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.BarToolGroup BarToolGroup}
2249 * and {@link OO.ui.ListToolGroup ListToolGroup}). MenuToolGroups contain selectable {@link OO.ui.Tool tools},
2250 * which are displayed by label in a dropdown menu. The tool's title is used as the label text, and the
2251 * menu label is updated to reflect which tool or tools are currently selected. If no tools are selected,
2252 * the menu label is empty. The menu can be configured with an indicator, icon, title, and/or header.
2253 *
2254 * MenuToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar
2255 * is set up.
2256 *
2257 * @example
2258 * // Example of a MenuToolGroup
2259 * var toolFactory = new OO.ui.ToolFactory();
2260 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
2261 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
2262 *
2263 * // We will be placing status text in this element when tools are used
2264 * var $area = $( '<p>' ).text( 'An example of a MenuToolGroup. Select a tool from the dropdown menu.' );
2265 *
2266 * // Define the tools that we're going to place in our toolbar
2267 *
2268 * function SettingsTool() {
2269 * SettingsTool.parent.apply( this, arguments );
2270 * this.reallyActive = false;
2271 * }
2272 * OO.inheritClass( SettingsTool, OO.ui.Tool );
2273 * SettingsTool.static.name = 'settings';
2274 * SettingsTool.static.icon = 'settings';
2275 * SettingsTool.static.title = 'Change settings';
2276 * SettingsTool.prototype.onSelect = function () {
2277 * $area.text( 'Settings tool clicked!' );
2278 * // Toggle the active state on each click
2279 * this.reallyActive = !this.reallyActive;
2280 * this.setActive( this.reallyActive );
2281 * // To update the menu label
2282 * this.toolbar.emit( 'updateState' );
2283 * };
2284 * SettingsTool.prototype.onUpdateState = function () {};
2285 * toolFactory.register( SettingsTool );
2286 *
2287 * function StuffTool() {
2288 * StuffTool.parent.apply( this, arguments );
2289 * this.reallyActive = false;
2290 * }
2291 * OO.inheritClass( StuffTool, OO.ui.Tool );
2292 * StuffTool.static.name = 'stuff';
2293 * StuffTool.static.icon = 'ellipsis';
2294 * StuffTool.static.title = 'More stuff';
2295 * StuffTool.prototype.onSelect = function () {
2296 * $area.text( 'More stuff tool clicked!' );
2297 * // Toggle the active state on each click
2298 * this.reallyActive = !this.reallyActive;
2299 * this.setActive( this.reallyActive );
2300 * // To update the menu label
2301 * this.toolbar.emit( 'updateState' );
2302 * };
2303 * StuffTool.prototype.onUpdateState = function () {};
2304 * toolFactory.register( StuffTool );
2305 *
2306 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
2307 * // used once (but not all defined tools must be used).
2308 * toolbar.setup( [
2309 * {
2310 * type: 'menu',
2311 * header: 'This is the (optional) header',
2312 * title: 'This is the (optional) title',
2313 * include: [ 'settings', 'stuff' ]
2314 * }
2315 * ] );
2316 *
2317 * // Create some UI around the toolbar and place it in the document
2318 * var frame = new OO.ui.PanelLayout( {
2319 * expanded: false,
2320 * framed: true
2321 * } );
2322 * var contentFrame = new OO.ui.PanelLayout( {
2323 * expanded: false,
2324 * padded: true
2325 * } );
2326 * frame.$element.append(
2327 * toolbar.$element,
2328 * contentFrame.$element.append( $area )
2329 * );
2330 * $( 'body' ).append( frame.$element );
2331 *
2332 * // Here is where the toolbar is actually built. This must be done after inserting it into the
2333 * // document.
2334 * toolbar.initialize();
2335 * toolbar.emit( 'updateState' );
2336 *
2337 * For more information about how to add tools to a MenuToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
2338 * For more information about toolbars in general, please see the [OOUI documentation on MediaWiki] [1].
2339 *
2340 * [1]: https://www.mediawiki.org/wiki/OOUI/Toolbars
2341 *
2342 * @class
2343 * @extends OO.ui.PopupToolGroup
2344 *
2345 * @constructor
2346 * @param {OO.ui.Toolbar} toolbar
2347 * @param {Object} [config] Configuration options
2348 */
2349 OO.ui.MenuToolGroup = function OoUiMenuToolGroup( toolbar, config ) {
2350 // Allow passing positional parameters inside the config object
2351 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
2352 config = toolbar;
2353 toolbar = config.toolbar;
2354 }
2355
2356 // Configuration initialization
2357 config = config || {};
2358
2359 // Parent constructor
2360 OO.ui.MenuToolGroup.parent.call( this, toolbar, config );
2361
2362 // Events
2363 this.toolbar.connect( this, { updateState: 'onUpdateState' } );
2364
2365 // Initialization
2366 this.$element.addClass( 'oo-ui-menuToolGroup' );
2367 };
2368
2369 /* Setup */
2370
2371 OO.inheritClass( OO.ui.MenuToolGroup, OO.ui.PopupToolGroup );
2372
2373 /* Static Properties */
2374
2375 /**
2376 * @static
2377 * @inheritdoc
2378 */
2379 OO.ui.MenuToolGroup.static.name = 'menu';
2380
2381 /* Methods */
2382
2383 /**
2384 * Handle the toolbar state being updated.
2385 *
2386 * When the state changes, the title of each active item in the menu will be joined together and
2387 * used as a label for the group. The label will be empty if none of the items are active.
2388 *
2389 * @private
2390 */
2391 OO.ui.MenuToolGroup.prototype.onUpdateState = function () {
2392 var name,
2393 labelTexts = [];
2394
2395 for ( name in this.tools ) {
2396 if ( this.tools[ name ].isActive() ) {
2397 labelTexts.push( this.tools[ name ].getTitle() );
2398 }
2399 }
2400
2401 this.setLabel( labelTexts.join( ', ' ) || ' ' );
2402 };
2403
2404 }( OO ) );
2405
2406 //# sourceMappingURL=oojs-ui-toolbars.js.map