mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.ViewRegistry.js
Trevor Parscal bf254f44da UI "Views" refactor
Objective:

Make it possible for inspectors to inspect nodes or annotations, rather
than only annotations. Meanwhile, also make it possible for dialogs to
edit an annotation.

Strategy:

Switch from using type patterns to associate inspectors with annotations
to using arrays of classes, similar to how dialogs already work.
Introduce a view registry which provides lookups for relationships
between models and views. This is more centralized and less repetitive
than implement matching functions for both annotations and nodes in both
the dialog and inspector factories.

Changes:

*.php
* Added links to new file

ve.AnnotationAction.js
* Removed unused parameter to filter annotations using a string or regexp

ve.dm.AnnotationSet.js
* Switched from property/value arguments to callbacks

ve.ui.*(Dialog|Inspector).js
* Replaced type patterns with class lists
* Added class to view registry

ve.ui.*Tool.js, ve.ui.Context.js
* Updated model/view relationship lookup

ve.ui.*Factory.js
* Removed overly-specific lookup functions

ve.ui.Inspector.js
* Removed typePattern property
* Updated model/view relationship lookup

ve.ui.ViewRegistry.js
* New class!
* Migrated node and annotation lookup functions from factories

Change-Id: Ic2bbcf072fdd87e5ce8a03fe1ae3e6d8d50e2593
2013-04-18 15:53:50 -07:00

92 lines
1.9 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.
*
* @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,
arr = annotations.get(),
matches = [];
for ( i = 0, len = arr.length; i < len; i++ ) {
annotation = arr[i];
for ( name in this.registry ) {
view = this.registry[name];
if ( this.isViewRelatedToModel( view, annotation ) ) {
matches.push( name );
break;
}
}
}
return matches;
};
/**
* Get a list of views for a node.
*
* @method
* @param {ve.dm.Node} node Node to be edited
* @returns {string[]} Symbolic names of views that can be used to edit node
*/
ve.ui.ViewRegistry.prototype.getViewsForNode = function ( node ) {
var name, view,
matches = [];
for ( name in this.registry ) {
view = this.registry[name];
if ( this.isViewRelatedToModel( view, node ) ) {
matches.push( name );
break;
}
}
return matches;
};
/* Initialization */
ve.ui.viewRegistry = new ve.ui.ViewRegistry();