Henning Snater has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/80386


Change subject: Implemented "move" functionality in snaklistview widget
......................................................................

Implemented "move" functionality in snaklistview widget

Change-Id: Ia8d6da83951c4bcfa3a84439a999da186a671a76
---
M lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
M lib/resources/wikibase.datamodel/wikibase.SnakList.js
M lib/tests/qunit/jquery.wikibase/jquery.wikibase.snaklistview.tests.js
M lib/tests/qunit/wikibase.datamodel/Wikibase.SnakList.tests.js
4 files changed, 256 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/86/80386/1

diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
index 63c4268..0dd71c9 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
@@ -451,6 +451,88 @@
                        throw new Error( 'Can not set value after 
initialization' );
                }
                $.Widget.prototype._setOption.call( this, key, value );
+       },
+
+       /**
+        * Moves a snak within the snak list.
+        * @since 0.4
+        *
+        * @param {wikibase.Snak} snak
+        * @param {number} toIndex
+        */
+       move: function( snak, toIndex ) {
+               var self = this,
+                       snakList;
+
+               if( snak instanceof wb.Snak ) {
+                       snakList = this.value();
+                       if( snakList ) {
+                               snakList.move( snak, toIndex );
+                       }
+               } else if( snak instanceof wb.SnakList ) {
+                       snakList = snak;
+               }
+
+               if( snakList ) {
+                       // Reflect new snak list order in snaklistview:
+                       snakList.each( function( i, snak ) {
+                               var $listItem = self._findListItem( snak );
+                               if( $listItem ) {
+                                       self._listview.move( 
self._findListItem( snak ), i );
+                               }
+                       } );
+               }
+       },
+
+       /**
+        * Moves a snak towards the top of the snak list by one step.
+        * @since 0.4
+        *
+        * @param {wikibase.Snak} snak
+        */
+       moveUp: function( snak ) {
+               var snakList = this.value();
+
+               if( snakList ) {
+                       this.move( snakList.moveUp( snak ) );
+               }
+       },
+
+       /**
+        * Moves a snak towards the bottom of the snak list by one step.
+        * @since 0.4
+        *
+        * @param {wikibase.Snak} snak
+        */
+       moveDown: function( snak ) {
+               var snakList = this.value();
+
+               if( snakList ) {
+                       this.move( snakList.moveDown( snak ) );
+               }
+       },
+
+       /**
+        * Finds a snak's snakview node within the snaklistview's listview 
widget.
+        * @since 0.4
+        *
+        * @param {wikibase.Snak} snak
+        * @return {jQuery|null}
+        */
+       _findListItem: function( snak ) {
+               var self = this,
+                       $snakview = null;
+
+               this._listview.items().each( function( i, itemNode ) {
+                       var $itemNode = $( itemNode );
+
+                       if( self._listview.listItemAdapter().liInstance( 
$itemNode ).snak().equals( snak ) ) {
+                               $snakview = $itemNode;
+                               return false;
+                       }
+               } );
+
+               return $snakview;
        }
 
 } );
diff --git a/lib/resources/wikibase.datamodel/wikibase.SnakList.js 
b/lib/resources/wikibase.datamodel/wikibase.SnakList.js
index a1a7472..a53c2f8 100644
--- a/lib/resources/wikibase.datamodel/wikibase.SnakList.js
+++ b/lib/resources/wikibase.datamodel/wikibase.SnakList.js
@@ -279,12 +279,13 @@
         *
         * @param {wikibase.Snak} snak Snak to move within the list.
         * @param {number} toIndex
+        * @return {wikibase.SnakList} This SnakList object.
         *
         * @throws {Error} if snak is not allowed to be moved to toIndex.
         */
        move: function( snak, toIndex ) {
                if( this.indexOf( snak ) === toIndex ) {
-                       return;
+                       return this;
                }
 
                var validIndices = this.getValidMoveIndices( snak );
@@ -330,6 +331,7 @@
                        }
                }
 
+               return this;
        },
 
        /**
@@ -337,7 +339,7 @@
         * @since 0.4
         *
         * @param {wikibase.Snak} snak
-        * @return {number} The snaks new index.
+        * @return {wikibase.SnakList} This SnakList object.
         */
        moveUp: function( snak ) {
                var index = this.indexOf( snak ),
@@ -350,7 +352,7 @@
                        }
                }
 
-               return this.indexOf( snak );
+               return this;
        },
 
        /**
@@ -358,7 +360,7 @@
         * @since 0.4
         *
         * @param {wikibase.Snak} snak
-        * @return {number} The snak's new index.
+        * @return {wikibase.SnakList} This SnakList object.
         */
        moveDown: function( snak ) {
                var index = this.indexOf( snak ),
@@ -371,7 +373,7 @@
                        }
                }
 
-               return this.indexOf( snak );
+               return this;
        },
 
        /**
diff --git 
a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.snaklistview.tests.js 
b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.snaklistview.tests.js
index bdc8052..ca6da06 100644
--- a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.snaklistview.tests.js
+++ b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.snaklistview.tests.js
@@ -48,6 +48,22 @@
                return snaklistview;
        }
 
+       /**
+        * Returns the concatenated string values of a snak list's snaks.
+        *
+        * @param {wikibase.SnakList} snakList
+        * @return {string}
+        */
+       function snakOrder( snakList ) {
+               var snakValues = [];
+
+               snakList.each( function( i, snak ) {
+                       snakValues.push( snak.getValue().getValue() );
+               } );
+
+               return snakValues.join( '' );
+       }
+
        QUnit.module( 'jquery.wikibase.snaklistview', 
window.QUnit.newWbEnvironment( {
                teardown: function() {
                        $( '.test_snaklistview' ).each( function( i, node ) {
@@ -684,4 +700,149 @@
                );
        } );
 
+       QUnit.test( 'move()', function( assert ) {
+               var snaks = [
+                       new wb.PropertyValueSnak( 'p1',  new dv.StringValue( 
'a' ) ),
+                       new wb.PropertyValueSnak( 'p1',  new dv.StringValue( 
'b' ) ),
+                       new wb.PropertyValueSnak( 'p2',  new dv.StringValue( 
'c' ) ),
+                       new wb.PropertyValueSnak( 'p2',  new dv.StringValue( 
'd' ) ),
+                       new wb.PropertyValueSnak( 'p2',  new dv.StringValue( 
'e' ) ),
+                       new wb.PropertyValueSnak( 'p3',  new dv.StringValue( 
'f' ) ),
+                       new wb.PropertyValueSnak( 'p4',  new dv.StringValue( 
'g' ) )
+               ];
+
+               var snakList = new wb.SnakList( snaks );
+
+               /**
+                * Array of test case definitions. Test case definition 
structure:
+                * [0] => Index of element to move
+                * [1] => Index where to move element
+                * [2] => Expected result when concatenating the string values 
of the snak list's snaks.
+                * @type {*[][]}
+                */
+               var testCases = [
+                       [ 0, 1, 'bacdefg' ],
+                       [ 0, 5, 'cdeabfg' ],
+                       [ 0, 6, 'cdefabg' ],
+                       [ 0, 7, 'cdefgab' ],
+                       [ 1, 0, 'bacdefg' ],
+                       [ 1, 5, 'cdeabfg' ],
+                       [ 1, 6, 'cdefabg' ],
+                       [ 1, 7, 'cdefgab' ],
+                       [ 2, 0, 'cdeabfg' ],
+                       [ 2, 3, 'abdcefg' ],
+                       [ 2, 4, 'abdecfg' ],
+                       [ 2, 6, 'abfcdeg' ],
+                       [ 2, 7, 'abfgcde' ],
+                       [ 3, 0, 'cdeabfg' ],
+                       [ 3, 2, 'abdcefg' ],
+                       [ 3, 4, 'abcedfg' ],
+                       [ 3, 6, 'abfcdeg' ],
+                       [ 3, 7, 'abfgcde' ],
+                       [ 4, 0, 'cdeabfg' ],
+                       [ 4, 2, 'abecdfg' ],
+                       [ 4, 3, 'abcedfg' ],
+                       [ 4, 6, 'abfcdeg' ],
+                       [ 4, 7, 'abfgcde' ],
+                       [ 5, 0, 'fabcdeg' ],
+                       [ 5, 2, 'abfcdeg' ],
+                       [ 5, 7, 'abcdegf' ],
+                       [ 6, 0, 'gabcdef' ],
+                       [ 6, 2, 'abgcdef' ],
+                       [ 6, 5, 'abcdegf' ]
+               ];
+
+               var $node,
+                       snaklistview;
+
+               for( var i = 0; i < testCases.length; i++ ) {
+                       $node = createSnaklistview( snakList );
+                       snaklistview = $node.data( 'snaklistview' );
+
+                       snaklistview.move( snaks[testCases[i][0]], 
testCases[i][1] );
+
+                       assert.equal(
+                               snakOrder( snaklistview.value() ),
+                               testCases[i][2],
+                               'Verified moving a snak with test set #' + i + 
'.'
+                       );
+               }
+
+               $node = createSnaklistview( snakList );
+               snaklistview = $node.data( 'snaklistview' );
+               snaklistview.move( snaks[1], 1 );
+
+               assert.equal(
+                       snakOrder( snaklistview.value() ),
+                       'abcdefg',
+                       'Nothing changed when trying to move a snak to an index 
it already has.'
+               );
+
+               assert.throws(
+                       function() {
+                               $node = createSnaklistview( snakList );
+                               snaklistview = $node.data( 'snaklistview' );
+                               snaklistview.move( snaks[0], 4 );
+                       },
+                       'move() throws an error when trying to move a snak to 
an invalid index.'
+               );
+       } );
+
+       QUnit.test( 'moveUp() and moveDown()', function( assert ) {
+               var snaks = [
+                       new wb.PropertyValueSnak( 'p1',  new dv.StringValue( 
'a' ) ),
+                       new wb.PropertyValueSnak( 'p1',  new dv.StringValue( 
'b' ) ),
+                       new wb.PropertyValueSnak( 'p2',  new dv.StringValue( 
'c' ) ),
+                       new wb.PropertyValueSnak( 'p2',  new dv.StringValue( 
'd' ) ),
+                       new wb.PropertyValueSnak( 'p2',  new dv.StringValue( 
'e' ) ),
+                       new wb.PropertyValueSnak( 'p3',  new dv.StringValue( 
'f' ) ),
+                       new wb.PropertyValueSnak( 'p4',  new dv.StringValue( 
'g' ) )
+               ];
+
+               var snakList = new wb.SnakList( snaks ),
+                       $node,
+                       snaklistview;
+
+               /**
+                * Array of test case definitions for moveUp() and moveDown() 
methods. Test case definition
+                * structure:
+                * [0] => Resulting order after moving the element having the 
same index in the snak list up.
+                * [1] => Resulting order after moving the element having the 
same index in the snak list down.
+                * @type {string[][]}
+                */
+               var testCases = [
+                       ['abcdefg', 'bacdefg' ],
+                       ['bacdefg', 'cdeabfg' ],
+                       ['cdeabfg', 'abdcefg' ],
+                       ['abdcefg', 'abcedfg' ],
+                       ['abcedfg', 'abfcdeg' ],
+                       ['abfcdeg', 'abcdegf' ],
+                       ['abcdegf', 'abcdefg' ]
+               ];
+
+               for( var i = 0; i < testCases.length; i++ ) {
+                       $node = createSnaklistview( snakList );
+                       snaklistview = $node.data( 'snaklistview' );
+
+                       snaklistview.moveUp( snaks[i] );
+
+                       assert.equal(
+                               snakOrder( snaklistview.value() ),
+                               testCases[i][0],
+                               'Moving up a snak with test set #' + i + '.'
+                       );
+
+                       $node = createSnaklistview( snakList );
+                       snaklistview = $node.data( 'snaklistview' );
+
+                       snaklistview.moveDown( snaks[i] );
+
+                       assert.equal(
+                               snakOrder( snaklistview.value() ),
+                               testCases[i][1],
+                               'Moved down a snak with test set #' + i + '.'
+                       );
+               }
+       } );
+
 } )( jQuery, mediaWiki, wikibase, dataValues );
diff --git a/lib/tests/qunit/wikibase.datamodel/Wikibase.SnakList.tests.js 
b/lib/tests/qunit/wikibase.datamodel/Wikibase.SnakList.tests.js
index 1f4f390..38ae6c8 100644
--- a/lib/tests/qunit/wikibase.datamodel/Wikibase.SnakList.tests.js
+++ b/lib/tests/qunit/wikibase.datamodel/Wikibase.SnakList.tests.js
@@ -261,31 +261,20 @@
                for( i = 0; i < testCases.length; i++ ) {
                        snakList = new wb.SnakList( snaks );
 
-                       assert.strictEqual(
-                               snakList.moveUp( snaks[i] ),
-                               testCases[i][0].indexOf( 
snaks[i].getValue().getValue() ),
-                               'moveUp() returns correct new index for test 
set #' + i + '.'
-                       );
-
                        assert.equal(
-                               snakOrder( snakList ),
+                               snakOrder( snakList.moveUp( snaks[i] ) ),
                                testCases[i][0],
-                               'Verified moving up a snak with test set #' + i 
+ '.'
+                               'Verified result of moveUp() with test set #' + 
i + '.'
                        );
 
                        snakList = new wb.SnakList( snaks );
 
-                       assert.strictEqual(
-                               snakList.moveDown( snaks[i] ),
-                               testCases[i][1].indexOf( 
snaks[i].getValue().getValue() ),
-                               'moveDown() returns correct new index for test 
set #' + i + '.'
+                       assert.equal(
+                               snakOrder( snakList.moveDown( snaks[i] ) ),
+                               testCases[i][1],
+                               'Verified result of moveDown() with test set #' 
+ i + '.'
                        );
 
-                       assert.equal(
-                               snakOrder( snakList ),
-                               testCases[i][1],
-                               'Verified moving down a snak with test set #' + 
i + '.'
-                       );
                }
 
        } );

-- 
To view, visit https://gerrit.wikimedia.org/r/80386
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia8d6da83951c4bcfa3a84439a999da186a671a76
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to