Skip to content

Commit

Permalink
Merge pull request #12 from songz/s4
Browse files Browse the repository at this point in the history
refactor search into webpack
  • Loading branch information
songz authored Feb 22, 2020
2 parents 0ccc811 + e55c070 commit d059fad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
22 changes: 14 additions & 8 deletions src/home.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/* globals clientData */
const moment = require('moment')
const initSearch = require('./search')

const noteMap = {}

const contentStr = clientData.reduce((acc, note, idx) => {
const title = note.name.toLowerCase()
noteMap[title] = note.name
return acc + `
<div class="card">
<div class='card-body'>
Expand All @@ -27,13 +32,14 @@ Modified ${moment(note.mtimeMs).fromNow()}
module.exports = () => {
const containerElement = document.querySelector('#mainAppElement')
containerElement.innerHTML = `
<div class="row">
<div class="col-12">
<div class="card-columns">
${contentStr}
</div>
<hr>
<div class="row">
<div class="col-12">
<div class="card-columns">
${contentStr}
</div>
<hr>
</div>
</div>
</div>
`
`
initSearch(noteMap)
}
17 changes: 9 additions & 8 deletions public/search.js → src/search.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
const resultContainer = document.querySelector('#resultListContainer')
const searchBox = document.querySelector('#searchBox')
const allNotes = document.querySelectorAll('.card-columns .card-title')
const noteMap = Array.from(allNotes).reduce((acc, e) => {
const title = e.innerText.toLowerCase()
acc[title] = e.innerText
return acc
}, {})

let resultElements = []
const resultContainer = document.querySelector('#resultListContainer')
searchBox.focus()
let noteMap = {}

const search = () => {
const searchQuery = searchBox.value
if (searchQuery.length < 2) return
Expand Down Expand Up @@ -73,3 +68,9 @@ searchBox.addEventListener('keyup', (e) => {
search()
})

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

module.exports = initSearch
2 changes: 1 addition & 1 deletion 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 Expand Up @@ -27,5 +28,4 @@ console.log(clientData)
</script>
<script src="/main.js"></script>

<script src="/search.js"></script>
<%- include('./layout/footer.ejs') %>

0 comments on commit d059fad

Please sign in to comment.