2011-12-05 21:10:19 +00:00
|
|
|
/**
|
|
|
|
* Creates an es.ListButtonTool object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends {es.ButtonTool}
|
|
|
|
* @param {es.ToolbarView} toolbar
|
|
|
|
* @param {String} name
|
|
|
|
*/
|
|
|
|
es.ListButtonTool = function( toolbar, name, data ) {
|
|
|
|
// Inheritance
|
2011-12-04 02:59:53 +00:00
|
|
|
es.ButtonTool.call( this, toolbar, name );
|
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 */
|
|
|
|
|
2011-12-04 02:59:53 +00:00
|
|
|
es.ListButtonTool.prototype.onClick = function() {
|
2011-12-06 00:42:15 +00:00
|
|
|
var stacks = [],
|
|
|
|
stack = [],
|
|
|
|
i,
|
|
|
|
j,
|
|
|
|
data;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Step 1
|
|
|
|
//
|
|
|
|
|
|
|
|
for( i = 0; i < this.nodes.length; i++ ) {
|
|
|
|
if ( this.nodes[i].getParent().getElementType() === 'listItem' ) {
|
|
|
|
if( stack.length > 0 ) {
|
|
|
|
stacks.push ( stack );
|
|
|
|
stack = [];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if( stack.length > 0 ) {
|
|
|
|
if ( this.nodes[i].getParent() === stack[stack.length - 1].getParent() ) {
|
|
|
|
stack.push( this.nodes[i] );
|
|
|
|
} else {
|
|
|
|
stacks.push ( stack );
|
|
|
|
stack = [this.nodes[i]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stack.push( this.nodes[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( stack.length > 0 ) {
|
|
|
|
stacks.push ( stack );
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Step 2
|
|
|
|
//
|
|
|
|
|
|
|
|
var insertAt,
|
|
|
|
removeLength,
|
|
|
|
tx;
|
|
|
|
|
|
|
|
for( i = 0; i < stacks.length; i++ ) {
|
|
|
|
removeLength = 0;
|
|
|
|
insertAt = this.toolbar.surfaceView.documentView.model.getOffsetFromNode(
|
|
|
|
stacks[i][0], false
|
|
|
|
);
|
|
|
|
data = [ { 'type': 'list' } ];
|
|
|
|
for( j = 0; j < stacks[i].length; j++ ) {
|
|
|
|
removeLength += stacks[i][j].getElementLength();
|
|
|
|
data = data
|
|
|
|
.concat( [ { 'type': 'listItem', 'attributes' : { 'styles': [ this.name ] } } ] )
|
|
|
|
.concat( stacks[i][j].getElementData() )
|
|
|
|
.concat( [ { 'type': '/listItem' } ] );
|
2011-12-04 02:59:53 +00:00
|
|
|
}
|
2011-12-06 00:42:15 +00:00
|
|
|
data = data.concat( [ { 'type': '/list' } ] );
|
|
|
|
|
|
|
|
tx = this.toolbar.surfaceView.model.getDocument().prepareInsertion(
|
|
|
|
insertAt,
|
|
|
|
data
|
|
|
|
);
|
|
|
|
this.toolbar.surfaceView.model.transact( tx );
|
2011-12-06 00:49:00 +00:00
|
|
|
|
|
|
|
tx = this.toolbar.surfaceView.model.getDocument().prepareRemoval(
|
|
|
|
new es.Range( insertAt+data.length, insertAt+removeLength+data.length)
|
|
|
|
);
|
|
|
|
this.toolbar.surfaceView.model.transact( tx );
|
|
|
|
|
|
|
|
|
2011-12-04 02:59:53 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
es.ListButtonTool.prototype.updateState = function( annotations, nodes ) {
|
|
|
|
// checks if all passed nodes are listItems of passed style
|
|
|
|
function check( nodes, style ) {
|
|
|
|
var parent, styles;
|
|
|
|
for( var i = 0; i < nodes.length; i++ ) {
|
|
|
|
parent = nodes[i].getParent();
|
|
|
|
if ( parent.getElementType() !== 'listItem' ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
styles = parent.getElementAttribute( 'styles' );
|
|
|
|
if ( styles[ styles.length - 1] !== style ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.nodes = nodes;
|
|
|
|
if ( check( this.nodes, this.name ) ) {
|
|
|
|
this.$.addClass( 'es-toolbarButtonTool-down' );
|
|
|
|
} else {
|
|
|
|
this.$.removeClass( 'es-toolbarButtonTool-down' );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2011-12-04 02:59:53 +00:00
|
|
|
es.Tool.tools.number = {
|
|
|
|
constructor: es.ListButtonTool,
|
|
|
|
name: 'number'
|
|
|
|
};
|
|
|
|
|
|
|
|
es.Tool.tools.bullet = {
|
|
|
|
constructor: es.ListButtonTool,
|
|
|
|
name: 'bullet'
|
|
|
|
};
|
|
|
|
|
2011-12-05 21:10:19 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
es.extendClass( es.ListButtonTool, es.ButtonTool );
|