2013-08-05 23:36:10 +00:00
|
|
|
/*!
|
2013-10-09 20:09:59 +00:00
|
|
|
* ObjectOriented UserInterface ToolGroup class.
|
2013-08-05 23:36:10 +00:00
|
|
|
*
|
2013-10-09 20:09:59 +00:00
|
|
|
* @copyright 2011-2013 OOJS Team and others; see AUTHORS.txt
|
2013-08-05 23:36:10 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-10-09 20:09:59 +00:00
|
|
|
* Collection of tools.
|
2013-08-05 23:36:10 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @abstract
|
2013-10-09 20:09:59 +00:00
|
|
|
* @extends OO.ui.Widget
|
|
|
|
* @mixins OO.ui.GroupElement
|
2013-08-05 23:36:10 +00:00
|
|
|
*
|
2013-08-27 23:28:29 +00:00
|
|
|
* Tools can be specified in the following ways:
|
|
|
|
* - A specific tool: `{ 'name': 'tool-name' }` or `'tool-name'`
|
|
|
|
* - All tools in a group: `{ 'group': 'group-name' }`
|
|
|
|
* - All tools: `'*'`
|
2013-08-09 18:29:09 +00:00
|
|
|
*
|
2013-08-05 23:36:10 +00:00
|
|
|
* @constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
* @param {OO.ui.Toolbar} toolbar
|
2013-09-25 10:21:09 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2013-08-27 23:28:29 +00:00
|
|
|
* @cfg {Array|string} [include=[]] List of tools to include
|
|
|
|
* @cfg {Array|string} [exclude=[]] List of tools to exclude
|
|
|
|
* @cfg {Array|string} [promote=[]] List of tools to promote to the beginning
|
|
|
|
* @cfg {Array|string} [demote=[]] List of tools to demote to the end
|
2013-08-05 23:36:10 +00:00
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup = function OoUiToolGroup( toolbar, config ) {
|
2013-08-09 18:29:09 +00:00
|
|
|
// Configuration initialization
|
|
|
|
config = config || {};
|
|
|
|
|
2013-08-05 23:36:10 +00:00
|
|
|
// Parent constructor
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.Widget.call( this, config );
|
2013-08-05 23:36:10 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.GroupElement.call( this, this.$$( '<div>' ) );
|
2013-08-05 23:36:10 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.toolbar = toolbar;
|
2013-08-09 18:29:09 +00:00
|
|
|
this.tools = {};
|
2013-08-27 23:28:29 +00:00
|
|
|
this.pressed = null;
|
2013-08-09 18:29:09 +00:00
|
|
|
this.include = config.include || [];
|
|
|
|
this.exclude = config.exclude || [];
|
|
|
|
this.promote = config.promote || [];
|
|
|
|
this.demote = config.demote || [];
|
2013-10-09 20:09:59 +00:00
|
|
|
this.onCapturedMouseUpHandler = OO.ui.bind( this.onCapturedMouseUp, this );
|
2013-08-05 23:36:10 +00:00
|
|
|
|
|
|
|
// Events
|
2013-08-27 23:28:29 +00:00
|
|
|
this.$.on( {
|
2013-10-09 20:09:59 +00:00
|
|
|
'mousedown': OO.ui.bind( this.onMouseDown, this ),
|
|
|
|
'mouseup': OO.ui.bind( this.onMouseUp, this ),
|
|
|
|
'mouseover': OO.ui.bind( this.onMouseOver, this ),
|
|
|
|
'mouseout': OO.ui.bind( this.onMouseOut, this )
|
2013-08-27 23:28:29 +00:00
|
|
|
} );
|
2013-10-03 21:10:06 +00:00
|
|
|
this.toolbar.getToolFactory().connect( this, { 'register': 'onToolFactoryRegister' } );
|
2013-08-05 23:36:10 +00:00
|
|
|
|
|
|
|
// Initialization
|
2013-10-09 20:09:59 +00:00
|
|
|
this.$group.addClass( 'oo-ui-toolGroup-tools' );
|
2013-08-27 23:28:29 +00:00
|
|
|
this.$
|
2013-10-09 20:09:59 +00:00
|
|
|
.addClass( 'oo-ui-toolGroup' )
|
2013-08-27 23:28:29 +00:00
|
|
|
.append( this.$group );
|
|
|
|
this.populate();
|
2013-08-05 23:36:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.inheritClass( OO.ui.ToolGroup, OO.ui.Widget );
|
2013-08-05 23:36:10 +00:00
|
|
|
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.mixinClass( OO.ui.ToolGroup, OO.ui.GroupElement );
|
2013-08-09 18:29:09 +00:00
|
|
|
|
2013-08-27 23:28:29 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @event update
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
/**
|
2013-10-23 23:27:12 +00:00
|
|
|
* Show labels in tooltips.
|
2013-08-27 23:28:29 +00:00
|
|
|
*
|
|
|
|
* @static
|
2013-10-23 23:27:12 +00:00
|
|
|
* @property {boolean}
|
2013-08-27 23:28:29 +00:00
|
|
|
* @inheritable
|
|
|
|
*/
|
2013-10-23 23:27:12 +00:00
|
|
|
OO.ui.ToolGroup.static.labelTooltips = false;
|
2013-08-27 23:28:29 +00:00
|
|
|
|
|
|
|
/**
|
2013-10-23 23:27:12 +00:00
|
|
|
* Show acceleration labels in tooltips.
|
2013-08-27 23:28:29 +00:00
|
|
|
*
|
|
|
|
* @static
|
2013-10-23 23:27:12 +00:00
|
|
|
* @property {boolean}
|
2013-08-27 23:28:29 +00:00
|
|
|
* @inheritable
|
|
|
|
*/
|
2013-10-23 23:27:12 +00:00
|
|
|
OO.ui.ToolGroup.static.accelTooltips = false;
|
2013-08-27 23:28:29 +00:00
|
|
|
|
2013-08-09 18:29:09 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2013-08-27 23:28:29 +00:00
|
|
|
/**
|
|
|
|
* Handle mouse down events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Mouse down event
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.onMouseDown = function ( e ) {
|
2013-08-27 23:28:29 +00:00
|
|
|
if ( !this.disabled && e.which === 1 ) {
|
|
|
|
this.pressed = this.getTargetTool( e );
|
|
|
|
if ( this.pressed ) {
|
|
|
|
this.pressed.setActive( true );
|
|
|
|
this.getElementDocument().addEventListener(
|
|
|
|
'mouseup', this.onCapturedMouseUpHandler, true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle captured mouse up events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {Event} e Mouse up event
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.onCapturedMouseUp = function ( e ) {
|
2013-08-27 23:28:29 +00:00
|
|
|
this.getElementDocument().removeEventListener( 'mouseup', this.onCapturedMouseUpHandler, true );
|
|
|
|
// onMouseUp may be called a second time, depending on where the mouse is when the button is
|
|
|
|
// released, but since `this.pressed` will no longer be true, the second call will be ignored.
|
|
|
|
this.onMouseUp( e );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse up events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Mouse up event
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.onMouseUp = function ( e ) {
|
2013-08-27 23:28:29 +00:00
|
|
|
var tool = this.getTargetTool( e );
|
|
|
|
|
|
|
|
if ( !this.disabled && e.which === 1 && this.pressed && this.pressed === tool ) {
|
|
|
|
this.pressed.onSelect();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.pressed = null;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse over events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Mouse over event
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.onMouseOver = function ( e ) {
|
2013-08-27 23:28:29 +00:00
|
|
|
var tool = this.getTargetTool( e );
|
|
|
|
|
|
|
|
if ( this.pressed && this.pressed === tool ) {
|
|
|
|
this.pressed.setActive( true );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle mouse out events.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Mouse out event
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.onMouseOut = function ( e ) {
|
2013-08-27 23:28:29 +00:00
|
|
|
var tool = this.getTargetTool( e );
|
|
|
|
|
|
|
|
if ( this.pressed && this.pressed === tool ) {
|
|
|
|
this.pressed.setActive( false );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the closest tool to a jQuery.Event.
|
|
|
|
*
|
2013-10-17 17:25:57 +00:00
|
|
|
* Only tool links are considered, which prevents other elements in the tool such as popups from
|
|
|
|
* triggering tool group interactions.
|
|
|
|
*
|
2013-08-27 23:28:29 +00:00
|
|
|
* @method
|
|
|
|
* @private
|
|
|
|
* @param {jQuery.Event} e
|
2013-10-09 20:09:59 +00:00
|
|
|
* @returns {OO.ui.Tool|null} Tool, `null` if none was found
|
2013-08-27 23:28:29 +00:00
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.getTargetTool = function ( e ) {
|
2013-10-17 17:25:57 +00:00
|
|
|
var $item = $( e.target ).closest( '.oo-ui-tool-link' );
|
2013-08-27 23:28:29 +00:00
|
|
|
if ( $item.length ) {
|
2013-10-17 17:25:57 +00:00
|
|
|
return $item.parent().data( 'oo-ui-tool' );
|
2013-08-27 23:28:29 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2013-08-09 18:29:09 +00:00
|
|
|
/**
|
|
|
|
* Handle tool registry register events.
|
|
|
|
*
|
2013-08-27 23:28:29 +00:00
|
|
|
* If a tool is registered after the group is created, we must repopulate the list to account for:
|
|
|
|
* - a tool being added that may be included
|
|
|
|
* - a tool already included being overridden
|
2013-08-09 18:29:09 +00:00
|
|
|
*
|
|
|
|
* @param {string} name Symbolic name of tool
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.onToolFactoryRegister = function () {
|
2013-08-27 23:28:29 +00:00
|
|
|
this.populate();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the toolbar this group is in.
|
|
|
|
*
|
2013-10-09 20:09:59 +00:00
|
|
|
* @return {OO.ui.Toolbar} Toolbar of group
|
2013-08-27 23:28:29 +00:00
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.getToolbar = function () {
|
2013-08-27 23:28:29 +00:00
|
|
|
return this.toolbar;
|
2013-08-09 18:29:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add and remove tools based on configuration.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.populate = function () {
|
2013-08-09 18:29:09 +00:00
|
|
|
var i, len, name, tool,
|
|
|
|
names = {},
|
2013-08-27 23:28:29 +00:00
|
|
|
add = [],
|
|
|
|
remove = [],
|
2013-10-03 21:10:06 +00:00
|
|
|
list = this.toolbar.getToolFactory().getTools(
|
|
|
|
this.include, this.exclude, this.promote, this.demote
|
|
|
|
);
|
2013-08-09 18:29:09 +00:00
|
|
|
|
|
|
|
// Build a list of needed tools
|
|
|
|
for ( i = 0, len = list.length; i < len; i++ ) {
|
|
|
|
name = list[i];
|
2013-08-27 23:28:29 +00:00
|
|
|
if ( this.toolbar.isToolAvailable( name ) ) {
|
|
|
|
tool = this.tools[name];
|
|
|
|
if ( !tool ) {
|
|
|
|
// Auto-initialize tools on first use
|
2013-10-03 21:10:06 +00:00
|
|
|
this.tools[name] = tool =
|
2013-10-23 23:27:12 +00:00
|
|
|
this.toolbar.getToolFactory().create( name, this );
|
|
|
|
tool.updateLabel();
|
2013-08-27 23:28:29 +00:00
|
|
|
}
|
2013-10-23 23:27:12 +00:00
|
|
|
this.toolbar.reserveTool( tool );
|
2013-08-27 23:28:29 +00:00
|
|
|
add.push( tool );
|
|
|
|
names[name] = true;
|
2013-08-09 18:29:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove tools that are no longer needed
|
|
|
|
for ( name in this.tools ) {
|
|
|
|
if ( !names[name] ) {
|
|
|
|
this.tools[name].destroy();
|
2013-10-23 23:27:12 +00:00
|
|
|
this.toolbar.releaseTool( this.tools[name] );
|
2013-08-27 23:28:29 +00:00
|
|
|
remove.push( this.tools[name] );
|
2013-08-09 18:29:09 +00:00
|
|
|
delete this.tools[name];
|
|
|
|
}
|
|
|
|
}
|
2013-08-27 23:28:29 +00:00
|
|
|
if ( remove.length ) {
|
|
|
|
this.removeItems( remove );
|
|
|
|
}
|
|
|
|
// Update emptiness state
|
|
|
|
if ( add.length ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
this.$.removeClass( 'oo-ui-toolGroup-empty' );
|
2013-08-27 23:28:29 +00:00
|
|
|
} else {
|
2013-10-09 20:09:59 +00:00
|
|
|
this.$.addClass( 'oo-ui-toolGroup-empty' );
|
2013-08-27 23:28:29 +00:00
|
|
|
}
|
2013-08-09 18:29:09 +00:00
|
|
|
// Re-add tools (moving existing ones to new locations)
|
2013-08-27 23:28:29 +00:00
|
|
|
this.addItems( add );
|
2013-08-09 18:29:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy tool group.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
2013-10-09 20:09:59 +00:00
|
|
|
OO.ui.ToolGroup.prototype.destroy = function () {
|
2013-08-09 18:29:09 +00:00
|
|
|
var name;
|
|
|
|
|
|
|
|
this.clearItems();
|
2013-10-03 21:10:06 +00:00
|
|
|
this.toolbar.getToolFactory().disconnect( this );
|
2013-08-09 18:29:09 +00:00
|
|
|
for ( name in this.tools ) {
|
2013-10-23 23:27:12 +00:00
|
|
|
this.toolbar.releaseTool( this.tools[name] );
|
2013-08-27 23:28:29 +00:00
|
|
|
this.tools[name].disconnect( this ).destroy();
|
|
|
|
delete this.tools[name];
|
2013-08-09 18:29:09 +00:00
|
|
|
}
|
|
|
|
this.$.remove();
|
|
|
|
};
|