2011-11-30 07:10:15 +00:00
|
|
|
// ToolbarView
|
2011-11-30 23:05:06 +00:00
|
|
|
es.ToolbarView = function( $container, surfaceView, config ) {
|
2011-11-30 23:51:06 +00:00
|
|
|
// Inheritance TODO: Do we still need it?
|
2011-11-30 07:35:59 +00:00
|
|
|
es.EventEmitter.call( this );
|
|
|
|
|
2011-11-29 21:34:56 +00:00
|
|
|
// References for use in closures
|
|
|
|
var _this = this,
|
|
|
|
$window = $( window );
|
|
|
|
|
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.$spacer = $('<div></div>');
|
2011-11-29 21:34:56 +00:00
|
|
|
this.$.after( this.$spacer );
|
2011-11-30 23:51:06 +00:00
|
|
|
this.tools = [];
|
|
|
|
|
2011-11-30 07:10:15 +00:00
|
|
|
|
2011-11-30 23:51:06 +00:00
|
|
|
// Events
|
2011-11-29 21:34:56 +00:00
|
|
|
/*
|
|
|
|
* This code is responsible for switching toolbar into floating mode when scrolling (with
|
2011-11-29 01:13:50 +00:00
|
|
|
* keyboard or mouse).
|
2011-11-29 21:34:56 +00:00
|
|
|
*/
|
|
|
|
$window.scroll( function() {
|
|
|
|
if ( _this.surfaceView.dimensions.scrollTop >= _this.surfaceView.dimensions.toolbarTop ) {
|
|
|
|
if ( ! _this.$.hasClass( 'float' ) ) {
|
|
|
|
var left = _this.$.offset().left,
|
|
|
|
right = $window.width() - _this.$.outerWidth() - left;
|
|
|
|
_this.$.css( 'right', right );
|
|
|
|
_this.$.css( 'left', left );
|
|
|
|
_this.$.addClass( 'float' );
|
|
|
|
_this.$spacer.height( _this.$.height() );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( _this.$.hasClass( 'float' ) ) {
|
|
|
|
_this.$.css( 'right', 0 );
|
|
|
|
_this.$.css( 'left', 0 );
|
|
|
|
_this.$.removeClass( 'float' );
|
|
|
|
_this.$spacer.height(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
2011-11-30 23:51:06 +00:00
|
|
|
this.surfaceView.model.on( 'select', function() {
|
|
|
|
_this.updateState();
|
|
|
|
} );
|
2011-12-01 21:44:46 +00:00
|
|
|
this.surfaceView.on( 'cursor', function() {
|
2011-11-30 23:51:06 +00:00
|
|
|
_this.updateState();
|
|
|
|
} );
|
2011-11-29 21:34:56 +00:00
|
|
|
|
2011-11-30 23:05:06 +00:00
|
|
|
this.config = config || [
|
2011-12-01 23:24:21 +00:00
|
|
|
{ 'name': 'textStyle', 'items' : [ 'bold', 'italic', 'link', 'clear', 'format' ] },
|
2011-12-02 06:43:26 +00:00
|
|
|
{ 'name': 'history', 'items' : [ 'undo', 'redo' ] },
|
|
|
|
{ 'name': 'list', 'items' : [ 'number', 'bullet' ] }
|
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-11-30 23:51:06 +00:00
|
|
|
es.ToolbarView.prototype.updateState = function() {
|
|
|
|
var selection = this.surfaceView.currentSelection,
|
2011-12-02 00:02:16 +00:00
|
|
|
annotations,
|
|
|
|
nodes = [];
|
2011-11-30 23:51:06 +00:00
|
|
|
|
|
|
|
if( selection.from === selection.to ) {
|
2011-12-01 23:25:56 +00:00
|
|
|
var insertionAnnotations = this.surfaceView.getInsertionAnnotations();
|
|
|
|
annotations = {
|
|
|
|
'full': insertionAnnotations,
|
|
|
|
'partial': [],
|
|
|
|
'all': insertionAnnotations
|
|
|
|
};
|
2011-12-02 00:02:16 +00:00
|
|
|
nodes.push( this.surfaceView.documentView.model.getNodeFromOffset( selection.from ) );
|
2011-11-30 23:51:06 +00:00
|
|
|
} else {
|
|
|
|
annotations = this.surfaceView.documentView.model.getAnnotationsFromRange( selection );
|
2011-12-02 00:02:16 +00:00
|
|
|
var startNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.start ),
|
|
|
|
endNode = this.surfaceView.documentView.model.getNodeFromOffset( selection.end );
|
|
|
|
if ( startNode === endNode ) {
|
|
|
|
nodes.push( startNode );
|
|
|
|
} else {
|
|
|
|
this.surfaceView.documentView.model.traverseLeafNodes( function( node ) {
|
|
|
|
nodes.push( node );
|
|
|
|
if( node === endNode ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}, startNode );
|
|
|
|
}
|
2011-11-30 23:51:06 +00:00
|
|
|
}
|
2011-12-01 00:37:17 +00:00
|
|
|
|
|
|
|
for( var i = 0; i < this.tools.length; i++ ) {
|
2011-12-02 00:02:16 +00:00
|
|
|
this.tools[i].updateState( annotations, nodes );
|
2011-12-01 00:37:17 +00:00
|
|
|
}
|
2011-11-29 21:34:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
es.ToolbarView.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++ ) {
|
2011-11-30 23:05:06 +00:00
|
|
|
var toolDefintion = es.Tool.tools[ this.config[i].items[j] ];
|
|
|
|
if ( toolDefintion ) {
|
|
|
|
var tool = new toolDefintion.constructor(
|
|
|
|
this, toolDefintion.name, toolDefintion.data
|
|
|
|
);
|
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
|
|
|
|
2011-11-30 22:44:11 +00:00
|
|
|
this.$groups.append( $group );
|
2011-11-29 21:34:56 +00:00
|
|
|
}
|
2011-11-30 07:35:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
es.extendClass( es.ToolbarView, es.EventEmitter );
|