Henning Snater has uploaded a new change for review.

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

Change subject: statementgrouplistview: Added value() function
......................................................................

statementgrouplistview: Added value() function

Change-Id: Ib72249bce43fc297870edc8101d40d91a87ad268
---
M lib/resources/jquery.wikibase/jquery.wikibase.statementgrouplistview.js
M lib/resources/jquery.wikibase/jquery.wikibase.statementlistview.js
M 
lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementgrouplistview.tests.js
M lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementlistview.tests.js
4 files changed, 160 insertions(+), 12 deletions(-)


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

diff --git 
a/lib/resources/jquery.wikibase/jquery.wikibase.statementgrouplistview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.statementgrouplistview.js
index cd8c651..b05c89e 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.statementgrouplistview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.statementgrouplistview.js
@@ -116,13 +116,6 @@
                        this.$listview = $( '<div/>' ).appendTo( this.element );
                }
 
-               var propertyIds = this.options.value.getKeys(),
-                       value = [];
-
-               for( var i = 0; i < propertyIds.length; i++ ) {
-                       value.push( this.options.value.getItemByKey( 
propertyIds[i] ) );
-               }
-
                this.$listview.listview( {
                        listItemAdapter: new 
$.wikibase.listview.ListItemAdapter( {
                                listItemWidget: $.wikibase.statementgroupview,
@@ -137,7 +130,19 @@
                                        };
                                }
                        } ),
-                       value: value
+                       value: self._statementGroupSetToStatementGroups( 
this.options.value )
+               } );
+       },
+
+       /**
+        * @private
+        *
+        * @param {wikibase.datamodel.StatementGroup} statementGroupSet
+        * @return {wikibase.datamodel.StatementList[]}
+        */
+       _statementGroupSetToStatementGroups: function( statementGroupSet ) {
+               return $.map( statementGroupSet.getKeys(), function( propertyId 
) {
+                       return statementGroupSet.getItemByKey( propertyId );
                } );
        },
 
@@ -218,10 +223,40 @@
        },
 
        /**
+        * Sets the widget's value or retrieves the widget's current value 
(including any pending
+        * changes). The value the widget was initialized with may be retrieve 
using
+        * `this.option( 'value' )`.
+        *
+        * @param {wikibase.datamodel.StatementGroupSet} [statementGroupSet]
+        * @return {wikibase.datamodel.StatementGroupSet|undefined}
+        */
+       value: function( statementGroupSet ) {
+               if( statementGroupSet === undefined ) {
+                       return new wb.datamodel.StatementGroupSet(
+                               $.map( this.$listview.data( 'listview' 
).value(), function( statementGroup ) {
+                                       return statementGroup.value();
+                               } )
+                       );
+               }
+               this.option( 'value', statementGroupSet );
+       },
+
+       /**
         * @inheritdoc
         * @protected
         */
        _setOption: function( key, value ) {
+               if( key === 'value' && value !== undefined ) {
+                       if( !( value instanceof wb.datamodel.StatementGroupSet 
) ) {
+                               throw new Error(
+                                       'value needs to be an instance of 
wb.datamodel.StatementGroupSet'
+                               );
+                       }
+                       this.$listview.data( 'listview' ).value(
+                               this._statementGroupSetToStatementGroups( value 
)
+                       );
+               }
+
                var response = PARENT.prototype._setOption.apply( this, 
arguments );
 
                if( key === 'disabled' ) {
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.statementlistview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.statementlistview.js
index 9cdeccc..f8ce6c6 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.statementlistview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.statementlistview.js
@@ -236,6 +236,15 @@
        },
 
        /**
+        * Returns whether the widget currently features any `statementview` 
widgets.
+        *
+        * @return {boolean}
+        */
+       isEmpty: function() {
+               return !this.listview().items().length;
+       },
+
+       /**
         * Adds a new, pending `statementview` to the `statementlistview`.
         * @see jQuery.wikibase.listview.enterNewItem
         *
diff --git 
a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementgrouplistview.tests.js
 
b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementgrouplistview.tests.js
index 478f51e..8e259b8 100644
--- 
a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementgrouplistview.tests.js
+++ 
b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementgrouplistview.tests.js
@@ -82,6 +82,66 @@
        );
 } );
 
+QUnit.test( 'value()', function( assert ) {
+       var statementGroupSet1 = new wb.datamodel.StatementGroupSet( [
+                       new wb.datamodel.StatementGroup( 'P1',
+                               new wb.datamodel.StatementList( [new 
wb.datamodel.Statement(
+                                       new wb.datamodel.Claim( new 
wb.datamodel.PropertyNoValueSnak( 'P1' ) )
+                               )] )
+                       )
+               ] ),
+               statementGroupSet2 = new wb.datamodel.StatementGroupSet( [
+                       new wb.datamodel.StatementGroup( 'P2',
+                               new wb.datamodel.StatementList( [new 
wb.datamodel.Statement(
+                                       new wb.datamodel.Claim( new 
wb.datamodel.PropertyNoValueSnak( 'P2' ) )
+                               )] )
+                       )
+               ] ),
+               $statementgrouplistview = createStatementgrouplistview( {
+                       value: statementGroupSet1
+               } ),
+               statementgrouplistview = $statementgrouplistview.data( 
'statementgrouplistview' );
+
+       assert.ok(
+               statementgrouplistview.value().equals( statementGroupSet1 ),
+               'Retrieved value.'
+       );
+
+       statementgrouplistview.value( statementGroupSet2 );
+
+       assert.ok(
+               statementgrouplistview.value().equals( statementGroupSet2 ),
+               'Retrieved value after setting a new value.'
+       );
+
+       var statementgrouplistviewListview = 
statementgrouplistview.$listview.data( 'listview' ),
+               statementgrouplistviewListviewLia = 
statementgrouplistviewListview.listItemAdapter(),
+               $statementgroupview = 
statementgrouplistviewListview.items().first(),
+               statementgroupview = 
statementgrouplistviewListviewLia.liInstance( $statementgroupview ),
+               statementGroup = new wb.datamodel.StatementGroup( 'P3',
+                       new wb.datamodel.StatementList( [new 
wb.datamodel.Statement(
+                               new wb.datamodel.Claim( new 
wb.datamodel.PropertyNoValueSnak( 'P3' ) )
+                       )] )
+               );
+
+       statementgroupview.value = function() {
+               return statementGroup;
+       };
+
+       assert.ok(
+               statementgrouplistview.value().equals(
+                       new wb.datamodel.StatementGroupSet( [statementGroup] )
+               ),
+               'Retrieved current value after setting a new value on the 
statementview encapsulated by '
+                       + 'the statementlistview.'
+       );
+
+       assert.ok(
+               statementgrouplistview.option( 'value' ).equals( 
statementGroupSet2 ),
+               'Retrieved value still persisting via option().'
+       );
+} );
+
 QUnit.test( 'enterNewItem', function( assert ) {
        var $statementgrouplistview = createStatementgrouplistview(),
                statementgrouplistview = $statementgrouplistview.data( 
'statementgrouplistview' );
diff --git 
a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementlistview.tests.js 
b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementlistview.tests.js
index 06579a5..e8b039e 100644
--- a/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementlistview.tests.js
+++ b/lib/tests/qunit/jquery.wikibase/jquery.wikibase.statementlistview.tests.js
@@ -71,8 +71,8 @@
        $statementlistview = createStatementlistview( {
                value: new wb.datamodel.StatementList( [
                        new wb.datamodel.Statement( new wb.datamodel.Claim(
-                               new wb.datamodel.PropertyNoValueSnak( 'P1' ) )
-                       )
+                               new wb.datamodel.PropertyNoValueSnak( 'P1' )
+                       ) )
                ] )
        } );
        statementlistview = $statementlistview.data( 'statementlistview' );
@@ -86,10 +86,10 @@
 QUnit.test( 'value()', function( assert ) {
        var statementList1 = new wb.datamodel.StatementList( [new 
wb.datamodel.Statement(
                        new wb.datamodel.Claim( new 
wb.datamodel.PropertyNoValueSnak( 'P1' ) )
-               ) ] ),
+               )] ),
                statementList2 = new wb.datamodel.StatementList( [new 
wb.datamodel.Statement(
                        new wb.datamodel.Claim( new 
wb.datamodel.PropertyNoValueSnak( 'P2' ) )
-               ) ] ),
+               )] ),
                $statementlistview = createStatementlistview( {
                        value: statementList1
                } ),
@@ -131,6 +131,50 @@
        );
 } );
 
+QUnit.test( 'isEmpty()', function( assert ) {
+       var $statementlistview = createStatementlistview(),
+               statementlistview = $statementlistview.data( 
'statementlistview' );
+
+       assert.ok(
+               statementlistview.isEmpty(),
+               'Verified isEmpty() returning TRUE when widget has been 
initialized with an empty '
+                       + 'StatementList.'
+       );
+
+       $statementlistview = createStatementlistview( {
+               value: new wb.datamodel.StatementList( [
+                       new wb.datamodel.Statement( new wb.datamodel.Claim(
+                                       new wb.datamodel.PropertyNoValueSnak( 
'P1' )
+                       ) )
+               ] )
+       } );
+       statementlistview = $statementlistview.data( 'statementlistview' );
+
+       assert.ok(
+               !statementlistview.isEmpty(),
+               'Verified isEmpty() returning FALSE when widget has been 
initialized with a filled '
+                       + 'StatmentList.'
+       );
+
+       statementlistview.value( new wb.datamodel.StatementList() );
+
+       assert.ok(
+               statementlistview.isEmpty(),
+               'Verified isEmpty() returning TRUE after setting an empty 
StatementList.'
+       );
+
+       statementlistview.value( new wb.datamodel.StatementList( [
+               new wb.datamodel.Statement( new wb.datamodel.Claim(
+                       new wb.datamodel.PropertyNoValueSnak( 'P2' )
+               ) )
+       ] ) );
+
+       assert.ok(
+               !statementlistview.isEmpty(),
+               'Verified isEmpty() returning FALSE after setting an filled 
StatementList.'
+       );
+} );
+
 QUnit.test( 'enterNewItem', function( assert ) {
        var $statementlistview = createStatementlistview(),
                statementlistview = $statementlistview.data( 
'statementlistview' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib72249bce43fc297870edc8101d40d91a87ad268
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