mediawiki-extensions-Visual.../modules/es/views/es.ToolbarView.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

// 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?
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;
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 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
* 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);
}
}
} );
this.surfaceView.on( 'cursor', function( annotations, nodes ) {
for( var i = 0; i < _this.tools.length; i++ ) {
_this.tools[i].updateState( annotations, nodes );
}
2011-11-30 23:51:06 +00:00
} );
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', 'indent', 'outdent' ] }
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
es.ToolbarView.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++ ) {
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 22:44:11 +00:00
this.$groups.append( $group );
2011-11-29 21:34:56 +00:00
}
};
es.extendClass( es.ToolbarView, es.EventEmitter );