/
ParshaPicker.js
131 lines (114 loc) · 3.24 KB
/
ParshaPicker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import parshiyot from '../build/parshiyot.json'
import fuzzy from '../src/fuzzy'
const Parsha = ({ ref, he }) => `<li
class="parsha"
data-target-id="parsha"
data-jump-to-book="${ref.b}"
data-jump-to-chapter="${ref.c}"
data-jump-to-verse="${ref.v}"
>
${he}
</li>
`
const Book = (book) => `
<li class="parsha-book">
<ol class="parsha-list">
${book.map(Parsha).join('')}
</ol>
</li>
`
/*
<li class="search-result">
<p class="search-result-tag">שלח</p>
<p class="search-result-tag"><strong>Sh</strong>e<strong>l</strong>a<strong>ch</strong></p>
</li>
<li class="search-result">
<p class="search-result-tag">בשלח</p>
<p class="search-result-tag">Be<strong>sh</strong>a<strong>l</strong>a<strong>ch</strong></p>
</li>
<li class="search-result">
<p class="search-result-tag">וישלח</p>
<p class="search-result-tag">Vayi<strong>shl</strong>a<strong>ch</strong></p>
</li>
*/
/*
<li class="search-result">
<p class="" style="text-align: center; color: hsla(0, 0%, 0%, 0.5);">No results</p>
</li>
*/
const Search = () => `
<div class="search">
<div class="search-bar">
<span class="search-icon">⚲</span>
<input class="search-input" placeholder="Search..." autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" autofocus />
</div>
<ol class="search-results u-hidden">
</ol>
</div>
`
const decorateString = ({ string, atIndexes, withDecoration }) => {
let indexesIndex = 0
return string
.split('')
.map((char, i) => {
if (atIndexes[indexesIndex] !== i) return char
++indexesIndex
return withDecoration(char)
}, '')
.join('')
}
const SearchResult = ({ string, indexes }) => `
<li class="search-result">
<p class="search-result-tag">${decorateString({
string,
atIndexes: indexes,
withDecoration: (c) => (`<strong>${c}</strong>`)
})}
</p>
</li>
`
const ParshaPicker = () => `
<div class="parsha-picker">
${Search()}
<ol class="parsha-books">
${parshiyot
.reduce((books, parsha) => {
const book = parsha.ref.b
books[book] = books[book] || []
books[book].push(parsha)
return books
}, [])
.map(Book)
.join('')
}
</ol>
</div>
`
const searchResults = (query) => fuzzy(parshiyot.map(parsha => parsha.en), query)
const htmlToElement = (html) => {
const template = document.createElement('template')
html = html.trim() // Never return a text node of whitespace as the result
template.innerHTML = html
return template.content.firstChild
}
const top = (n) => (_, i) => i < n
const search = ({ jumper, query }) => {
const searchResultsElement = jumper.querySelector('.search-results')
if (query) {
searchResultsElement.classList.remove('u-hidden')
jumper.querySelector('.parsha-books').classList.add('u-hidden')
searchResultsElement.innerHTML = ''
searchResults(query)
.filter(top(5))
.map(SearchResult)
.map(htmlToElement)
.forEach(result => {
searchResultsElement.appendChild(result)
})
} else {
searchResultsElement.classList.add('u-hidden')
jumper.querySelector('.parsha-books').classList.remove('u-hidden')
}
}
export { search }
export default ParshaPicker