2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor user interface ListButtonTool class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.ui.ListButtonTool object.
|
2012-06-20 01:20:28 +00:00
|
|
|
*
|
2011-12-05 21:10:19 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
2012-02-06 23:50:56 +00:00
|
|
|
* @extends {ve.ui.ButtonTool}
|
|
|
|
* @param {ve.ui.Toolbar} toolbar
|
2011-12-05 21:10:19 +00:00
|
|
|
* @param {String} name
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.ListButtonTool = function( toolbar, name, title, data ) {
|
2011-12-05 21:10:19 +00:00
|
|
|
// Inheritance
|
2012-02-06 23:50:56 +00:00
|
|
|
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 */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.ListButtonTool.prototype.list = function( nodes, style ) {
|
2012-06-20 22:04:15 +00:00
|
|
|
var surfaceModel = this.toolbar.getSurfaceView().getModel(),
|
|
|
|
documentModel = surfaceModel.getDocument(),
|
2012-06-20 19:27:46 +00:00
|
|
|
selection = surfaceModel.getSelection(),
|
2012-06-20 22:04:15 +00:00
|
|
|
groups = documentModel.getCoveredSiblingGroups( selection ),
|
|
|
|
previousList,
|
|
|
|
groupRange,
|
|
|
|
group,
|
|
|
|
tx;
|
|
|
|
for ( var i = 0; i < groups.length; i++ ) {
|
|
|
|
group = groups[i];
|
|
|
|
if ( group.grandparent && group.grandparent.getType() === 'list' ) {
|
|
|
|
if ( group.grandparent !== previousList ) {
|
2012-06-20 19:27:46 +00:00
|
|
|
// Change the list style
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChange(
|
2012-06-20 22:04:15 +00:00
|
|
|
documentModel, group.grandparent.getOffset(), 'style', style
|
2012-06-20 19:27:46 +00:00
|
|
|
),
|
|
|
|
selection
|
|
|
|
);
|
|
|
|
// Skip this one next time
|
2012-06-20 22:04:15 +00:00
|
|
|
previousList = group.grandparent;
|
2012-06-20 19:27:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-06-20 22:04:15 +00:00
|
|
|
// Get a range that covers the whole group
|
|
|
|
groupRange = new ve.Range(
|
|
|
|
group.nodes[0].getOuterRange().start,
|
|
|
|
group.nodes[group.nodes.length - 1].getOuterRange().end
|
2012-06-20 19:27:46 +00:00
|
|
|
);
|
|
|
|
// Convert everything to paragraphs first
|
|
|
|
surfaceModel.change(
|
2012-06-20 22:04:15 +00:00
|
|
|
ve.dm.Transaction.newFromContentBranchConversion(
|
|
|
|
documentModel, groupRange, 'paragraph'
|
|
|
|
),
|
2012-06-20 19:27:46 +00:00
|
|
|
selection
|
|
|
|
);
|
|
|
|
// Wrap everything in a list and each content branch in a listItem
|
2012-06-20 22:04:15 +00:00
|
|
|
tx = ve.dm.Transaction.newFromWrap(
|
|
|
|
documentModel,
|
|
|
|
groupRange,
|
|
|
|
[],
|
|
|
|
[{ 'type': 'list', 'attributes': { 'style': style } }],
|
|
|
|
[],
|
|
|
|
[{ 'type': 'listItem' }]
|
2012-06-20 19:27:46 +00:00
|
|
|
);
|
2012-06-20 22:04:15 +00:00
|
|
|
surfaceModel.change( tx, tx.translateRange( selection ) );
|
2012-06-20 19:27:46 +00:00
|
|
|
}
|
2011-12-07 22:57:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-20 22:04:15 +00:00
|
|
|
ve.ui.ListButtonTool.prototype.unlist = function( node ) {
|
2012-07-25 21:24:39 +00:00
|
|
|
/**
|
|
|
|
* Recursively prepare to unwrap all lists in a given range.
|
|
|
|
*
|
|
|
|
* This function will find all lists covered wholly or partially by the given range, as well
|
|
|
|
* as all lists inside these lists, and return their inner ranges. This means that all sublists
|
|
|
|
* will be found even if range doesn't cover them.
|
|
|
|
*
|
|
|
|
* To actually unwrap the list, feed the returned ranges to ve.dm.Transaction.newFromWrap(),
|
|
|
|
* in order.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Document} documentModel
|
|
|
|
* @param {ve.Range} range
|
|
|
|
* @returns {ve.Range[]} Array of inner ranges of lists
|
|
|
|
*/
|
|
|
|
function getUnlistRanges( documentModel, range ) {
|
|
|
|
var groups = documentModel.getCoveredSiblingGroups( range ),
|
|
|
|
// Collect ranges in an object for deduplication
|
|
|
|
unlistRanges = {}, range,
|
|
|
|
i, j, k, group, previousList, list, listItem,
|
|
|
|
subList, endOffset = 0;
|
|
|
|
for ( i = 0; i < groups.length; i++ ) {
|
|
|
|
group = groups[i];
|
|
|
|
list = group.grandparent;
|
|
|
|
if ( list && list.getType() === 'list' && list !== previousList ) {
|
|
|
|
// Unwrap the parent list
|
|
|
|
range = list.getRange();
|
|
|
|
if ( range.end > endOffset ) {
|
|
|
|
unlistRanges[range.start + '-' + range.end] = range;
|
|
|
|
endOffset = range.end;
|
|
|
|
}
|
|
|
|
// Skip this list next time
|
|
|
|
previousList = list;
|
|
|
|
// Recursively unwrap any sublists of the list
|
|
|
|
for ( j = 0; j < list.children.length; j++ ) {
|
|
|
|
listItem = list.children[j];
|
|
|
|
if ( listItem.getType() === 'listItem' ) {
|
|
|
|
for ( k = 0; k < listItem.children.length; k++ ) {
|
|
|
|
subList = listItem.children[k];
|
|
|
|
if ( subList.getType() === 'list' ) {
|
|
|
|
// Recurse
|
|
|
|
unlistRanges = ve.extendObject( unlistRanges, getUnlistRanges(
|
|
|
|
documentModel, subList.getRange()
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unlistRanges;
|
|
|
|
}
|
|
|
|
|
2012-06-20 22:04:15 +00:00
|
|
|
var surfaceModel = this.toolbar.getSurfaceView().getModel(),
|
|
|
|
documentModel = surfaceModel.getDocument(),
|
|
|
|
selection = surfaceModel.getSelection(),
|
2012-07-25 21:24:39 +00:00
|
|
|
unlistRangesObj = getUnlistRanges( documentModel, selection ),
|
|
|
|
unlistRangesArr = [],
|
|
|
|
i, j;
|
|
|
|
for ( i in unlistRangesObj ) {
|
|
|
|
unlistRangesArr.push( unlistRangesObj[i] );
|
|
|
|
}
|
|
|
|
for ( i = 0; i < unlistRangesArr.length; i++ ) {
|
|
|
|
// Unwrap the range given by unlistRanges[i]
|
|
|
|
tx = ve.dm.Transaction.newFromWrap(
|
|
|
|
documentModel,
|
|
|
|
unlistRangesArr[i],
|
|
|
|
[ { 'type': 'list' } ],
|
|
|
|
[],
|
|
|
|
[ { 'type': 'listItem' } ],
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
selection = tx.translateRange( selection );
|
|
|
|
surfaceModel.change( tx );
|
|
|
|
// Translate all the remaining ranges for this transaction
|
|
|
|
// TODO ideally we'd have a way to merge all these transactions into one and execute that instead
|
|
|
|
for ( j = i + 1; j < unlistRangesArr.length; j++ ) {
|
|
|
|
unlistRangesArr[j] = tx.translateRange( unlistRangesArr[j] );
|
2012-06-20 22:04:15 +00:00
|
|
|
}
|
2011-12-08 04:18:15 +00:00
|
|
|
}
|
2012-07-25 21:24:39 +00:00
|
|
|
// Update the selection
|
|
|
|
surfaceModel.change( null, selection );
|
2011-12-07 22:57:17 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.ListButtonTool.prototype.onClick = function() {
|
2011-12-10 00:06:37 +00:00
|
|
|
this.toolbar.surfaceView.model.breakpoint();
|
2011-12-07 22:57:17 +00:00
|
|
|
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
|
|
|
}
|
2011-12-10 00:06:37 +00:00
|
|
|
this.toolbar.surfaceView.model.breakpoint();
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.ListButtonTool.prototype.updateState = function( annotations, nodes ) {
|
2012-07-20 00:24:54 +00:00
|
|
|
var surfaceView = this.toolbar.getSurfaceView(),
|
2012-06-20 22:01:18 +00:00
|
|
|
surfaceModel = surfaceView.getModel(),
|
|
|
|
doc = surfaceView.getDocument(),
|
|
|
|
selection = surfaceModel.getSelection(),
|
|
|
|
leaves = doc.selectNodes( selection, 'leaves' );
|
2012-06-20 01:20:28 +00:00
|
|
|
|
2012-06-20 22:01:18 +00:00
|
|
|
function areListItemsOfStyle( leaves, style ){
|
2012-06-20 01:20:28 +00:00
|
|
|
var listNode = null;
|
2012-06-20 22:01:18 +00:00
|
|
|
|
|
|
|
for ( var i=0; i < leaves.length; i++ ) {
|
|
|
|
listNode = leaves[i].node;
|
2012-06-20 01:20:28 +00:00
|
|
|
// Get the list node
|
|
|
|
while( listNode && listNode.getType() !== 'list' ) {
|
|
|
|
listNode = listNode.getParent();
|
2012-06-20 22:01:18 +00:00
|
|
|
if ( listNode === null ) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-04 02:59:53 +00:00
|
|
|
}
|
2012-06-20 22:01:18 +00:00
|
|
|
if( listNode.getModel().getAttribute('style') !== style ) {
|
|
|
|
return false;
|
2011-12-04 02:59:53 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-20 22:01:18 +00:00
|
|
|
return true;
|
2011-12-04 02:59:53 +00:00
|
|
|
}
|
2011-12-07 22:57:17 +00:00
|
|
|
|
2012-06-20 22:01:18 +00:00
|
|
|
if ( areListItemsOfStyle( leaves, this.name ) ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
this.$.addClass( 'es-toolbarButtonTool-down' );
|
|
|
|
} else {
|
2011-12-07 22:57:17 +00:00
|
|
|
this.$.removeClass( 'es-toolbarButtonTool-down' );
|
2011-12-04 02:59:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Tool.tools.number = {
|
|
|
|
'constructor': ve.ui.ListButtonTool,
|
2011-12-09 21:16:42 +00:00
|
|
|
'name': 'number',
|
2012-06-20 22:01:18 +00:00
|
|
|
'title': ve.msg( 'visualeditor-listbutton-number-tooltip' )
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Tool.tools.bullet = {
|
|
|
|
'constructor': ve.ui.ListButtonTool,
|
2011-12-09 21:16:42 +00:00
|
|
|
'name': 'bullet',
|
2012-06-20 22:01:18 +00:00
|
|
|
'title': ve.msg( 'visualeditor-listbutton-bullet-tooltip' )
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.extendClass( ve.ui.ListButtonTool, ve.ui.ButtonTool );
|