Merge "Align "What's this" vertically"
[lhc/web/wiklou.git] / resources / lib / oojs-ui / oojs-ui-toolbars.js
1 /*!
2 * OOjs UI v0.23.0
3 * https://www.mediawiki.org/wiki/OOjs_UI
4 *
5 * Copyright 2011–2017 OOjs UI Team and other contributors.
6 * Released under the MIT license
7 * http://oojs.mit-license.org
8 *
9 * Date: 2017-09-05T21:23:58Z
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.width() <= 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.width() + this.$actions.width();
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 OOjs UI 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 [OOjs UI documentation on MediaWiki][1].
564 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/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 if ( this.active ) {
794 this.$element.addClass( 'oo-ui-tool-active' );
795 this.setFlags( { progressive: true } );
796 } else {
797 this.$element.removeClass( 'oo-ui-tool-active' );
798 this.setFlags( { progressive: false } );
799 }
800 };
801
802 /**
803 * Set the tool #title.
804 *
805 * @param {string|Function} title Title text or a function that returns text
806 * @chainable
807 */
808 OO.ui.Tool.prototype.setTitle = function ( title ) {
809 this.title = OO.ui.resolveMsg( title );
810 this.updateTitle();
811 return this;
812 };
813
814 /**
815 * Get the tool #title.
816 *
817 * @return {string} Title text
818 */
819 OO.ui.Tool.prototype.getTitle = function () {
820 return this.title;
821 };
822
823 /**
824 * Get the tool's symbolic name.
825 *
826 * @return {string} Symbolic name of tool
827 */
828 OO.ui.Tool.prototype.getName = function () {
829 return this.constructor.static.name;
830 };
831
832 /**
833 * Update the title.
834 */
835 OO.ui.Tool.prototype.updateTitle = function () {
836 var titleTooltips = this.toolGroup.constructor.static.titleTooltips,
837 accelTooltips = this.toolGroup.constructor.static.accelTooltips,
838 accel = this.toolbar.getToolAccelerator( this.constructor.static.name ),
839 tooltipParts = [];
840
841 this.$title.text( this.title );
842 this.$accel.text( accel );
843
844 if ( titleTooltips && typeof this.title === 'string' && this.title.length ) {
845 tooltipParts.push( this.title );
846 }
847 if ( accelTooltips && typeof accel === 'string' && accel.length ) {
848 tooltipParts.push( accel );
849 }
850 if ( tooltipParts.length ) {
851 this.$link.attr( 'title', tooltipParts.join( ' ' ) );
852 } else {
853 this.$link.removeAttr( 'title' );
854 }
855 };
856
857 /**
858 * Destroy tool.
859 *
860 * Destroying the tool removes all event handlers and the tool’s DOM elements.
861 * Call this method whenever you are done using a tool.
862 */
863 OO.ui.Tool.prototype.destroy = function () {
864 this.toolbar.disconnect( this );
865 this.$element.remove();
866 };
867
868 /**
869 * ToolGroups are collections of {@link OO.ui.Tool tools} that are used in a {@link OO.ui.Toolbar toolbar}.
870 * The type of toolgroup ({@link OO.ui.ListToolGroup list}, {@link OO.ui.BarToolGroup bar}, or {@link OO.ui.MenuToolGroup menu})
871 * to which a tool belongs determines how the tool is arranged and displayed in the toolbar. Toolgroups
872 * themselves are created on demand with a {@link OO.ui.ToolGroupFactory toolgroup factory}.
873 *
874 * Toolgroups can contain individual tools, groups of tools, or all available tools, as specified
875 * using the `include` config option. See OO.ui.ToolFactory#extract on documentation of the format.
876 * The options `exclude`, `promote`, and `demote` support the same formats.
877 *
878 * See {@link OO.ui.Toolbar toolbars} for a full example. For more information about toolbars in general,
879 * please see the [OOjs UI documentation on MediaWiki][1].
880 *
881 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
882 *
883 * @abstract
884 * @class
885 * @extends OO.ui.Widget
886 * @mixins OO.ui.mixin.GroupElement
887 *
888 * @constructor
889 * @param {OO.ui.Toolbar} toolbar
890 * @param {Object} [config] Configuration options
891 * @cfg {Array|string} [include] List of tools to include in the toolgroup, see above.
892 * @cfg {Array|string} [exclude] List of tools to exclude from the toolgroup, see above.
893 * @cfg {Array|string} [promote] List of tools to promote to the beginning of the toolgroup, see above.
894 * @cfg {Array|string} [demote] List of tools to demote to the end of the toolgroup, see above.
895 * This setting is particularly useful when tools have been added to the toolgroup
896 * en masse (e.g., via the catch-all selector).
897 */
898 OO.ui.ToolGroup = function OoUiToolGroup( toolbar, config ) {
899 // Allow passing positional parameters inside the config object
900 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
901 config = toolbar;
902 toolbar = config.toolbar;
903 }
904
905 // Configuration initialization
906 config = config || {};
907
908 // Parent constructor
909 OO.ui.ToolGroup.parent.call( this, config );
910
911 // Mixin constructors
912 OO.ui.mixin.GroupElement.call( this, config );
913
914 // Properties
915 this.toolbar = toolbar;
916 this.tools = {};
917 this.pressed = null;
918 this.autoDisabled = false;
919 this.include = config.include || [];
920 this.exclude = config.exclude || [];
921 this.promote = config.promote || [];
922 this.demote = config.demote || [];
923 this.onCapturedMouseKeyUpHandler = this.onCapturedMouseKeyUp.bind( this );
924
925 // Events
926 this.$element.on( {
927 mousedown: this.onMouseKeyDown.bind( this ),
928 mouseup: this.onMouseKeyUp.bind( this ),
929 keydown: this.onMouseKeyDown.bind( this ),
930 keyup: this.onMouseKeyUp.bind( this ),
931 focus: this.onMouseOverFocus.bind( this ),
932 blur: this.onMouseOutBlur.bind( this ),
933 mouseover: this.onMouseOverFocus.bind( this ),
934 mouseout: this.onMouseOutBlur.bind( this )
935 } );
936 this.toolbar.getToolFactory().connect( this, { register: 'onToolFactoryRegister' } );
937 this.aggregate( { disable: 'itemDisable' } );
938 this.connect( this, { itemDisable: 'updateDisabled' } );
939
940 // Initialization
941 this.$group.addClass( 'oo-ui-toolGroup-tools' );
942 this.$element
943 .addClass( 'oo-ui-toolGroup' )
944 .append( this.$group );
945 this.populate();
946 };
947
948 /* Setup */
949
950 OO.inheritClass( OO.ui.ToolGroup, OO.ui.Widget );
951 OO.mixinClass( OO.ui.ToolGroup, OO.ui.mixin.GroupElement );
952
953 /* Events */
954
955 /**
956 * @event update
957 */
958
959 /* Static Properties */
960
961 /**
962 * Show labels in tooltips.
963 *
964 * @static
965 * @inheritable
966 * @property {boolean}
967 */
968 OO.ui.ToolGroup.static.titleTooltips = false;
969
970 /**
971 * Show acceleration labels in tooltips.
972 *
973 * Note: The OOjs UI library does not include an accelerator system, but does contain
974 * a hook for one. To use an accelerator system, subclass the {@link OO.ui.Toolbar toolbar} and
975 * override the {@link OO.ui.Toolbar#getToolAccelerator getToolAccelerator} method, which is
976 * meant to return a label that describes the accelerator keys for a given tool (e.g., 'Ctrl + M').
977 *
978 * @static
979 * @inheritable
980 * @property {boolean}
981 */
982 OO.ui.ToolGroup.static.accelTooltips = false;
983
984 /**
985 * Automatically disable the toolgroup when all tools are disabled
986 *
987 * @static
988 * @inheritable
989 * @property {boolean}
990 */
991 OO.ui.ToolGroup.static.autoDisable = true;
992
993 /**
994 * @abstract
995 * @static
996 * @inheritable
997 * @property {string}
998 */
999 OO.ui.ToolGroup.static.name = null;
1000
1001 /* Methods */
1002
1003 /**
1004 * @inheritdoc
1005 */
1006 OO.ui.ToolGroup.prototype.isDisabled = function () {
1007 return this.autoDisabled || OO.ui.ToolGroup.parent.prototype.isDisabled.apply( this, arguments );
1008 };
1009
1010 /**
1011 * @inheritdoc
1012 */
1013 OO.ui.ToolGroup.prototype.updateDisabled = function () {
1014 var i, item, allDisabled = true;
1015
1016 if ( this.constructor.static.autoDisable ) {
1017 for ( i = this.items.length - 1; i >= 0; i-- ) {
1018 item = this.items[ i ];
1019 if ( !item.isDisabled() ) {
1020 allDisabled = false;
1021 break;
1022 }
1023 }
1024 this.autoDisabled = allDisabled;
1025 }
1026 OO.ui.ToolGroup.parent.prototype.updateDisabled.apply( this, arguments );
1027 };
1028
1029 /**
1030 * Handle mouse down and key down events.
1031 *
1032 * @protected
1033 * @param {jQuery.Event} e Mouse down or key down event
1034 */
1035 OO.ui.ToolGroup.prototype.onMouseKeyDown = function ( e ) {
1036 if (
1037 !this.isDisabled() &&
1038 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1039 ) {
1040 this.pressed = this.findTargetTool( e );
1041 if ( this.pressed ) {
1042 this.pressed.setActive( true );
1043 this.getElementDocument().addEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler, true );
1044 this.getElementDocument().addEventListener( 'keyup', this.onCapturedMouseKeyUpHandler, true );
1045 return false;
1046 }
1047 }
1048 };
1049
1050 /**
1051 * Handle captured mouse up and key up events.
1052 *
1053 * @protected
1054 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1055 */
1056 OO.ui.ToolGroup.prototype.onCapturedMouseKeyUp = function ( e ) {
1057 this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseKeyUpHandler, true );
1058 this.getElementDocument().removeEventListener( 'keyup', this.onCapturedMouseKeyUpHandler, true );
1059 // onMouseKeyUp may be called a second time, depending on where the mouse is when the button is
1060 // released, but since `this.pressed` will no longer be true, the second call will be ignored.
1061 this.onMouseKeyUp( e );
1062 };
1063
1064 /**
1065 * Handle mouse up and key up events.
1066 *
1067 * @protected
1068 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1069 */
1070 OO.ui.ToolGroup.prototype.onMouseKeyUp = function ( e ) {
1071 var tool = this.findTargetTool( e );
1072
1073 if (
1074 !this.isDisabled() && this.pressed && this.pressed === tool &&
1075 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1076 ) {
1077 this.pressed.onSelect();
1078 this.pressed = null;
1079 e.preventDefault();
1080 e.stopPropagation();
1081 }
1082
1083 this.pressed = null;
1084 };
1085
1086 /**
1087 * Handle mouse over and focus events.
1088 *
1089 * @protected
1090 * @param {jQuery.Event} e Mouse over or focus event
1091 */
1092 OO.ui.ToolGroup.prototype.onMouseOverFocus = function ( e ) {
1093 var tool = this.findTargetTool( e );
1094
1095 if ( this.pressed && this.pressed === tool ) {
1096 this.pressed.setActive( true );
1097 }
1098 };
1099
1100 /**
1101 * Handle mouse out and blur events.
1102 *
1103 * @protected
1104 * @param {jQuery.Event} e Mouse out or blur event
1105 */
1106 OO.ui.ToolGroup.prototype.onMouseOutBlur = function ( e ) {
1107 var tool = this.findTargetTool( e );
1108
1109 if ( this.pressed && this.pressed === tool ) {
1110 this.pressed.setActive( false );
1111 }
1112 };
1113
1114 /**
1115 * Get the closest tool to a jQuery.Event.
1116 *
1117 * Only tool links are considered, which prevents other elements in the tool such as popups from
1118 * triggering tool group interactions.
1119 *
1120 * @private
1121 * @param {jQuery.Event} e
1122 * @return {OO.ui.Tool|null} Tool, `null` if none was found
1123 */
1124 OO.ui.ToolGroup.prototype.findTargetTool = function ( e ) {
1125 var tool,
1126 $item = $( e.target ).closest( '.oo-ui-tool-link' );
1127
1128 if ( $item.length ) {
1129 tool = $item.parent().data( 'oo-ui-tool' );
1130 }
1131
1132 return tool && !tool.isDisabled() ? tool : null;
1133 };
1134
1135 /**
1136 * Handle tool registry register events.
1137 *
1138 * If a tool is registered after the group is created, we must repopulate the list to account for:
1139 *
1140 * - a tool being added that may be included
1141 * - a tool already included being overridden
1142 *
1143 * @protected
1144 * @param {string} name Symbolic name of tool
1145 */
1146 OO.ui.ToolGroup.prototype.onToolFactoryRegister = function () {
1147 this.populate();
1148 };
1149
1150 /**
1151 * Get the toolbar that contains the toolgroup.
1152 *
1153 * @return {OO.ui.Toolbar} Toolbar that contains the toolgroup
1154 */
1155 OO.ui.ToolGroup.prototype.getToolbar = function () {
1156 return this.toolbar;
1157 };
1158
1159 /**
1160 * Add and remove tools based on configuration.
1161 */
1162 OO.ui.ToolGroup.prototype.populate = function () {
1163 var i, len, name, tool,
1164 toolFactory = this.toolbar.getToolFactory(),
1165 names = {},
1166 add = [],
1167 remove = [],
1168 list = this.toolbar.getToolFactory().getTools(
1169 this.include, this.exclude, this.promote, this.demote
1170 );
1171
1172 // Build a list of needed tools
1173 for ( i = 0, len = list.length; i < len; i++ ) {
1174 name = list[ i ];
1175 if (
1176 // Tool exists
1177 toolFactory.lookup( name ) &&
1178 // Tool is available or is already in this group
1179 ( this.toolbar.isToolAvailable( name ) || this.tools[ name ] )
1180 ) {
1181 // Hack to prevent infinite recursion via ToolGroupTool. We need to reserve the tool before
1182 // creating it, but we can't call reserveTool() yet because we haven't created the tool.
1183 this.toolbar.tools[ name ] = true;
1184 tool = this.tools[ name ];
1185 if ( !tool ) {
1186 // Auto-initialize tools on first use
1187 this.tools[ name ] = tool = toolFactory.create( name, this );
1188 tool.updateTitle();
1189 }
1190 this.toolbar.reserveTool( tool );
1191 add.push( tool );
1192 names[ name ] = true;
1193 }
1194 }
1195 // Remove tools that are no longer needed
1196 for ( name in this.tools ) {
1197 if ( !names[ name ] ) {
1198 this.tools[ name ].destroy();
1199 this.toolbar.releaseTool( this.tools[ name ] );
1200 remove.push( this.tools[ name ] );
1201 delete this.tools[ name ];
1202 }
1203 }
1204 if ( remove.length ) {
1205 this.removeItems( remove );
1206 }
1207 // Update emptiness state
1208 if ( add.length ) {
1209 this.$element.removeClass( 'oo-ui-toolGroup-empty' );
1210 } else {
1211 this.$element.addClass( 'oo-ui-toolGroup-empty' );
1212 }
1213 // Re-add tools (moving existing ones to new locations)
1214 this.addItems( add );
1215 // Disabled state may depend on items
1216 this.updateDisabled();
1217 };
1218
1219 /**
1220 * Destroy toolgroup.
1221 */
1222 OO.ui.ToolGroup.prototype.destroy = function () {
1223 var name;
1224
1225 this.clearItems();
1226 this.toolbar.getToolFactory().disconnect( this );
1227 for ( name in this.tools ) {
1228 this.toolbar.releaseTool( this.tools[ name ] );
1229 this.tools[ name ].disconnect( this ).destroy();
1230 delete this.tools[ name ];
1231 }
1232 this.$element.remove();
1233 };
1234
1235 /**
1236 * A ToolFactory creates tools on demand. All tools ({@link OO.ui.Tool Tools}, {@link OO.ui.PopupTool PopupTools},
1237 * and {@link OO.ui.ToolGroupTool ToolGroupTools}) must be registered with a tool factory. Tools are
1238 * registered by their symbolic name. See {@link OO.ui.Toolbar toolbars} for an example.
1239 *
1240 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
1241 *
1242 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1243 *
1244 * @class
1245 * @extends OO.Factory
1246 * @constructor
1247 */
1248 OO.ui.ToolFactory = function OoUiToolFactory() {
1249 // Parent constructor
1250 OO.ui.ToolFactory.parent.call( this );
1251 };
1252
1253 /* Setup */
1254
1255 OO.inheritClass( OO.ui.ToolFactory, OO.Factory );
1256
1257 /* Methods */
1258
1259 /**
1260 * Get tools from the factory
1261 *
1262 * @param {Array|string} [include] Included tools, see #extract for format
1263 * @param {Array|string} [exclude] Excluded tools, see #extract for format
1264 * @param {Array|string} [promote] Promoted tools, see #extract for format
1265 * @param {Array|string} [demote] Demoted tools, see #extract for format
1266 * @return {string[]} List of tools
1267 */
1268 OO.ui.ToolFactory.prototype.getTools = function ( include, exclude, promote, demote ) {
1269 var i, len, included, promoted, demoted,
1270 auto = [],
1271 used = {};
1272
1273 // Collect included and not excluded tools
1274 included = OO.simpleArrayDifference( this.extract( include ), this.extract( exclude ) );
1275
1276 // Promotion
1277 promoted = this.extract( promote, used );
1278 demoted = this.extract( demote, used );
1279
1280 // Auto
1281 for ( i = 0, len = included.length; i < len; i++ ) {
1282 if ( !used[ included[ i ] ] ) {
1283 auto.push( included[ i ] );
1284 }
1285 }
1286
1287 return promoted.concat( auto ).concat( demoted );
1288 };
1289
1290 /**
1291 * Get a flat list of names from a list of names or groups.
1292 *
1293 * Normally, `collection` is an array of tool specifications. Tools can be specified in the
1294 * following ways:
1295 *
1296 * - To include an individual tool, use the symbolic name: `{ name: 'tool-name' }` or `'tool-name'`.
1297 * - To include all tools in a group, use the group name: `{ group: 'group-name' }`. (To assign the
1298 * tool to a group, use OO.ui.Tool.static.group.)
1299 *
1300 * Alternatively, to include all tools that are not yet assigned to any other toolgroup, use the
1301 * catch-all selector `'*'`.
1302 *
1303 * If `used` is passed, tool names that appear as properties in this object will be considered
1304 * already assigned, and will not be returned even if specified otherwise. The tool names extracted
1305 * by this function call will be added as new properties in the object.
1306 *
1307 * @private
1308 * @param {Array|string} collection List of tools, see above
1309 * @param {Object} [used] Object containing information about used tools, see above
1310 * @return {string[]} List of extracted tool names
1311 */
1312 OO.ui.ToolFactory.prototype.extract = function ( collection, used ) {
1313 var i, len, item, name, tool,
1314 names = [];
1315
1316 collection = !Array.isArray( collection ) ? [ collection ] : collection;
1317
1318 for ( i = 0, len = collection.length; i < len; i++ ) {
1319 item = collection[ i ];
1320 if ( item === '*' ) {
1321 for ( name in this.registry ) {
1322 tool = this.registry[ name ];
1323 if (
1324 // Only add tools by group name when auto-add is enabled
1325 tool.static.autoAddToCatchall &&
1326 // Exclude already used tools
1327 ( !used || !used[ name ] )
1328 ) {
1329 names.push( name );
1330 if ( used ) {
1331 used[ name ] = true;
1332 }
1333 }
1334 }
1335 } else {
1336 // Allow plain strings as shorthand for named tools
1337 if ( typeof item === 'string' ) {
1338 item = { name: item };
1339 }
1340 if ( OO.isPlainObject( item ) ) {
1341 if ( item.group ) {
1342 for ( name in this.registry ) {
1343 tool = this.registry[ name ];
1344 if (
1345 // Include tools with matching group
1346 tool.static.group === item.group &&
1347 // Only add tools by group name when auto-add is enabled
1348 tool.static.autoAddToGroup &&
1349 // Exclude already used tools
1350 ( !used || !used[ name ] )
1351 ) {
1352 names.push( name );
1353 if ( used ) {
1354 used[ name ] = true;
1355 }
1356 }
1357 }
1358 // Include tools with matching name and exclude already used tools
1359 } else if ( item.name && ( !used || !used[ item.name ] ) ) {
1360 names.push( item.name );
1361 if ( used ) {
1362 used[ item.name ] = true;
1363 }
1364 }
1365 }
1366 }
1367 }
1368 return names;
1369 };
1370
1371 /**
1372 * ToolGroupFactories create {@link OO.ui.ToolGroup toolgroups} on demand. The toolgroup classes must
1373 * specify a symbolic name and be registered with the factory. The following classes are registered by
1374 * default:
1375 *
1376 * - {@link OO.ui.BarToolGroup BarToolGroups} (‘bar’)
1377 * - {@link OO.ui.MenuToolGroup MenuToolGroups} (‘menu’)
1378 * - {@link OO.ui.ListToolGroup ListToolGroups} (‘list’)
1379 *
1380 * See {@link OO.ui.Toolbar toolbars} for an example.
1381 *
1382 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
1383 *
1384 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1385 *
1386 * @class
1387 * @extends OO.Factory
1388 * @constructor
1389 */
1390 OO.ui.ToolGroupFactory = function OoUiToolGroupFactory() {
1391 var i, l, defaultClasses;
1392 // Parent constructor
1393 OO.Factory.call( this );
1394
1395 defaultClasses = this.constructor.static.getDefaultClasses();
1396
1397 // Register default toolgroups
1398 for ( i = 0, l = defaultClasses.length; i < l; i++ ) {
1399 this.register( defaultClasses[ i ] );
1400 }
1401 };
1402
1403 /* Setup */
1404
1405 OO.inheritClass( OO.ui.ToolGroupFactory, OO.Factory );
1406
1407 /* Static Methods */
1408
1409 /**
1410 * Get a default set of classes to be registered on construction.
1411 *
1412 * @return {Function[]} Default classes
1413 */
1414 OO.ui.ToolGroupFactory.static.getDefaultClasses = function () {
1415 return [
1416 OO.ui.BarToolGroup,
1417 OO.ui.ListToolGroup,
1418 OO.ui.MenuToolGroup
1419 ];
1420 };
1421
1422 /**
1423 * Popup tools open a popup window when they are selected from the {@link OO.ui.Toolbar toolbar}. Each popup tool is configured
1424 * 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
1425 * an #onSelect or #onUpdateState method, as these methods have been implemented already.
1426 *
1427 * // Example of a popup tool. When selected, a popup tool displays
1428 * // a popup window.
1429 * function HelpTool( toolGroup, config ) {
1430 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
1431 * padded: true,
1432 * label: 'Help',
1433 * head: true
1434 * } }, config ) );
1435 * this.popup.$body.append( '<p>I am helpful!</p>' );
1436 * };
1437 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
1438 * HelpTool.static.name = 'help';
1439 * HelpTool.static.icon = 'help';
1440 * HelpTool.static.title = 'Help';
1441 * toolFactory.register( HelpTool );
1442 *
1443 * For an example of a toolbar that contains a popup tool, see {@link OO.ui.Toolbar toolbars}. For more information about
1444 * toolbars in genreral, please see the [OOjs UI documentation on MediaWiki][1].
1445 *
1446 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1447 *
1448 * @abstract
1449 * @class
1450 * @extends OO.ui.Tool
1451 * @mixins OO.ui.mixin.PopupElement
1452 *
1453 * @constructor
1454 * @param {OO.ui.ToolGroup} toolGroup
1455 * @param {Object} [config] Configuration options
1456 */
1457 OO.ui.PopupTool = function OoUiPopupTool( toolGroup, config ) {
1458 // Allow passing positional parameters inside the config object
1459 if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
1460 config = toolGroup;
1461 toolGroup = config.toolGroup;
1462 }
1463
1464 // Parent constructor
1465 OO.ui.PopupTool.parent.call( this, toolGroup, config );
1466
1467 // Mixin constructors
1468 OO.ui.mixin.PopupElement.call( this, config );
1469
1470 // Initialization
1471 this.popup.setPosition( toolGroup.getToolbar().position === 'bottom' ? 'above' : 'below' );
1472 this.$element
1473 .addClass( 'oo-ui-popupTool' )
1474 .append( this.popup.$element );
1475 };
1476
1477 /* Setup */
1478
1479 OO.inheritClass( OO.ui.PopupTool, OO.ui.Tool );
1480 OO.mixinClass( OO.ui.PopupTool, OO.ui.mixin.PopupElement );
1481
1482 /* Methods */
1483
1484 /**
1485 * Handle the tool being selected.
1486 *
1487 * @inheritdoc
1488 */
1489 OO.ui.PopupTool.prototype.onSelect = function () {
1490 if ( !this.isDisabled() ) {
1491 this.popup.toggle();
1492 }
1493 this.setActive( false );
1494 return false;
1495 };
1496
1497 /**
1498 * Handle the toolbar state being updated.
1499 *
1500 * @inheritdoc
1501 */
1502 OO.ui.PopupTool.prototype.onUpdateState = function () {
1503 this.setActive( false );
1504 };
1505
1506 /**
1507 * A ToolGroupTool is a special sort of tool that can contain other {@link OO.ui.Tool tools}
1508 * and {@link OO.ui.ToolGroup toolgroups}. The ToolGroupTool was specifically designed to be used
1509 * inside a {@link OO.ui.BarToolGroup bar} toolgroup to provide access to additional tools from
1510 * the bar item. Included tools will be displayed in a dropdown {@link OO.ui.ListToolGroup list}
1511 * when the ToolGroupTool is selected.
1512 *
1513 * // Example: ToolGroupTool with two nested tools, 'setting1' and 'setting2', defined elsewhere.
1514 *
1515 * function SettingsTool() {
1516 * SettingsTool.parent.apply( this, arguments );
1517 * };
1518 * OO.inheritClass( SettingsTool, OO.ui.ToolGroupTool );
1519 * SettingsTool.static.name = 'settings';
1520 * SettingsTool.static.title = 'Change settings';
1521 * SettingsTool.static.groupConfig = {
1522 * icon: 'settings',
1523 * label: 'ToolGroupTool',
1524 * include: [ 'setting1', 'setting2' ]
1525 * };
1526 * toolFactory.register( SettingsTool );
1527 *
1528 * For more information, please see the [OOjs UI documentation on MediaWiki][1].
1529 *
1530 * Please note that this implementation is subject to change per [T74159] [2].
1531 *
1532 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars#ToolGroupTool
1533 * [2]: https://phabricator.wikimedia.org/T74159
1534 *
1535 * @abstract
1536 * @class
1537 * @extends OO.ui.Tool
1538 *
1539 * @constructor
1540 * @param {OO.ui.ToolGroup} toolGroup
1541 * @param {Object} [config] Configuration options
1542 */
1543 OO.ui.ToolGroupTool = function OoUiToolGroupTool( toolGroup, config ) {
1544 // Allow passing positional parameters inside the config object
1545 if ( OO.isPlainObject( toolGroup ) && config === undefined ) {
1546 config = toolGroup;
1547 toolGroup = config.toolGroup;
1548 }
1549
1550 // Parent constructor
1551 OO.ui.ToolGroupTool.parent.call( this, toolGroup, config );
1552
1553 // Properties
1554 this.innerToolGroup = this.createGroup( this.constructor.static.groupConfig );
1555
1556 // Events
1557 this.innerToolGroup.connect( this, { disable: 'onToolGroupDisable' } );
1558
1559 // Initialization
1560 this.$link.remove();
1561 this.$element
1562 .addClass( 'oo-ui-toolGroupTool' )
1563 .append( this.innerToolGroup.$element );
1564 };
1565
1566 /* Setup */
1567
1568 OO.inheritClass( OO.ui.ToolGroupTool, OO.ui.Tool );
1569
1570 /* Static Properties */
1571
1572 /**
1573 * Toolgroup configuration.
1574 *
1575 * The toolgroup configuration consists of the tools to include, as well as an icon and label
1576 * to use for the bar item. Tools can be included by symbolic name, group, or with the
1577 * wildcard selector. Please see {@link OO.ui.ToolGroup toolgroup} for more information.
1578 *
1579 * @property {Object.<string,Array>}
1580 */
1581 OO.ui.ToolGroupTool.static.groupConfig = {};
1582
1583 /* Methods */
1584
1585 /**
1586 * Handle the tool being selected.
1587 *
1588 * @inheritdoc
1589 */
1590 OO.ui.ToolGroupTool.prototype.onSelect = function () {
1591 this.innerToolGroup.setActive( !this.innerToolGroup.active );
1592 return false;
1593 };
1594
1595 /**
1596 * Synchronize disabledness state of the tool with the inner toolgroup.
1597 *
1598 * @private
1599 * @param {boolean} disabled Element is disabled
1600 */
1601 OO.ui.ToolGroupTool.prototype.onToolGroupDisable = function ( disabled ) {
1602 this.setDisabled( disabled );
1603 };
1604
1605 /**
1606 * Handle the toolbar state being updated.
1607 *
1608 * @inheritdoc
1609 */
1610 OO.ui.ToolGroupTool.prototype.onUpdateState = function () {
1611 this.setActive( false );
1612 };
1613
1614 /**
1615 * Build a {@link OO.ui.ToolGroup toolgroup} from the specified configuration.
1616 *
1617 * @param {Object.<string,Array>} group Toolgroup configuration. Please see {@link OO.ui.ToolGroup toolgroup} for
1618 * more information.
1619 * @return {OO.ui.ListToolGroup}
1620 */
1621 OO.ui.ToolGroupTool.prototype.createGroup = function ( group ) {
1622 if ( group.include === '*' ) {
1623 // Apply defaults to catch-all groups
1624 if ( group.label === undefined ) {
1625 group.label = OO.ui.msg( 'ooui-toolbar-more' );
1626 }
1627 }
1628
1629 return this.toolbar.getToolGroupFactory().create( 'list', this.toolbar, group );
1630 };
1631
1632 /**
1633 * BarToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
1634 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
1635 * and {@link OO.ui.ListToolGroup ListToolGroup}). The {@link OO.ui.Tool tools} in a BarToolGroup are
1636 * displayed by icon in a single row. The title of the tool is displayed when users move the mouse over
1637 * the tool.
1638 *
1639 * BarToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar is
1640 * set up.
1641 *
1642 * @example
1643 * // Example of a BarToolGroup with two tools
1644 * var toolFactory = new OO.ui.ToolFactory();
1645 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
1646 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
1647 *
1648 * // We will be placing status text in this element when tools are used
1649 * var $area = $( '<p>' ).text( 'Example of a BarToolGroup with two tools.' );
1650 *
1651 * // Define the tools that we're going to place in our toolbar
1652 *
1653 * // Create a class inheriting from OO.ui.Tool
1654 * function SearchTool() {
1655 * SearchTool.parent.apply( this, arguments );
1656 * }
1657 * OO.inheritClass( SearchTool, OO.ui.Tool );
1658 * // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
1659 * // of 'icon' and 'title' (displayed icon and text).
1660 * SearchTool.static.name = 'search';
1661 * SearchTool.static.icon = 'search';
1662 * SearchTool.static.title = 'Search...';
1663 * // Defines the action that will happen when this tool is selected (clicked).
1664 * SearchTool.prototype.onSelect = function () {
1665 * $area.text( 'Search tool clicked!' );
1666 * // Never display this tool as "active" (selected).
1667 * this.setActive( false );
1668 * };
1669 * SearchTool.prototype.onUpdateState = function () {};
1670 * // Make this tool available in our toolFactory and thus our toolbar
1671 * toolFactory.register( SearchTool );
1672 *
1673 * // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
1674 * // little popup window (a PopupWidget).
1675 * function HelpTool( toolGroup, config ) {
1676 * OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
1677 * padded: true,
1678 * label: 'Help',
1679 * head: true
1680 * } }, config ) );
1681 * this.popup.$body.append( '<p>I am helpful!</p>' );
1682 * }
1683 * OO.inheritClass( HelpTool, OO.ui.PopupTool );
1684 * HelpTool.static.name = 'help';
1685 * HelpTool.static.icon = 'help';
1686 * HelpTool.static.title = 'Help';
1687 * toolFactory.register( HelpTool );
1688 *
1689 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
1690 * // used once (but not all defined tools must be used).
1691 * toolbar.setup( [
1692 * {
1693 * // 'bar' tool groups display tools by icon only
1694 * type: 'bar',
1695 * include: [ 'search', 'help' ]
1696 * }
1697 * ] );
1698 *
1699 * // Create some UI around the toolbar and place it in the document
1700 * var frame = new OO.ui.PanelLayout( {
1701 * expanded: false,
1702 * framed: true
1703 * } );
1704 * var contentFrame = new OO.ui.PanelLayout( {
1705 * expanded: false,
1706 * padded: true
1707 * } );
1708 * frame.$element.append(
1709 * toolbar.$element,
1710 * contentFrame.$element.append( $area )
1711 * );
1712 * $( 'body' ).append( frame.$element );
1713 *
1714 * // Here is where the toolbar is actually built. This must be done after inserting it into the
1715 * // document.
1716 * toolbar.initialize();
1717 *
1718 * For more information about how to add tools to a bar tool group, please see {@link OO.ui.ToolGroup toolgroup}.
1719 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
1720 *
1721 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
1722 *
1723 * @class
1724 * @extends OO.ui.ToolGroup
1725 *
1726 * @constructor
1727 * @param {OO.ui.Toolbar} toolbar
1728 * @param {Object} [config] Configuration options
1729 */
1730 OO.ui.BarToolGroup = function OoUiBarToolGroup( toolbar, config ) {
1731 // Allow passing positional parameters inside the config object
1732 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
1733 config = toolbar;
1734 toolbar = config.toolbar;
1735 }
1736
1737 // Parent constructor
1738 OO.ui.BarToolGroup.parent.call( this, toolbar, config );
1739
1740 // Initialization
1741 this.$element.addClass( 'oo-ui-barToolGroup' );
1742 };
1743
1744 /* Setup */
1745
1746 OO.inheritClass( OO.ui.BarToolGroup, OO.ui.ToolGroup );
1747
1748 /* Static Properties */
1749
1750 /**
1751 * @static
1752 * @inheritdoc
1753 */
1754 OO.ui.BarToolGroup.static.titleTooltips = true;
1755
1756 /**
1757 * @static
1758 * @inheritdoc
1759 */
1760 OO.ui.BarToolGroup.static.accelTooltips = true;
1761
1762 /**
1763 * @static
1764 * @inheritdoc
1765 */
1766 OO.ui.BarToolGroup.static.name = 'bar';
1767
1768 /**
1769 * PopupToolGroup is an abstract base class used by both {@link OO.ui.MenuToolGroup MenuToolGroup}
1770 * and {@link OO.ui.ListToolGroup ListToolGroup} to provide a popup--an overlaid menu or list of tools with an
1771 * optional icon and label. This class can be used for other base classes that also use this functionality.
1772 *
1773 * @abstract
1774 * @class
1775 * @extends OO.ui.ToolGroup
1776 * @mixins OO.ui.mixin.IconElement
1777 * @mixins OO.ui.mixin.IndicatorElement
1778 * @mixins OO.ui.mixin.LabelElement
1779 * @mixins OO.ui.mixin.TitledElement
1780 * @mixins OO.ui.mixin.FlaggedElement
1781 * @mixins OO.ui.mixin.ClippableElement
1782 * @mixins OO.ui.mixin.TabIndexedElement
1783 *
1784 * @constructor
1785 * @param {OO.ui.Toolbar} toolbar
1786 * @param {Object} [config] Configuration options
1787 * @cfg {string} [header] Text to display at the top of the popup
1788 */
1789 OO.ui.PopupToolGroup = function OoUiPopupToolGroup( toolbar, config ) {
1790 // Allow passing positional parameters inside the config object
1791 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
1792 config = toolbar;
1793 toolbar = config.toolbar;
1794 }
1795
1796 // Configuration initialization
1797 config = $.extend( {
1798 indicator: config.indicator === undefined ? ( toolbar.position === 'bottom' ? 'up' : 'down' ) : config.indicator
1799 }, config );
1800
1801 // Parent constructor
1802 OO.ui.PopupToolGroup.parent.call( this, toolbar, config );
1803
1804 // Properties
1805 this.active = false;
1806 this.dragging = false;
1807 this.onBlurHandler = this.onBlur.bind( this );
1808 this.$handle = $( '<span>' );
1809
1810 // Mixin constructors
1811 OO.ui.mixin.IconElement.call( this, config );
1812 OO.ui.mixin.IndicatorElement.call( this, config );
1813 OO.ui.mixin.LabelElement.call( this, config );
1814 OO.ui.mixin.TitledElement.call( this, config );
1815 OO.ui.mixin.FlaggedElement.call( this, config );
1816 OO.ui.mixin.ClippableElement.call( this, $.extend( {}, config, { $clippable: this.$group } ) );
1817 OO.ui.mixin.TabIndexedElement.call( this, $.extend( {}, config, { $tabIndexed: this.$handle } ) );
1818
1819 // Events
1820 this.$handle.on( {
1821 keydown: this.onHandleMouseKeyDown.bind( this ),
1822 keyup: this.onHandleMouseKeyUp.bind( this ),
1823 mousedown: this.onHandleMouseKeyDown.bind( this ),
1824 mouseup: this.onHandleMouseKeyUp.bind( this )
1825 } );
1826
1827 // Initialization
1828 this.$handle
1829 .addClass( 'oo-ui-popupToolGroup-handle' )
1830 .attr( 'role', 'button' )
1831 .append( this.$icon, this.$label, this.$indicator );
1832 // If the pop-up should have a header, add it to the top of the toolGroup.
1833 // Note: If this feature is useful for other widgets, we could abstract it into an
1834 // OO.ui.HeaderedElement mixin constructor.
1835 if ( config.header !== undefined ) {
1836 this.$group
1837 .prepend( $( '<span>' )
1838 .addClass( 'oo-ui-popupToolGroup-header' )
1839 .text( config.header )
1840 );
1841 }
1842 this.$element
1843 .addClass( 'oo-ui-popupToolGroup' )
1844 .prepend( this.$handle );
1845 };
1846
1847 /* Setup */
1848
1849 OO.inheritClass( OO.ui.PopupToolGroup, OO.ui.ToolGroup );
1850 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IconElement );
1851 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.IndicatorElement );
1852 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.LabelElement );
1853 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TitledElement );
1854 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.FlaggedElement );
1855 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.ClippableElement );
1856 OO.mixinClass( OO.ui.PopupToolGroup, OO.ui.mixin.TabIndexedElement );
1857
1858 /* Methods */
1859
1860 /**
1861 * @inheritdoc
1862 */
1863 OO.ui.PopupToolGroup.prototype.setDisabled = function () {
1864 // Parent method
1865 OO.ui.PopupToolGroup.parent.prototype.setDisabled.apply( this, arguments );
1866
1867 if ( this.isDisabled() && this.isElementAttached() ) {
1868 this.setActive( false );
1869 }
1870 };
1871
1872 /**
1873 * Handle focus being lost.
1874 *
1875 * The event is actually generated from a mouseup/keyup, so it is not a normal blur event object.
1876 *
1877 * @protected
1878 * @param {MouseEvent|KeyboardEvent} e Mouse up or key up event
1879 */
1880 OO.ui.PopupToolGroup.prototype.onBlur = function ( e ) {
1881 // Only deactivate when clicking outside the dropdown element
1882 if ( $( e.target ).closest( '.oo-ui-popupToolGroup' )[ 0 ] !== this.$element[ 0 ] ) {
1883 this.setActive( false );
1884 }
1885 };
1886
1887 /**
1888 * @inheritdoc
1889 */
1890 OO.ui.PopupToolGroup.prototype.onMouseKeyUp = function ( e ) {
1891 // Only close toolgroup when a tool was actually selected
1892 if (
1893 !this.isDisabled() && this.pressed && this.pressed === this.findTargetTool( e ) &&
1894 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1895 ) {
1896 this.setActive( false );
1897 }
1898 return OO.ui.PopupToolGroup.parent.prototype.onMouseKeyUp.call( this, e );
1899 };
1900
1901 /**
1902 * Handle mouse up and key up events.
1903 *
1904 * @protected
1905 * @param {jQuery.Event} e Mouse up or key up event
1906 */
1907 OO.ui.PopupToolGroup.prototype.onHandleMouseKeyUp = function ( e ) {
1908 if (
1909 !this.isDisabled() &&
1910 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1911 ) {
1912 return false;
1913 }
1914 };
1915
1916 /**
1917 * Handle mouse down and key down events.
1918 *
1919 * @protected
1920 * @param {jQuery.Event} e Mouse down or key down event
1921 */
1922 OO.ui.PopupToolGroup.prototype.onHandleMouseKeyDown = function ( e ) {
1923 if (
1924 !this.isDisabled() &&
1925 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
1926 ) {
1927 this.setActive( !this.active );
1928 return false;
1929 }
1930 };
1931
1932 /**
1933 * Switch into 'active' mode.
1934 *
1935 * When active, the popup is visible. A mouseup event anywhere in the document will trigger
1936 * deactivation.
1937 *
1938 * @param {boolean} value The active state to set
1939 */
1940 OO.ui.PopupToolGroup.prototype.setActive = function ( value ) {
1941 var containerWidth, containerLeft;
1942 value = !!value;
1943 if ( this.active !== value ) {
1944 this.active = value;
1945 if ( value ) {
1946 this.getElementDocument().addEventListener( 'mouseup', this.onBlurHandler, true );
1947 this.getElementDocument().addEventListener( 'keyup', this.onBlurHandler, true );
1948
1949 this.$clippable.css( 'left', '' );
1950 // Try anchoring the popup to the left first
1951 this.$element.addClass( 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left' );
1952 this.toggleClipping( true );
1953 if ( this.isClippedHorizontally() ) {
1954 // Anchoring to the left caused the popup to clip, so anchor it to the right instead
1955 this.toggleClipping( false );
1956 this.$element
1957 .removeClass( 'oo-ui-popupToolGroup-left' )
1958 .addClass( 'oo-ui-popupToolGroup-right' );
1959 this.toggleClipping( true );
1960 }
1961 if ( this.isClippedHorizontally() ) {
1962 // Anchoring to the right also caused the popup to clip, so just make it fill the container
1963 containerWidth = this.$clippableScrollableContainer.width();
1964 containerLeft = this.$clippableScrollableContainer.offset().left;
1965
1966 this.toggleClipping( false );
1967 this.$element.removeClass( 'oo-ui-popupToolGroup-right' );
1968
1969 this.$clippable.css( {
1970 left: -( this.$element.offset().left - containerLeft ),
1971 width: containerWidth
1972 } );
1973 }
1974 } else {
1975 this.getElementDocument().removeEventListener( 'mouseup', this.onBlurHandler, true );
1976 this.getElementDocument().removeEventListener( 'keyup', this.onBlurHandler, true );
1977 this.$element.removeClass(
1978 'oo-ui-popupToolGroup-active oo-ui-popupToolGroup-left oo-ui-popupToolGroup-right'
1979 );
1980 this.toggleClipping( false );
1981 }
1982 }
1983 };
1984
1985 /**
1986 * ListToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
1987 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.MenuToolGroup MenuToolGroup}
1988 * and {@link OO.ui.BarToolGroup BarToolGroup}). The {@link OO.ui.Tool tools} in a ListToolGroup are displayed
1989 * by label in a dropdown menu. The title of the tool is used as the label text. The menu itself can be configured
1990 * with a label, icon, indicator, header, and title.
1991 *
1992 * ListToolGroups can be configured to be expanded and collapsed. Collapsed lists will have a ‘More’ option that
1993 * users can select to see the full list of tools. If a collapsed toolgroup is expanded, a ‘Fewer’ option permits
1994 * users to collapse the list again.
1995 *
1996 * ListToolGroups are created by a {@link OO.ui.ToolGroupFactory toolgroup factory} when the toolbar is set up. The factory
1997 * requires the ListToolGroup's symbolic name, 'list', which is specified along with the other configurations. For more
1998 * information about how to add tools to a ListToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
1999 *
2000 * @example
2001 * // Example of a ListToolGroup
2002 * var toolFactory = new OO.ui.ToolFactory();
2003 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
2004 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
2005 *
2006 * // Configure and register two tools
2007 * function SettingsTool() {
2008 * SettingsTool.parent.apply( this, arguments );
2009 * }
2010 * OO.inheritClass( SettingsTool, OO.ui.Tool );
2011 * SettingsTool.static.name = 'settings';
2012 * SettingsTool.static.icon = 'settings';
2013 * SettingsTool.static.title = 'Change settings';
2014 * SettingsTool.prototype.onSelect = function () {
2015 * this.setActive( false );
2016 * };
2017 * SettingsTool.prototype.onUpdateState = function () {};
2018 * toolFactory.register( SettingsTool );
2019 * // Register two more tools, nothing interesting here
2020 * function StuffTool() {
2021 * StuffTool.parent.apply( this, arguments );
2022 * }
2023 * OO.inheritClass( StuffTool, OO.ui.Tool );
2024 * StuffTool.static.name = 'stuff';
2025 * StuffTool.static.icon = 'search';
2026 * StuffTool.static.title = 'Change the world';
2027 * StuffTool.prototype.onSelect = function () {
2028 * this.setActive( false );
2029 * };
2030 * StuffTool.prototype.onUpdateState = function () {};
2031 * toolFactory.register( StuffTool );
2032 * toolbar.setup( [
2033 * {
2034 * // Configurations for list toolgroup.
2035 * type: 'list',
2036 * label: 'ListToolGroup',
2037 * icon: 'ellipsis',
2038 * title: 'This is the title, displayed when user moves the mouse over the list toolgroup',
2039 * header: 'This is the header',
2040 * include: [ 'settings', 'stuff' ],
2041 * allowCollapse: ['stuff']
2042 * }
2043 * ] );
2044 *
2045 * // Create some UI around the toolbar and place it in the document
2046 * var frame = new OO.ui.PanelLayout( {
2047 * expanded: false,
2048 * framed: true
2049 * } );
2050 * frame.$element.append(
2051 * toolbar.$element
2052 * );
2053 * $( 'body' ).append( frame.$element );
2054 * // Build the toolbar. This must be done after the toolbar has been appended to the document.
2055 * toolbar.initialize();
2056 *
2057 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki][1].
2058 *
2059 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
2060 *
2061 * @class
2062 * @extends OO.ui.PopupToolGroup
2063 *
2064 * @constructor
2065 * @param {OO.ui.Toolbar} toolbar
2066 * @param {Object} [config] Configuration options
2067 * @cfg {Array} [allowCollapse] Allow the specified tools to be collapsed. By default, collapsible tools
2068 * will only be displayed if users click the ‘More’ option displayed at the bottom of the list. If
2069 * the list is expanded, a ‘Fewer’ option permits users to collapse the list again. Any tools that
2070 * are included in the toolgroup, but are not designated as collapsible, will always be displayed.
2071 * To open a collapsible list in its expanded state, set #expanded to 'true'.
2072 * @cfg {Array} [forceExpand] Expand the specified tools. All other tools will be designated as collapsible.
2073 * Unless #expanded is set to true, the collapsible tools will be collapsed when the list is first opened.
2074 * @cfg {boolean} [expanded=false] Expand collapsible tools. This config is only relevant if tools have
2075 * been designated as collapsible. When expanded is set to true, all tools in the group will be displayed
2076 * when the list is first opened. Users can collapse the list with a ‘Fewer’ option at the bottom.
2077 */
2078 OO.ui.ListToolGroup = function OoUiListToolGroup( toolbar, config ) {
2079 // Allow passing positional parameters inside the config object
2080 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
2081 config = toolbar;
2082 toolbar = config.toolbar;
2083 }
2084
2085 // Configuration initialization
2086 config = config || {};
2087
2088 // Properties (must be set before parent constructor, which calls #populate)
2089 this.allowCollapse = config.allowCollapse;
2090 this.forceExpand = config.forceExpand;
2091 this.expanded = config.expanded !== undefined ? config.expanded : false;
2092 this.collapsibleTools = [];
2093
2094 // Parent constructor
2095 OO.ui.ListToolGroup.parent.call( this, toolbar, config );
2096
2097 // Initialization
2098 this.$element.addClass( 'oo-ui-listToolGroup' );
2099 };
2100
2101 /* Setup */
2102
2103 OO.inheritClass( OO.ui.ListToolGroup, OO.ui.PopupToolGroup );
2104
2105 /* Static Properties */
2106
2107 /**
2108 * @static
2109 * @inheritdoc
2110 */
2111 OO.ui.ListToolGroup.static.name = 'list';
2112
2113 /* Methods */
2114
2115 /**
2116 * @inheritdoc
2117 */
2118 OO.ui.ListToolGroup.prototype.populate = function () {
2119 var i, len, allowCollapse = [];
2120
2121 OO.ui.ListToolGroup.parent.prototype.populate.call( this );
2122
2123 // Update the list of collapsible tools
2124 if ( this.allowCollapse !== undefined ) {
2125 allowCollapse = this.allowCollapse;
2126 } else if ( this.forceExpand !== undefined ) {
2127 allowCollapse = OO.simpleArrayDifference( Object.keys( this.tools ), this.forceExpand );
2128 }
2129
2130 this.collapsibleTools = [];
2131 for ( i = 0, len = allowCollapse.length; i < len; i++ ) {
2132 if ( this.tools[ allowCollapse[ i ] ] !== undefined ) {
2133 this.collapsibleTools.push( this.tools[ allowCollapse[ i ] ] );
2134 }
2135 }
2136
2137 // Keep at the end, even when tools are added
2138 this.$group.append( this.getExpandCollapseTool().$element );
2139
2140 this.getExpandCollapseTool().toggle( this.collapsibleTools.length !== 0 );
2141 this.updateCollapsibleState();
2142 };
2143
2144 /**
2145 * Get the expand/collapse tool for this group
2146 *
2147 * @return {OO.ui.Tool} Expand collapse tool
2148 */
2149 OO.ui.ListToolGroup.prototype.getExpandCollapseTool = function () {
2150 var ExpandCollapseTool;
2151 if ( this.expandCollapseTool === undefined ) {
2152 ExpandCollapseTool = function () {
2153 ExpandCollapseTool.parent.apply( this, arguments );
2154 };
2155
2156 OO.inheritClass( ExpandCollapseTool, OO.ui.Tool );
2157
2158 ExpandCollapseTool.prototype.onSelect = function () {
2159 this.toolGroup.expanded = !this.toolGroup.expanded;
2160 this.toolGroup.updateCollapsibleState();
2161 this.setActive( false );
2162 };
2163 ExpandCollapseTool.prototype.onUpdateState = function () {
2164 // Do nothing. Tool interface requires an implementation of this function.
2165 };
2166
2167 ExpandCollapseTool.static.name = 'more-fewer';
2168
2169 this.expandCollapseTool = new ExpandCollapseTool( this );
2170 }
2171 return this.expandCollapseTool;
2172 };
2173
2174 /**
2175 * @inheritdoc
2176 */
2177 OO.ui.ListToolGroup.prototype.onMouseKeyUp = function ( e ) {
2178 // Do not close the popup when the user wants to show more/fewer tools
2179 if (
2180 $( e.target ).closest( '.oo-ui-tool-name-more-fewer' ).length &&
2181 ( e.which === OO.ui.MouseButtons.LEFT || e.which === OO.ui.Keys.SPACE || e.which === OO.ui.Keys.ENTER )
2182 ) {
2183 // HACK: Prevent the popup list from being hidden. Skip the PopupToolGroup implementation (which
2184 // hides the popup list when a tool is selected) and call ToolGroup's implementation directly.
2185 return OO.ui.ListToolGroup.parent.parent.prototype.onMouseKeyUp.call( this, e );
2186 } else {
2187 return OO.ui.ListToolGroup.parent.prototype.onMouseKeyUp.call( this, e );
2188 }
2189 };
2190
2191 OO.ui.ListToolGroup.prototype.updateCollapsibleState = function () {
2192 var i, len;
2193
2194 this.getExpandCollapseTool()
2195 .setIcon( this.expanded ? 'collapse' : 'expand' )
2196 .setTitle( OO.ui.msg( this.expanded ? 'ooui-toolgroup-collapse' : 'ooui-toolgroup-expand' ) );
2197
2198 for ( i = 0, len = this.collapsibleTools.length; i < len; i++ ) {
2199 this.collapsibleTools[ i ].toggle( this.expanded );
2200 }
2201
2202 // Re-evaluate clipping, because our height has changed
2203 this.clip();
2204 };
2205
2206 /**
2207 * MenuToolGroups are one of three types of {@link OO.ui.ToolGroup toolgroups} that are used to
2208 * create {@link OO.ui.Toolbar toolbars} (the other types of groups are {@link OO.ui.BarToolGroup BarToolGroup}
2209 * and {@link OO.ui.ListToolGroup ListToolGroup}). MenuToolGroups contain selectable {@link OO.ui.Tool tools},
2210 * which are displayed by label in a dropdown menu. The tool's title is used as the label text, and the
2211 * menu label is updated to reflect which tool or tools are currently selected. If no tools are selected,
2212 * the menu label is empty. The menu can be configured with an indicator, icon, title, and/or header.
2213 *
2214 * MenuToolGroups are created by a {@link OO.ui.ToolGroupFactory tool group factory} when the toolbar
2215 * is set up.
2216 *
2217 * @example
2218 * // Example of a MenuToolGroup
2219 * var toolFactory = new OO.ui.ToolFactory();
2220 * var toolGroupFactory = new OO.ui.ToolGroupFactory();
2221 * var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
2222 *
2223 * // We will be placing status text in this element when tools are used
2224 * var $area = $( '<p>' ).text( 'An example of a MenuToolGroup. Select a tool from the dropdown menu.' );
2225 *
2226 * // Define the tools that we're going to place in our toolbar
2227 *
2228 * function SettingsTool() {
2229 * SettingsTool.parent.apply( this, arguments );
2230 * this.reallyActive = false;
2231 * }
2232 * OO.inheritClass( SettingsTool, OO.ui.Tool );
2233 * SettingsTool.static.name = 'settings';
2234 * SettingsTool.static.icon = 'settings';
2235 * SettingsTool.static.title = 'Change settings';
2236 * SettingsTool.prototype.onSelect = function () {
2237 * $area.text( 'Settings tool clicked!' );
2238 * // Toggle the active state on each click
2239 * this.reallyActive = !this.reallyActive;
2240 * this.setActive( this.reallyActive );
2241 * // To update the menu label
2242 * this.toolbar.emit( 'updateState' );
2243 * };
2244 * SettingsTool.prototype.onUpdateState = function () {};
2245 * toolFactory.register( SettingsTool );
2246 *
2247 * function StuffTool() {
2248 * StuffTool.parent.apply( this, arguments );
2249 * this.reallyActive = false;
2250 * }
2251 * OO.inheritClass( StuffTool, OO.ui.Tool );
2252 * StuffTool.static.name = 'stuff';
2253 * StuffTool.static.icon = 'ellipsis';
2254 * StuffTool.static.title = 'More stuff';
2255 * StuffTool.prototype.onSelect = function () {
2256 * $area.text( 'More stuff tool clicked!' );
2257 * // Toggle the active state on each click
2258 * this.reallyActive = !this.reallyActive;
2259 * this.setActive( this.reallyActive );
2260 * // To update the menu label
2261 * this.toolbar.emit( 'updateState' );
2262 * };
2263 * StuffTool.prototype.onUpdateState = function () {};
2264 * toolFactory.register( StuffTool );
2265 *
2266 * // Finally define which tools and in what order appear in the toolbar. Each tool may only be
2267 * // used once (but not all defined tools must be used).
2268 * toolbar.setup( [
2269 * {
2270 * type: 'menu',
2271 * header: 'This is the (optional) header',
2272 * title: 'This is the (optional) title',
2273 * include: [ 'settings', 'stuff' ]
2274 * }
2275 * ] );
2276 *
2277 * // Create some UI around the toolbar and place it in the document
2278 * var frame = new OO.ui.PanelLayout( {
2279 * expanded: false,
2280 * framed: true
2281 * } );
2282 * var contentFrame = new OO.ui.PanelLayout( {
2283 * expanded: false,
2284 * padded: true
2285 * } );
2286 * frame.$element.append(
2287 * toolbar.$element,
2288 * contentFrame.$element.append( $area )
2289 * );
2290 * $( 'body' ).append( frame.$element );
2291 *
2292 * // Here is where the toolbar is actually built. This must be done after inserting it into the
2293 * // document.
2294 * toolbar.initialize();
2295 * toolbar.emit( 'updateState' );
2296 *
2297 * For more information about how to add tools to a MenuToolGroup, please see {@link OO.ui.ToolGroup toolgroup}.
2298 * For more information about toolbars in general, please see the [OOjs UI documentation on MediaWiki] [1].
2299 *
2300 * [1]: https://www.mediawiki.org/wiki/OOjs_UI/Toolbars
2301 *
2302 * @class
2303 * @extends OO.ui.PopupToolGroup
2304 *
2305 * @constructor
2306 * @param {OO.ui.Toolbar} toolbar
2307 * @param {Object} [config] Configuration options
2308 */
2309 OO.ui.MenuToolGroup = function OoUiMenuToolGroup( toolbar, config ) {
2310 // Allow passing positional parameters inside the config object
2311 if ( OO.isPlainObject( toolbar ) && config === undefined ) {
2312 config = toolbar;
2313 toolbar = config.toolbar;
2314 }
2315
2316 // Configuration initialization
2317 config = config || {};
2318
2319 // Parent constructor
2320 OO.ui.MenuToolGroup.parent.call( this, toolbar, config );
2321
2322 // Events
2323 this.toolbar.connect( this, { updateState: 'onUpdateState' } );
2324
2325 // Initialization
2326 this.$element.addClass( 'oo-ui-menuToolGroup' );
2327 };
2328
2329 /* Setup */
2330
2331 OO.inheritClass( OO.ui.MenuToolGroup, OO.ui.PopupToolGroup );
2332
2333 /* Static Properties */
2334
2335 /**
2336 * @static
2337 * @inheritdoc
2338 */
2339 OO.ui.MenuToolGroup.static.name = 'menu';
2340
2341 /* Methods */
2342
2343 /**
2344 * Handle the toolbar state being updated.
2345 *
2346 * When the state changes, the title of each active item in the menu will be joined together and
2347 * used as a label for the group. The label will be empty if none of the items are active.
2348 *
2349 * @private
2350 */
2351 OO.ui.MenuToolGroup.prototype.onUpdateState = function () {
2352 var name,
2353 labelTexts = [];
2354
2355 for ( name in this.tools ) {
2356 if ( this.tools[ name ].isActive() ) {
2357 labelTexts.push( this.tools[ name ].getTitle() );
2358 }
2359 }
2360
2361 this.setLabel( labelTexts.join( ', ' ) || ' ' );
2362 };
2363
2364 }( OO ) );
2365
2366 //# sourceMappingURL=oojs-ui-toolbars.js.map