mediawiki-extensions-Visual.../modules/es/tools/es.IndentationButtonTool.js
Trevor Parscal a48d2a5787 * Added shortcut for links (ctrl/cmd + K)
* Added title attributes for tools
2011-12-09 21:16:42 +00:00

116 lines
2.7 KiB
JavaScript

/**
* 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 );
this.data = data;
};
/* Methods */
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();
};
es.IndentationButtonTool.prototype.updateState = function( annotations, nodes ) {
function areListItems( nodes ) {
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 ) ) {
this.$.removeClass( 'es-toolbarButtonTool-disabled' );
} else {
this.$.addClass( 'es-toolbarButtonTool-disabled' );
}
};
/* Registration */
es.Tool.tools.indent = {
'constructor': es.IndentationButtonTool,
'name': 'indent',
'title': 'Increase indentation'
};
es.Tool.tools.outdent = {
'constructor': es.IndentationButtonTool,
'name': 'outdent',
'title': 'Reduce indentation'
};
/* Inheritance */
es.extendClass( es.IndentationButtonTool, es.ButtonTool );