forked from akivajgordon/tikkun.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
266 lines (204 loc) · 7.19 KB
/
index.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* global gtag */
import { EventEmitter } from 'events'
import { InfiniteScroller, IntegerIterator, title as getTitle, physicalLocationFromRef } from './src'
import Page from './components/Page'
import ParshaPicker, { search } from './components/ParshaPicker'
import Search from './components/Search'
import utils from './components/utils'
import pageTitles from './build/page-titles.json'
const { htmlToElement, whenKey, purgeNode } = utils
const insertBefore = (parent, child) => {
parent.insertAdjacentElement('afterbegin', child)
}
const insertAfter = (parent, child) => {
parent.insertAdjacentElement('beforeend', child)
}
const cache = {}
const unpackCache = (cache) => Object.keys(cache || {}).map(key => cache[key])
let isShowingParshaPicker = false
let scroll
const state = {}
const renderTitle = ({ title }) => {
document.querySelector('[data-target-id="parsha-title"]').innerHTML = title
}
const render = ({ cache, showAnnotations }) => {
unpackCache(cache)
.forEach(({ node, content }) => {
const el = htmlToElement(Page(content, showAnnotations))
const firstChild = node.firstChild
if (firstChild) {
node.replaceChild(el, firstChild)
} else {
node.appendChild(el)
}
})
}
const setState = (updates) => {
const newState = Object.assign(state, updates)
render(newState)
}
const purgeObject = (obj) => {
for (const key in obj) {
delete obj[key]
}
}
const makePageNode = ({ title }) => {
const node = document.createElement('div')
node.classList.add('tikkun-page')
node.setAttribute('data-page-title', title)
return node
}
const scrollToLine = ({ node, lineIndex }) => {
const lines = [...node.querySelectorAll('.line')]
const line = lines[lineIndex]
const book = document.querySelector('.tikkun-book')
book.scrollTop = line.offsetTop + (line.offsetHeight / 2) - (book.offsetHeight / 2)
}
const app = {
jumpTo: ({ ref, scroll: _scroll }) => {
purgeObject(cache)
scroll = (_scroll === 'esther' ? EstherScroll : TorahScroll).new({ startingAtRef: ref })
purgeNode(document.querySelector('[data-target-id="tikkun-book"]'))
scroll.fetchNext()
.then(renderNext)
.then((pageNode) => {
scrollToLine({ node: pageNode, lineIndex: scroll.startingLineNumber - 1 })
})
toggleParshaPicker()
return Promise.resolve()
}
}
const refOf = element => {
const refPart = (part) => Number(element.getAttribute(`data-jump-to-${part}`))
return { b: refPart('book'), c: refPart('chapter'), v: refPart('verse') }
}
const showParshaPicker = () => {
const searchEmitter = new EventEmitter()
const s = Search({ search, emitter: searchEmitter })
const jumper = ParshaPicker(s, searchEmitter, ({ ref, scroll }) => app.jumpTo({ ref: refOf(ref), scroll }))
document.querySelector('#js-app').appendChild(jumper)
gtag('event', 'view', {
event_category: 'navigation'
})
setTimeout(() => s.focus(), 0)
}
const toggleParshaPicker = () => {
isShowingParshaPicker = !isShowingParshaPicker
;[
'[data-test-id="annotations-toggle"]',
'[data-target-id="repo-link"]',
'[data-target-id="tikkun-book"]'
]
.map(selector => document.querySelector(selector))
.map(el => el.classList)
.forEach(classList => {
classList.toggle('u-hidden')
classList.toggle('mod-animated')
})
if (isShowingParshaPicker) {
showParshaPicker()
} else {
document.querySelector('#js-app').removeChild(document.querySelector('.parsha-picker'))
}
}
const toggleAnnotations = () => {
const toggle = document.querySelector('[data-target-id="annotations-toggle"]')
toggle.checked = !toggle.checked
setState({ showAnnotations: toggle.checked })
}
const updatePageTitle = () => {
const bookBoundingRect = document.querySelector('.tikkun-book').getBoundingClientRect()
const centerOfBookRelativeToViewport = {
x: bookBoundingRect.left + (bookBoundingRect.width / 2),
y: bookBoundingRect.top + (bookBoundingRect.height / 2)
}
const pageAtCenter = [...document.elementsFromPoint(centerOfBookRelativeToViewport.x, centerOfBookRelativeToViewport.y)]
.find(el => el.className.includes('tikkun-page'))
renderTitle({ title: pageAtCenter.getAttribute('data-page-title') })
}
let lastCalled = Date.now()
const throttle = f => {
if (Date.now() - lastCalled > 300) {
lastCalled = Date.now()
f()
}
}
const renderPage = ({ insertStrategy: insert }) => ({ key, content, title }) => {
const node = makePageNode({ title })
cache[key] = { node, content }
insert(document.querySelector('[data-target-id="tikkun-book"]'), node)
setState({
cache,
showAnnotations: document.querySelector('[data-target-id="annotations-toggle"]').checked
})
renderTitle({ title })
return node
}
const renderPrevious = renderPage({ insertStrategy: insertBefore })
const renderNext = renderPage({ insertStrategy: insertAfter })
const fetchPage = ({ path, title }) => window.fetch(path)
.then((res) => res.json())
.then((page) => ({ key: path, content: page, title }))
.catch((err) => {
console.error(err)
})
const Scroll = {
new: ({ scroll, makePath, makeTitle, startingAtRef = { b: 1, c: 1, v: 1 } }) => {
const { pageNumber, lineNumber } = physicalLocationFromRef({ ref: startingAtRef, scroll })
const iterator = IntegerIterator.new({ startingAt: pageNumber })
return {
fetchPrevious: () => {
const n = iterator.previous()
if (n <= 0) return Promise.resolve()
return fetchPage({ path: makePath(n), title: makeTitle(n) })
},
fetchNext: () => {
const n = iterator.next()
return fetchPage({ path: makePath(n), title: makeTitle(n) })
},
startingLineNumber: lineNumber
}
}
}
const TorahScroll = {
new: ({ startingAtRef }) => {
return Scroll.new({
scroll: 'torah',
makePath: n => `/build/pages/${n}.json`,
makeTitle: n => getTitle(pageTitles[n - 1]),
startingAtRef
})
}
}
const EstherScroll = {
new: ({ startingAtRef }) => {
return Scroll.new({
scroll: 'esther',
makePath: n => `/build/pages/${n}-esther.json`,
makeTitle: n => 'אסתר',
startingAtRef
})
}
}
document.addEventListener('DOMContentLoaded', () => {
InfiniteScroller
.new({
container: document.querySelector('[data-target-id="tikkun-book"]'),
fetchPreviousContent: { fetch: () => scroll.fetchPrevious(), render: renderPrevious },
fetchNextContent: { fetch: () => scroll.fetchNext(), render: renderNext }
})
.attach()
document.querySelector('[data-target-id="tikkun-book"]').addEventListener('scroll', () => {
throttle(() => updatePageTitle())
})
document.querySelector('[data-target-id="annotations-toggle"]').addEventListener('change', (e) => {
const showAnnotations = e.target.checked
setState({ showAnnotations })
})
document.addEventListener('keydown', whenKey('Shift', toggleAnnotations))
document.addEventListener('keyup', whenKey('Shift', toggleAnnotations))
document.querySelector('[data-target-id="parsha-title"]').addEventListener('click', toggleParshaPicker)
document.addEventListener('keydown', whenKey('/', toggleParshaPicker))
app.jumpTo({ ref: { b: 1, c: 1, v: 1 }, scroll: 'torah' })
.then(toggleParshaPicker)
})