# all files / src/ Toolbar.js

21.52% Statements 17/79
0% Branches 0/26
0% Functions 0/14
21.52% Lines 17/79

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533

    1×

    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

    1×

     /**
     * Toolbars are complex interface components that permit users to easily access a variety
     * of {@link OO.ui.Tool tools} (e.g., formatting commands) and actions, which are additional commands that are
     * part of the toolbar, but not configured as tools.
     *
     * Individual tools are customized and then registered with a {@link OO.ui.ToolFactory tool factory}, which creates
     * the tools on demand. Each tool has a symbolic name (used when registering the tool), a title (e.g., ‘Insert
     * image’), and an icon.
     *
     * Individual tools are organized in {@link OO.ui.ToolGroup toolgroups}, which can be {@link OO.ui.MenuToolGroup menus}
     * of tools, {@link OO.ui.ListToolGroup lists} of tools, or a single {@link OO.ui.BarToolGroup bar} of tools.
     * The arrangement and order of the toolgroups is customized when the toolbar is set up. Tools can be presented in
     * any order, but each can only appear once in the toolbar.
     *
     * The toolbar can be synchronized with the state of the external "application", like a text
     * editor's editing area, marking tools as active/inactive (e.g. a 'bold' tool would be shown as
     * active when the text cursor was inside bolded text) or enabled/disabled (e.g. a table caption
     * tool would be disabled while the user is not editing a table). A state change is signalled by
     * emitting the {@link #event-updateState 'updateState' event}, which calls Tools'
     * {@link OO.ui.Tool#onUpdateState onUpdateState method}.
     *
     * The following is an example of a basic toolbar.
     *
     *     @example
     *     // Example of a toolbar
     *     // Create the toolbar
     *     var toolFactory = new OO.ui.ToolFactory();
     *     var toolGroupFactory = new OO.ui.ToolGroupFactory();
     *     var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
     *
     *     // We will be placing status text in this element when tools are used
     *     var $area = $( '<p>' ).text( 'Toolbar example' );
     *
     *     // Define the tools that we're going to place in our toolbar
     *
     *     // Create a class inheriting from OO.ui.Tool
     *     function SearchTool() {
     *         SearchTool.parent.apply( this, arguments );
     *     }
     *     OO.inheritClass( SearchTool, OO.ui.Tool );
     *     // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
     *     // of 'icon' and 'title' (displayed icon and text).
     *     SearchTool.static.name = 'search';
     *     SearchTool.static.icon = 'search';
     *     SearchTool.static.title = 'Search...';
     *     // Defines the action that will happen when this tool is selected (clicked).
     *     SearchTool.prototype.onSelect = function () {
     *         $area.text( 'Search tool clicked!' );
     *         // Never display this tool as "active" (selected).
     *         this.setActive( false );
     *     };
     *     SearchTool.prototype.onUpdateState = function () {};
     *     // Make this tool available in our toolFactory and thus our toolbar
     *     toolFactory.register( SearchTool );
     *
     *     // Register two more tools, nothing interesting here
     *     function SettingsTool() {
     *         SettingsTool.parent.apply( this, arguments );
     *     }
     *     OO.inheritClass( SettingsTool, OO.ui.Tool );
     *     SettingsTool.static.name = 'settings';
     *     SettingsTool.static.icon = 'settings';
     *     SettingsTool.static.title = 'Change settings';
     *     SettingsTool.prototype.onSelect = function () {
     *         $area.text( 'Settings tool clicked!' );
     *         this.setActive( false );
     *     };
     *     SettingsTool.prototype.onUpdateState = function () {};
     *     toolFactory.register( SettingsTool );
     *
     *     // Register two more tools, nothing interesting here
     *     function StuffTool() {
     *         StuffTool.parent.apply( this, arguments );
     *     }
     *     OO.inheritClass( StuffTool, OO.ui.Tool );
     *     StuffTool.static.name = 'stuff';
     *     StuffTool.static.icon = 'ellipsis';
     *     StuffTool.static.title = 'More stuff';
     *     StuffTool.prototype.onSelect = function () {
     *         $area.text( 'More stuff tool clicked!' );
     *         this.setActive( false );
     *     };
     *     StuffTool.prototype.onUpdateState = function () {};
     *     toolFactory.register( StuffTool );
     *
     *     // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
     *     // little popup window (a PopupWidget).
     *     function HelpTool( toolGroup, config ) {
     *         OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
     *             padded: true,
     *             label: 'Help',
     *             head: true
     *         } }, config ) );
     *         this.popup.$body.append( '<p>I am helpful!</p>' );
     *     }
     *     OO.inheritClass( HelpTool, OO.ui.PopupTool );
     *     HelpTool.static.name = 'help';
     *     HelpTool.static.icon = 'help';
     *     HelpTool.static.title = 'Help';
     *     toolFactory.register( HelpTool );
     *
     *     // Finally define which tools and in what order appear in the toolbar. Each tool may only be
     *     // used once (but not all defined tools must be used).
     *     toolbar.setup( [
     *         {
     *             // 'bar' tool groups display tools' icons only, side-by-side.
     *             type: 'bar',
     *             include: [ 'search', 'help' ]
     *         },
     *         {
     *             // 'list' tool groups display both the titles and icons, in a dropdown list.
     *             type: 'list',
     *             indicator: 'down',
     *             label: 'More',
     *             include: [ 'settings', 'stuff' ]
     *         }
     *         // Note how the tools themselves are toolgroup-agnostic - the same tool can be displayed
     *         // either in a 'list' or a 'bar'. There is a 'menu' tool group too, not showcased here,
     *         // since it's more complicated to use. (See the next example snippet on this page.)
     *     ] );
     *
     *     // Create some UI around the toolbar and place it in the document
     *     var frame = new OO.ui.PanelLayout( {
     *         expanded: false,
     *         framed: true
     *     } );
     *     var contentFrame = new OO.ui.PanelLayout( {
     *         expanded: false,
     *         padded: true
     *     } );
     *     frame.$element.append(
     *         toolbar.$element,
     *         contentFrame.$element.append( $area )
     *     );
     *     $( 'body' ).append( frame.$element );
     *
     *     // Here is where the toolbar is actually built. This must be done after inserting it into the
     *     // document.
     *     toolbar.initialize();
     *     toolbar.emit( 'updateState' );
     *
     * The following example extends the previous one to illustrate 'menu' toolgroups and the usage of
     * {@link #event-updateState 'updateState' event}.
     *
     *     @example
     *     // Create the toolbar
     *     var toolFactory = new OO.ui.ToolFactory();
     *     var toolGroupFactory = new OO.ui.ToolGroupFactory();
     *     var toolbar = new OO.ui.Toolbar( toolFactory, toolGroupFactory );
     *
     *     // We will be placing status text in this element when tools are used
     *     var $area = $( '<p>' ).text( 'Toolbar example' );
     *
     *     // Define the tools that we're going to place in our toolbar
     *
     *     // Create a class inheriting from OO.ui.Tool
     *     function SearchTool() {
     *         SearchTool.parent.apply( this, arguments );
     *     }
     *     OO.inheritClass( SearchTool, OO.ui.Tool );
     *     // Each tool must have a 'name' (used as an internal identifier, see later) and at least one
     *     // of 'icon' and 'title' (displayed icon and text).
     *     SearchTool.static.name = 'search';
     *     SearchTool.static.icon = 'search';
     *     SearchTool.static.title = 'Search...';
     *     // Defines the action that will happen when this tool is selected (clicked).
     *     SearchTool.prototype.onSelect = function () {
     *         $area.text( 'Search tool clicked!' );
     *         // Never display this tool as "active" (selected).
     *         this.setActive( false );
     *     };
     *     SearchTool.prototype.onUpdateState = function () {};
     *     // Make this tool available in our toolFactory and thus our toolbar
     *     toolFactory.register( SearchTool );
     *
     *     // Register two more tools, nothing interesting here
     *     function SettingsTool() {
     *         SettingsTool.parent.apply( this, arguments );
     *         this.reallyActive = false;
     *     }
     *     OO.inheritClass( SettingsTool, OO.ui.Tool );
     *     SettingsTool.static.name = 'settings';
     *     SettingsTool.static.icon = 'settings';
     *     SettingsTool.static.title = 'Change settings';
     *     SettingsTool.prototype.onSelect = function () {
     *         $area.text( 'Settings tool clicked!' );
     *         // Toggle the active state on each click
     *         this.reallyActive = !this.reallyActive;
     *         this.setActive( this.reallyActive );
     *         // To update the menu label
     *         this.toolbar.emit( 'updateState' );
     *     };
     *     SettingsTool.prototype.onUpdateState = function () {};
     *     toolFactory.register( SettingsTool );
     *
     *     // Register two more tools, nothing interesting here
     *     function StuffTool() {
     *         StuffTool.parent.apply( this, arguments );
     *         this.reallyActive = false;
     *     }
     *     OO.inheritClass( StuffTool, OO.ui.Tool );
     *     StuffTool.static.name = 'stuff';
     *     StuffTool.static.icon = 'ellipsis';
     *     StuffTool.static.title = 'More stuff';
     *     StuffTool.prototype.onSelect = function () {
     *         $area.text( 'More stuff tool clicked!' );
     *         // Toggle the active state on each click
     *         this.reallyActive = !this.reallyActive;
     *         this.setActive( this.reallyActive );
     *         // To update the menu label
     *         this.toolbar.emit( 'updateState' );
     *     };
     *     StuffTool.prototype.onUpdateState = function () {};
     *     toolFactory.register( StuffTool );
     *
     *     // This is a PopupTool. Rather than having a custom 'onSelect' action, it will display a
     *     // little popup window (a PopupWidget). 'onUpdateState' is also already implemented.
     *     function HelpTool( toolGroup, config ) {
     *         OO.ui.PopupTool.call( this, toolGroup, $.extend( { popup: {
     *             padded: true,
     *             label: 'Help',
     *             head: true
     *         } }, config ) );
     *         this.popup.$body.append( '<p>I am helpful!</p>' );
     *     }
     *     OO.inheritClass( HelpTool, OO.ui.PopupTool );
     *     HelpTool.static.name = 'help';
     *     HelpTool.static.icon = 'help';
     *     HelpTool.static.title = 'Help';
     *     toolFactory.register( HelpTool );
     *
     *     // Finally define which tools and in what order appear in the toolbar. Each tool may only be
     *     // used once (but not all defined tools must be used).
     *     toolbar.setup( [
     *         {
     *             // 'bar' tool groups display tools' icons only, side-by-side.
     *             type: 'bar',
     *             include: [ 'search', 'help' ]
     *         },
     *         {
     *             // 'menu' tool groups display both the titles and icons, in a dropdown menu.
     *             // Menu label indicates which items are selected.
     *             type: 'menu',
     *             indicator: 'down',
     *             include: [ 'settings', 'stuff' ]
     *         }
     *     ] );
     *
     *     // Create some UI around the toolbar and place it in the document
     *     var frame = new OO.ui.PanelLayout( {
     *         expanded: false,
     *         framed: true
     *     } );
     *     var contentFrame = new OO.ui.PanelLayout( {
     *         expanded: false,
     *         padded: true
     *     } );
     *     frame.$element.append(
     *         toolbar.$element,
     *         contentFrame.$element.append( $area )
     *     );
     *     $( 'body' ).append( frame.$element );
     *
     *     // Here is where the toolbar is actually built. This must be done after inserting it into the
     *     // document.
     *     toolbar.initialize();
     *     toolbar.emit( 'updateState' );
     *
     * @class
     * @extends OO.ui.Element
     * @mixins OO.EventEmitter
     * @mixins OO.ui.mixin.GroupElement
     *
     * @constructor
     * @param {OO.ui.ToolFactory} toolFactory Factory for creating tools
     * @param {OO.ui.ToolGroupFactory} toolGroupFactory Factory for creating toolgroups
     * @param {Object} [config] Configuration options
     * @cfg {boolean} [actions] Add an actions section to the toolbar. Actions are commands that are included
     *  in the toolbar, but are not configured as tools. By default, actions are displayed on the right side of
     *  the toolbar.
     * @cfg {string} [position='top'] Whether the toolbar is positioned above ('top') or below ('bottom') content.
     */
    OO.ui.Toolbar = function OoUiToolbar( toolFactory, toolGroupFactory, config ) {
    	// Allow passing positional parameters inside the config object
    	if ( OO.isPlainObject( toolFactory ) && config === undefined ) {
    		config = toolFactory;
    		toolFactory = config.toolFactory;
    		toolGroupFactory = config.toolGroupFactory;
    	}

    	// Configuration initialization
    	config = config || {};

    	// Parent constructor
    	OO.ui.Toolbar.parent.call( this, config );

    	// Mixin constructors
    	OO.EventEmitter.call( this );
    	OO.ui.mixin.GroupElement.call( this, config );

    	// Properties
    	this.toolFactory = toolFactory;
    	this.toolGroupFactory = toolGroupFactory;
    	this.groups = [];
    	this.tools = {};
    	this.position = config.position || 'top';
    	this.$bar = $( '<div>' );
    	this.$actions = $( '<div>' );
    	this.initialized = false;
    	this.narrowThreshold = null;
    	this.onWindowResizeHandler = this.onWindowResize.bind( this );

    	// Events
    	this.$element
    		.add( this.$bar ).add( this.$group ).add( this.$actions )
    		.on( 'mousedown keydown', this.onPointerDown.bind( this ) );

    	// Initialization
    	this.$group.addClass( 'oo-ui-toolbar-tools' );
    	if ( config.actions ) {
    		this.$bar.append( this.$actions.addClass( 'oo-ui-toolbar-actions' ) );
    	}
    	this.$bar
    		.addClass( 'oo-ui-toolbar-bar' )
    		.append( this.$group, '<div style="clear:both"></div>' );
    	// Possible classes: oo-ui-toolbar-position-top, oo-ui-toolbar-position-bottom
    	this.$element.addClass( 'oo-ui-toolbar oo-ui-toolbar-position-' + this.position ).append( this.$bar );
    };

    /* Setup */

    OO.inheritClass( OO.ui.Toolbar, OO.ui.Element );
    OO.mixinClass( OO.ui.Toolbar, OO.EventEmitter );
    OO.mixinClass( OO.ui.Toolbar, OO.ui.mixin.GroupElement );

    /* Events */

    /**
     * @event updateState
     *
     * An 'updateState' event must be emitted on the Toolbar (by calling `toolbar.emit( 'updateState' )`)
     * every time the state of the application using the toolbar changes, and an update to the state of
     * tools is required.
     *
     * @param {...Mixed} data Application-defined parameters
     */

    /* Methods */

    /**
     * Get the tool factory.
     *
     * @return {OO.ui.ToolFactory} Tool factory
     */
    OO.ui.Toolbar.prototype.getToolFactory = function () {
    	return this.toolFactory;
    };

    /**
     * Get the toolgroup factory.
     *
     * @return {OO.Factory} Toolgroup factory
     */
    OO.ui.Toolbar.prototype.getToolGroupFactory = function () {
    	return this.toolGroupFactory;
    };

    /**
     * Handles mouse down events.
     *
     * @private
     * @param {jQuery.Event} e Mouse down event
     */
    OO.ui.Toolbar.prototype.onPointerDown = function ( e ) {
    	var $closestWidgetToEvent = $( e.target ).closest( '.oo-ui-widget' ),
    		$closestWidgetToToolbar = this.$element.closest( '.oo-ui-widget' );
    	if ( !$closestWidgetToEvent.length || $closestWidgetToEvent[ 0 ] === $closestWidgetToToolbar[ 0 ] ) {
    		return false;
    	}
    };

    /**
     * Handle window resize event.
     *
     * @private
     * @param {jQuery.Event} e Window resize event
     */
    OO.ui.Toolbar.prototype.onWindowResize = function () {
    	this.$element.toggleClass(
    		'oo-ui-toolbar-narrow',
    		this.$bar.width() <= this.getNarrowThreshold()
    	);
    };

    /**
     * Get the (lazily-computed) width threshold for applying the oo-ui-toolbar-narrow
     * class.
     *
     * @private
     * @return {number} Width threshold in pixels
     */
    OO.ui.Toolbar.prototype.getNarrowThreshold = function () {
    	if ( this.narrowThreshold === null ) {
    		this.narrowThreshold = this.$group.width() + this.$actions.width();
    	}
    	return this.narrowThreshold;
    };

    /**
     * Sets up handles and preloads required information for the toolbar to work.
     * This must be called after it is attached to a visible document and before doing anything else.
     */
    OO.ui.Toolbar.prototype.initialize = function () {
    	if ( !this.initialized ) {
    		this.initialized = true;
    		$( this.getElementWindow() ).on( 'resize', this.onWindowResizeHandler );
    		this.onWindowResize();
    	}
    };

    /**
     * Set up the toolbar.
     *
     * The toolbar is set up with a list of toolgroup configurations that specify the type of
     * toolgroup ({@link OO.ui.BarToolGroup bar}, {@link OO.ui.MenuToolGroup menu}, or {@link OO.ui.ListToolGroup list})
     * to add and which tools to include, exclude, promote, or demote within that toolgroup. Please
     * see {@link OO.ui.ToolGroup toolgroups} for more information about including tools in toolgroups.
     *
     * @param {Object.<string,Array>} groups List of toolgroup configurations
     * @param {Array|string} [groups.include] Tools to include in the toolgroup
     * @param {Array|string} [groups.exclude] Tools to exclude from the toolgroup
     * @param {Array|string} [groups.promote] Tools to promote to the beginning of the toolgroup
     * @param {Array|string} [groups.demote] Tools to demote to the end of the toolgroup
     */
    OO.ui.Toolbar.prototype.setup = function ( groups ) {
    	var i, len, type, group,
    		items = [],
    		defaultType = 'bar';

    	// Cleanup previous groups
    	this.reset();

    	// Build out new groups
    	for ( i = 0, len = groups.length; i < len; i++ ) {
    		group = groups[ i ];
    		if ( group.include === '*' ) {
    			// Apply defaults to catch-all groups
    			if ( group.type === undefined ) {
    				group.type = 'list';
    			}
    			if ( group.label === undefined ) {
    				group.label = OO.ui.msg( 'ooui-toolbar-more' );
    			}
    		}
    		// Check type has been registered
    		type = this.getToolGroupFactory().lookup( group.type ) ? group.type : defaultType;
    		items.push(
    			this.getToolGroupFactory().create( type, this, group )
    		);
    	}
    	this.addItems( items );
    };

    /**
     * Remove all tools and toolgroups from the toolbar.
     */
    OO.ui.Toolbar.prototype.reset = function () {
    	var i, len;

    	this.groups = [];
    	this.tools = {};
    	for ( i = 0, len = this.items.length; i < len; i++ ) {
    		this.items[ i ].destroy();
    	}
    	this.clearItems();
    };

    /**
     * Destroy the toolbar.
     *
     * Destroying the toolbar removes all event handlers and DOM elements that constitute the toolbar. Call
     * this method whenever you are done using a toolbar.
     */
    OO.ui.Toolbar.prototype.destroy = function () {
    	$( this.getElementWindow() ).off( 'resize', this.onWindowResizeHandler );
    	this.reset();
    	this.$element.remove();
    };

    /**
     * Check if the tool is available.
     *
     * Available tools are ones that have not yet been added to the toolbar.
     *
     * @param {string} name Symbolic name of tool
     * @return {boolean} Tool is available
     */
    OO.ui.Toolbar.prototype.isToolAvailable = function ( name ) {
    	return !this.tools[ name ];
    };

    /**
     * Prevent tool from being used again.
     *
     * @param {OO.ui.Tool} tool Tool to reserve
     */
    OO.ui.Toolbar.prototype.reserveTool = function ( tool ) {
    	this.tools[ tool.getName() ] = tool;
    };

    /**
     * Allow tool to be used again.
     *
     * @param {OO.ui.Tool} tool Tool to release
     */
    OO.ui.Toolbar.prototype.releaseTool = function ( tool ) {
    	delete this.tools[ tool.getName() ];
    };

    /**
     * Get accelerator label for tool.
     *
     * The OOjs UI library does not contain an accelerator system, but this is the hook for one. To
     * use an accelerator system, subclass the toolbar and override this method, which is meant to return a label
     * that describes the accelerator keys for the tool passed (by symbolic name) to the method.
     *
     * @param {string} name Symbolic name of tool
     * @return {string|undefined} Tool accelerator label if available
     */
    OO.ui.Toolbar.prototype.getToolAccelerator = function () {
    	return undefined;
    };