mediawiki-extensions-Visual.../modules/ve/ui/ve.ui.Toolbar.js

142 lines
3.4 KiB
JavaScript
Raw Normal View History

/**
* VisualEditor user interface Toolbar class.
*
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Editing toolbar.
*
* @class
* @constructor
*/
ve.ui.Toolbar = function( $container, surfaceView, config ) {
2011-11-30 23:51:06 +00:00
// Inheritance TODO: Do we still need it?
ve.EventEmitter.call( this );
if ( !surfaceView ) {
return;
}
2011-11-29 21:34:56 +00:00
// References for use in closures
var _this = this,
$window = $( window );
2011-11-29 21:34:56 +00:00
2011-11-30 23:51:06 +00:00
// Properties
2011-11-29 21:34:56 +00:00
this.surfaceView = surfaceView;
this.$ = $container;
2011-11-30 22:44:11 +00:00
this.$groups = $( '<div class="es-toolbarGroups"></div>' ).prependTo( this.$ );
2011-11-30 23:51:06 +00:00
this.tools = [];
// Update tools on selection and all transactions.
this.surfaceView.model.on( 'change', ve.proxy( this.updateTools, this ) );
2011-11-29 21:34:56 +00:00
2011-11-30 23:05:06 +00:00
this.config = config || [
{ 'name': 'history', 'items' : ['undo', 'redo'] },
{ 'name': 'textStyle', 'items' : ['format'] },
{ 'name': 'textStyle', 'items' : ['bold', 'italic', 'link', 'clear'] },
{ 'name': 'list', 'items' : ['number', 'bullet', 'outdent', 'indent'] }
2011-11-29 21:34:56 +00:00
];
2011-11-30 23:51:06 +00:00
this.setup();
};
2011-11-29 21:34:56 +00:00
/* Methods */
/**
* Triggers update events on all tools.
*
* @method
*/
ve.ui.Toolbar.prototype.updateTools = function() {
var model = this.surfaceView.getModel(),
doc = model.getDocument(),
annotations,
nodes = [],
range = model.getSelection(),
startNode,
endNode,
_this = this,
i;
if ( range !== null ) {
if ( range.from === range.to ){
nodes.push( doc.getNodeFromOffset( range.from ) );
} else {
startNode = doc.getNodeFromOffset( range.from );
endNode = doc.getNodeFromOffset ( range.end );
if(startNode.type === 'document' || endNode.type === 'document') {
// Clear state
for ( i = 0; i < this.tools.length; i++ ) {
this.tools[i].clearState();
}
return;
}
// These should be different, alas just in case.
if ( startNode === endNode ) {
nodes.push( startNode );
} else {
model.getDocument().getDocumentNode().traverseLeafNodes( function( node ) {
nodes.push( node );
if( node === endNode ) {
return false;
}
}, startNode );
}
}
if ( range.getLength() > 0 ) {
annotations = doc.getAnnotationsFromRange( range );
} else {
// Clear context
_this.surfaceView.contextView.clear();
annotations = doc.getAnnotationsFromOffset(
doc.getNearestContentOffset( range.start - 1 )
);
}
// Update state
for ( i = 0; i < this.tools.length; i++ ) {
this.tools[i].updateState( annotations, nodes );
}
} else {
// Clear state
for ( i = 0; i < this.tools.length; i++ ) {
this.tools[i].clearState();
}
}
};
ve.ui.Toolbar.prototype.getSurfaceView = function() {
return this.surfaceView;
};
ve.ui.Toolbar.prototype.setup = function() {
2011-11-30 22:44:11 +00:00
for ( var i = 0; i < this.config.length; i++ ) {
var $group = $( '<div>' )
.addClass( 'es-toolbarGroup' )
.addClass( 'es-toolbarGroup-' + this.config[i].name );
if ( this.config[i].label ) {
$group.append(
$( '<div>' ).addClass( 'es-toolbarLabel' ).html( this.config[i].label )
);
}
for ( var j = 0; j < this.config[i].items.length; j++ ) {
var toolDefintion = ve.ui.Tool.tools[ this.config[i].items[j] ];
2011-11-30 23:05:06 +00:00
if ( toolDefintion ) {
var tool = new toolDefintion.constructor(
this, toolDefintion.name, toolDefintion.title, toolDefintion.data
2011-11-30 23:05:06 +00:00
);
2011-11-30 23:51:06 +00:00
this.tools.push( tool );
2011-11-30 23:05:06 +00:00
$group.append( tool.$ );
}
2011-11-29 21:34:56 +00:00
}
this.$groups.append( $group );
2011-11-29 21:34:56 +00:00
}
};
ve.extendClass( ve.ui.Toolbar, ve.EventEmitter );