https://www.mediawiki.org/wiki/Special:Code/MediaWiki/105476
Revision: 105476 Author: inez Date: 2011-12-07 22:57:17 +0000 (Wed, 07 Dec 2011) Log Message: ----------- Refactor class ListButtonTool. Method list now works. Modified Paths: -------------- trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js Modified: trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js =================================================================== --- trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js 2011-12-07 22:54:13 UTC (rev 105475) +++ trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js 2011-12-07 22:57:17 UTC (rev 105476) @@ -18,88 +18,112 @@ /* Methods */ -es.ListButtonTool.prototype.onClick = function() { - if ( !this.$.hasClass( 'es-toolbarButtonTool-down' ) ) { +es.ListButtonTool.prototype.list = function( nodes, style ) { + var surface = this.toolbar.surfaceView, + selection = surface.currentSelection.clone(), + stack = [], + stacks = [], + listItems = [], + parent, + styles, + insertAt, + removeLength, + data, + tx, + i, + j; - var surface = this.toolbar.surfaceView, - stack = [], - stacks = [], - listItems = [], - parent, - styles, - insertAt, - removeLength, - data, - tx, - i, - j; - - for( i = 0; i < this.nodes.length; i++ ) { - parent = this.nodes[i].getParent(); - if ( parent.getElementType() === 'listItem' ) { - if ( stack.length > 0 ) { + for( i = 0; i < nodes.length; i++ ) { + parent = nodes[i].getParent(); + if ( parent.getElementType() === 'listItem' ) { + if ( stack.length > 0 ) { + stacks.push( stack ); + stack = []; + } + listItems.push( parent ); + } else { + if( stack.length > 0 ) { + if ( parent === stack[stack.length - 1].getParent() ) { + stack.push( nodes[i] ); + } else { stacks.push( stack ); - stack = []; + stack = [ nodes[i] ]; } - listItems.push( parent ); } else { - if( stack.length > 0 ) { - if ( parent === stack[stack.length - 1].getParent() ) { - stack.push( this.nodes[i] ); - } else { - stacks.push( stack ); - stack = [this.nodes[i]]; - } - } else { - stack.push( this.nodes[i] ); - } + stack.push( nodes[i] ); } } - if( stack.length > 0 ) { - stacks.push( stack ); + } + if( stack.length > 0 ) { + stacks.push( stack ); + } + + if ( selection.from === selection.to ) { + selection.from += 2; + selection.to += 2; + } else { + if ( nodes[0].getParent().getElementType() != 'listItem' ) { + if ( selection.from < selection.to ) { + selection.from += 2; + } else { + selection.to += 2; + } } + if ( selection.from < selection.to ) { + selection.to += (stacks.length * 2) + (nodes.length - listItems.length - 1) * 2; + } else { + selection.from += (stacks.length * 2) + (nodes.length - listItems.length - 1) * 2; + } + } - for( i = 0; i < listItems.length; i++ ) { - styles = listItems[i].getElementAttribute( 'styles' ); - if ( styles[styles.length - 1] !== this.name ) { - styles.splice( styles.length - 1, 1, this.name ); - tx = surface.model.getDocument().prepareElementAttributeChange( - surface.documentView.model.getOffsetFromNode( listItems[i], false ), - 'set', - 'styles', - styles - ); - surface.model.transact( tx ); - } + for( i = 0; i < listItems.length; i++ ) { + styles = listItems[i].getElementAttribute( 'styles' ); + if ( styles[styles.length - 1] !== style ) { + styles.splice( styles.length - 1, 1, style ); + tx = surface.model.getDocument().prepareElementAttributeChange( + surface.documentView.model.getOffsetFromNode( listItems[i], false ), + 'set', + 'styles', + styles + ); + surface.model.transact( tx ); } + } - for( i = 0; i < stacks.length; i++ ) { - removeLength = 0; - insertAt = surface.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' } ] ); - } - data = data.concat( [ { 'type': '/list' } ] ); + for( i = 0; i < stacks.length; i++ ) { + removeLength = 0; + insertAt = surface.documentView.model.getOffsetFromNode( stacks[i][0], false ); - tx = surface.model.getDocument().prepareInsertion( insertAt, data ); - surface.model.transact( tx ); + data = [ { 'type': 'list' } ]; + for( j = 0; j < stacks[i].length; j++ ) { + removeLength += stacks[i][j].getElementLength(); - tx = surface.model.getDocument().prepareRemoval( - new es.Range( insertAt + data.length, insertAt + removeLength + data.length ) - ); - surface.model.transact( tx ); + data = data + .concat( [ { + 'type': 'listItem', + 'attributes' : { 'styles': [ this.name ] } + } ] ) + .concat( stacks[i][j].getElementData() ) + .concat( [ { 'type': '/listItem' } ] ); } + data = data.concat( [ { 'type': '/list' } ] ); - } else { -/* + tx = surface.model.getDocument().prepareInsertion( insertAt, data ); + surface.model.transact( tx ); + + tx = surface.model.getDocument().prepareRemoval( + new es.Range( insertAt + data.length, insertAt + removeLength + data.length ) + ); + surface.model.transact( tx ); + + } + + surface.model.select( selection, true ); +}; + +es.ListButtonTool.prototype.unlist = function() { + alert( 'This functionality is not yet supported.' ); + /* // unlist // // Step 1 @@ -174,12 +198,18 @@ } } */ +}; + +es.ListButtonTool.prototype.onClick = function() { + if ( !this.$.hasClass( 'es-toolbarButtonTool-down' ) ) { + this.list( this.nodes, this.name ); + } else { + this.unlist( this.nodes ); } }; es.ListButtonTool.prototype.updateState = function( annotations, nodes ) { - // checks if all passed nodes are listItems of passed style - function check( nodes, style ) { + function areListItemsOfStyle( nodes, style ) { var parent, styles; for( var i = 0; i < nodes.length; i++ ) { parent = nodes[i].getParent(); @@ -193,12 +223,12 @@ } return true; } - + this.nodes = nodes; - if ( check( this.nodes, this.name ) ) { + if ( areListItemsOfStyle( this.nodes, this.name ) ) { this.$.addClass( 'es-toolbarButtonTool-down' ); } else { - this.$.removeClass( 'es-toolbarButtonTool-down' ); + this.$.removeClass( 'es-toolbarButtonTool-down' ); } }; _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs