[MediaWiki-commits] [Gerrit] Implemented ordering of snaks within SnakList - change (mediawiki...Wikibase)
jenkins-bot has submitted this change and it was merged. Change subject: Implemented ordering of snaks within SnakList .. Implemented ordering of snaks within SnakList (bug 52391) Implemented "move" functions in wikibase.SnakList which allow moving snaks within the SnakList while considering the snaks being grouped by property. Change-Id: Ic3e23827144b047038790afddf86996c3efa5c34 --- M lib/resources/wikibase.datamodel/wikibase.SnakList.js M lib/tests/qunit/wikibase.datamodel/Wikibase.SnakList.tests.js M lib/tests/qunit/wikibase.datamodel/Wikibase.claim.tests.js M lib/tests/qunit/wikibase.datamodel/wikibase.Statement.tests.js 4 files changed, 416 insertions(+), 58 deletions(-) Approvals: Tobias Gritschacher: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/resources/wikibase.datamodel/wikibase.SnakList.js b/lib/resources/wikibase.datamodel/wikibase.SnakList.js index c10e37b..9b9caf3 100644 --- a/lib/resources/wikibase.datamodel/wikibase.SnakList.js +++ b/lib/resources/wikibase.datamodel/wikibase.SnakList.js @@ -3,6 +3,7 @@ * @ingroup WikibaseLib * @licence GNU GPL v2+ * @author Daniel Werner < daniel.wer...@wikimedia.de > + * @author H. Snater < mediaw...@snater.com > */ ( function( wb, $ ) { 'use strict'; @@ -151,11 +152,16 @@ ) { return false; } - for( var i in this._snaks ) { - if( !snakList.hasSnak( this._snaks[i] ) ) { + + var otherSnaks = snakList.toArray(); + + // Compare to other snak lists snaks considering order: + for( var i = 0; i < otherSnaks.length; i++ ) { + if( !this._snaks[i].equals( otherSnaks[i] ) ) { return false; } } + return true; }, @@ -209,6 +215,179 @@ }, /** +* Returns a snak's index within the snak list. Returns -1 when the snak could not be found. +* @since 0.4 +* +* @param {wikibase.Snak} snak +* @return {number} +*/ + indexOf: function( snak ) { + for( var i = 0; i < this._snaks.length; i++ ) { + if( this._snaks[i].equals( snak ) ) { + return i; + } + } + return -1; + }, + + /** +* Returns the indices of the snak list where a certain snak may be moved to. A snak may be +* moved within its property group. It may also be moved to the slots between property groups +* which involves moving the whole property group the snak belongs to. +* @since 0.4 +* +* @param {wikibase.Snak} snak +* @return {number[]} +*/ + getValidMoveIndices: function( snak ) { + var self = this, + indices = [], + isGroupLast = false; + + this.each( function( i, snakListSnak ) { + if( snakListSnak.getPropertyId() === snak.getPropertyId() ) { + // Detect slots within the snak's property group. + if( snakListSnak !== snak ) { + indices.push( i ); + } else { + var nextSnak = self._snaks[i + 1]; + if( nextSnak && nextSnak.getPropertyId() !== snak.getPropertyId() ) { + // Snak is the last of its group. + isGroupLast = true; + } + } + } else { + // Detect slots between property groups. + var previousSnak = self._snaks[i - 1], + isNewPropertyGroup = ( + i !== 0 + && snakListSnak.getPropertyId() !== previousSnak.getPropertyId() + ); + + if( + // Since this snak's property group is not at the top of the snak list, the + // snak (with its group) may always be moved to the top: + i === 0 + // The snak (with its group) may always be moved to between groups except to + // adjacent slots between property groups since the snak's property group would + // in fact not be moved. + || isNewPropertyGroup && prev
[MediaWiki-commits] [Gerrit] Implemented ordering of snaks within SnakList - change (mediawiki...Wikibase)
Henning Snater has uploaded a new change for review. https://gerrit.wikimedia.org/r/80005 Change subject: Implemented ordering of snaks within SnakList .. Implemented ordering of snaks within SnakList (bug 52391) Implemented "move" functions in wikibase.SnakList which allow moving snaks within the SnakList while considering the snaks being grouped by property. Change-Id: Ic3e23827144b047038790afddf86996c3efa5c34 --- M lib/resources/wikibase.datamodel/wikibase.SnakList.js M lib/tests/qunit/wikibase.datamodel/Wikibase.SnakList.tests.js 2 files changed, 374 insertions(+), 3 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/05/80005/1 diff --git a/lib/resources/wikibase.datamodel/wikibase.SnakList.js b/lib/resources/wikibase.datamodel/wikibase.SnakList.js index d9a7653..a1a7472 100644 --- a/lib/resources/wikibase.datamodel/wikibase.SnakList.js +++ b/lib/resources/wikibase.datamodel/wikibase.SnakList.js @@ -3,6 +3,7 @@ * @ingroup WikibaseLib * @licence GNU GPL v2+ * @author Daniel Werner < daniel.wer...@wikimedia.de > + * @author H. Snater < mediaw...@snater.com > */ ( function( wb, $ ) { 'use strict'; @@ -151,11 +152,16 @@ ) { return false; } - for( var i in this._snaks ) { - if( !snakList.hasSnak( this._snaks[i] ) ) { + + var otherSnaks = snakList.toArray(); + + // Compare to other snak lists snaks considering order: + for( var i = 0; i < otherSnaks.length; i++ ) { + if( !this._snaks[i].equals( otherSnaks[i] ) ) { return false; } } + return true; }, @@ -196,6 +202,179 @@ }, /** +* Returns a snak's index within the snak list. Returns -1 when the snak could not be found. +* @since 0.4 +* +* @param {wikibase.Snak} snak +* @return {number} +*/ + indexOf: function( snak ) { + for( var i = 0; i < this._snaks.length; i++ ) { + if( this._snaks[i].equals( snak ) ) { + return i; + } + } + return -1; + }, + + /** +* Returns the indices of the snak list where a certain snak may be moved to. A snak may be +* moved within its property group. It may also be moved to the slots between property groups +* which involves moving the whole property group the snak belongs to. +* @since 0.4 +* +* @param {wikibase.Snak} snak +* @return {number[]} +*/ + getValidMoveIndices: function( snak ) { + var self = this, + indices = [], + isGroupLast = false; + + this.each( function( i, snakListSnak ) { + if( snakListSnak.getPropertyId() === snak.getPropertyId() ) { + // Detect slots within the snak's property group. + if( snakListSnak !== snak ) { + indices.push( i ); + } else { + var nextSnak = self._snaks[i + 1]; + if( nextSnak && nextSnak.getPropertyId() !== snak.getPropertyId() ) { + // Snak is the last of its group. + isGroupLast = true; + } + } + } else { + // Detect slots between property groups. + var previousSnak = self._snaks[i - 1], + isNewPropertyGroup = ( + i !== 0 + && snakListSnak.getPropertyId() !== previousSnak.getPropertyId() + ); + + if( + // Since this snak's property group is not at the top of the snak list, the + // snak (with its group) may always be moved to the top: + i === 0 + // The snak (with its group) may always be moved to between groups except to + // adjacent slots between property groups since the snak's property group would + // in fact not be moved. + || isNewPropertyGroup && previousSnak.getPropertyId() !== snak.getPropertyId() +