Henning Snater has uploaded a new change for review. https://gerrit.wikimedia.org/r/190456
Change subject: referenceview: Removed internal caching of initial values ...................................................................... referenceview: Removed internal caching of initial values Change-Id: Ifb546b27f1d29baad5b1cbe92db24428c5a05cd2 --- M lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js 1 file changed, 41 insertions(+), 78 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/56/190456/1 diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js index ccdbdb8..fc628c3 100644 --- a/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js +++ b/lib/resources/jquery.wikibase/jquery.wikibase.referenceview.js @@ -86,24 +86,6 @@ }, /** - * `Reference` object represented by this widget. - * @property {wikibase.datamodel.Reference} - * @private - */ - _reference: null, - - /** - * Caches the `SnakList` of the `Reference` `Snak`s the `referenceview` has been initialized - * with. The `Snak`s are split into groups featuring the same `Property`. Removing one of those - * groups results in losing the reference to those `Snak`s. Therefore, `_initialSnakList` is - * used to rebuild the list of `Snak`s when cancelling and is used to query whether the `Snak`s - * represent the initial state. - * @property {wikibase.datamodel.SnakList} - * @private - */ - _initialSnakList: null, - - /** * Whether the widget is currently in edit mode. * @property {boolean} [_isInEditMode=false] * @private @@ -128,19 +110,6 @@ var self = this; - if ( this.option( 'value' ) ) { - this._reference = this.option( 'value' ); - // Overwrite the value since listItemAdapter is the snaklistview prototype which - // requires a wb.datamodel.SnakList object for initialization: - // FIXME: Do not overwrite the value - this._initialSnakList = this._reference.getSnaks(); - this.options.value = this._initialSnakList.getGroupedSnakLists(); - } - - if( !this._initialSnakList ) { - this._initialSnakList = new wb.datamodel.SnakList(); - } - this.$listview.listview( { listItemAdapter: new $.wikibase.listview.ListItemAdapter( { listItemWidget: $.wikibase.snaklistview, @@ -154,7 +123,7 @@ }; } } ), - value: this.option( 'value' ) + value: this.options.value ? this.options.value.getSnaks().getGroupedSnakLists() : [] } ); this._updateReferenceHashClass( this.value() ); @@ -239,19 +208,12 @@ * Sets the `Reference` to be represented by the view or returns the `Reference` currently * represented by the widget. * - * @param {wikibase.datamodel.Reference} [reference] + * @param {wikibase.datamodel.Reference|null} [reference] * @return {wikibase.datamodel.Reference|null|undefined} - * - * @throws {Error} when trying to set the value to something different that a - * `wikibase.datamodel.Reference` object. */ value: function( reference ) { if( reference ) { - if( !( reference instanceof wb.datamodel.Reference ) ) { - throw new Error( 'Value has to be an instance of wikibase.datamodel.Reference' ); - } - this._reference = reference; - return; + this.option( 'value', reference ); } var snakList = new wb.datamodel.SnakList(); @@ -263,10 +225,10 @@ } } ); - if( this._reference || snakList.length ) { + if( this.options.value || snakList.length ) { return new wb.datamodel.Reference( snakList, - this._reference ? this._reference.getHash() : undefined + this.options.value ? this.options.value.getHash() : undefined ); } @@ -321,7 +283,9 @@ this._trigger( 'afterstopediting', null, [ dropValue ] ); } else { this._saveReferenceApiCall() - .done( function( savedObject ) { + .done( function( savedReference ) { + self.options.value = savedReference; + self._stopEditingReferenceSnaks( dropValue ); self.enable(); @@ -354,41 +318,24 @@ * @param {boolean} dropValue */ _stopEditingReferenceSnaks: function( dropValue ) { - var listview = this.$listview.data( 'listview' ), - lia = listview.listItemAdapter(), - $snaklistviews = listview.items(), - i; + var listview = this.$listview.data( 'listview' ); - if( !dropValue ) { - // When saving the qualifier snaks, reset the initial qualifiers to the new ones. - this._initialSnakList = new wb.datamodel.SnakList(); - } + $.each( listview.value(), function() { + this.stopEditing( dropValue ); - if( $snaklistviews.length ) { - for( i = 0; i < $snaklistviews.length; i++ ) { - var snaklistview = lia.liInstance( $snaklistviews.eq( i ) ); - snaklistview.stopEditing( dropValue ); - - if( dropValue && !snaklistview.value() ) { - // Remove snaklistview from referenceview if no snakviews are left in - // that snaklistview: - listview.removeItem( snaklistview.element ); - } else if ( !dropValue ) { - // Gather all the current snaks in a single SnakList to set to reset the - // initial qualifiers: - this._initialSnakList.merge( snaklistview.value() ); - } + if( dropValue && !this.value() ) { + // Remove snaklistview from referenceview if no snakviews are left in that + // snaklistview: + listview.removeItem( this.element ); } - } + } ); this.clear(); - var snakLists = this._initialSnakList.getGroupedSnakLists(); - - if( snakLists ) { - for( i = 0; i < snakLists.length; i++ ) { - listview.addItem( snakLists[i] ); - } + if( this.options.value ) { + this.options.value.getSnaks().getGroupedSnakLists().each( function() { + listview.addItem( this ); + } ); } }, @@ -444,7 +391,9 @@ currentSnakList = currentReference ? currentReference.getSnaks() : new wb.datamodel.SnakList(); - return currentSnakList.equals( this._initialSnakList ); + return currentSnakList.equals( + this.options.value ? this.options.value.getSnaks() : new wb.datamodel.SnakList() + ); }, /** @@ -476,17 +425,21 @@ /** * Triggers the API call to save the reference. + * @see wikibase.entityChangers.ReferencesChanger.setReference * @private * - * @return {jQuery.promise} + * @return {Object} jQuery.Promise + * @return {Function} return.done + * @return {Reference} return.done.savedReference + * @return {Function} return.fail + * @return {wikibase.api.RepoApiError} error */ _saveReferenceApiCall: function() { var self = this, - guid = this.option( 'statementGuid' ); + guid = this.options.statementGuid; - return this.option( 'referencesChanger' ).setReference( guid, this.value() ) + return this.options.referencesChanger.setReference( guid, this.value() ) .done( function( savedReference ) { - self._reference = savedReference; self._updateReferenceHashClass( savedReference ); } ); }, @@ -509,8 +462,18 @@ /** * @inheritdoc * @protected + * + * @throws {Error} when trying to set the value to something different that a + * `wikibase.datamodel.Reference` object. */ _setOption: function( key, value ) { + if( key === 'value' ) { + if( !( value instanceof wb.datamodel.Reference ) ) { + throw new Error( 'Value has to be an instance of wikibase.datamodel.Reference' ); + } + // TODO: Redraw + } + var response = PARENT.prototype._setOption.apply( this, arguments ); if( key === 'disabled' ) { -- To view, visit https://gerrit.wikimedia.org/r/190456 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ifb546b27f1d29baad5b1cbe92db24428c5a05cd2 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