From 301ce0e2add8c848f7abbce652d31a6383544332 Mon Sep 17 00:00:00 2001 From: chuanlin2018 Date: Wed, 1 Jun 2022 14:37:50 -0400 Subject: [PATCH] Fixed keyword filter issue. --- .../app/landing/filters/filters.component.ts | 74 +++++++++++++------ .../resultlist/resultlist.component.ts | 10 ++- .../src/app/landing/topic/topic.component.ts | 2 +- 3 files changed, 59 insertions(+), 27 deletions(-) diff --git a/angular/src/app/landing/filters/filters.component.ts b/angular/src/app/landing/filters/filters.component.ts index 3b91c2fed..557976d2a 100644 --- a/angular/src/app/landing/filters/filters.component.ts +++ b/angular/src/app/landing/filters/filters.component.ts @@ -114,7 +114,7 @@ export class FiltersComponent implements OnInit { forensicsNodeExpanded: boolean = true; comheight: string = '50px'; // parent div height comwidth: string; // parent div width - maxDropdownLabelLength: number = 30; + dropdownLabelLengthLimit: number = 30; filterStyle = {'width':'100%', 'background-color': '#FFFFFF','font-weight': '400','font-style': 'italic'}; @@ -580,42 +580,68 @@ export class FiltersComponent implements OnInit { * suggestedKeywordsLkup - stores the real ketwords * @param event - search query that user typed into the keyword filter box */ - updateSuggestedKeywords(event: any) { + updateSuggestedKeywords(event: any) { let keyword = event.query; - let keywordAbbr: string; this.suggestedKeywords = []; this.suggestedKeywordsLkup = {}; + // Handle current keyword: update suggested keywords and lookup for (let i = 0; i < this.keywords.length; i++) { let keyw = this.keywords[i].trim(); if (keyw.toLowerCase().indexOf(keyword.toLowerCase()) >= 0) { - //If the keyword length is greater than the maximum length, we want to truncate - //it so that the length is close the maximum length. - if(keyw.length > this.maxDropdownLabelLength){ - let wordCount = 1; - while(keyw.split(' ', wordCount).join(' ').length < this.maxDropdownLabelLength) { - wordCount++; - } - - keywordAbbr = keyw.substring(0, keyw.split(' ', wordCount).join(' ').length); - if(keywordAbbr.trim().length < keyw.length) keywordAbbr = keywordAbbr + "..."; - }else - keywordAbbr = keyw; + this.suggestedKeywords.push(this.shortenKeyword(keyw)); + this.suggestedKeywordsLkup[this.shortenKeyword(keyw)] = keyw; + } + } - let i = 1; - let tmpKeyword = keywordAbbr; - while(Object.keys(this.suggestedKeywordsLkup).indexOf(tmpKeyword) >= 0 && i < 100){ - tmpKeyword = keywordAbbr + "(" + i + ")"; - i++; + // Handle selected keyword: update suggested keywords lookup. Lookup array must cover all selected keywords. + this.selectedKeywords.forEach(kw => { + for (let i = 0; i < this.keywords.length; i++) { + let keyw = this.keywords[i].trim(); + if (keyw.toLowerCase().indexOf(kw.toLowerCase()) >= 0) { + this.suggestedKeywordsLkup[this.shortenKeyword(keyw)] = keyw; } - keywordAbbr = tmpKeyword; + } + }) + + this.suggestedKeywords = this.sortAlphabetically(this.suggestedKeywords); + } - this.suggestedKeywords.push(keywordAbbr); - this.suggestedKeywordsLkup[keywordAbbr] = keyw; + /** + * Some keywords are very long. They cause problem when display both in suggested keyword list or + * selected keyword list. This function returns the first few words of the input keyword. The length + * of the return string is based on this.dropdownLabelLengthLimit but not exactly. + * If the length of the input keyword is less than dropdownLabelLengthLimit, the input keyword will be returned. + * Otherwise, It selects the first few words whose total length is just exceed the length limit followed by "...". + * @param keyword + * @returns Keyword abbreviate + */ + shortenKeyword(keyword: string) { + let keywordAbbr: string; + + //If the keyword length is greater than the maximum length, we want to truncate + //it so that the length is close the maximum length. + + if(keyword.length > this.dropdownLabelLengthLimit){ + let wordCount = 1; + while(keyword.split(' ', wordCount).join(' ').length < this.dropdownLabelLengthLimit) { + wordCount++; } + + keywordAbbr = keyword.substring(0, keyword.split(' ', wordCount).join(' ').length); + if(keywordAbbr.trim().length < keyword.length) keywordAbbr = keywordAbbr + "..."; + }else + keywordAbbr = keyword; + + let i = 1; + let tmpKeyword = keywordAbbr; + while(Object.keys(this.suggestedKeywordsLkup).indexOf(tmpKeyword) >= 0 && i < 100){ + tmpKeyword = keywordAbbr + "(" + i + ")"; + i++; } + keywordAbbr = tmpKeyword; - this.suggestedKeywords = this.sortAlphabetically(this.suggestedKeywords); + return keywordAbbr; } /** diff --git a/angular/src/app/landing/resultlist/resultlist.component.ts b/angular/src/app/landing/resultlist/resultlist.component.ts index 467ed2ded..e0f9c1465 100644 --- a/angular/src/app/landing/resultlist/resultlist.component.ts +++ b/angular/src/app/landing/resultlist/resultlist.component.ts @@ -419,13 +419,19 @@ export class ResultlistComponent implements OnInit { break; case "keyword": + // Loop through each keyword in each search result. Display those that match + // the keywords from keyword filter this.searchResults.forEach((object) => { if(object.active == true){ object.active = false; object["keyword"].forEach((keyword) => { - if(keyword.includes(filter.split("=")[1])) - object.active = true; + //Loop through each search keyword from keyword filter + filter.split("=")[1].split(",").forEach(kw => { + if(keyword.includes(kw)){ + object.active = true; + } + }) }) } }); diff --git a/angular/src/app/landing/topic/topic.component.ts b/angular/src/app/landing/topic/topic.component.ts index 0ab4a8cfb..ee5971ce2 100644 --- a/angular/src/app/landing/topic/topic.component.ts +++ b/angular/src/app/landing/topic/topic.component.ts @@ -50,7 +50,7 @@ export class TopicComponent implements OnInit { ngOnInit() { if(this.record) { this.recordType = (new NERDResource(this.record)).resourceLabel(); - console.log('this.recordType', this.recordType); + if(this.recordType == "Science Theme") { this.fieldName = "topic"; this.record['topic'].forEach(topic => {