r105476 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r105475‎ | r105476 | r105477 >
Date:22:57, 7 December 2011
Author:inez
Status:deferred
Tags:
Comment:
Refactor class ListButtonTool. Method list now works.
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js
@@ -18,88 +18,112 @@
1919
2020 /* Methods */
2121
22 -es.ListButtonTool.prototype.onClick = function() {
23 - if ( !this.$.hasClass( 'es-toolbarButtonTool-down' ) ) {
 22+es.ListButtonTool.prototype.list = function( nodes, style ) {
 23+ var surface = this.toolbar.surfaceView,
 24+ selection = surface.currentSelection.clone(),
 25+ stack = [],
 26+ stacks = [],
 27+ listItems = [],
 28+ parent,
 29+ styles,
 30+ insertAt,
 31+ removeLength,
 32+ data,
 33+ tx,
 34+ i,
 35+ j;
2436
25 - var surface = this.toolbar.surfaceView,
26 - stack = [],
27 - stacks = [],
28 - listItems = [],
29 - parent,
30 - styles,
31 - insertAt,
32 - removeLength,
33 - data,
34 - tx,
35 - i,
36 - j;
37 -
38 - for( i = 0; i < this.nodes.length; i++ ) {
39 - parent = this.nodes[i].getParent();
40 - if ( parent.getElementType() === 'listItem' ) {
41 - if ( stack.length > 0 ) {
 37+ for( i = 0; i < nodes.length; i++ ) {
 38+ parent = nodes[i].getParent();
 39+ if ( parent.getElementType() === 'listItem' ) {
 40+ if ( stack.length > 0 ) {
 41+ stacks.push( stack );
 42+ stack = [];
 43+ }
 44+ listItems.push( parent );
 45+ } else {
 46+ if( stack.length > 0 ) {
 47+ if ( parent === stack[stack.length - 1].getParent() ) {
 48+ stack.push( nodes[i] );
 49+ } else {
4250 stacks.push( stack );
43 - stack = [];
 51+ stack = [ nodes[i] ];
4452 }
45 - listItems.push( parent );
4653 } else {
47 - if( stack.length > 0 ) {
48 - if ( parent === stack[stack.length - 1].getParent() ) {
49 - stack.push( this.nodes[i] );
50 - } else {
51 - stacks.push( stack );
52 - stack = [this.nodes[i]];
53 - }
54 - } else {
55 - stack.push( this.nodes[i] );
56 - }
 54+ stack.push( nodes[i] );
5755 }
5856 }
59 - if( stack.length > 0 ) {
60 - stacks.push( stack );
 57+ }
 58+ if( stack.length > 0 ) {
 59+ stacks.push( stack );
 60+ }
 61+
 62+ if ( selection.from === selection.to ) {
 63+ selection.from += 2;
 64+ selection.to += 2;
 65+ } else {
 66+ if ( nodes[0].getParent().getElementType() != 'listItem' ) {
 67+ if ( selection.from < selection.to ) {
 68+ selection.from += 2;
 69+ } else {
 70+ selection.to += 2;
 71+ }
6172 }
 73+ if ( selection.from < selection.to ) {
 74+ selection.to += (stacks.length * 2) + (nodes.length - listItems.length - 1) * 2;
 75+ } else {
 76+ selection.from += (stacks.length * 2) + (nodes.length - listItems.length - 1) * 2;
 77+ }
 78+ }
6279
63 - for( i = 0; i < listItems.length; i++ ) {
64 - styles = listItems[i].getElementAttribute( 'styles' );
65 - if ( styles[styles.length - 1] !== this.name ) {
66 - styles.splice( styles.length - 1, 1, this.name );
67 - tx = surface.model.getDocument().prepareElementAttributeChange(
68 - surface.documentView.model.getOffsetFromNode( listItems[i], false ),
69 - 'set',
70 - 'styles',
71 - styles
72 - );
73 - surface.model.transact( tx );
74 - }
 80+ for( i = 0; i < listItems.length; i++ ) {
 81+ styles = listItems[i].getElementAttribute( 'styles' );
 82+ if ( styles[styles.length - 1] !== style ) {
 83+ styles.splice( styles.length - 1, 1, style );
 84+ tx = surface.model.getDocument().prepareElementAttributeChange(
 85+ surface.documentView.model.getOffsetFromNode( listItems[i], false ),
 86+ 'set',
 87+ 'styles',
 88+ styles
 89+ );
 90+ surface.model.transact( tx );
7591 }
 92+ }
7693
77 - for( i = 0; i < stacks.length; i++ ) {
78 - removeLength = 0;
79 - insertAt = surface.documentView.model.getOffsetFromNode( stacks[i][0], false );
80 - data = [ { 'type': 'list' } ];
81 - for( j = 0; j < stacks[i].length; j++ ) {
82 - removeLength += stacks[i][j].getElementLength();
83 - data = data
84 - .concat( [ {
85 - 'type': 'listItem',
86 - 'attributes' : { 'styles': [ this.name ] }
87 - } ] )
88 - .concat( stacks[i][j].getElementData() )
89 - .concat( [ { 'type': '/listItem' } ] );
90 - }
91 - data = data.concat( [ { 'type': '/list' } ] );
 94+ for( i = 0; i < stacks.length; i++ ) {
 95+ removeLength = 0;
 96+ insertAt = surface.documentView.model.getOffsetFromNode( stacks[i][0], false );
9297
93 - tx = surface.model.getDocument().prepareInsertion( insertAt, data );
94 - surface.model.transact( tx );
 98+ data = [ { 'type': 'list' } ];
 99+ for( j = 0; j < stacks[i].length; j++ ) {
 100+ removeLength += stacks[i][j].getElementLength();
95101
96 - tx = surface.model.getDocument().prepareRemoval(
97 - new es.Range( insertAt + data.length, insertAt + removeLength + data.length )
98 - );
99 - surface.model.transact( tx );
 102+ data = data
 103+ .concat( [ {
 104+ 'type': 'listItem',
 105+ 'attributes' : { 'styles': [ this.name ] }
 106+ } ] )
 107+ .concat( stacks[i][j].getElementData() )
 108+ .concat( [ { 'type': '/listItem' } ] );
100109 }
 110+ data = data.concat( [ { 'type': '/list' } ] );
101111
102 - } else {
103 -/*
 112+ tx = surface.model.getDocument().prepareInsertion( insertAt, data );
 113+ surface.model.transact( tx );
 114+
 115+ tx = surface.model.getDocument().prepareRemoval(
 116+ new es.Range( insertAt + data.length, insertAt + removeLength + data.length )
 117+ );
 118+ surface.model.transact( tx );
 119+
 120+ }
 121+
 122+ surface.model.select( selection, true );
 123+};
 124+
 125+es.ListButtonTool.prototype.unlist = function() {
 126+ alert( 'This functionality is not yet supported.' );
 127+ /*
104128 // unlist
105129 //
106130 // Step 1
@@ -174,12 +198,18 @@
175199 }
176200 }
177201 */
 202+};
 203+
 204+es.ListButtonTool.prototype.onClick = function() {
 205+ if ( !this.$.hasClass( 'es-toolbarButtonTool-down' ) ) {
 206+ this.list( this.nodes, this.name );
 207+ } else {
 208+ this.unlist( this.nodes );
178209 }
179210 };
180211
181212 es.ListButtonTool.prototype.updateState = function( annotations, nodes ) {
182 - // checks if all passed nodes are listItems of passed style
183 - function check( nodes, style ) {
 213+ function areListItemsOfStyle( nodes, style ) {
184214 var parent, styles;
185215 for( var i = 0; i < nodes.length; i++ ) {
186216 parent = nodes[i].getParent();
@@ -193,12 +223,12 @@
194224 }
195225 return true;
196226 }
197 -
 227+
198228 this.nodes = nodes;
199 - if ( check( this.nodes, this.name ) ) {
 229+ if ( areListItemsOfStyle( this.nodes, this.name ) ) {
200230 this.$.addClass( 'es-toolbarButtonTool-down' );
201231 } else {
202 - this.$.removeClass( 'es-toolbarButtonTool-down' );
 232+ this.$.removeClass( 'es-toolbarButtonTool-down' );
203233 }
204234 };
205235

Status & tagging log