2012-05-21 19:37:30 +00:00
|
|
|
/**
|
|
|
|
* Creates an ve.ui.ListButtonTool object.
|
2012-06-13 23:06:21 +00:00
|
|
|
*
|
2012-05-21 19:37:30 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.ui.ButtonTool}
|
|
|
|
* @param {ve.ui.Toolbar} toolbar
|
|
|
|
* @param {String} name
|
|
|
|
*/
|
|
|
|
ve.ui.ListButtonTool = function( toolbar, name, title, data ) {
|
|
|
|
// Inheritance
|
|
|
|
ve.ui.ButtonTool.call( this, toolbar, name, title );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.data = data;
|
|
|
|
this.nodes = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
ve.ui.ListButtonTool.prototype.list = function( nodes, style ) {
|
2012-06-15 00:21:37 +00:00
|
|
|
var surface = this.toolbar.surfaceView,
|
|
|
|
model = surface.getModel(),
|
|
|
|
doc = model.getDocument(),
|
|
|
|
selection = model.getSelection(),
|
|
|
|
siblings = doc.selectNodes( selection, 'siblings'),
|
|
|
|
outerRange = null,
|
|
|
|
tx;
|
2012-05-21 19:37:30 +00:00
|
|
|
|
2012-06-15 00:21:37 +00:00
|
|
|
if ( siblings.length > 1 ){
|
|
|
|
outerRange = new ve.Range(
|
|
|
|
siblings[0].nodeOuterRange.from,
|
|
|
|
siblings[siblings.length-1].nodeOuterRange.to
|
|
|
|
);
|
2012-06-19 19:55:17 +00:00
|
|
|
} else if( siblings[0].node['parent'] !== null ) {
|
2012-06-15 00:21:37 +00:00
|
|
|
outerRange = ( siblings[0].node['parent'].getOuterRange() );
|
|
|
|
}
|
|
|
|
|
2012-06-19 19:55:17 +00:00
|
|
|
if( outerRange instanceof ve.Range ) {
|
|
|
|
// Convert everything to paragraphs first
|
|
|
|
tx = ve.dm.Transaction.newFromContentBranchConversion( doc, outerRange, 'paragraph' );
|
|
|
|
model.change( tx );
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2012-05-21 19:37:30 +00:00
|
|
|
};
|
|
|
|
|
2012-06-15 00:21:37 +00:00
|
|
|
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'),
|
2012-06-19 17:04:51 +00:00
|
|
|
listNode,
|
2012-06-15 00:21:37 +00:00
|
|
|
tx;
|
2012-05-21 19:37:30 +00:00
|
|
|
|
2012-06-19 17:04:51 +00:00
|
|
|
if ( siblings.length === 0 ) {
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
listNode = siblings[0].node;
|
|
|
|
// Get the parent list node
|
2012-06-19 19:55:17 +00:00
|
|
|
while( listNode && listNode.getType() !== 'list' ) {
|
2012-06-19 17:04:51 +00:00
|
|
|
listNode = listNode.getParent();
|
2012-06-15 00:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tx = ve.dm.Transaction.newFromWrap(
|
|
|
|
doc,
|
2012-06-19 17:04:51 +00:00
|
|
|
listNode.getRange(),
|
2012-06-15 00:21:37 +00:00
|
|
|
[ { 'type': 'list' } ],
|
|
|
|
[],
|
|
|
|
[ { 'type': 'listItem' } ],
|
|
|
|
[]
|
|
|
|
);
|
2012-05-21 19:37:30 +00:00
|
|
|
|
2012-06-19 05:05:05 +00:00
|
|
|
model.change( tx );
|
2012-06-15 00:21:37 +00:00
|
|
|
};
|
2012-06-19 17:04:51 +00:00
|
|
|
|
2012-05-21 19:37:30 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
this.toolbar.surfaceView.model.breakpoint();
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.ListButtonTool.prototype.updateState = function( annotations, nodes ) {
|
2012-06-19 17:04:51 +00:00
|
|
|
|
2012-06-19 19:55:17 +00:00
|
|
|
function areListItemsOfStyle( nodes, style ){
|
|
|
|
var listNode = null;
|
2012-06-19 17:04:51 +00:00
|
|
|
for ( var i=0; i < nodes.length; i++ ) {
|
2012-06-19 19:55:17 +00:00
|
|
|
listNode = nodes[i];
|
|
|
|
// Get the list node
|
|
|
|
while( listNode && listNode.getType() !== 'list' ) {
|
|
|
|
listNode = listNode.getParent();
|
2012-05-21 19:37:30 +00:00
|
|
|
}
|
2012-06-19 19:55:17 +00:00
|
|
|
if( listNode && listNode.getAttribute('style') === style ) {
|
|
|
|
return true;
|
2012-05-21 19:37:30 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-19 19:55:17 +00:00
|
|
|
return false;
|
2012-05-21 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
2012-06-19 17:04:51 +00:00
|
|
|
|
2012-06-19 19:55:17 +00:00
|
|
|
if ( areListItemsOfStyle( nodes, this.name ) ) {
|
2012-05-21 19:37:30 +00:00
|
|
|
this.$.addClass( 'es-toolbarButtonTool-down' );
|
|
|
|
} else {
|
|
|
|
this.$.removeClass( 'es-toolbarButtonTool-down' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.Tool.tools.number = {
|
|
|
|
'constructor': ve.ui.ListButtonTool,
|
|
|
|
'name': 'number',
|
2012-06-19 08:30:30 +00:00
|
|
|
'title': mw.msg( 'visualeditor-listbutton-number-tooltip' ),
|
2012-05-21 19:37:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.Tool.tools.bullet = {
|
|
|
|
'constructor': ve.ui.ListButtonTool,
|
|
|
|
'name': 'bullet',
|
2012-06-19 08:30:30 +00:00
|
|
|
'title': mw.msg( 'visualeditor-listbutton-bullet-tooltip' ),
|
2012-05-21 19:37:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.ui.ListButtonTool, ve.ui.ButtonTool );
|