mediawiki-extensions-Visual.../modules/es/tools/es.FormatDropdownTool.js

101 lines
2.3 KiB
JavaScript
Raw Normal View History

es.FormatDropdownTool = function( toolbar, name ) {
es.DropdownTool.call( this, toolbar, name );
this.formats = [
{
'name': 'Paragraph',
'type' : 'paragraph'
},
{
'name': 'Heading Level 1',
'type' : 'heading',
'attributes': { 'level': 1 }
},
{
'name': 'Heading Level 2',
'type' : 'heading',
'attributes': { 'level': 2 }
},
{
'name': 'Heading Level 3',
'type' : 'heading',
'attributes': { 'level': 3 }
},
{
'name': 'Heading Level 4',
'type' : 'heading',
'attributes': { 'level': 4 }
},
{
'name': 'Heading Level 5',
'type' : 'heading',
'attributes': { 'level': 5 }
},
{
'name': 'Heading Level 6',
'type' : 'heading',
'attributes': { 'level': 6 }
},
{
'name': 'Preformatted',
'type' : 'pre'
}
];
this.$select.append( '<option>' );
for ( var i = 0; i < this.formats.length; i++ ) {
$( '<option>' )
.val( i )
.html( this.formats[i].name )
.appendTo( this.$select );
}
};
es.FormatDropdownTool.prototype.onChange = function() {
var index = this.$select.val();
if ( index in this.formats ) {
var txs = this.toolbar.surfaceView.model.getDocument().prepareLeafConversion(
this.toolbar.surfaceView.currentSelection,
this.formats[index].type,
this.formats[index].attributes
)
for ( var i = 0; i < txs.length; i++ ) {
this.toolbar.surfaceView.model.transact( txs[i] );
}
}
};
es.FormatDropdownTool.prototype.updateState = function( annotations, nodes ) {
var format = {
'type': nodes[0].getElementType(),
'attributes': nodes[0].getElement().attributes
};
for( var i = 1; i < nodes.length; i++ ) {
if ( format.type != nodes[i].getElementType()
|| !es.compareObjects( format.attributes, nodes[i].element.attributes ) ) {
format = null;
break;
}
}
if ( format === null ) {
this.$select.val( null );
} else {
for ( var i = 0; i < this.formats.length; i++ ) {
if ( format.type === this.formats[i].type
&& es.compareObjects( format.attributes, this.formats[i].attributes ) ) {
this.$select.val( i );
break;
}
}
}
};
es.Tool.tools.format = {
constructor: es.FormatDropdownTool,
name: 'format'
};
es.extendClass( es.FormatDropdownTool, es.DropdownTool );