Tobias Gritschacher has submitted this change and it was merged.

Change subject: Moving "add" toolbar initialization from snaklistview to 
referenceview
......................................................................


Moving "add" toolbar initialization from snaklistview to referenceview

The referenceview should control the toolbar to add new snaks to its list. This 
allows
adapting the "add" toolbbar to the toolbar controller specifically for the 
referenceview
which will be done in another change set.
Along with this change, the "add" button for adding snaks to a reference will 
be disabled
as long as the list of snaks is not valid.

Change-Id: I1e9841e1683e6d503be1ffd654fd97f62b93f7aa
---
M lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
M lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
M lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
M lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
4 files changed, 69 insertions(+), 34 deletions(-)

Approvals:
  Tobias Gritschacher: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
index 9a40864..873555a 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.edittoolbar.js
@@ -170,7 +170,10 @@
                                var isEmpty = $.isFunction( 
self._interactionWidget.isEmpty )
                                        && self._interactionWidget.isEmpty();
 
-                               if ( self._interactionWidget.isValid() && 
!isEmpty ) {
+                               var isInitial = $.isFunction( 
self._interactionWidget.isInitialValue )
+                                       && 
self._interactionWidget.isInitialValue();
+
+                               if ( self._interactionWidget.isValid() && 
!isEmpty && !isInitial ) {
                                        self.editGroup.btnSave.enable();
                                } else {
                                        self.editGroup.btnSave.disable();
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
index 0f318b5..d24637a 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js
@@ -27,9 +27,18 @@
        _reference: null,
 
        /**
+        * Node of the "add" toolbar used to add snaks to the reference. The 
node exists only while in
+        * edit mode.
+        * @type {jQuery}
+        */
+       $addToolbar: null,
+
+       /**
         * @see jQuery.wikibase.snaklistview._create
         */
        _create: function() {
+               var self = this;
+
                if ( this.option( 'value' ) ) {
                        this._reference = this.option( 'value' );
                        // Overwrite the value since the parent snaklistview 
widget require a wb.SnakList
@@ -38,6 +47,28 @@
                }
                PARENT.prototype._create.call( this );
                this._updateReferenceHashClass( this.value() );
+
+               // TODO: Make use of toolbar controller
+               this.element
+               .on( 'referenceviewstartediting', function( event, dropValue ) {
+                       // Append "add" toolbar to add new snaks:
+                       self.$addToolbar = mw.template( 'wb-toolbar', '' 
).appendTo( self.element );
+                       self.element.addtoolbar( {
+                               toolbarParentSelector: '> .wb-ui-toolbar',
+                               customAction: function( event, $parent ) {
+                                       self.enterNewItem();
+                               },
+                               eventPrefix: 
$.wikibase.referenceview.prototype.widgetEventPrefix
+                       } );
+               } )
+               .on( 'referenceviewafterstopediting', function( event, 
dropValue ) {
+                               self.element.data( 'addtoolbar' ).destroy();
+                               self.$addToolbar.remove();
+               } )
+               .on( 'referenceviewchange', function( event ) {
+                       var toolbar = self.element.data( 'addtoolbar' ).toolbar;
+                       toolbar[self.isValid() ? 'enable' : 'disable']();
+               } );
        },
 
        /**
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
index b25d460..d89c27c 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.snaklistview.js
@@ -94,13 +94,6 @@
        _lia: null,
 
        /**
-        * Node of the "add" toolbar used to add snaks to the reference. The 
node exists only while in
-        * edit mode.
-        * @type {jQuery}
-        */
-       $addToolbar: null,
-
-       /**
         * Whether the snaklistview is currently in edit mode.
         * @type {boolean}
         */
@@ -172,7 +165,10 @@
                } )
                .on( self._lia.prefixedEvent( 'stopediting' ),
                        function( event, dropValue, newSnak ) {
-                               if ( !self.isValid() && !dropValue && 
!self.__continueSnakviewStopEditing ) {
+                               if (
+                                       !self.isValid() && 
!this.isInitialValue()
+                                       && !dropValue && 
!self.__continueSnakviewStopEditing
+                               ) {
                                        event.preventDefault();
                                        return;
                                }
@@ -215,16 +211,6 @@
                                self._createRemoveToolbar( $( item ) );
                        } );
 
-                       // Append "add" toolbar to add new snaks:
-                       this.$addToolbar = mw.template( 'wb-toolbar', '' 
).appendTo( this.element );
-                       this.element.addtoolbar( {
-                               toolbarParentSelector: '> .wb-ui-toolbar',
-                               customAction: function( event, $parent ) {
-                                       self.enterNewItem();
-                               },
-                               eventPrefix: 
$.wikibase.referenceview.prototype.widgetEventPrefix
-                       } );
-
                        this.element.addClass( 'wb-edit' );
                        this._isInEditMode = true;
 
@@ -245,7 +231,8 @@
                initially: function( e, dropValue ) {
                        if(
                                !this.isInEditMode()
-                               || !this.isValid() && !dropValue && 
!this.__continueStopEditing
+                               || !this.isValid() && !this.isInitialValue()
+                               && !dropValue && !this.__continueStopEditing
                        ) {
                                e.cancel();
                        }
@@ -260,9 +247,6 @@
                        if ( dropValue ) {
                                // If the whole item was pending, remove the 
whole list item. This has to be
                                // performed in the widget using the 
snaklistview.
-
-                               this.element.data( 'addtoolbar' ).destroy();
-                               this.$addToolbar.remove();
 
                                self.__continueSnakviewStopEditing = false;
 
@@ -297,9 +281,6 @@
                                        } );
                                        self.__continueSnakviewStopEditing = 
false;
 
-                                       self.element.data( 'addtoolbar' 
).destroy();
-                                       self.$addToolbar.remove();
-
                                        self.enable();
 
                                        self.element.removeClass( 'wb-edit' );
@@ -321,9 +302,6 @@
                                        self.__continueStopEditing = false;
                                } );
                        } else {
-                               this.element.data( 'addtoolbar' ).destroy();
-                               this.$addToolbar.remove();
-
                                self.__continueStopEditing = false;
                                // Creating a new snaklistview is managed in 
the object using the snaklistview (e.g.
                                // in the statementview). Creating a 
snaklistview will end up in here after having
@@ -414,7 +392,6 @@
         */
        isValid: function() {
                var self = this,
-                       snakList = new wb.SnakList(),
                        isValid = true;
 
                if ( this._listview.items().length === 0 ) {
@@ -427,12 +404,33 @@
                        if ( !snakview.isValid() ) {
                                isValid = false;
                                return false;
-                       } else {
-                               snakList.addSnak( snakview.snak() );
                        }
                } );
 
-               return ( isValid && ( !this._snakList || 
!this._snakList.equals( snakList ) ) );
+               return isValid;
+       },
+
+       /**
+        * Returns whether the current snaks of the listview are the same than 
the ones the snaklistview
+        * got initialized with.
+        * @since 0.4
+        *
+        * @return {boolean}
+        */
+       isInitialValue: function() {
+               var self = this,
+                       snakList = new wb.SnakList();
+
+               if ( !this.isValid() || !this._snakList ) {
+                       return false;
+               }
+
+               $.each( this._listview.items(), function( i, item ) {
+                       var snakview = self._lia.liInstance( $( item ) );
+                       snakList.addSnak( snakview.snak() );
+               } );
+
+               return this._snakList.equals( snakList );
        },
 
        /**
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
index ee73cab..2265c6b 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.statementview.js
@@ -142,7 +142,10 @@
 
                                                        // Only handling saving 
a new reference here, so the reference needs to
                                                        // be valid. Canceling 
is handled in "afterstopediting".
-                                                       if ( 
!liInstance.isValid() && !dropValue ) {
+                                                       if (
+                                                               
!liInstance.isValid() && !liInstance.isInitialValue()
+                                                               && !dropValue
+                                                       ) {
                                                                
event.preventDefault();
                                                                return;
                                                        }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I1e9841e1683e6d503be1ffd654fd97f62b93f7aa
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Aude <[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