mediawiki-extensions-Visual.../modules/ve/ui/tools/ve.ui.ListButtonTool.js

146 lines
3.5 KiB
JavaScript
Raw Normal View History

2011-12-05 21:10:19 +00:00
/**
* Creates an ve.ui.ListButtonTool object.
*
2011-12-05 21:10:19 +00:00
* @class
* @constructor
* @extends {ve.ui.ButtonTool}
* @param {ve.ui.Toolbar} toolbar
2011-12-05 21:10:19 +00:00
* @param {String} name
*/
ve.ui.ListButtonTool = function( toolbar, name, title, data ) {
2011-12-05 21:10:19 +00:00
// Inheritance
ve.ui.ButtonTool.call( this, toolbar, name, title );
2011-12-05 21:10:19 +00:00
// Properties
2011-12-04 02:59:53 +00:00
this.data = data;
this.nodes = [];
};
2011-12-05 21:10:19 +00:00
/* Methods */
ve.ui.ListButtonTool.prototype.list = function( nodes, style ) {
var surface = this.toolbar.surfaceView,
model = surface.getModel(),
doc = model.getDocument(),
selection = model.getSelection(),
siblings = doc.selectNodes( selection, 'siblings'),
outerRange = null,
tx;
if ( siblings.length > 1 ){
outerRange = new ve.Range(
siblings[0].nodeOuterRange.from, siblings[siblings.length-1].nodeOuterRange.to
);
} else if ( siblings[0].node.parent !== null ) {
outerRange = ( siblings[0].node.parent.getOuterRange() );
}
2011-12-07 01:02:36 +00:00
if ( outerRange instanceof ve.Range ) {
// Convert everything to paragraphs first
tx = ve.dm.Transaction.newFromContentBranchConversion( doc, outerRange, 'paragraph' );
model.change( tx );
// Wrap everything in a list and each content branch in a listItem
tx = ve.dm.Transaction.newFromWrap(
doc,
outerRange,
[],
[{ 'type': 'list', 'attributes': { 'style': style } }],
[],
[{ 'type': 'listItem' }]
);
model.change ( tx );
// Modify selection
siblings = doc.selectNodes( selection, 'siblings'),
outerRange = new ve.Range(
siblings[0].nodeRange.from, siblings[siblings.length-1].nodeRange.to
);
model.change( null, outerRange );
}
};
ve.ui.ListButtonTool.prototype.unlist = function( node ){
var surface = this.toolbar.surfaceView,
model = surface.getModel(),
doc = model.getDocument(),
selection = model.getSelection(),
siblings = doc.selectNodes( selection, 'siblings'),
listNode,
tx;
if ( siblings.length === 0 ) {
return ;
2011-12-08 04:18:15 +00:00
}
listNode = siblings[0].node;
// Get the parent list node
while( listNode && listNode.getType() !== 'list' ) {
listNode = listNode.getParent();
2011-12-08 04:18:15 +00:00
}
tx = ve.dm.Transaction.newFromWrap(
doc,
listNode.getRange(),
[ { 'type': 'list' } ],
[],
[ { 'type': 'listItem' } ],
[]
);
2011-12-08 04:18:15 +00:00
model.change( tx );
};
ve.ui.ListButtonTool.prototype.onClick = function() {
this.toolbar.surfaceView.model.breakpoint();
if ( !this.$.hasClass( 'es-toolbarButtonTool-down' ) ) {
this.list( this.nodes, this.name );
} else {
this.unlist( this.nodes );
2011-12-04 02:59:53 +00:00
}
this.toolbar.surfaceView.model.breakpoint();
2011-12-04 02:59:53 +00:00
};
ve.ui.ListButtonTool.prototype.updateState = function( annotations, nodes ) {
function areListItemsOfStyle( nodes, style ){
var listNode = null;
for ( var i=0; i < nodes.length; i++ ) {
listNode = nodes[i];
// Get the list node
while( listNode && listNode.getType() !== 'list' ) {
listNode = listNode.getParent();
2011-12-04 02:59:53 +00:00
}
if( listNode && listNode.getAttribute('style') === style ) {
return true;
2011-12-04 02:59:53 +00:00
}
}
return false;
2011-12-04 02:59:53 +00:00
}
if ( areListItemsOfStyle( nodes, this.name ) ) {
2011-12-04 02:59:53 +00:00
this.$.addClass( 'es-toolbarButtonTool-down' );
} else {
this.$.removeClass( 'es-toolbarButtonTool-down' );
2011-12-04 02:59:53 +00:00
}
};
2011-12-05 21:10:19 +00:00
/* Registration */
ve.ui.Tool.tools.number = {
'constructor': ve.ui.ListButtonTool,
'name': 'number',
'title': ve.msg( 'visualeditor-listbutton-number-tooltip' ),
2011-12-04 02:59:53 +00:00
};
ve.ui.Tool.tools.bullet = {
'constructor': ve.ui.ListButtonTool,
'name': 'bullet',
'title': ve.msg( 'visualeditor-listbutton-bullet-tooltip' ),
2011-12-04 02:59:53 +00:00
};
2011-12-05 21:10:19 +00:00
/* Inheritance */
ve.extendClass( ve.ui.ListButtonTool, ve.ui.ButtonTool );