Henning Snater has uploaded a new change for review.

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


Change subject: claimview/claimlistview refactoring
......................................................................

claimview/claimlistview refactoring

Removed the separate claimlistview's API request when adding a new claim. The 
claimlistview's
action when adding a new claim has been moved to the "afterstopediting" event 
handling.

Change-Id: I0b9ac927efba5599bde5f25f760752dfac0e5d2d
---
M lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
M lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
M lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
3 files changed, 61 insertions(+), 124 deletions(-)


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

diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
index 135477b..2bb402c 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.claimlistview.js
@@ -337,90 +337,10 @@
                // first time the claim (or at least a part of it) drops out of 
edit mode:
                // TODO: not nice to use the event of the main snak, perhaps 
the claimview could offer one!
                $newClaim
-               .on( this._lmwEvent( 'stopediting' ), function( event, 
dropValue ) {
-                       var newClaimview = self._lmwInstance( $newClaim ),
-                               newSnak = newClaimview.$mainSnak.data( 
'snakview' ).snak();
-
-                       if ( !self._lmwInstance( $newClaim ).isValid() && 
!dropValue ) {
-                               event.preventDefault();
-                               return;
-                       }
-
-                       if ( self.__continueStopEditing ) {
-                               self.__continueStopEditing = false;
-                               $newClaim.off( self._lmwEvent( 'stopediting' ) 
);
-                               return;
-                       }
-
-                       if ( !dropValue ) {
-                               event.preventDefault();
-                       } else {
-                               self.element.removeClass( 'wb-error' );
-                       }
-
-                       // TODO: right now, if the claim is not valid (e.g. 
because data type not yet
-                       //       supported), the edit mode will close when 
saving without showing any hint!
-
-                       if( !dropValue && newSnak ) {
-                               // TODO: either change toggleActionMessage to 
do the disabling, then execute the
-                               //       callback and finally execute the 
toggle animation, OR separate the
-                               //       disabling and call it manually, then 
do the API call, then toggle.
-                               var api = new wb.RepoApi(),
-                                       newClaim,
-                                       guidGenerator = new 
wb.utilities.ClaimGuidGenerator(),
-                                       guid = guidGenerator.newGuid( 
mw.config.get( 'wbEntityId' ) );
-
-                               if ( newClaimview.$references ) {
-                                       newClaim = new wb.Statement(
-                                               newSnak,
-                                               null, // TODO: Qualifiers
-                                               [], // TODO: References
-                                               null, // TODO: Rank
-                                               guid
-                                       )
-                               } else {
-                                       newClaim = new wb.Claim(
-                                               newSnak,
-                                               null, // TODO: Qualifiers
-                                               guid
-                                       )
-                               }
-
-                               api.setClaim( newClaim, 
wb.getRevisionStore().getBaseRevision() )
-                               .done( function( newClaimWithGUID, pageInfo ) {
-                                       wb.getRevisionStore().setClaimRevision(
-                                               pageInfo.lastrevid,
-                                               newClaimWithGUID.getGuid()
-                                       );
-
-                                       // Continue stopEditing event by 
triggering it again skipping
-                                       // claimlistview's API call.
-                                       self.__continueStopEditing = true;
-
-                                       self._lmwInstance( $( event.target ) 
).stopEditing( dropValue );
-
-                                       self._trigger( 'itemadded', null, [ 
newSnak, $newClaim ] );
-
-                                       // destroy new claim input form and add 
claim to this list
-                                       self._lmwInstance( $newClaim 
).destroy();
-                                       $newClaim.remove();
-                                       self._addClaim( newClaimWithGUID ); // 
display new claim with final GUID
-                               } )
-                               .fail( function( errorCode, details ) {
-                                       var claimview = self._lmwInstance( 
$newClaim ),
-                                               error = 
wb.RepoApiError.newFromApiResponse(
-                                                       errorCode, details, 
'save'
-                                               );
-
-                                       claimview.enable();
-                                       claimview.element.addClass( 'wb-error' 
);
-
-                                       claimview._trigger( 'toggleerror', 
null, [error] );
-                               } );
-                       }
-               } )
                .on( this._lmwEvent( 'afterstopediting' ), function( event, 
dropValue ) {
-                       if( dropValue || !self._lmwInstance( $newClaim 
).$mainSnak.data( 'snakview' ).snak() ) {
+                       var snak = self._lmwInstance( $newClaim 
).$mainSnak.data( 'snakview' ).snak();
+
+                       if( dropValue || !snak ) {
                                // if new claim is canceled before saved, or if 
it is invalid, we simply remove
                                // and forget about it
                                self._trigger( 'canceled', null, [ null, 
$newClaim ] );
@@ -429,6 +349,17 @@
                                $newClaim.remove();
                                self.element.find( '.wb-claim-section' )
                                        .not( ':has( >.wb-edit )' 
).removeClass( 'wb-edit' );
+                       } else {
+                               var newClaimWithGuid = self._lmwInstance( 
$newClaim ).value();
+
+                               self._trigger( 'itemadded', null, [ snak, 
$newClaim ] );
+
+                               // Destroy new claim input form and add claim 
to this list:
+                               self._lmwInstance( $newClaim ).destroy();
+                               $newClaim.remove();
+
+                               // Display new claim with final GUID:
+                               self._addClaim( newClaimWithGuid );
                        }
                } );
        },
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
index 4037049..2eb2e40 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
@@ -303,7 +303,7 @@
                                self._isInEditMode = false;
 
                                self._trigger( 'afterstopediting', null, [ 
dropValue ] );
-                       } else if ( this._claim && !this.__continueStopEditing 
) {
+                       } else if ( !this.__continueStopEditing ) {
                                // editing an existing claim
                                self._saveClaimApiCall()
                                .done( function( savedClaim, pageInfo ) {
@@ -345,12 +345,6 @@
 
                                        self.__continueStopEditing = false;
                                } );
-                       } else if ( !this.__continueStopEditing ) {
-                               // Adding a new claim is managed in 
claimlistview and will end up in here after
-                               // having performed the API call adding the 
claim.
-                               self.element.removeClass( 'wb-edit' );
-                               this._isInEditMode = false;
-                               self._trigger( 'afterstopediting', null, [ 
dropValue ] );
                        }
                }
        } ),
@@ -411,6 +405,21 @@
        },
 
        /**
+        * Instantiates a claim with the claimview's current value.
+        * @since 0.4
+        *
+        * @param {string} guid
+        * @return {wb.Claim}
+        */
+       _instantiateClaim: function( guid ) {
+               return new wb.Claim(
+                       this.$mainSnak.data( 'snakview' ).snak(),
+                       ( this._qualifiers ) ? this._qualifiers.value() : null,
+                       guid
+               );
+       },
+
+       /**
         * Triggers the API call to save the claim.
         * @since 0.4
         *
@@ -423,21 +432,28 @@
                var self = this,
                        api = new wb.RepoApi(),
                        revStore = wb.getRevisionStore(),
-                       guid = this.value().getGuid(),
-                       claim = new wb.Claim(
-                               this.$mainSnak.data( 'snakview' ).snak(),
-                               ( this._qualifiers ) ? this._qualifiers.value() 
: null,
-                               guid
-                       );
+                       guid;
 
-               return api.setClaim( claim, revStore.getClaimRevision( guid ) )
+               if ( this.value() ) {
+                       guid = this.value().getGuid();
+               } else {
+                       var guidGenerator = new 
wb.utilities.ClaimGuidGenerator();
+                       guid = guidGenerator.newGuid( mw.config.get( 
'wbEntityId' ) );
+               }
+
+               return api.setClaim( this._instantiateClaim( guid ), 
revStore.getClaimRevision( guid ) )
                .done( function( savedClaim, pageInfo ) {
                        // Update revision store:
                        revStore.setClaimRevision( pageInfo.lastrevid, 
savedClaim.getGuid() );
 
                        // Update model of represented Claim:
                        self._claim = savedClaim;
-                       self._qualifiers.value( savedClaim.getQualifiers() );
+
+                       // If the claim was pending (adding a new claim instead 
of editing an existing one),
+                       // there are no qualifiers set yet.
+                       if ( self._qualifiers ) {
+                               self._qualifiers.value( 
savedClaim.getQualifiers() );
+                       }
                } );
        },
 
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
index be73e67..4362613 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
@@ -164,33 +164,17 @@
        },
 
        /**
-        * Triggers the API call to save the satement.
-        * @see jQuery.wikibase.claimview._saveClaimApiCall
-        *
-        * @return {jQuery.Promise}
+        * Instantiates a statement with the statementview's current value.
+        * @see jQuery.wikibase.claimview._instantiateClaim
         */
-       _saveClaimApiCall: function() {
-               var self = this,
-                       api = new wb.RepoApi(),
-                       revStore = wb.getRevisionStore(),
-                       guid = this.value().getGuid(),
-                       statement = new wb.Statement(
-                               this.$mainSnak.data( 'snakview' ).snak(),
-                               ( this._qualifiers ) ? this._qualifiers.value() 
: null,
-                               this.getReferences(),
-                               null, // TODO: Rank
-                               guid
-                       );
-
-               return api.setClaim( statement, revStore.getClaimRevision( guid 
) )
-                       .done( function( newStatement, pageInfo ) {
-                               // Update revision store:
-                               revStore.setClaimRevision( pageInfo.lastrevid, 
newStatement.getGuid() );
-
-                               // Update model of represented Claim:
-                               self._claim = newStatement;
-                               self._qualifiers.value( 
newStatement.getQualifiers() );
-                       } );
+       _instantiateClaim: function( guid ) {
+               return new wb.Statement(
+                       this.$mainSnak.data( 'snakview' ).snak(),
+                       ( this._qualifiers ) ? this._qualifiers.value() : null,
+                       this.getReferences(),
+                       null, // TODO: Rank
+                       guid
+               );
        },
 
        /**
@@ -213,6 +197,12 @@
                var self = this,
                        references = [];
 
+               // If the statement is pending (not yet stored), the listview 
widget for the references is
+               // not defined.
+               if ( !this.$references.data( 'listview' ) ) {
+                       return references;
+               }
+
                $.each( this.$references.data( 'listview' ).items(), function( 
i, item ) {
                        var referenceview = self._referenceviewLia.liInstance( 
$( item ) );
                        references.push( referenceview.value() );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b9ac927efba5599bde5f25f760752dfac0e5d2d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <henning.sna...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to