forked from SilverEzhik/radical-lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
152 lines (140 loc) · 4.48 KB
/
script.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
// clear javascript warning and show the ui
{
var el = document.getElementById("js-error");
el.parentNode.removeChild(el);
document.getElementById("tool").style.display = '';
}
var krad;
{
var re_krad = /(.) : (.*)/;
var request_krad = new XMLHttpRequest();
request_krad.open('GET', "kradzip/kradfile", true);
request_krad.send(null);
request_krad.onreadystatechange = function () {
if (request_krad.readyState === 4 && request_krad.status === 200) {
var type = request_krad.getResponseHeader('Content-Type');
if (type.indexOf("text") !== 1) {
var lines = request_krad.responseText.split("\n");
krad = new Map;
lines.forEach(e => {
var split = re_krad.exec(e);
if (split != null && split[1] != "#") {
krad.set(split[1], split[2].split(" "));
}
});
}
}
}
}
var radk;
{
var re_radk = /^\$ (.)/; // radk regex
var request_radk = new XMLHttpRequest();
request_radk.open('GET', "kradzip/radkfile", true);
request_radk.send(null);
request_radk.onreadystatechange = function () {
if (request_radk.readyState === 4 && request_radk.status === 200) {
var type = request_radk.getResponseHeader('Content-Type');
if (type.indexOf("text") !== 1) {
var lines = request_radk.responseText.split("\n");
radk = new Map;
var cur = "";
lines.forEach(e => {
var split = re_radk.exec(e);
if (split != null) {
cur = split[1];
radk.set(cur, []);
} else {
if (e.charAt(0) != "#") {
radk.set(cur, radk.get(cur).concat(e.split("")));
}
}
});
}
}
}
}
function clearInput() {
let input = document.getElementById("input");
input.value = "";
input.focus();
}
let radical_list = document.getElementById("radical-list");
function getRadicals(textbox) {
radical_list.innerHTML = "";
let radicals = krad.get(textbox.value);
if (radicals == undefined) {
radical_list.innerHTML = "無";
} else {
radicals.forEach(e => {
radical_list.innerHTML += ("<a onclick=\"addRadical(this)\">" + e + "</a> ");
});
}
}
function addRadical(btn) {
selected_radicals.add(btn.innerText);
}
var selected_radicals = {};
selected_radicals.div = document.getElementById("selected-radicals-list");
selected_radicals.list = [];
selected_radicals.marked = null;
selected_radicals.add = function(r) {
if (this.list.includes(r)) {
return;
}
this.list.push(r);
this.div.innerHTML += ("<a onclick=\"removeRadical(this)\">" + r + "</a> ");
updateKanji();
}
selected_radicals.remove = function(r) {
if (!this.list.includes(r)) {
return;
}
selected_radicals.div.childNodes.forEach(e => {
// if clicked on the marked button, delete
if (e == this.marked && e.innerText == r) {
this.list.splice(this.list.indexOf(r), 1);
this.div.removeChild(e);
marked = null;
updateKanji();
// else, mark this one
} else if (e.innerText == r) {
if (this.marked != null) {
this.marked.id = "";
}
this.marked = e;
e.id = "marked";
} else {
e.id = "";
}
});
}
function removeRadical(btn) {
selected_radicals.remove(btn.innerText);
}
var kanji_list = document.getElementById("kanji-list");
function updateKanji() {
kanji_list.innerHTML = "";
if (selected_radicals.list.length == 0) {
return;
}
var kanji = radk.get(selected_radicals.list[0]).slice();
for (var i = 1; i < selected_radicals.list.length; i++) {
kanji = kanji.filter(function(n) {
return radk.get(selected_radicals.list[i]).indexOf(n) > -1;
});
}
kanji.forEach(e => {
kanji_list.innerHTML += ("<a onclick=\"addKanji(this)\">" + e + "</a> ");
});
}
var output = document.getElementById("output");
function addKanji(btn) {
output.value += btn.innerText;
}
function openJisho() {
if (output.value != "") {
window.open("https://jisho.org/search/" + output.value, "_blank");
}
return false;
}