Skip to content

Commit

Permalink
fix search bug and add css
Browse files Browse the repository at this point in the history
  • Loading branch information
Song Zheng committed Feb 22, 2020
1 parent 9e360f9 commit e55c070
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
75 changes: 39 additions & 36 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
const initSearch = (noteMap) => {
const searchBox = document.querySelector('#searchBox')
const allNotes = document.querySelectorAll('.card-columns .card-title')
const resultContainer = document.querySelector('#resultListContainer')
const searchBox = document.querySelector('#searchBox')
const allNotes = document.querySelectorAll('.card-columns .card-title')
let resultElements = []
let noteMap = {}

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)
)
})
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) {
Expand Down Expand Up @@ -70,4 +68,9 @@ searchBox.addEventListener('keyup', (e) => {
search()
})

const initSearch = (titleMap) => {
searchBox.focus()
noteMap = titleMap
}

module.exports = initSearch
1 change: 1 addition & 0 deletions views/notes.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<%- include('./layout/header.ejs') %>
<link rel="stylesheet" href="/edit.css">
<div class="container" style="margin-top: 20px;">
<div class="d-flex justify-content-between">
<h1>
Expand Down

0 comments on commit e55c070

Please sign in to comment.