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, data ) {
|
2011-12-04 02:59:53 +00:00
|
|
|
es.ButtonTool.call( this, toolbar, name );
|
|
|
|
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() {
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
|
|
|
|
es.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++ ) {
|
|
|
|
if ( nodes[i].getParent().getElementType() !== 'listItem' ) {
|
|
|
|
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 */
|
|
|
|
|
2011-12-04 02:59:53 +00:00
|
|
|
es.Tool.tools.indent = {
|
|
|
|
constructor: es.IndentationButtonTool,
|
|
|
|
name: 'indent'
|
|
|
|
};
|
|
|
|
|
|
|
|
es.Tool.tools.outdent = {
|
|
|
|
constructor: es.IndentationButtonTool,
|
|
|
|
name: 'outdent'
|
|
|
|
};
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2011-12-04 02:54:33 +00:00
|
|
|
es.extendClass( es.IndentationButtonTool, es.ButtonTool );
|