Merge "Further delay search index build in ReferenceSearchWidget"

This commit is contained in:
jenkins-bot 2024-04-30 10:49:17 +00:00 committed by Gerrit Code Review
commit 29e8ba9ea0
2 changed files with 13 additions and 18 deletions

View file

@ -13,6 +13,7 @@
* @constructor
* @extends OO.ui.SearchWidget
* @param {Object} [config] Configuration options
* @property {Object[]|null} index Null when the index needs to be rebuild
*/
ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( config ) {
// Configuration initialization
@ -24,9 +25,8 @@ ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( config ) {
ve.ui.MWReferenceSearchWidget.super.call( this, config );
// Properties
this.index = [];
this.index = null;
this.indexEmpty = true;
this.built = false;
// Initialization
this.$element.addClass( 've-ui-mwReferenceSearchWidget' );
@ -86,7 +86,7 @@ ve.ui.MWReferenceSearchWidget.prototype.onInternalListUpdate = function ( groups
if ( groupsChanged.some( function ( groupName ) {
return groupName.indexOf( 'mwReference/' ) === 0;
} ) ) {
this.built = false;
this.index = null;
}
};
@ -96,19 +96,14 @@ ve.ui.MWReferenceSearchWidget.prototype.onInternalListUpdate = function ( groups
* This will occur after changes to any InternalItemNode.
*/
ve.ui.MWReferenceSearchWidget.prototype.onListNodeUpdate = function () {
this.built = false;
this.index = null;
};
/**
* Build a searchable index of references.
* Manually re-populates the list of search results after {@see setInternalList} was called.
*/
ve.ui.MWReferenceSearchWidget.prototype.buildIndex = function () {
if ( !this.built ) {
this.index = this.buildSearchIndex();
this.built = true;
// Re-populate
this.onQueryChange();
}
};
/**
@ -200,6 +195,10 @@ ve.ui.MWReferenceSearchWidget.prototype.buildSearchResults = function ( query )
query = query.trim().toLowerCase();
const items = [];
if ( !this.index ) {
this.index = this.buildSearchIndex();
}
for ( let i = 0; i < this.index.length; i++ ) {
const item = this.index[ i ];
if ( item.text.indexOf( query ) >= 0 ) {

View file

@ -5,23 +5,19 @@ QUnit.module( 've.ui.MWReferenceSearchWidget (Cite)', ve.test.utils.newMwEnviron
QUnit.test( 'buildIndex', function ( assert ) {
const widget = new ve.ui.MWReferenceSearchWidget();
widget.internalList = { getNodeGroups: () => ( {} ) };
assert.false( widget.built );
assert.strictEqual( widget.index, null );
widget.buildIndex();
assert.true( widget.built );
assert.deepEqual( widget.index, [] );
widget.onInternalListUpdate( [ 'mwReference/' ] );
assert.false( widget.built );
assert.deepEqual( widget.index, [] );
assert.strictEqual( widget.index, null );
widget.buildIndex();
assert.true( widget.built );
assert.deepEqual( widget.index, [] );
widget.onListNodeUpdate();
assert.false( widget.built );
assert.deepEqual( widget.index, [] );
assert.strictEqual( widget.index, null );
} );
QUnit.test( 'buildSearchIndex when empty', function ( assert ) {