mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.ViewRegistry.js
Trevor Parscal 3f3c87ce24 Link inspector fixes
ve.ce.Node.css
* Added prefixes for use of box-sizing

ve.ui.MWLinkInspector.js
* Whitespace

ve.ui.Inspector.css
* Corrected input width, always 100% wide now by using box-sizing

ve.ui.DialogButtonTool.js, ve.ui.Context.js
* Updated use of getViewsForNode

ve.ui.ViewRegistry.js
* Added inheritance-based prioritization for matching views with annotations and nodes

Bug: 47413

Change-Id: I286a28002c1691e58bbd7de04ed08cceb8b3bb07
2013-04-24 18:29:55 -07:00

113 lines
3.1 KiB
JavaScript

/*!
* VisualEditor UserInterface ViewRegistry class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* UserInterface view registry.
*
* @class
* @extends ve.Registry
*
* @constructor
*/
ve.ui.ViewRegistry = function VeUiViewRegistry() {
// Parent constructor
ve.Registry.call( this );
};
/* Inheritance */
ve.inheritClass( ve.ui.ViewRegistry, ve.Registry );
/* Methods */
ve.ui.ViewRegistry.prototype.isViewRelatedToModel = function ( view, model ) {
var classes = view.static.modelClasses || [],
i = classes.length;
while ( classes[--i] ) {
if ( model instanceof classes[i] ) {
return true;
}
}
return false;
};
/**
* Get a list of views from a set of annotations.
*
* The most specific view 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 views that can be used to inspect annotations
*/
ve.ui.ViewRegistry.prototype.getViewsForAnnotations = function ( annotations ) {
if ( annotations.isEmpty() ) {
return [];
}
var i, len, annotation, name, view, candidateView, candidateViewName,
arr = annotations.get(),
matches = [];
for ( i = 0, len = arr.length; i < len; i++ ) {
annotation = arr[i];
candidateView = null;
for ( name in this.registry ) {
view = this.registry[name];
if ( this.isViewRelatedToModel( view, annotation ) ) {
if ( !candidateView || view.prototype instanceof candidateView ) {
candidateView = view;
candidateViewName = name;
}
}
}
if ( candidateView ) {
matches.push( candidateViewName );
}
}
return matches;
};
/**
* Get a view for a node.
*
* The most specific view 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 view that can be used to edit node
*/
ve.ui.ViewRegistry.prototype.getViewForNode = function ( node ) {
var name, view, candidateView, candidateViewName;
for ( name in this.registry ) {
view = this.registry[name];
if ( this.isViewRelatedToModel( view, node ) ) {
if ( !candidateView || view.prototype instanceof candidateView ) {
candidateView = view;
candidateViewName = name;
}
}
}
return candidateViewName;
};
/* Initialization */
ve.ui.viewRegistry = new ve.ui.ViewRegistry();