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 * @constructor
* @extends OO.ui.SearchWidget * @extends OO.ui.SearchWidget
* @param {Object} [config] Configuration options * @param {Object} [config] Configuration options
* @property {Object[]|null} index Null when the index needs to be rebuild
*/ */
ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( config ) { ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( config ) {
// Configuration initialization // Configuration initialization
@ -24,9 +25,8 @@ ve.ui.MWReferenceSearchWidget = function VeUiMWReferenceSearchWidget( config ) {
ve.ui.MWReferenceSearchWidget.super.call( this, config ); ve.ui.MWReferenceSearchWidget.super.call( this, config );
// Properties // Properties
this.index = []; this.index = null;
this.indexEmpty = true; this.indexEmpty = true;
this.built = false;
// Initialization // Initialization
this.$element.addClass( 've-ui-mwReferenceSearchWidget' ); this.$element.addClass( 've-ui-mwReferenceSearchWidget' );
@ -86,7 +86,7 @@ ve.ui.MWReferenceSearchWidget.prototype.onInternalListUpdate = function ( groups
if ( groupsChanged.some( function ( groupName ) { if ( groupsChanged.some( function ( groupName ) {
return groupName.indexOf( 'mwReference/' ) === 0; 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. * This will occur after changes to any InternalItemNode.
*/ */
ve.ui.MWReferenceSearchWidget.prototype.onListNodeUpdate = function () { 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 () { ve.ui.MWReferenceSearchWidget.prototype.buildIndex = function () {
if ( !this.built ) { this.onQueryChange();
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(); query = query.trim().toLowerCase();
const items = []; const items = [];
if ( !this.index ) {
this.index = this.buildSearchIndex();
}
for ( let i = 0; i < this.index.length; i++ ) { for ( let i = 0; i < this.index.length; i++ ) {
const item = this.index[ i ]; const item = this.index[ i ];
if ( item.text.indexOf( query ) >= 0 ) { 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 ) { QUnit.test( 'buildIndex', function ( assert ) {
const widget = new ve.ui.MWReferenceSearchWidget(); const widget = new ve.ui.MWReferenceSearchWidget();
widget.internalList = { getNodeGroups: () => ( {} ) }; widget.internalList = { getNodeGroups: () => ( {} ) };
assert.false( widget.built ); assert.strictEqual( widget.index, null );
widget.buildIndex(); widget.buildIndex();
assert.true( widget.built );
assert.deepEqual( widget.index, [] ); assert.deepEqual( widget.index, [] );
widget.onInternalListUpdate( [ 'mwReference/' ] ); widget.onInternalListUpdate( [ 'mwReference/' ] );
assert.false( widget.built ); assert.strictEqual( widget.index, null );
assert.deepEqual( widget.index, [] );
widget.buildIndex(); widget.buildIndex();
assert.true( widget.built );
assert.deepEqual( widget.index, [] ); assert.deepEqual( widget.index, [] );
widget.onListNodeUpdate(); widget.onListNodeUpdate();
assert.false( widget.built ); assert.strictEqual( widget.index, null );
assert.deepEqual( widget.index, [] );
} ); } );
QUnit.test( 'buildSearchIndex when empty', function ( assert ) { QUnit.test( 'buildSearchIndex when empty', function ( assert ) {