mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.ToolFactory.js
Trevor Parscal d2dfb9ac4f Split oojs-ui from ve.ui
* Move and rename generic parts of ve.ui to OO.ui
* We now have a UI test suite because ve.Element (outside ve.ui)
  is now part of oojs-ui, so it needs a test suite.
* Added to the MW test run (just like we do for unicodejs).
* Updated csslint config (also added ve-mw and syntaxhighlight
  which were missing).

oojs-ui still depends on the TriggerRegistry in VE, this is addressed
in a follow-up commit.

Change-Id: Iec147155c1ddf20b73a4d15d87b8742207032312
2013-10-28 22:40:08 -07:00

105 lines
2.9 KiB
JavaScript

/*!
* VisualEditor UserInterface ToolFactory class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Factory for tools.
*
* @class
* @extends OO.ui.ToolFactory
*
* @constructor
*/
ve.ui.ToolFactory = function OoUiToolFactory() {
// Parent constructor
OO.ui.ToolFactory.call( this );
};
/* Inheritance */
OO.inheritClass( ve.ui.ToolFactory, OO.ui.ToolFactory );
/* Methods */
/**
* Get a list of tools from a set of annotations.
*
* The most specific tool will be chosen based on inheritance - mostly. The order of being added
* also matters if the candidate classes aren't all in the same inheritance chain, and since object
* properties aren't necessarily ordered it's not predictable what the effect of ordering will be.
*
* TODO: Add tracking of order of registration using an array and prioritize the most recently
* registered candidate.
*
* @method
* @param {ve.dm.AnnotationSet} annotations Annotations to be inspected
* @returns {string[]} Symbolic names of tools that can be used to inspect annotations
*/
ve.ui.ToolFactory.prototype.getToolsForAnnotations = function ( annotations ) {
if ( annotations.isEmpty() ) {
return [];
}
var i, len, annotation, name, tool, candidateTool, candidateToolName,
arr = annotations.get(),
matches = [];
for ( i = 0, len = arr.length; i < len; i++ ) {
annotation = arr[i];
candidateTool = null;
for ( name in this.registry ) {
tool = this.registry[name];
if ( tool.static.isCompatibleWith( annotation ) ) {
if ( !candidateTool || tool.prototype instanceof candidateTool ) {
candidateTool = tool;
candidateToolName = name;
}
}
}
if ( candidateTool ) {
matches.push( candidateToolName );
}
}
return matches;
};
/**
* Get a tool for a node.
*
* The most specific tool will be chosen based on inheritance - mostly. The order of being added
* also matters if the candidate classes aren't all in the same inheritance chain, and since object
* properties aren't necessarily ordered it's not predictable what the effect of ordering will be.
*
* TODO: Add tracking of order of registration using an array and prioritize the most recently
* registered candidate.
*
* @method
* @param {ve.dm.Node} node Node to be edited
* @returns {string|undefined} Symbolic name of tool that can be used to edit node
*/
ve.ui.ToolFactory.prototype.getToolForNode = function ( node ) {
var name, tool, candidateTool, candidateToolName;
if ( !node.isInspectable() ) {
return undefined;
}
for ( name in this.registry ) {
tool = this.registry[name];
if ( tool.static.isCompatibleWith( node ) ) {
if ( !candidateTool || tool.prototype instanceof candidateTool ) {
candidateTool = tool;
candidateToolName = name;
}
}
}
return candidateToolName;
};
/* Initialization */
ve.ui.toolFactory = new ve.ui.ToolFactory();