2011-11-30 07:10:15 +00:00
|
|
|
// ToolbarView
|
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-04-06 15:43:14 +00:00
|
|
|
|
|
|
|
this.surfaceView.on( 'rangeChange', function( e ) {
|
|
|
|
if ( e.new !== null ) {
|
2012-03-22 20:53:26 +00:00
|
|
|
var annotations = _this.surfaceView.getAnnotations(),
|
|
|
|
nodes = [],
|
|
|
|
model = _this.surfaceView.documentView.model;
|
2012-04-06 15:43:14 +00:00
|
|
|
|
|
|
|
if ( e.new.from === e.new.to ) {
|
|
|
|
nodes.push( model.getNodeFromOffset( e.new.from ) );
|
2012-03-15 22:06:40 +00:00
|
|
|
} else {
|
2012-04-06 15:43:14 +00:00
|
|
|
var startNode = model.getNodeFromOffset( e.new.start ),
|
|
|
|
endNode = model.getNodeFromOffset( e.new.end );
|
2012-03-22 20:53:26 +00:00
|
|
|
if ( startNode === endNode ) {
|
|
|
|
nodes.push( startNode );
|
|
|
|
} else {
|
|
|
|
model.traverseLeafNodes( function( node ) {
|
|
|
|
nodes.push( node );
|
|
|
|
if( node === endNode ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}, startNode );
|
|
|
|
}
|
2012-03-15 22:06:40 +00:00
|
|
|
}
|
|
|
|
|
2012-04-02 22:28:26 +00:00
|
|
|
for ( i = 0; i < _this.tools.length; i++ ) {
|
2012-03-15 22:06:40 +00:00
|
|
|
_this.tools[i].updateState( annotations, nodes );
|
|
|
|
}
|
2012-03-22 20:53:26 +00:00
|
|
|
} else {
|
2012-04-02 22:28:26 +00:00
|
|
|
for ( i = 0; i < _this.tools.length; i++ ) {
|
2012-03-22 20:53:26 +00:00
|
|
|
_this.tools[i].clearState();
|
|
|
|
}
|
2012-03-15 22:06:40 +00:00
|
|
|
}
|
2012-04-06 15:43:14 +00:00
|
|
|
} );
|
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-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
|
|
|
}
|
2011-11-30 07:35:59 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.ui.Toolbar, ve.EventEmitter );
|