2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor user interface Toolbar class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Editing toolbar.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Toolbar = function( $container, surfaceView, config ) {
|
2011-11-30 23:51:06 +00:00
|
|
|
// Inheritance TODO: Do we still need it?
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.EventEmitter.call( this );
|
2011-12-09 01:28:44 +00:00
|
|
|
if ( !surfaceView ) {
|
|
|
|
return;
|
|
|
|
}
|
2011-11-30 07:35:59 +00:00
|
|
|
|
2011-11-29 21:34:56 +00:00
|
|
|
// References for use in closures
|
|
|
|
var _this = this,
|
2012-03-03 00:17:15 +00:00
|
|
|
$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;
|
2011-11-30 07:10:15 +00:00
|
|
|
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 = [];
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
// 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 || [
|
2011-12-02 21:31:49 +00:00
|
|
|
{ 'name': 'history', 'items' : ['undo', 'redo'] },
|
|
|
|
{ 'name': 'textStyle', 'items' : ['format'] },
|
|
|
|
{ 'name': 'textStyle', 'items' : ['bold', 'italic', 'link', 'clear'] },
|
2011-12-10 07:17:44 +00:00
|
|
|
{ '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
|
|
|
|
2011-12-06 23:48:47 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
2012-07-19 03:40:49 +00:00
|
|
|
_this = this,
|
|
|
|
i;
|
2012-06-20 01:20:28 +00:00
|
|
|
|
|
|
|
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 );
|
2012-06-21 04:53:42 +00:00
|
|
|
|
|
|
|
if(startNode.type === 'document' || endNode.type === 'document') {
|
|
|
|
// Clear state
|
|
|
|
for ( i = 0; i < this.tools.length; i++ ) {
|
|
|
|
this.tools[i].clearState();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-20 01:20:28 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Toolbar.prototype.getSurfaceView = function() {
|
2011-12-06 23:48:47 +00:00
|
|
|
return this.surfaceView;
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Toolbar.prototype.setup = function() {
|
2011-11-30 22:44:11 +00:00
|
|
|
for ( var i = 0; i < this.config.length; i++ ) {
|
2011-11-30 07:10:15 +00:00
|
|
|
var $group = $( '<div>' )
|
|
|
|
.addClass( 'es-toolbarGroup' )
|
2011-11-30 23:40:33 +00:00
|
|
|
.addClass( 'es-toolbarGroup-' + this.config[i].name );
|
|
|
|
if ( this.config[i].label ) {
|
|
|
|
$group.append(
|
|
|
|
$( '<div>' ).addClass( 'es-toolbarLabel' ).html( this.config[i].label )
|
2011-11-30 07:10:15 +00:00
|
|
|
);
|
2011-11-30 23:40:33 +00:00
|
|
|
}
|
2011-11-30 07:10:15 +00:00
|
|
|
|
|
|
|
for ( var j = 0; j < this.config[i].items.length; j++ ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
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(
|
2011-12-09 21:16:42 +00:00
|
|
|
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
|
|
|
}
|
2011-11-30 07:10:15 +00:00
|
|
|
|
2012-03-03 00:17:15 +00:00
|
|
|
this.$groups.append( $group );
|
2011-11-29 21:34:56 +00:00
|
|
|
}
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2011-11-30 07:35:59 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.ui.Toolbar, ve.EventEmitter );
|