From 6b237dfbbb2253b296bee52315618c649796fa08 Mon Sep 17 00:00:00 2001 From: dtookey Date: Wed, 20 Jan 2021 13:11:46 -0500 Subject: [PATCH] search will correctly match files based on keyword --- src/ts_source/fileutils.ts | 29 ++++++++++++----------------- src/ts_source/search.ts | 20 +++++++++++++------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/ts_source/fileutils.ts b/src/ts_source/fileutils.ts index be7fafb..af42c14 100644 --- a/src/ts_source/fileutils.ts +++ b/src/ts_source/fileutils.ts @@ -114,27 +114,24 @@ export class DocumentDirectory { private static walkCards(d: DirectoryNode): Map { let cardsByCategory = new Map(); - for (let i = 0, l = d.children.length; i < l; i++) { - let child = d.children[i]; + for (let child of d.children) { if (child instanceof DirectoryNode) { let dir = child as DirectoryNode; if (dir.containsDirectory()) { let childDirectories = dir.getDirectories(); - for (let j = 0, k = childDirectories.length; j < k; j++) { - let dir = childDirectories[j]; + for (let dir of childDirectories) { let subCards = DocumentDirectory.walkCards(dir); this.mergeMaps(cardsByCategory, subCards); } } let sCards = dir.getDocuments(); let category = dir.getCategory(); - for (let sCard in sCards) { - let scrd = sCards[sCard]; + for (let sCard of sCards) { let cards = cardsByCategory.get(category); if (cards === undefined || cards === null) { cards = []; } - cards.push(scrd.toCard()); + cards.push(sCard.toCard()); cardsByCategory.set(category, cards); } } else { @@ -153,8 +150,7 @@ export class DocumentDirectory { private static mergeMaps(a: Map, b: Map) { let keys = Object.keys(b); - for (let z in keys) { - let key = keys[z]; + for (let key of keys) { let sa = a.get(key) if (sa === undefined || sa === null) { sa = []; @@ -213,12 +209,12 @@ export class FileNode { "imagePath": imageName, "urlText": this.filePath, "altText": "", - "fileCard": true + "fileCard": true, + "open": this.open } let altKeys = Object.keys(altProps); - for (let kidx in altKeys) { - let key = altKeys[kidx]; + for (let key of altKeys) { cardObj[key] = altProps[key] } @@ -251,9 +247,9 @@ export class DirectoryNode extends FileNode { let stats = fs.lstatSync(filePath); if (stats.isDirectory()) { let contents = fs.readdirSync(filePath); - for (let i = 0, l = contents.length; i < l; i++) { - if (path.extname(contents[i]) === ".json") continue; - let childPath = path.join(filePath, contents[i]); + for (let fileName of contents) { + if (path.extname(fileName) === ".json") continue; + let childPath = path.join(filePath, fileName); let childStats = fs.lstatSync(childPath); if (childStats.isDirectory()) { this.children.push(new DirectoryNode(childPath, this)); @@ -282,8 +278,7 @@ export class DirectoryNode extends FileNode { } containsDirectory(): Boolean { - for (let i = 0, l = this.children.length; i < l; i++) { - let child = this.children[i]; + for (let child of this.children) { if (child instanceof DirectoryNode) return true; } return false diff --git a/src/ts_source/search.ts b/src/ts_source/search.ts index e19d676..d651f05 100644 --- a/src/ts_source/search.ts +++ b/src/ts_source/search.ts @@ -14,7 +14,7 @@ function register() { search(term); } - }else{ + } else { let term = searchBar.val().toString(); if (!term || 0 === term.length) { reset() @@ -23,7 +23,7 @@ function register() { }); - searchButton.on("click", ()=>{ + searchButton.on("click", () => { let term = searchBar.val().toString(); if (!term || 0 === term.length) { reset() @@ -33,7 +33,7 @@ function register() { }) } -function reset(){ +function reset() { buildUiFromConfig("resources-landing-page.json"); } @@ -47,7 +47,7 @@ function search(term: string) { expandAllContainers(); } -function copyMissingKeys(webCards: object, fileCards: Map){ +function copyMissingKeys(webCards: object, fileCards: Map) { let elementConfig = Configurator.loadAppConfig("resources-landing-page.json"); let containerKeys = Object.keys(elementConfig); for (let containerKey of containerKeys) { @@ -127,8 +127,14 @@ function addToFileMap(map: Map, categoryKey: string, card) { function cardContainsTerm(term: string, card: object): boolean { let titleContains = card["title"].toLowerCase().includes(term.toLowerCase()); let descriptionContains = card["description"].toLowerCase().includes(term.toLowerCase()); + + let keywordContains = false; + if (card["keywords"]) { + keywordContains = card["keywords"].toLowerCase().includes(term.toLowerCase()); + } + //todo: add keywords - return titleContains || descriptionContains; + return titleContains || descriptionContains || keywordContains; } function highlightCardTerm(term: string, card: object): object { @@ -143,9 +149,9 @@ function escapeRegExp(term: string): string { return term.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string } -function expandAllContainers(){ +function expandAllContainers() { let containers = $('[id$="collapser"]') - for(let container of containers){ + for (let container of containers) { container.click() }