Skip to content

Commit

Permalink
Merge branch 'feature/LF2874/loading-indicator' into 'master'
Browse files Browse the repository at this point in the history
Feature/lf2874/loading indicator

See merge request lfor/autocomplete-lhc!161
  • Loading branch information
jcy1225 committed Mar 7, 2024
2 parents 58cc218 + 007c7c0 commit 93e9909
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
This log documents significant changes for each release. This project follows
[Semantic Versioning](http://semver.org/).

## [19.2.0] - 2024-03-07
### Added
- "showLoadingIndicator" option to show loading indicator on a Search autocomplete,
defaults to true.

## [19.1.1] - 2024-02-15
### Fixed
- An issue where the search list items are not clickable after switching out
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "autocomplete-lhc",
"version": "19.1.1",
"version": "19.2.0",
"main": [
"source/auto_completion.css",
"source/polyfill.js",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "autocomplete-lhc",
"version": "19.1.1",
"version": "19.2.0",
"description": "",
"main": "source/index.js",
"config": {
Expand Down
3 changes: 3 additions & 0 deletions source/autoCompBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -2834,6 +2834,9 @@ if (typeof Def === 'undefined')
} else {
this.active = false;
this.hide();
// Hide the loading indicator for Search autocomplete, if it is still shown due to
// previous query not finished yet.
this.progressElement?.classList.remove('show');
}
this.oldElementValue = this.domCache.get('elemVal');
}
Expand Down
45 changes: 44 additions & 1 deletion source/autoCompSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
*/
showListOnFocusIfEmpty_: false,

/**
* Whether to show a loading indicator when search is taking place.
*/
showLoadingIndicator_: true,


/**
* The constructor. (See Prototype's Class.create method.)
Expand Down Expand Up @@ -236,8 +241,10 @@
* <li>colHeaders - Used when tableFormat is true, this is an array of
* column headers for the columns in the table. If this is not supplied, no header
* row will be created.</li>
* <li>showListOnFocusIfEmpty - (default: false) Whether to show a list when the
* <li>showListOnFocusIfEmpty - (default: false) Whether to show a list when the
* empty field receives focus.</li>
* <li>showLoadingIndicator - (default: true) Whether to show a loading indicator
* during search.</li>
* <ul>Somewhat obsolete, but not yet deprecated, parameters:
* <li>buttonID - the ID of the button (if there is one) which activates
* a search. If you use this option, do not set matchListValue.</li>
Expand Down Expand Up @@ -298,6 +305,20 @@

this.showListOnFocusIfEmpty_ = options['showListOnFocusIfEmpty'] || false;

if (options['showLoadingIndicator'] === false)
this.showLoadingIndicator_ = false;

// If loading indicator is enabled, put the field into a span and add a <progress> element.
if (this.showLoadingIndicator_) {
var fieldParent = this.element.parentElement;
var fieldContainer = document.createElement('span');
fieldContainer.classList.add('loading-indicator-container');
fieldParent.replaceChild(fieldContainer, this.element);
fieldContainer.appendChild(this.element);
this.progressElement = document.createElement('progress');
fieldContainer.appendChild(this.progressElement);
}

// Do not use the synchronous request option. On Windows and Firefox,
// if you use synchronous, and hit control+enter to run a search, the
// Firefox Downloads window opens. I don't know why. See my post
Expand Down Expand Up @@ -335,6 +356,22 @@
},


/**
* Frees any references this autocompleter has to DOM objects.
* Overrides detachFromDOM() in autoCompBase.js.
*/
detachFromDOM: function() {
// Remove the containing element with class 'loading-indicator-container',
// if any.
var fieldParent = this.element.parentElement;
if (fieldParent.classList.contains('loading-indicator-container')) {
var originalParent = fieldParent.parentElement;
originalParent.replaceChild(this.element, fieldParent);
}
Def.Autocompleter.Search.superclass.detachFromDOM.apply(this);
},


/**
* Initializes the itemToDataIndex_ map.
*/
Expand Down Expand Up @@ -408,6 +445,8 @@
this.onComplete(results, true);
}
if (!results) { // i.e. if it wasn't cached
if (this.showLoadingIndicator_)
this.progressElement?.classList.add('show');
// Run the search
if (searchFn)
this.useSearchFn(searchStr, Def.Autocompleter.Search.EXPANDED_COUNT);
Expand Down Expand Up @@ -810,6 +849,8 @@
if (this.lastAjaxRequest_ === resultData) {
this.lastAjaxRequest_ = null;
}
if (this.showLoadingIndicator_)
this.progressElement?.classList.remove('show');
const usedSearchFn = !!resultData.results;
if (resultData.status === 200 || usedSearchFn) { // 200 is the "OK" status
if (usedSearchFn) {
Expand Down Expand Up @@ -1127,6 +1168,8 @@
this.onComplete(results, true);
}
if (!results) {
if (this.showLoadingIndicator_)
this.progressElement?.classList.add('show');
if (this.search)
this.useSearchFn(fieldVal, Def.Autocompleter.Base.MAX_ITEMS_BELOW_FIELD);
else
Expand Down
19 changes: 19 additions & 0 deletions source/auto_completion.css
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@
}


.loading-indicator-container {
position: relative;
display: inline-block;
}

.loading-indicator-container progress {
display: none;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 5px;
}

.loading-indicator-container progress.show {
display: inline-block;
}


/* Class "auto_complete" is the completion options div */
.auto_complete {
width: auto;
Expand Down
48 changes: 48 additions & 0 deletions test/cypress/integration/searchList.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,52 @@ describe('search lists', function() {
po.getAjaxAbortCount().should('equal', 2);
});
});

describe('loading indicator', function() {
beforeEach(function() {
po.openTestPage();
cy.window().then(function(win) {
win.Def.jqueryLite.ajaxFactory(2000);
});
});

it('should disable loading indicator', function() {
cy.get(po.searchCNESel).click().type('ar');
// A shorter timeout than the ajax delay to make sure the <progress> element never showed.
cy.get(po.searchCNESel + ' + progress', {timeout: 300}).should('not.exist');
});

it('should show loading indicator', function() {
cy.get(po.alleleSearch).click().type('rs');
cy.get(po.alleleSearch + ' + progress').should('have.class', 'show');
cy.get(po.alleleSearch + ' + progress').should('not.have.class', 'show');
// multi-select search field
cy.get(po.multiSearchCWE).click().type('rs');
cy.get(po.multiSearchCWE + ' + progress').should('have.class', 'show');
cy.get(po.multiSearchCWE + ' + progress').should('not.have.class', 'show');
// field with a search button
cy.get('#fe_search_button_cne').click().type('ab');
cy.get('#fe_search_button_cne_button').click();
cy.get('#fe_search_button_cne + progress').should('have.class', 'show');
cy.get('#fe_search_button_cne + progress').should('not.have.class', 'show');
});

it('should hide loading indicator when the field is cleared', function() {
cy.get(po.alleleSearch).clear().type('a');
cy.get(po.alleleSearch + ' + progress', {timeout: 300}).should('have.class', 'show');
cy.get(po.alleleSearch).clear();
// A shorter timeout than the ajax delay to make sure the <progress> element goes away before the ajax request finishes.
cy.get(po.alleleSearch + ' + progress', {timeout: 300}).should('not.have.class', 'show');
});

it('should remove wrapper element when the destroyed', function() {
cy.get('.loading-indicator-container ' + po.alleleSearch).should('exist');
cy.get('#dest_allele_search').click();
cy.get('.loading-indicator-container ' + po.alleleSearch).should('not.exist');
// multi-select search field
cy.get('.loading-indicator-container ' + po.multiSearchCWE).should('exist');
cy.get('#dest_multi_sel_search_cwe').click();
cy.get('.loading-indicator-container ' + po.multiSearchCWE).should('not.exist');
});
});
});
2 changes: 2 additions & 0 deletions test/pages/autocomp_atr.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
placeholder="Type rs">
</label>
</div>
<button id="dest_allele_search" type="button">Destroy autocompleter</button>

<div class="field">
A test for case-senstive matching with search lists
Expand Down Expand Up @@ -180,6 +181,7 @@ <h2>Multi-select lists</h2>
A Search CWE multi-select autocompleter:&nbsp;&nbsp;</label>
<input autocomplete="off" id="multi_sel_search_cwe" type="text" />
</div>
<button id="dest_multi_sel_search_cwe" type="button">Destroy autocompleter</button>

<div class="field" id="tableFormatMultiSearchCWESection">
<label for="table_format_multi_sel_search_cwe" id="table_format_multi_sel_search_cwe_lbl">
Expand Down
7 changes: 7 additions & 0 deletions test/pages/autocompleter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var fe_prefetch_cwe_autoComp = new Def.Autocompleter.Prefetch('prefetch_cwe',
var opts = {};
opts['matchListValue']=true
opts['autocomp']=true
opts['showLoadingIndicator']=false
new Def.Autocompleter.Search('fe_search_cne',
'/form/get_search_res_list?fd_id=1284', opts);

Expand Down Expand Up @@ -208,8 +209,11 @@ opts = {
'suggestionMode': Def.Autocompleter.USE_STATISTICS,
'autocomp': true
};
var fe_multi_sel_search_cwe_autoComp =
new Def.Autocompleter.Search('multi_sel_search_cwe',
'/form/get_search_res_list?fd_id=2163', opts);
document.querySelector('#dest_multi_sel_search_cwe').addEventListener('click', (event)=>{
fe_multi_sel_search_cwe_autoComp.destroy()});


// Long prefetched list autocompleter with an odd number of items (for checking
Expand Down Expand Up @@ -299,8 +303,11 @@ new Def.Autocompleter.Prefetch('item_num_match_test', longList, opts);

// A list to test for correct sorting of results.
var opts = {valueCols: [0], tableFormat: true};
var fe_allele_search_autoComp =
new Def.Autocompleter.Search('allele_search',
'/form/get_search_res_list?fd_id=alleles', opts);
document.querySelector('#dest_allele_search').addEventListener('click', (event)=>{
fe_allele_search_autoComp.destroy()});

// A search list to test for case-sensitive matching
new Def.Autocompleter.Search('cs_match_search',
Expand Down

0 comments on commit 93e9909

Please sign in to comment.