mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Tool.js
Rob Moen 8ba81f4d67 Add interplatform, i18n shortcuts to ui buttons.
Objective:
* Put command shortcuts in button title attributes. (Bug 42919)
* Provide a registry for platform specific command triggers and their
corresponding i18n messages. (Bug 44012)
* Enable loading of triggers after ve.Surface is created. (lazy load)

Changes:

VisualEditor.i18n.php
* Add default trigger i18n messags for mac and pc system platforms

VisusalEditor.php, demos/ve/index.php
* Add links to files

ve.init.mw.Platform.js
* Define getUserLanguage and getSystemPlatform methods.

ve.init.sa.Platform.js
* Define getUserLanguage and getSystemPlatform methods.

ve.init.Platform.js
* Define abstract methods: getUserLangauge, getSystemPlatform

ve.ui.BoldBUttonTool.js, ve.ui.IndentButtonTool.js, ve.ui.ItalicButtonTool.js,
ve.ui.LinkButtonTool.js, ve.ui.OutdentButtonTool.js, ve.ui.RedoButtonTool.js,
ve.ui.UndoButtonTool.js
* Add registration for command triggers.

ve.Surface.js
* Methodize loading of triggers.
* Bind register event to ve.triggerRegistry to allow lazy loading of triggers.

ve.ui.Tool.js
* Init pre-registered tooltip messages.
* Update tool titles when new triggers are loaded.

ve.CommandRegistry.js
* Remove command registration ( moved to buttons themselves )

ve.TriggerRegistry.js
* New class for registering triggers.

ve.init.mw.ViewPageTarget.js
* Changed instance of unindent command to outdent.

Change-Id: Id8580a3f81aac751db0b7482422a73912648dfed
2013-01-17 15:28:32 -08:00

115 lines
2.3 KiB
JavaScript

/*!
* VisualEditor UserInterface Tool class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* UserInterface tool.
*
* @abstract
* @constructor
* @param {ve.ui.Toolbar} toolbar
*/
ve.ui.Tool = function VeUiTool( toolbar ) {
var tool = this;
// Properties
this.toolbar = toolbar;
this.$ = $( '<div class="ve-ui-tool"></div>' );
// Events
this.toolbar.addListenerMethods(
this, { 'updateState': 'onUpdateState', 'clearState': 'onClearState' }
);
ve.triggerRegistry.on( 'register', function ( name ) {
if ( name === ve.init.platform.getUserLanguage() + '.' + tool.constructor.static.name ) {
tool.setTitle();
}
} );
// Initialization
this.setTitle();
};
/* Static Properties */
/**
* @static
* @property
* @inheritable
*/
ve.ui.Tool.static = {};
/**
* Symbolic name of tool.
*
* @abstract
* @static
* @property
* @type {string}
*/
ve.ui.Tool.static.name = '';
/**
* Message key for tool title.
*
* @abstract
* @static
* @property
* @type {string}
*/
ve.ui.Tool.static.titleMessage = '';
/* Methods */
/**
* Sets the tool title attribute in the dom.
*
* Combines trigger i18n with tooltip message if trigger exists.
* Otherwise defaults to titleMessage value.
*
* @abstract
* @method
*/
ve.ui.Tool.prototype.setTitle = function () {
var trigger = ve.triggerRegistry.lookup(
ve.init.platform.getUserLanguage() + '.' +
this.constructor.static.name
),
labelMessage = ve.msg( this.constructor.static.titleMessage );
if ( trigger ) {
labelMessage = labelMessage.replace( '$1', ve.msg( trigger.labelMessage ) );
}
this.$.attr( 'title', labelMessage );
};
/**
* 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
);
};
/**
* Handle the toolbar state being cleared.
*
* This is an abstract method that must be overridden in a concrete subclass.
*
* @abstract
* @method
*/
ve.ui.Tool.prototype.onClearState = function () {
throw new Error(
've.ui.Tool.onClearState not implemented in this subclass:' + this.constructor
);
};