Skip to content

Commit cd9bfe9

Browse files
authored
Merge pull request #73 from wmde/removeMoveFeature
Remove unused move feature from SnakList
2 parents c6243f9 + 645736f commit cd9bfe9

File tree

2 files changed

+1
-310
lines changed

2 files changed

+1
-310
lines changed

src/SnakList.js

Lines changed: 1 addition & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ wb.datamodel.SnakList = util.inherit( 'WbDataModelSnakList', PARENT, function( s
2424
*
2525
* @param {string} propertyId
2626
* @return {wikibase.datamodel.SnakList}
27+
* @private
2728
*/
2829
getFilteredSnakList: function( propertyId ) {
2930
if( !propertyId ) {
@@ -94,161 +95,6 @@ wb.datamodel.SnakList = util.inherit( 'WbDataModelSnakList', PARENT, function( s
9495
} );
9596

9697
return propertyIds;
97-
},
98-
99-
/**
100-
* Returns the indices of the snak list where a certain snak may be moved to. A snak may be
101-
* moved within its property group. It may also be moved to the slots between property groups
102-
* which involves moving the whole property group the snak belongs to.
103-
*
104-
* @param {wikibase.datamodel.Snak} snak
105-
* @return {number[]}
106-
*/
107-
getValidMoveIndices: function( snak ) {
108-
var self = this,
109-
indices = [],
110-
isGroupLast = false;
111-
112-
this.each( function( i, snakListSnak ) {
113-
if( snakListSnak.getPropertyId() === snak.getPropertyId() ) {
114-
// Detect slots within the snak's property group.
115-
if( snakListSnak !== snak ) {
116-
indices.push( i );
117-
} else {
118-
var nextSnak = self._items[i + 1];
119-
if( nextSnak && nextSnak.getPropertyId() !== snak.getPropertyId() ) {
120-
// Snak is the last of its group.
121-
isGroupLast = true;
122-
}
123-
}
124-
} else {
125-
// Detect slots between property groups.
126-
var previousSnak = self._items[i - 1],
127-
isNewPropertyGroup = (
128-
i !== 0
129-
&& snakListSnak.getPropertyId() !== previousSnak.getPropertyId()
130-
);
131-
132-
if(
133-
// Since this snak's property group is not at the top of the snak list, the
134-
// snak (with its group) may always be moved to the top:
135-
i === 0
136-
// The snak (with its group) may always be moved to between groups except to
137-
// adjacent slots between property groups since the snak's property group would
138-
// in fact not be moved.
139-
|| isNewPropertyGroup && previousSnak.getPropertyId() !== snak.getPropertyId()
140-
) {
141-
indices.push( i );
142-
}
143-
}
144-
} );
145-
146-
// Allow moving to last position if snak is not at the end already:
147-
if( snak !== this._items[this._items.length - 1] ) {
148-
indices.push( this._items.length );
149-
}
150-
151-
return indices;
152-
},
153-
154-
/**
155-
* Moves a SnakList's Snak to a new index.
156-
*
157-
* @param {wikibase.datamodel.Snak} snak Snak to move within the list.
158-
* @param {number} toIndex
159-
* @return {wikibase.datamodel.SnakList} This SnakList object.
160-
*
161-
* @throws {Error} if snak is not allowed to be moved to toIndex.
162-
*/
163-
move: function( snak, toIndex ) {
164-
if( this.indexOf( snak ) === toIndex ) {
165-
return this;
166-
}
167-
168-
var validIndices = this.getValidMoveIndices( snak );
169-
170-
if( $.inArray( toIndex, validIndices ) === -1 ) {
171-
throw new Error( 'Tried to move snak to index ' + toIndex + ' but only the following '
172-
+ 'indices are allowed: ' + validIndices.join( ', ' ) );
173-
}
174-
175-
var previousSnak = this._items[toIndex -1],
176-
nextSnak = this._items[toIndex + 1],
177-
insertBefore = this._items[toIndex];
178-
179-
if(
180-
previousSnak && previousSnak.getPropertyId() === snak.getPropertyId()
181-
|| nextSnak && nextSnak.getPropertyId() === snak.getPropertyId()
182-
) {
183-
// Moving snak within its property group.
184-
this._items.splice( this.indexOf( snak ), 1 );
185-
186-
if( insertBefore ) {
187-
this._items.splice( toIndex, 0, snak );
188-
} else {
189-
this._items.push( snak );
190-
}
191-
} else {
192-
// Moving the whole snak group.
193-
var groupedSnaks = [];
194-
195-
for( var i = 0; i < this._items.length; i++ ) {
196-
if( this._items[i].getPropertyId() === snak.getPropertyId() ) {
197-
groupedSnaks.push( this._items[i] );
198-
}
199-
}
200-
201-
for( i = 0; i < groupedSnaks.length; i++ ) {
202-
this._items.splice( this.indexOf( groupedSnaks[i] ), 1 );
203-
if( insertBefore ) {
204-
this._items.splice( this.indexOf( insertBefore ), 0, groupedSnaks[i] );
205-
} else {
206-
this._items.push( groupedSnaks[i] );
207-
}
208-
}
209-
}
210-
211-
return this;
212-
},
213-
214-
/**
215-
* Moves a snak towards the top of the snak list by one step.
216-
*
217-
* @param {wikibase.datamodel.Snak} snak
218-
* @return {wikibase.datamodel.SnakList} This SnakList object.
219-
*/
220-
moveUp: function( snak ) {
221-
var index = this.indexOf( snak ),
222-
validIndices = this.getValidMoveIndices( snak );
223-
224-
for( var i = validIndices.length - 1; i >= 0; i-- ) {
225-
if( validIndices[i] < index ) {
226-
this.move( snak, validIndices[i] );
227-
break;
228-
}
229-
}
230-
231-
return this;
232-
},
233-
234-
/**
235-
* Moves a snak towards the bottom of the snak list by one step.
236-
*
237-
* @param {wikibase.datamodel.Snak} snak
238-
* @return {wikibase.datamodel.SnakList} This SnakList object.
239-
*/
240-
moveDown: function( snak ) {
241-
var index = this.indexOf( snak ),
242-
validIndices = this.getValidMoveIndices( snak );
243-
244-
for( var i = 0; i < validIndices.length; i++ ) {
245-
if( validIndices[i] > index ) {
246-
this.move( snak, validIndices[i] );
247-
break;
248-
}
249-
}
250-
251-
return this;
25298
}
25399
} );
254100

tests/SnakList.tests.js

Lines changed: 0 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -125,159 +125,4 @@ QUnit.test( 'merge()', function( assert ) {
125125
}
126126
} );
127127

128-
QUnit.test( 'getValidMoveIndices()', function( assert ) {
129-
assert.expect( 8 );
130-
var snaks = testSets[2],
131-
snakList = new wb.datamodel.SnakList( snaks );
132-
133-
/**
134-
* Expected indices where the individual snaks (with or without its groups) may be moved to.
135-
* @property {number[][]}
136-
*/
137-
var validIndices = [
138-
[1, 5, 6, 7],
139-
[0, 5, 6, 7],
140-
[0, 3, 4, 6, 7],
141-
[0, 2, 4, 6, 7],
142-
[0, 2, 3, 6, 7],
143-
[0, 2, 7],
144-
[0, 2, 5]
145-
];
146-
147-
for( var i = 0; i < validIndices.length; i++ ) {
148-
assert.deepEqual(
149-
snakList.getValidMoveIndices( snaks[i] ),
150-
validIndices[i],
151-
'Verified indices example Snak #' + i + ' may be moved to.'
152-
);
153-
}
154-
155-
snakList = new wb.datamodel.SnakList(
156-
[ new wb.datamodel.PropertyValueSnak( 'P1', new dv.StringValue( 'a' ) ) ]
157-
);
158-
159-
assert.strictEqual(
160-
snakList.getValidMoveIndices( snakList.toArray()[0] ).length,
161-
0,
162-
'No indices returned when SnakList does not contain more than one Snak.'
163-
);
164-
165-
} );
166-
167-
QUnit.test( 'move()', function( assert ) {
168-
assert.expect( 30 );
169-
var snaks = testSets[2],
170-
snakList;
171-
172-
/**
173-
* Array of test case definitions. Test case definition structure:
174-
* [0] => Index of element to move
175-
* [1] => Index where to move element
176-
* [2] => Expected result when concatenating the string values of the SnakList's Snaks.
177-
* @property {*[][]}
178-
*/
179-
var testCases = [
180-
[ 0, 1, 'bacdefg' ],
181-
[ 0, 5, 'cdeabfg' ],
182-
[ 0, 6, 'cdefabg' ],
183-
[ 0, 7, 'cdefgab' ],
184-
[ 1, 0, 'bacdefg' ],
185-
[ 1, 5, 'cdeabfg' ],
186-
[ 1, 6, 'cdefabg' ],
187-
[ 1, 7, 'cdefgab' ],
188-
[ 2, 0, 'cdeabfg' ],
189-
[ 2, 3, 'abdcefg' ],
190-
[ 2, 4, 'abdecfg' ],
191-
[ 2, 6, 'abfcdeg' ],
192-
[ 2, 7, 'abfgcde' ],
193-
[ 3, 0, 'cdeabfg' ],
194-
[ 3, 2, 'abdcefg' ],
195-
[ 3, 4, 'abcedfg' ],
196-
[ 3, 6, 'abfcdeg' ],
197-
[ 3, 7, 'abfgcde' ],
198-
[ 4, 0, 'cdeabfg' ],
199-
[ 4, 2, 'abecdfg' ],
200-
[ 4, 3, 'abcedfg' ],
201-
[ 4, 6, 'abfcdeg' ],
202-
[ 4, 7, 'abfgcde' ],
203-
[ 5, 0, 'fabcdeg' ],
204-
[ 5, 2, 'abfcdeg' ],
205-
[ 5, 7, 'abcdegf' ],
206-
[ 6, 0, 'gabcdef' ],
207-
[ 6, 2, 'abgcdef' ],
208-
[ 6, 5, 'abcdegf' ]
209-
];
210-
211-
for( var i = 1; i < testCases.length; i++ ) {
212-
snakList = new wb.datamodel.SnakList( snaks );
213-
214-
snakList.move( snaks[testCases[i][0]], testCases[i][1] );
215-
216-
assert.equal(
217-
snakOrder( snakList ),
218-
testCases[i][2],
219-
'Verified moving a Snak with test set #' + i + '.'
220-
);
221-
}
222-
223-
snakList = new wb.datamodel.SnakList( snaks );
224-
snakList.move( snaks[0], 0 );
225-
226-
assert.equal(
227-
snakOrder( snakList ),
228-
'abcdefg',
229-
'Nothing changed when trying to move a Snak to an index it already has.'
230-
);
231-
232-
assert.throws(
233-
function() {
234-
snakList = new wb.datamodel.SnakList( snaks );
235-
snakList.move( 0, 4 );
236-
},
237-
'move() throws an error when trying to move a Snak to an invalid index.'
238-
);
239-
} );
240-
241-
QUnit.test( 'moveUp() and moveDown()', function( assert ) {
242-
assert.expect( 14 );
243-
var snaks = testSets[2],
244-
snakList;
245-
246-
/**
247-
* Array of test case definitions for moveUp() and moveDown() methods. Test case definition
248-
* structure:
249-
* [0] => Resulting order after moving the element having the same index in the SnakList up.
250-
* [1] => Resulting order after moving the element having the same index in the SnakList down.
251-
* @property {string[][]}
252-
*/
253-
var testCases = [
254-
['abcdefg', 'bacdefg' ],
255-
['bacdefg', 'cdeabfg' ],
256-
['cdeabfg', 'abdcefg' ],
257-
['abdcefg', 'abcedfg' ],
258-
['abcedfg', 'abfcdeg' ],
259-
['abfcdeg', 'abcdegf' ],
260-
['abcdegf', 'abcdefg' ]
261-
];
262-
263-
for( var i = 0; i < testCases.length; i++ ) {
264-
snakList = new wb.datamodel.SnakList( snaks );
265-
266-
assert.equal(
267-
snakOrder( snakList.moveUp( snaks[i] ) ),
268-
testCases[i][0],
269-
'Verified result of moveUp() with test set #' + i + '.'
270-
);
271-
272-
snakList = new wb.datamodel.SnakList( snaks );
273-
274-
assert.equal(
275-
snakOrder( snakList.moveDown( snaks[i] ) ),
276-
testCases[i][1],
277-
'Verified result of moveDown() with test set #' + i + '.'
278-
);
279-
280-
}
281-
} );
282-
283128
}( wikibase, dataValues, jQuery, QUnit ) );

0 commit comments

Comments
 (0)