Daniel Werner has submitted this change and it was merged.

Change subject: Removed __continueStopEditing flag
......................................................................


Removed __continueStopEditing flag

Event listeners are attached dynamically instead of using
a flag to determine how to continue event handling.

Change-Id: I8b7973fadd0e26ab29902e72a1fafb966a89d53f
---
M lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
M lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
M lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
M repo/tests/selenium/statements/references_spec.rb
4 files changed, 110 insertions(+), 78 deletions(-)

Approvals:
  Daniel Werner: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
index 988febc..b0d003d 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.claimview.js
@@ -137,19 +137,6 @@
                .on ( 'snakviewchange', function( event, status ) {
                        self._trigger( 'change' );
                } )
-               .on( 'snakviewstopediting', function( event, dropValue ) {
-                       // React on key stroke events (e.g. pressing enter or 
ESC key)
-                       if (
-                               ( !self.isValid() || self.isInitialValue() )
-                               && !dropValue && !self.__continueStopEditing
-                       ) {
-                               event.preventDefault();
-                       } else if ( !self.__continueStopEditing ) {
-                               // Do not exit snakview's edit mode yet; Let 
any API request be performed first.
-                               event.preventDefault();
-                               self.stopEditing( dropValue );
-                       }
-               } )
                .on( 'valueviewchange', function( e ) {
                        if ( self.errorTooltipAnchor ) {
                                self.errorTooltipAnchor.getTooltip().hide();
@@ -172,23 +159,12 @@
                        } )
                        .on( 'snaklistviewchange', function( event ) {
                                self._trigger( 'change' );
-                       } )
-                       .on( 'snaklistviewstopediting', function( event, 
dropValue ) {
-                               // React on key stroke events (e.g. pressing 
enter or ESC key)
-                               if (
-                                       ( !self.isValid() || 
self.isInitialValue() )
-                                       && !dropValue && 
!self.__continueStopEditing
-                               ) {
-                                       event.preventDefault();
-                               } else if ( !self.__continueStopEditing ) {
-                                       // Do not exit snakview's edit mode 
yet; Let any API request be performed first.
-                                       event.preventDefault();
-                                       self.stopEditing( dropValue );
-                               }
                        } );
 
                        this._qualifiers = $qualifiers.data( 'snaklistview' );
                }
+
+               this._attachEditModeEventHandlers();
 
                if ( this._claim || this.options.predefined.mainSnak ) {
                        var property = this._claim
@@ -282,12 +258,11 @@
                natively: function( e, dropValue ) {
                        var self = this;
 
+                       this._detachEditModeEventHandlers();
+
                        this.disable();
 
                        if ( dropValue ) {
-                               // nothing to update
-                               this.__continueStopEditing = true;
-
                                if ( this.$mainSnak.data( 'snakview' ) ) {
                                        this.$mainSnak.data( 'snakview' 
).stopEditing( dropValue );
                                }
@@ -296,27 +271,22 @@
                                        this._qualifiers.stopEditing( dropValue 
);
                                }
 
-                               this.__continueStopEditing = false;
-
                                self.enable();
                                self.element.removeClass( 'wb-edit' );
                                self._isInEditMode = false;
 
+                               self._attachEditModeEventHandlers();
+
                                self._trigger( 'afterstopediting', null, [ 
dropValue ] );
-                       } else if ( !this.__continueStopEditing ) {
+                       } else {
                                // editing an existing claim
                                self._saveClaimApiCall()
                                .done( function( savedClaim, pageInfo ) {
-                                       self.__continueStopEditing = true;
-
                                        self.$mainSnak.data( 'snakview' 
).stopEditing( dropValue );
 
                                        if ( self._qualifiers ) {
-                                               
self._qualifiers.__continueStopEditing = true;
                                                self._qualifiers.stopEditing();
                                        }
-
-                                       self.__continueStopEditing = false;
 
                                        self.enable();
 
@@ -330,6 +300,8 @@
                                        self.element.removeClass( 'wb-edit' );
                                        self._isInEditMode = false;
 
+                                       self._attachEditModeEventHandlers();
+
                                        // transform toolbar and snak view 
after save complete
                                        self._trigger( 'afterstopediting', 
null, [ dropValue ] );
                                } )
@@ -341,9 +313,9 @@
                                        self.enable();
                                        self.element.addClass( 'wb-error' );
 
-                                       self._trigger( 'toggleError', null, [ 
error ] );
+                                       self._attachEditModeEventHandlers();
 
-                                       self.__continueStopEditing = false;
+                                       self._trigger( 'toggleError', null, [ 
error ] );
                                } );
                        }
                }
@@ -361,6 +333,36 @@
        },
 
        /**
+        * Attaches event listeners that shall trigger stopping the claimview's 
edit mode.
+        * @since 0.4
+        */
+       _attachEditModeEventHandlers: function() {
+               var self = this;
+
+               this.$mainSnak.one( 'snakviewstopediting', function( event, 
dropValue ) {
+                       self.stopEditing( dropValue );
+               } );
+
+               if ( this._qualifiers ) {
+                       this._qualifiers.element.one( 
'snaklistviewstopediting', function( event, dropValue ) {
+                               self.stopEditing( dropValue );
+                       } );
+               }
+       },
+
+       /**
+        * Detaches event listeners that shall trigger stopping the claimview's 
edit mode.
+        * @since 0.4
+        */
+       _detachEditModeEventHandlers: function() {
+               this.$mainSnak.off( 'snakviewstopediting' );
+
+               if ( this._qualifiers ) {
+                       this._qualifiers.element.off( 'snaklistviewstopediting' 
);
+               }
+       },
+
+       /**
         * Removes the claimview.
         */
        remove: $.NativeEventHandler( 'remove', {
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
index 5f9946c..92d989b 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
@@ -66,12 +66,27 @@
                }
                PARENT.prototype._create.call( this );
 
-               this.element.on( this.widgetName + 'stopediting.' + 
this.widgetName, function( event, dropValue ) {
-                       if ( !dropValue && !self.__continueStopEditing ) {
+               this._updateReferenceHashClass( this.value() );
+       },
+
+       /**
+        * @see jQuery.wikibase.snaklistview._attachEditModeEventHandlers
+        */
+       _attachEditModeEventHandlers: function() {
+               var self = this;
+
+               this.element.one( this.widgetName + 'stopediting.' + 
this.widgetName, function( event, dropValue ) {
+                       self.disable();
+
+                       if ( !dropValue ) {
+                               event.preventDefault();
                                self._saveReferenceApiCall()
                                .done( function( savedObject, pageInfo ) {
-                                       self.__continueStopEditing = true;
+                                       self.element.off( self.widgetName + 
'stopediting.' + self.widgetName );
                                        self.stopEditing( dropValue );
+                                       if ( self.$listview ) {
+                                               
self._attachEditModeEventHandlers();
+                                       }
                                } )
                                .fail( function( errorCode, details ) {
                                        var error = 
wb.RepoApiError.newFromApiResponse(
@@ -81,14 +96,22 @@
                                        self.enable();
                                        self.element.addClass( 'wb-error' );
 
-                                       self._trigger( 'toggleError', null, [ 
error ] );
+                                       self._attachEditModeEventHandlers();
 
-                                       self.__continueStopEditing = false;
+                                       self._trigger( 'toggleError', null, [ 
error ] );
                                } );
                        }
                } );
 
-               this._updateReferenceHashClass( this.value() );
+               PARENT.prototype._attachEditModeEventHandlers.call( this );
+       },
+
+       /**
+        * @see jQuery.wikibase.snaklistview._detachEditModeEventHandlers
+        */
+       _detachEditModeEventHandlers: function() {
+               this.element.off( this.widgetName + 'stopediting.' + 
this.widgetName );
+               PARENT.prototype._detachEditModeEventHandlers.call( this );
        },
 
        /**
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
index a952bfe..f83ee82 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
@@ -160,25 +160,14 @@
                                self._trigger( 'change' );
                        } );
                } )
-               .on( self._lia.prefixedEvent( 'change.' ) + this.widgetName
+               .on( this._lia.prefixedEvent( 'change.' ) + this.widgetName
                        + ' listviewitemremoved.' + this.widgetName, function( 
event ) {
                                // Forward the "change" event to external 
components (e.g. the edit toolbar).
                                self._trigger( 'change' );
                        }
-               )
-               .on( self._lia.prefixedEvent( 'stopediting.' + this.widgetName 
),
-                       function( event, dropValue, newSnak ) {
-                               if (
-                                       ( !self.isValid() || 
self.isInitialValue() )
-                                       && !dropValue && 
!self.__continueStopEditing
-                               ) {
-                                       event.preventDefault();
-                               } else if ( !self.__continueStopEditing ) {
-                                       event.preventDefault();
-                                       self.stopEditing( dropValue );
-                               }
-                       }
                );
+
+               this._attachEditModeEventHandlers();
        },
 
        /**
@@ -226,6 +215,10 @@
                        this.element.removeClass( 'wb-error' );
                },
                natively: function( e, dropValue ) {
+                       var self = this;
+
+                       this._detachEditModeEventHandlers();
+
                        this.disable();
 
                        if ( dropValue ) {
@@ -235,15 +228,7 @@
                                // Re-create the list view to restore snakviews 
that have been removed during
                                // editing:
                                this.createListView();
-
-                               this.enable();
-                               this.element.removeClass( 'wb-edit' );
-                               this._isInEditMode = false;
-
-                               this._trigger( 'afterstopediting', null, [ 
dropValue ] );
-                       } else if ( this.__continueStopEditing ) {
-                               var self = this;
-
+                       } else {
                                $.each( this._listview.items(), function( i, 
item ) {
                                        var $item = $( item ),
                                                snakview = 
self._lia.liInstance( $item );
@@ -253,16 +238,15 @@
                                        // After saving, the property should 
not be editable anymore.
                                        snakview.options.locked.property = true;
                                } );
-                               this.__continueStopEditing = false;
-
-                               this.enable();
-
-                               this.element.removeClass( 'wb-edit' );
-                               this._isInEditMode = false;
-
-                               // Transform toolbar and snak view after save 
complete
-                               this._trigger( 'afterstopediting', null, [ 
dropValue ] );
                        }
+
+                       this.enable();
+
+                       this.element.removeClass( 'wb-edit' );
+                       this._isInEditMode = false;
+
+                       // Transform toolbar and snak view after save complete
+                       this._trigger( 'afterstopediting', null, [ dropValue ] 
);
                }
        } ),
 
@@ -278,6 +262,29 @@
        },
 
        /**
+        * Attaches event listeners that shall trigger stopping the 
snaklistview's edit mode.
+        * @since 0.4
+        */
+       _attachEditModeEventHandlers: function() {
+               var self = this;
+
+               this.$listview.one( this._lia.prefixedEvent( 'stopediting.' + 
this.widgetName ),
+                       function( event, dropValue, newSnak ) {
+                               event.preventDefault();
+                               self.stopEditing( dropValue );
+                       }
+               );
+       },
+
+       /**
+        * Detaches event listeners that shall trigger stopping the 
snaklistview's edit mode.
+        * @since 0.4
+        */
+       _detachEditModeEventHandlers: function() {
+               this.$listview.off( this._lia.prefixedEvent( 'stopediting.' + 
this.widgetName ) );
+       },
+
+       /**
         * Sets/Returns the current list of snaks represented by the view. If 
there are no snaks, null
         * will be returned.
         * @since 0.4
diff --git a/repo/tests/selenium/statements/references_spec.rb 
b/repo/tests/selenium/statements/references_spec.rb
index 78883fa..6bd3433 100644
--- a/repo/tests/selenium/statements/references_spec.rb
+++ b/repo/tests/selenium/statements/references_spec.rb
@@ -221,7 +221,7 @@
         page.cancelReference?.should be_true
         page.editReference1?.should be_false
         page.addReferenceToFirstClaim?.should be_false
-        page.saveReference
+        page.referenceValueInput_element.send_keys :return
         ajax_wait
         page.wait_for_statement_request_finished
         page.reference1Property.should == properties_cm[1]["label"]

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I8b7973fadd0e26ab29902e72a1fafb966a89d53f
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to