mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Tool.js
Trevor Parscal 7233ea8f1b EventEmitter API cleanup
The EventEmitter API we inherited from Node.js and then bastardized was
getting awkward and cumbersome. The number of uses of ve.bind was getting
out of control, and removing events meant caching the bound method in a
property. Many of the "features" of EventEmitter wasn't even being used,
some causing overhead, others just causing bloat. This change cleans up
how EventEmitter is used throughout the codebase.

The new event emitter API includes:
* emit - identical to the previous API, no longer throws an error if you
  emit error without a handler
* once - identical to the previous API, still introduces a wrapper* on -
  compatible with the previous API but has some new features
* off - identical to removeListener in the previous API
* connect - very similar to addListenerMethods but doesn't wrap callbacks
  in closures anymore
* disconnect - new, basically the opposite of addListenerMethods

Another change that is made in this commit is mixing in rather than
inheriting from EventEmitter.

Finally, there are changes throughout the codebase anywhere
connect/disconnect could be used.

Change-Id: Ic3085d39172a8a719ce7f036690f673e59848d3a
2013-05-02 15:05:59 -07:00

121 lines
2.5 KiB
JavaScript

/*!
* VisualEditor UserInterface Tool class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* UserInterface tool.
*
* @class
* @abstract
* @extends ve.ui.Widget
*
* @constructor
* @param {ve.ui.Toolbar} toolbar
* @param {Object} [config] Config options
*/
ve.ui.Tool = function VeUiTool( toolbar, config ) {
// Parent constructor
ve.ui.Widget.call( this, config );
// Properties
this.toolbar = toolbar;
// Events
this.toolbar.connect( this, { 'updateState': 'onUpdateState' } );
ve.triggerRegistry.connect( this, { 'register': 'onTriggerRegistryRegister' } );
// Initialization
this.setTitle();
this.$.addClass( 've-ui-Tool' );
};
/* Inheritance */
ve.inheritClass( ve.ui.Tool, ve.ui.Widget );
/* Static Properties */
/**
* Symbolic name of tool.
*
* @abstract
* @static
* @property {string}
*/
ve.ui.Tool.static.name = '';
/**
* CSS class name, rendered as ve-ui-dropdownTool-cssName
*
* If this is left as null, static.name is used instead.
*
* @abstract
* @static
* @property {string}
*/
ve.ui.Tool.static.cssName = null;
/**
* Message key for tool title.
*
* @abstract
* @static
* @property {string}
*/
ve.ui.Tool.static.titleMessage = null;
/* Methods */
/**
* Handle trigger registry register events.
*
* If a trigger is registered after the tool is loaded, this handler will ensure the tool's title is
* updated to reflect the available key command for the tool.
*
* @param {string} name Symbolic name of trigger
*/
ve.ui.Tool.prototype.onTriggerRegistryRegister = function ( name ) {
if ( name === this.constructor.static.name ) {
this.setTitle();
}
};
/**
* Handle the toolbar state being updated.
*
* This is an abstract method that must be overridden in a concrete subclass.
*
* @abstract
* @method
*/
ve.ui.Tool.prototype.onUpdateState = function () {
throw new Error(
've.ui.Tool.onUpdateState not implemented in this subclass:' + this.constructor
);
};
/**
* Sets the tool title attribute in the dom.
*
* Combines trigger i18n with tooltip message if trigger exists.
* Otherwise defaults to titleMessage value.
*
* @abstract
* @method
* @chainable
*/
ve.ui.Tool.prototype.setTitle = function () {
var trigger = ve.triggerRegistry.lookup( this.constructor.static.name ),
labelMessage = this.constructor.static.titleMessage,
labelText = labelMessage ? ve.msg( labelMessage ) : '';
if ( trigger ) {
labelText += ' [' + trigger.getMessage() + ']';
}
this.$.attr( 'title', labelText );
return this;
};