-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (136 loc) · 3.9 KB
/
index.js
File metadata and controls
161 lines (136 loc) · 3.9 KB
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
/**
* @typedef {{
* name: string
* explain: string
* tuan: string
* xiang: string
* single: string[]
* website: {
* wiki: string
* ctext: string
* }
* }} HexagramData
*/
/* script:
var filterOut = (v, i) => v !== "" && ![5, 8, 11, 14, 17, 20].includes(i)
var text = [...document.querySelectorAll("#content3 .ctext:not(.opt)")].map(e=>e.outerText).filter(filterOut)
var website = window.location.href
var [title, explain] = text[0].split(":")
var data = `
{ // ${title}
name: "${title}",
explain: "${explain}",
tuan: "${text[1]}",
xiang: "${text[2]}",
single: [
"${text[3]}",
"${text[4]}",
"${text[5]}",
"${text[6]}",
"${text[7]}",
"${text[8]}",
],
website: {
ctext: "${website}",
}
},`
console.log(data)
*/
let CURRENT_HEXAGRAM = 0b111111
class HTMLBuilder {
/**
* @param {string[]} list
* @returns {string}
*/
static getSingleList(list) {
return list.reduce((res, cur, idx) => {
const [count, context] = cur.split(":")
return res + `<li class="explain-list"><span class="small-title">${count}:</span>${context}</li>`
}, `<ul class="list-block">`) + "</ul>"
}
/**
* @param {HexagramData} data
* @returns {string}
*/
static getExplainHTML(data) {
return `
<h1 id="hexagram-name">${data.name}</h1>
<p>${data.explain}</p>
<p><span class="small-title">彖傳:</span>${data.tuan}</p>
<p><span class="small-title">象傳:</span>${data.xiang}</p>
${HTMLBuilder.getSingleList(data.single)}
`
}
/**
* @param {HexagramData} data
* @returns {string}
*/
static getWebsiteHTML(data) {
let res = `
<ul class="list-block">
<li class="explain-list">
<a href="https://zh.wikipedia.org/wiki/${data.name}卦" target="_blank" class="website-link">維基百科:${data.name}卦</a>
</li>
`
if (data.website.ctext !== "") res += `
<li class="explain-list">
<a href="${data.website.ctext}" target="_blank" class="website-link">中國哲學書電子化計畫:${data.name}卦</a>
</li>
`
return res + "</ul>"
}
}
class DataManager {
/**
* @param {string} id
* @returns
*/
static updateCurrentTarget(id) {
switch (id) {
case "1":
CURRENT_HEXAGRAM ^= 0b1
return
case "2":
CURRENT_HEXAGRAM ^= 0b10
return
case "3":
CURRENT_HEXAGRAM ^= 0b100
return
case "4":
CURRENT_HEXAGRAM ^= 0b1000
return
case "5":
CURRENT_HEXAGRAM ^= 0b10000
return
case "6":
CURRENT_HEXAGRAM ^= 0b100000
return
default: return
}
}
static updateContext() {
const data = HEXAGRAM_LIST[CURRENT_HEXAGRAM]
if (!data) return
const explain = HTMLBuilder.getExplainHTML(data)
const website = HTMLBuilder.getWebsiteHTML(data)
document.getElementById("explain").innerHTML = explain
document.getElementById("website").innerHTML = website
}
}
class HtmlEvents {
static onPageLoad() {
document
.querySelectorAll(".hexagram")
.forEach(el => el.addEventListener("click", HtmlEvents.onHexagramClick))
}
/**
* @param {PointerEvent} ev
*/
static onHexagramClick(ev) {
const el = ev.target
if (!el) return
el.toggleAttribute("data-negtive")
DataManager.updateCurrentTarget(el.id.split("-")[1])
DataManager.updateContext()
}
}