2011-12-05 21:10:19 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.FormatDropdownTool object.
|
2011-12-05 21:10:19 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @extends {ve.ui.DropdownTool}
|
|
|
|
* @param {ve.ui.Toolbar} toolbar
|
2011-12-05 21:10:19 +00:00
|
|
|
* @param {String} name
|
|
|
|
* @param {Object[]} items
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.FormatDropdownTool = function( toolbar, name, title ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.DropdownTool.call( this, toolbar, name, title, [
|
2012-03-05 22:08:35 +00:00
|
|
|
{
|
2011-12-04 02:59:53 +00:00
|
|
|
'name': 'paragraph',
|
|
|
|
'label': 'Paragraph',
|
|
|
|
'type' : 'paragraph'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'heading-1',
|
|
|
|
'label': 'Heading 1',
|
|
|
|
'type' : 'heading',
|
|
|
|
'attributes': { 'level': 1 }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'heading-2',
|
|
|
|
'label': 'Heading 2',
|
|
|
|
'type' : 'heading',
|
|
|
|
'attributes': { 'level': 2 }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'heading-3',
|
|
|
|
'label': 'Heading 3',
|
|
|
|
'type' : 'heading',
|
|
|
|
'attributes': { 'level': 3 }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'heading-4',
|
|
|
|
'label': 'Heading 4',
|
|
|
|
'type' : 'heading',
|
|
|
|
'attributes': { 'level': 4 }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'heading-5',
|
|
|
|
'label': 'Heading 5',
|
|
|
|
'type' : 'heading',
|
|
|
|
'attributes': { 'level': 5 }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'heading-6',
|
|
|
|
'label': 'Heading 6',
|
|
|
|
'type' : 'heading',
|
|
|
|
'attributes': { 'level': 6 }
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'name': 'pre',
|
|
|
|
'label': 'Preformatted',
|
|
|
|
'type' : 'pre'
|
|
|
|
}
|
|
|
|
] );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.FormatDropdownTool.prototype.onSelect = function( item ) {
|
2012-03-14 00:29:41 +00:00
|
|
|
this.toolbar.surfaceView.stopPolling();
|
2011-12-04 02:59:53 +00:00
|
|
|
var txs = this.toolbar.surfaceView.model.getDocument().prepareLeafConversion(
|
2012-03-14 00:29:41 +00:00
|
|
|
this.toolbar.surfaceView.getSelectionRange(),
|
2011-12-04 02:59:53 +00:00
|
|
|
item.type,
|
|
|
|
item.attributes
|
|
|
|
);
|
2011-12-08 03:14:10 +00:00
|
|
|
for ( var i = 0; i < txs.length; i++ ) {
|
|
|
|
this.toolbar.surfaceView.model.transact( txs[i] );
|
|
|
|
}
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.FormatDropdownTool.prototype.updateState = function( annotations, nodes ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
// Get type and attributes of the first node
|
|
|
|
var i,
|
|
|
|
format = {
|
|
|
|
'type': nodes[0].getElementType(),
|
|
|
|
'attributes': nodes[0].getElement().attributes
|
|
|
|
};
|
|
|
|
// Look for mismatches, in which case format should be null
|
|
|
|
for ( i = 1; i < nodes.length; i++ ) {
|
|
|
|
if ( format.type != nodes[i].getElementType() ||
|
2012-02-06 23:50:56 +00:00
|
|
|
!ve.compareObjects( format.attributes, nodes[i].element.attributes ) ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
format = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( format === null ) {
|
2011-12-06 00:10:30 +00:00
|
|
|
this.$label.html( ' ' );
|
2011-12-04 02:59:53 +00:00
|
|
|
} else {
|
|
|
|
var items = this.menuView.getItems();
|
|
|
|
for ( i = 0; i < items.length; i++ ) {
|
|
|
|
if (
|
|
|
|
format.type === items[i].type &&
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.compareObjects( format.attributes, items[i].attributes )
|
2011-12-04 02:59:53 +00:00
|
|
|
) {
|
2011-12-06 00:10:30 +00:00
|
|
|
this.$label.text( items[i].label );
|
2011-12-04 02:59:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Tool.tools.format = {
|
|
|
|
'constructor': ve.FormatDropdownTool,
|
2011-12-09 21:16:42 +00:00
|
|
|
'name': 'format',
|
|
|
|
'title': 'Change format'
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.FormatDropdownTool, ve.ui.DropdownTool );
|