Index: trunk/extensions/VisualEditor/modules/es/tools/es.ListButtonTool.js |
— | — | @@ -18,88 +18,112 @@ |
19 | 19 | |
20 | 20 | /* Methods */ |
21 | 21 | |
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; |
24 | 36 | |
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 { |
42 | 50 | stacks.push( stack ); |
43 | | - stack = []; |
| 51 | + stack = [ nodes[i] ]; |
44 | 52 | } |
45 | | - listItems.push( parent ); |
46 | 53 | } 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] ); |
57 | 55 | } |
58 | 56 | } |
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 | + } |
61 | 72 | } |
| 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 | + } |
62 | 79 | |
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 ); |
75 | 91 | } |
| 92 | + } |
76 | 93 | |
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 ); |
92 | 97 | |
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(); |
95 | 101 | |
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' } ] ); |
100 | 109 | } |
| 110 | + data = data.concat( [ { 'type': '/list' } ] ); |
101 | 111 | |
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 | + /* |
104 | 128 | // unlist |
105 | 129 | // |
106 | 130 | // Step 1 |
— | — | @@ -174,12 +198,18 @@ |
175 | 199 | } |
176 | 200 | } |
177 | 201 | */ |
| 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 ); |
178 | 209 | } |
179 | 210 | }; |
180 | 211 | |
181 | 212 | 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 ) { |
184 | 214 | var parent, styles; |
185 | 215 | for( var i = 0; i < nodes.length; i++ ) { |
186 | 216 | parent = nodes[i].getParent(); |
— | — | @@ -193,12 +223,12 @@ |
194 | 224 | } |
195 | 225 | return true; |
196 | 226 | } |
197 | | - |
| 227 | + |
198 | 228 | this.nodes = nodes; |
199 | | - if ( check( this.nodes, this.name ) ) { |
| 229 | + if ( areListItemsOfStyle( this.nodes, this.name ) ) { |
200 | 230 | this.$.addClass( 'es-toolbarButtonTool-down' ); |
201 | 231 | } else { |
202 | | - this.$.removeClass( 'es-toolbarButtonTool-down' ); |
| 232 | + this.$.removeClass( 'es-toolbarButtonTool-down' ); |
203 | 233 | } |
204 | 234 | }; |
205 | 235 | |