mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-26 23:31:02 +00:00
519d225d2f
Change-Id: Ifa96a9f70fa8d149a4c403521aaa88a3e0546ef0
96 lines
2.5 KiB
JavaScript
96 lines
2.5 KiB
JavaScript
// ToolbarView
|
|
ve.ui.Toolbar = function( $container, surfaceView, config ) {
|
|
// Inheritance TODO: Do we still need it?
|
|
ve.EventEmitter.call( this );
|
|
if ( !surfaceView ) {
|
|
return;
|
|
}
|
|
|
|
// References for use in closures
|
|
var _this = this,
|
|
$window = $( window );
|
|
|
|
// Properties
|
|
this.surfaceView = surfaceView;
|
|
this.$ = $container;
|
|
this.$groups = $( '<div class="es-toolbarGroups"></div>' ).prependTo( this.$ );
|
|
this.tools = [];
|
|
|
|
this.surfaceView.surfaceObserver.on( 'select', function ( selection ) {
|
|
var i;
|
|
if ( selection !== null ) {
|
|
var annotations = _this.surfaceView.getAnnotations(),
|
|
nodes = [],
|
|
model = _this.surfaceView.documentView.model;
|
|
|
|
if ( selection.from === selection.to ) {
|
|
nodes.push( model.getNodeFromOffset( selection.from ) );
|
|
} else {
|
|
var startNode = model.getNodeFromOffset( selection.start ),
|
|
endNode = model.getNodeFromOffset( selection.end );
|
|
if ( startNode === endNode ) {
|
|
nodes.push( startNode );
|
|
} else {
|
|
model.traverseLeafNodes( function( node ) {
|
|
nodes.push( node );
|
|
if( node === endNode ) {
|
|
return false;
|
|
}
|
|
}, startNode );
|
|
}
|
|
}
|
|
|
|
for ( i = 0; i < _this.tools.length; i++ ) {
|
|
_this.tools[i].updateState( annotations, nodes );
|
|
}
|
|
} else {
|
|
for ( i = 0; i < _this.tools.length; i++ ) {
|
|
_this.tools[i].clearState();
|
|
}
|
|
}
|
|
|
|
});
|
|
|
|
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'] }
|
|
];
|
|
this.setup();
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
ve.ui.Toolbar.prototype.getSurfaceView = function() {
|
|
return this.surfaceView;
|
|
};
|
|
|
|
ve.ui.Toolbar.prototype.setup = function() {
|
|
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] ];
|
|
if ( toolDefintion ) {
|
|
var tool = new toolDefintion.constructor(
|
|
this, toolDefintion.name, toolDefintion.title, toolDefintion.data
|
|
);
|
|
this.tools.push( tool );
|
|
$group.append( tool.$ );
|
|
}
|
|
}
|
|
|
|
this.$groups.append( $group );
|
|
}
|
|
};
|
|
|
|
ve.extendClass( ve.ui.Toolbar, ve.EventEmitter );
|