-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject_control.js
118 lines (106 loc) · 3.63 KB
/
project_control.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
// new window life span ///////////////////////////////////////////////////////////////////
// button action
function newWindow() {
document.getElementById("start").style.display = "none"
document.getElementById("UI").style.display = "block"
newPage()
}
// a handle of the new window that othe functions can operate
var myNewWindow
// auto close child window on mother window close
window.addEventListener("beforeunload", function (e) {
// Do something
myNewWindow.close()
}, false);
// create new window and set style
function newPage() {
// open a new window, specify size to make a mini window
myNewWindow = window.open("", "", "width=400,height=400")
// apply style // https://stackoverflow.com/a/26300876/7037749
myNewWindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="small_window.css"></head><body></body></html>');
// test display
display_chapter()
}
// global states /////////////////////////////////////////////////////////////////////////////
// current verse displayed
let b_cur = 43
let c_cur = 3
let v_cur = 16
// content display ///////////////////////////////////////////////////////////////////////
// main function
function display_verse(b, c, v) {
// parse
let b_input = parseInt(b)
let c_input = parseInt(c)
let v_input = parseInt(v)
if (!(verse_exists(b_input, c_input, v_input))) {
throw ('invalid input')
}
// if desired verse in the current chapter
if (b_cur == b_input && c_cur == c_input) {
v_cur = v_input
scroll_to_verse()
} else {
// if desired verse not in the current chapter
b_cur = b_input
c_cur = c_input
v_cur = v_input
display_chapter()
}
}
// display bcv of a new chapter
function display_chapter() {
// clear
myNewWindow.document.body.innerHTML = '';
myNewWindow.document.title = si_book_name[b_cur]
// get all verses in a chapter
let verses = get_verses_chapter(b_cur, c_cur)
// append verses to html
let text_class = "odd"
for (let i = 0; i < verses.length; i++) {
// prepare text block
let aPieceOfText = document.createElement("p")
aPieceOfText.innerHTML = bible_versions([b_cur, c_cur, verses[i]])
aPieceOfText.id = "" + (i + 1)
aPieceOfText.className = text_class
aPieceOfText.style = "font-size:" + font_size() + "px;"
// append text
myNewWindow.document.body.appendChild(aPieceOfText);
// alter text class for styling
if (text_class == "odd") {
text_class = "even"
} else {
text_class = "odd"
}
}
// scroll to position
setTimeout(function () {
go_to_verse(v_cur)
}, 100);
}
// scroll controlls ////////////////////////////////////////
function scroll_to_verse() {
// only work for chrome
// https://stackoverflow.com/a/49842367/7037749
var testDiv = myNewWindow.document.getElementById("" + v_cur);
const y = testDiv.getBoundingClientRect().top + myNewWindow.scrollY;
myNewWindow.scroll({
top: y,
behavior: 'smooth'
});
}
function go_to_verse() {
// only work for chrome
// https://stackoverflow.com/a/49842367/7037749
var testDiv = myNewWindow.document.getElementById("" + v_cur);
const y = testDiv.getBoundingClientRect().top + myNewWindow.scrollY;
myNewWindow.scroll({
top: y
});
}
function scorll_to_next(i) {
if (verse_exists(b_cur, c_cur, v_cur + i)) {
v_cur += i
scroll_to_verse()
}
}