jenkins-bot has submitted this change and it was merged. Change subject: Simplify statementview and add invariant checks ......................................................................
Simplify statementview and add invariant checks Change-Id: I3f6b76c3bcb24ce2efe64c4e02ef82aca5c1407e --- M view/resources/jquery/wikibase/jquery.wikibase.statementview.js 1 file changed, 52 insertions(+), 84 deletions(-) Approvals: Jonas Kress (WMDE): Looks good to me, approved Thiemo Mättig (WMDE): Looks good to me, but someone else must approve JanZerebecki: Looks good to me, approved jenkins-bot: Verified diff --git a/view/resources/jquery/wikibase/jquery.wikibase.statementview.js b/view/resources/jquery/wikibase/jquery.wikibase.statementview.js index 4d24286..bea6ea1 100644 --- a/view/resources/jquery/wikibase/jquery.wikibase.statementview.js +++ b/view/resources/jquery/wikibase/jquery.wikibase.statementview.js @@ -481,36 +481,22 @@ * @return {boolean} */ isInitialValue: function() { - var i; + if ( !this.isInEditMode() ) { + mw.log.warn( 'statementview::isInitialValue should only be called in edit mode' ); + return true; + } if ( this.options.value ) { - if ( this._rankSelector && !this._rankSelector.isInitialValue() ) { + if ( !this._rankSelector.isInitialValue() ) { return false; } - var snaklistviews = this._qualifiers ? this._qualifiers.value() : [], - qualifiers = new wb.datamodel.SnakList(); - - // Generate a SnakList object featuring all current qualifier snaks to be able to - // compare it to the SnakList object the claimview has been initialized with: - for ( i = 0; i < snaklistviews.length; i++ ) { - qualifiers.merge( snaklistviews[i].value() ); - } - + var qualifiers = this._getQualifiers(); if ( !qualifiers.equals( this.options.value.getClaim().getQualifiers() ) ) { return false; } - var referenceviews = this._referencesListview ? this._referencesListview.value() : [], - references = new wb.datamodel.ReferenceList(); - - for ( i = 0; i < referenceviews.length; i++ ) { - var reference = referenceviews[i].value(); - if ( reference ) { - references.addItem( reference ); - } - } - + var references = this._getReferences(); if ( !references.equals( this.options.value.getReferences() ) ) { return false; } @@ -528,26 +514,23 @@ * @return {wikibase.datamodel.Statement|null} */ _instantiateStatement: function( guid ) { + if ( !this.isInEditMode() ) { + mw.log.warn( 'statementview::_instantiateStatement should only be called in edit mode' ); + return null; + } + var mainSnak = this._mainSnakSnakView.snak(); if ( !mainSnak ) { return null; } - var qualifiers = new wb.datamodel.SnakList(), - snaklistviews = this._qualifiers ? this._qualifiers.value() : []; - - // Combine qualifiers grouped by property to a single SnakList: - for ( var i = 0; i < snaklistviews.length; i++ ) { - qualifiers.merge( snaklistviews[i].value() ); - } + var qualifiers = this._getQualifiers(); return new wb.datamodel.Statement( new wb.datamodel.Claim( mainSnak, qualifiers, guid ), - new wb.datamodel.ReferenceList( this._getReferences() ), - this._rankSelector ? this._rankSelector.value() : ( this.options.value - ? this.options.value.getRank() - : wb.datamodel.Statement.RANK.NORMAL ) + this._getReferences(), + this._rankSelector.value() ); }, @@ -563,32 +546,35 @@ }, /** + * @private + * + * @return {wikibase.datamodel.SnakList} + */ + _getQualifiers: function() { + var qualifiers = new wb.datamodel.SnakList(), + snaklistviews = this._qualifiers.value(); + + // Combine qualifiers grouped by property to a single SnakList: + for ( var i = 0; i < snaklistviews.length; i++ ) { + qualifiers.merge( snaklistviews[i].value() ); + } + + return qualifiers; + }, + + /** * Returns all `Reference`s currently specified in the view (including all pending changes). * * @private * - * @return {wikibase.datamodel.Reference[]} + * @return {wikibase.datamodel.ReferenceList} */ _getReferences: function() { - var references = []; - - // If the statement is pending (not yet stored), the listview widget for the references is - // not defined. - if ( !this._referencesListview ) { - return references; - } - - var lia = this._referencesListview.listItemAdapter(); - - $.each( this._referencesListview.items(), function( i, item ) { - var referenceview = lia.liInstance( $( item ) ), - reference = referenceview ? referenceview.value() : null; - if ( reference ) { - references.push( reference ); - } - } ); - - return references; + return new wb.datamodel.ReferenceList( + $.map( this._referencesListview.value(), function( referenceview ) { + return referenceview.value(); + } ) + ); }, /** @@ -772,50 +758,32 @@ * @return {boolean} */ isValid: function() { - var snaklistviews, - i; - - if ( this._mainSnakSnakView && !this._mainSnakSnakView.isValid() ) { - return false; + if ( !this.isInEditMode() ) { + mw.log.warn( 'statementview::isValid should only be called in edit mode' ); + return true; } - if ( this._hasInvalidReferences() ) { - return false; - } - - if ( this._qualifiers ) { - snaklistviews = this._qualifiers.value(); - - if ( snaklistviews.length ) { - for ( i = 0; i < snaklistviews.length; i++ ) { - if ( !snaklistviews[i].isValid() ) { - return false; - } - } - } - } - - return this._instantiateStatement( null ) instanceof wb.datamodel.Statement; + return this._mainSnakSnakView.isValid() && + this._listViewIsValid( this._qualifiers ) && + this._listViewIsValid( this._referencesListview ) && + this._instantiateStatement( null ) instanceof wb.datamodel.Statement; }, /** + * @param {jQuery.wikibase.listview} listview * @return {boolean} */ - _hasInvalidReferences: function() { - var isInvalid = false; + _listViewIsValid: function( listview ) { + var isValid = true; - if ( !this._referencesListview ) { - return isInvalid; - } - - $.each( this._referencesListview.value(), function ( key, referenceView ) { - if ( !referenceView.isValid() ) { - isInvalid = true; + $.each( listview.value(), function ( key, view ) { + if ( !view.isValid() ) { + isValid = false; return false; } } ); - return isInvalid; + return isValid; }, /** -- To view, visit https://gerrit.wikimedia.org/r/269965 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I3f6b76c3bcb24ce2efe64c4e02ef82aca5c1407e Gerrit-PatchSet: 8 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Adrian Heine <adrian.l...@wikimedia.de> Gerrit-Reviewer: Adrian Heine <adrian.l...@wikimedia.de> Gerrit-Reviewer: JanZerebecki <jan.wikime...@zerebecki.de> Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits