mediawiki-extensions-Visual.../modules/es/tools/es.IndentationButtonTool.js

116 lines
2.7 KiB
JavaScript
Raw Normal View History

2011-12-05 21:10:19 +00:00
/**
* Creates an es.IndentationButtonTool object.
*
* @class
* @constructor
* @extends {es.ButtonTool}
* @param {es.ToolbarView} toolbar
* @param {String} name
*/
es.IndentationButtonTool = function( toolbar, name, title, data ) {
es.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 */
2011-12-04 02:59:53 +00:00
es.IndentationButtonTool.prototype.onClick = function() {
if ( !this.$.hasClass( 'es-toolbarButtonTool-disabled' ) ) {
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 );
}
}
};
es.IndentationButtonTool.prototype.indent = function( listItems ) {
var surface = this.toolbar.surfaceView,
styles,
i;
for ( i = 0; i < listItems.length; i++ ) {
styles = listItems[i].getElementAttribute( 'styles' );
if ( styles.length < 6 ) {
styles.push( styles[styles.length - 1] );
tx = surface.model.getDocument().prepareElementAttributeChange(
surface.documentView.model.getOffsetFromNode( listItems[i], false ),
'set',
'styles',
styles
);
surface.model.transact( tx );
}
}
surface.emitCursor();
};
es.IndentationButtonTool.prototype.outdent = function( listItems ) {
var surface = this.toolbar.surfaceView,
styles,
i;
for ( i = 0; i < listItems.length; i++ ) {
styles = listItems[i].getElementAttribute( 'styles' );
if ( styles.length > 1 ) {
styles.splice( styles.length - 1, 1);
tx = surface.model.getDocument().prepareElementAttributeChange(
surface.documentView.model.getOffsetFromNode( listItems[i], false ),
'set',
'styles',
styles
);
surface.model.transact( tx );
}
}
surface.emitCursor();
2011-12-04 02:59:53 +00:00
};
es.IndentationButtonTool.prototype.updateState = function( annotations, nodes ) {
function areListItems( nodes ) {
2011-12-04 02:59:53 +00:00
for( var i = 0; i < nodes.length; i++ ) {
if ( nodes[i].getParent().getElementType() !== 'listItem' ) {
return false;
}
}
return true;
}
this.nodes = nodes;
if ( areListItems( this.nodes ) ) {
2011-12-04 02:59:53 +00:00
this.$.removeClass( 'es-toolbarButtonTool-disabled' );
} else {
this.$.addClass( 'es-toolbarButtonTool-disabled' );
2011-12-04 02:59:53 +00:00
}
};
2011-12-05 21:10:19 +00:00
/* Registration */
2011-12-04 02:59:53 +00:00
es.Tool.tools.indent = {
'constructor': es.IndentationButtonTool,
'name': 'indent',
'title': 'Increase indentation'
2011-12-04 02:59:53 +00:00
};
es.Tool.tools.outdent = {
'constructor': es.IndentationButtonTool,
'name': 'outdent',
'title': 'Reduce indentation'
2011-12-04 02:59:53 +00:00
};
2011-12-05 21:10:19 +00:00
/* Inheritance */
es.extendClass( es.IndentationButtonTool, es.ButtonTool );