-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Song Zheng
committed
Feb 22, 2020
1 parent
77c34ed
commit 165e6ca
Showing
4 changed files
with
87 additions
and
84 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const initSearch = (noteMap) => { | ||
const searchBox = document.querySelector('#searchBox') | ||
const allNotes = document.querySelectorAll('.card-columns .card-title') | ||
|
||
let resultElements = [] | ||
const resultContainer = document.querySelector('#resultListContainer') | ||
searchBox.focus() | ||
const search = () => { | ||
const searchQuery = searchBox.value | ||
if (searchQuery.length < 2) return | ||
fetch(`/search/${searchQuery}`).then( r=> r.text() ).then(data => { | ||
// invalidate old requests | ||
if( searchBox.value !== searchQuery) return | ||
const dataArr = data.split('\n').filter( d => d ).map( d => { | ||
const result = d.substr(5) // get rid of data/ prefix | ||
const resultArr = result.split(':') | ||
const title = resultArr.shift() | ||
const match = resultArr.join(':') | ||
return { | ||
title, match | ||
} | ||
}) | ||
resultContainer.innerHTML = '' | ||
const newTitleMap = {...noteMap} | ||
resultElements = dataArr.map( (obj, idx) => { | ||
delete newTitleMap[obj.title.toLowerCase()] | ||
return new Result(obj.title, obj.match, idx) | ||
}) | ||
Object.keys(newTitleMap).forEach( title => { | ||
if (!title.includes(searchQuery.toLowerCase())) { | ||
return | ||
} | ||
resultElements.push( | ||
new Result(newTitleMap[title], '', resultElements.length) | ||
) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
function Result(name, value, idx) { | ||
const div = document.createElement('div') | ||
div.className = 'resultContainer' | ||
if (!idx) { | ||
div.className += ' selected' | ||
} | ||
div.innerHTML = ` | ||
<p> | ||
<span class="title"> | ||
${name}: | ||
</span> | ||
${value} | ||
</p> | ||
` | ||
div.addEventListener('click', () => { | ||
this.select() | ||
}) | ||
|
||
resultContainer.append(div) | ||
this.select = () => { | ||
window.location = `/notes/${name}` | ||
} | ||
} | ||
|
||
searchBox.addEventListener('keyup', (e) => { | ||
console.log(e.key) | ||
if (e.key === 'Enter') { | ||
return resultElements[0].select() | ||
} | ||
search() | ||
}) | ||
|
||
module.exports = initSearch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters