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

128 lines
2.8 KiB
JavaScript
Raw Normal View History

var i18nDictionary = {
'text': 'Text',
'list': 'Lists'
};
var i18n = function( key ) {
if ( i18nDictionary[key] ) {
return i18nDictionary[key];
} else {
return key;
}
};
2011-11-23 21:50:47 +00:00
es.ToolbarView = function( $container, surfaceView ) {
// Reference for use in closures
var _this = this;
2011-11-23 21:50:47 +00:00
this.$ = $container;
this.surfaceView = surfaceView;
2011-11-23 21:50:47 +00:00
this.tools = [
{
name: 'text',
items: [
2011-11-23 21:50:47 +00:00
{
type: 'select',
name: 'formatting',
items: [
'',
'paragraph',
'preformatted',
'heading1',
'heading2',
'heading3',
'heading4',
'heading5',
'heading6'
],
state: function() {
return 'paragraph';
2011-11-23 21:50:47 +00:00
}
},
{
type: 'button',
name: 'bold'
},
{
type: 'button',
name: 'italic'
2011-11-23 21:50:47 +00:00
}
]
},
'/',
{
name: 'list',
items: [
2011-11-23 21:50:47 +00:00
{
type: 'button',
name: 'bullet'
2011-11-23 21:50:47 +00:00
},
{
type: 'button',
name: 'number'
2011-11-23 21:50:47 +00:00
}
]
}
];
this.setup();
2011-11-23 21:50:47 +00:00
this.surfaceView.model.on( 'select', function( selection ) {
_this.selection = selection;
_this.updateToolbar();
2011-11-23 21:50:47 +00:00
} );
this.surfaceView.on( 'update', function(a) {
_this.selection = _this.surfaceView.currentSelection;
_this.updateToolbar();
} );
};
es.ToolbarView.prototype.updateToolbar = function() {
if ( this.selection.from != this.selection.to ) {
// var nodes = this.surfaceView.documentView.selectNodes( this.selection, true);
// var nodes = nodes[0].node.selectNodes ( nodes[0].range, true );
}
2011-11-23 21:50:47 +00:00
};
2011-11-28 20:28:28 +00:00
es.ToolbarView.prototype.setup = function() {
2011-11-23 21:50:47 +00:00
for ( var i = this.tools.length - 1; i >= 0; i-- ) {
if ( !es.isPlainObject( this.tools[i] ) ) {
// divider
2011-11-23 21:50:47 +00:00
if ( this.tools[i] === '/' ) {
2011-11-28 20:28:28 +00:00
this.$.prepend( '<div class="es-toolbarDivider">' );
}
} else {
2011-11-23 21:50:47 +00:00
var $group = $( '<div>' )
.addClass( 'es-toolbarGroup' )
.addClass( 'es-toolbarGroup-' + this.tools[i].name )
2011-11-23 21:50:47 +00:00
.append(
$( '<div>' ).addClass( 'es-toolbarLabel' ).html( i18n( this.tools[i].name ) )
2011-11-23 21:50:47 +00:00
);
for ( var j = 0; j < this.tools[i].items.length; j++ ) {
var $tool = $('<div>').addClass( 'es-toolbarTool' );
if ( this.tools[i].items[j].type === 'button' ) {
// button
$tool.append(
$( '<img>' ).attr( 'src', 'images/' + this.tools[i].items[j].name + '.png')
2011-11-23 21:50:47 +00:00
);
} else if ( this.tools[i].items[j].type === 'select' ) {
// select
$select = $( '<select>' );
for ( var h = 0; h < this.tools[i].items[j].items.length; h++ ) {
$select.append(
$( '<option>' )
.html( i18n ( this.tools[i].items[j].items[h] ) )
.val( this.tools[i].items[j].items[h] )
);
}
$tool.append( $select );
2011-11-23 21:50:47 +00:00
}
$group.append( $tool );
this.tools[i].items[j].$ = $tool;
2011-11-23 21:50:47 +00:00
}
this.$.prepend( $group );
2011-11-23 21:50:47 +00:00
}
2011-11-28 20:28:28 +00:00
}
};