2011-12-05 21:10:19 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.ui.IndentationButtonTool 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.IndentationButtonTool = function( toolbar, name, title, data ) {
|
|
|
|
ve.ui.ButtonTool.call( this, toolbar, name, title );
|
2011-12-04 02:59:53 +00:00
|
|
|
this.data = data;
|
|
|
|
};
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.IndentationButtonTool.prototype.onClick = function() {
|
2011-12-07 20:06:04 +00:00
|
|
|
if ( !this.$.hasClass( 'es-toolbarButtonTool-disabled' ) ) {
|
2011-12-08 23:45:07 +00:00
|
|
|
var listItems = [],
|
|
|
|
listItem,
|
|
|
|
i;
|
|
|
|
for ( i = 0; i < this.nodes.length; i++ ) {
|
|
|
|
listItem = this.nodes[i].getParent();
|
|
|
|
if ( listItems.length > 0 ) {
|
|
|
|
if (listItem != listItems[listItems.length - 1]) {
|
|
|
|
listItems.push( listItem );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listItems.push( listItem );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( this.name === 'indent' ) {
|
|
|
|
this.indent( listItems );
|
|
|
|
} else if ( this.name === 'outdent' ) {
|
|
|
|
this.outdent( listItems );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.IndentationButtonTool.prototype.indent = function( listItems ) {
|
2011-12-08 23:45:07 +00:00
|
|
|
var surface = this.toolbar.surfaceView,
|
|
|
|
styles,
|
|
|
|
i;
|
|
|
|
|
|
|
|
for ( i = 0; i < listItems.length; i++ ) {
|
|
|
|
styles = listItems[i].getElementAttribute( 'styles' );
|
|
|
|
if ( styles.length < 6 ) {
|
|
|
|
tx = surface.model.getDocument().prepareElementAttributeChange(
|
|
|
|
surface.documentView.model.getOffsetFromNode( listItems[i], false ),
|
|
|
|
'styles',
|
2012-02-22 21:23:28 +00:00
|
|
|
styles.concat( styles[styles.length - 1] )
|
2011-12-08 23:45:07 +00:00
|
|
|
);
|
2012-06-20 01:20:28 +00:00
|
|
|
surface.model.change( tx );
|
2011-12-08 23:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
surface.emitCursor();
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.IndentationButtonTool.prototype.outdent = function( listItems ) {
|
2011-12-08 23:45:07 +00:00
|
|
|
var surface = this.toolbar.surfaceView,
|
|
|
|
styles,
|
|
|
|
i;
|
|
|
|
|
|
|
|
for ( i = 0; i < listItems.length; i++ ) {
|
|
|
|
styles = listItems[i].getElementAttribute( 'styles' );
|
|
|
|
if ( styles.length > 1 ) {
|
|
|
|
tx = surface.model.getDocument().prepareElementAttributeChange(
|
|
|
|
surface.documentView.model.getOffsetFromNode( listItems[i], false ),
|
|
|
|
'styles',
|
2012-02-22 21:23:28 +00:00
|
|
|
styles.slice( 0, styles.length - 1 )
|
2011-12-08 23:45:07 +00:00
|
|
|
);
|
2012-06-20 01:20:28 +00:00
|
|
|
surface.model.change( tx );
|
2011-12-08 23:45:07 +00:00
|
|
|
}
|
2011-12-07 20:06:04 +00:00
|
|
|
}
|
2011-12-08 23:45:07 +00:00
|
|
|
surface.emitCursor();
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.IndentationButtonTool.prototype.updateState = function( annotations, nodes ) {
|
2011-12-07 20:06:04 +00:00
|
|
|
function areListItems( nodes ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
for( var i = 0; i < nodes.length; i++ ) {
|
2012-06-20 01:20:28 +00:00
|
|
|
if (
|
|
|
|
nodes[i].parent !== null &&
|
|
|
|
nodes[i].getParent().getType() !== 'listItem' )
|
|
|
|
{
|
2011-12-04 02:59:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-08 23:45:07 +00:00
|
|
|
this.nodes = nodes;
|
|
|
|
if ( areListItems( this.nodes ) ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
this.$.removeClass( 'es-toolbarButtonTool-disabled' );
|
|
|
|
} else {
|
2011-12-07 20:06:04 +00:00
|
|
|
this.$.addClass( 'es-toolbarButtonTool-disabled' );
|
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.indent = {
|
|
|
|
'constructor': ve.ui.IndentationButtonTool,
|
2011-12-09 21:16:42 +00:00
|
|
|
'name': 'indent',
|
2012-06-20 01:20:28 +00:00
|
|
|
'title': ve.msg( 'visualeditor-indentationbutton-indent-tooltip' ),
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.ui.Tool.tools.outdent = {
|
|
|
|
'constructor': ve.ui.IndentationButtonTool,
|
2011-12-09 21:16:42 +00:00
|
|
|
'name': 'outdent',
|
2012-06-20 01:20:28 +00:00
|
|
|
'title': ve.msg( 'visualeditor-indentationbutton-outdent-tooltip' ),
|
2011-12-04 02:59:53 +00:00
|
|
|
};
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2012-04-02 22:28:26 +00:00
|
|
|
ve.extendClass( ve.ui.IndentationButtonTool, ve.ui.ButtonTool );
|