2013-04-15 21:35:32 +00:00
|
|
|
/*!
|
|
|
|
* 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.
|
|
|
|
*
|
2013-04-25 01:18:27 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2013-04-15 21:35:32 +00:00
|
|
|
* @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 [];
|
|
|
|
}
|
|
|
|
|
2013-04-25 01:18:27 +00:00
|
|
|
var i, len, annotation, name, view, candidateView, candidateViewName,
|
2013-04-15 21:35:32 +00:00
|
|
|
arr = annotations.get(),
|
|
|
|
matches = [];
|
|
|
|
|
|
|
|
for ( i = 0, len = arr.length; i < len; i++ ) {
|
|
|
|
annotation = arr[i];
|
2013-04-25 01:18:27 +00:00
|
|
|
candidateView = null;
|
2013-04-15 21:35:32 +00:00
|
|
|
for ( name in this.registry ) {
|
|
|
|
view = this.registry[name];
|
|
|
|
if ( this.isViewRelatedToModel( view, annotation ) ) {
|
2013-04-25 01:18:27 +00:00
|
|
|
if ( !candidateView || view.prototype instanceof candidateView ) {
|
|
|
|
candidateView = view;
|
|
|
|
candidateViewName = name;
|
|
|
|
}
|
2013-04-15 21:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 01:18:27 +00:00
|
|
|
if ( candidateView ) {
|
|
|
|
matches.push( candidateViewName );
|
|
|
|
}
|
2013-04-15 21:35:32 +00:00
|
|
|
}
|
|
|
|
return matches;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-04-25 01:18:27 +00:00
|
|
|
* 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.
|
2013-04-15 21:35:32 +00:00
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Node} node Node to be edited
|
2013-04-25 01:18:27 +00:00
|
|
|
* @returns {string|undefined} Symbolic name of view that can be used to edit node
|
2013-04-15 21:35:32 +00:00
|
|
|
*/
|
2013-04-25 01:18:27 +00:00
|
|
|
ve.ui.ViewRegistry.prototype.getViewForNode = function ( node ) {
|
|
|
|
var name, view, candidateView, candidateViewName;
|
2013-04-15 21:35:32 +00:00
|
|
|
|
|
|
|
for ( name in this.registry ) {
|
|
|
|
view = this.registry[name];
|
|
|
|
if ( this.isViewRelatedToModel( view, node ) ) {
|
2013-04-25 01:18:27 +00:00
|
|
|
if ( !candidateView || view.prototype instanceof candidateView ) {
|
|
|
|
candidateView = view;
|
|
|
|
candidateViewName = name;
|
|
|
|
}
|
2013-04-15 21:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-25 01:18:27 +00:00
|
|
|
return candidateViewName;
|
2013-04-15 21:35:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
ve.ui.viewRegistry = new ve.ui.ViewRegistry();
|