-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevents.js
234 lines (234 loc) · 8.63 KB
/
events.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
var worker = {"postMessage": function(a) {}}; // very hack
var config = {};
let page;
var q = "";
var results;
function h(t) {
return t.replace(/[h‘’]/igu, "'");
}
function getConflictRegex(g) {
var conflict = [...g];
for (var i = 0; i < conflict.length; i++) {
conflict[i] = !isVowel(conflict[i]) ?
(g.slice(0, i) || "") + g[i]
// wheeeeee
.replace(/[bfpv]/, m => "[" + (/[bf]/.test(m) ? ["p", "v", m] : ["b", "f", m]).sort().join("") + "]")
.replace(/[cjsz]/, m => "[" + (/[cz]/.test(m) ? ["j", "s", m] : ["c", "z", m]).sort().join("") + "]")
.replace(/[dt]/, "[dt]")
.replace(/[gkx]/, "[gkx]")
.replace(/[lr]/, "[lr]")
.replace(/[mn]/, "[mn]")
+ (g.slice(i + 1, conflict.length) || "")
:
i == conflict.length - 1 ? g.slice(0, i) + "[aeiou]" : null;
}
conflict = conflict.join("|").replace(/\|+/, "|");
return conflict;
}
window.addEventListener("scroll", function(e) {
if (window.innerHeight + window.scrollY >= document.body.scrollHeight - 100) {
page++;
load(results, page);
checkLength();
}
});
function checkLength() {
if ((page + 1) * 100 - 1 >= results.length) {
id("bottom").innerHTML = results.length == 0 ? "" : "no more results";
}
}
function clearResults() {
id("results").innerHTML = "";
id("info").innerHTML = "";
id("bottom").innerHTML = "";
id("length").innerHTML = "";
}
var timer;
id("search").addEventListener("input", function() {
clearTimeout(timer);
q = id("search").value;
if (q.length) {
addClassById("clear-wrap", "show");
} else {
removeClassById("clear-wrap", "show");
}
if (!config["regex"]) q = q.trim();
results = null;
clearResults();
redirect();
timer = setTimeout(function() {
if (q.length) {
id("bottom").innerHTML = "loading...";
if (!config["rhyme"] && !config["regex"]) {
// lujvo things
try {
if (/\s/.test(q)) {
const lujvo = getLujvo(h(q), config["lujvo.cmevla"])[0];
id("info").append(createHTMLElement("p", null, [
"→\u{a0}",
createHTMLElement("a", {"href": "?q=" + encodeURIComponent(lujvo) + jvoptionsUrl()}, [createHTMLElement("i", null, [lujvo])])
]));
} else {
const veljvo = getVeljvo(h(q)).join(" ");
id("info").append(createHTMLElement("p", null, [
"↑\u{a0}",
createHTMLElement("a", {"href": "?q=" + encodeURIComponent(veljvo) + jvoptionsUrl()}, [createHTMLElement("i", null, [veljvo])])
]));
if (h(q) != getLujvo(veljvo, config["lujvo.cmevla"])[0]) {
id("info").append(createHTMLElement("p", null, [
"best:\u{a0}",
createHTMLElement("a", {
"id": "best",
"href": "?q=" + encodeURIComponent(getLujvo(veljvo, config["lujvo.cmevla"])[0]) + jvoptionsUrl()
}, [])
]));
const mabla = jvokaha(h(q));
const best = jvokaha(getLujvo(veljvo, config["lujvo.cmevla"])[0]);
const hyphens = ["r", "n", "y", "'y", "y'", "'y'"];
for (var m = 0, b = 0; m < mabla.length; m++, b++) {
if (hyphens.includes(mabla[m])) {
if (!hyphens.includes(best[b])) {
m++;
} else if (hyphens.includes(best[b]) && mabla[m] == best[b]) {
id("best").append(createHTMLElement("i", null, [best[b]]));
m++; b++;
}
} else if (hyphens.includes(best[b])) {
mabla.splice(m, 0, "");
}
if (mabla[m] != best[b]) {
id("best").append(createHTMLElement("i", {"className": "err"}, [best[b]]));
} else {
id("best").append(createHTMLElement("i", null, [best[b]]));
}
}
}
}
} catch (e) {
// not lujvo
}
if (isGismu(q) && (VALID.includes(q.slice(0, 2)) || VALID.includes(q.slice(2, 4)))) {
id("info").append(createHTMLElement("p", null, [
createHTMLElement("a", {"href": "?q=" + encodeURIComponent(getConflictRegex(q)) + "®ex=tight"}, ["↑ find gismu conflicts?"])
]));
}
} else if (config["regex"]) {
// bad regex
try {
_ = new RegExp(q);
} catch (e) {
id("info").append(createHTMLElement("p", null, [e.message.split(": ").slice(-1)[0].toLowerCase()]));
}
}
worker.postMessage({"query": q, "config": config});
} else {
results = null;
clearResults();
page = 0;
}
}, 100);
});
// modes
id("sm").addEventListener("click", function() {
searchMode(false);
});
id("jvo-cme").addEventListener("click", function() {
searchMode(true);
});
id("jvo-x").addEventListener("click", function() {
searchMode(config["lujvo.cmevla"]);
});
id("rm").addEventListener("click", function() {
rhymeMode(false);
});
id("rhyme-y").addEventListener("click", function() {
rhymeMode(true);
});
id("xm").addEventListener("click", function() {
regexMode(false, false);
});
id("regex-i").addEventListener("click", function() {
regexMode(true, false);
});
id("regex-tight").addEventListener("click", function() {
regexMode(false, true);
});
function removeClasses() {
document.body.classList.remove("rhyme");
document.body.classList.remove("regex");
}
function setBodyClass(className) {
document.body.classList.add(className);
}
function addClassById(_id, className) {
id(_id).classList.add(className);
}
function removeClassById(_id, className) {
id(_id).classList.remove(className);
}
function toggleClassById(_id, className) {
if (id(_id).classList.contains(className))
id(_id).classList.remove(className);
else
id(_id).classList.add(className);
}
function dispatchSearchInputEvent() {
id("search").dispatchEvent(new Event("input", {"bubbles": true}));
}
function searchMode(togglecme) {
clearTimeout(timer);
removeClasses();
removeClassById("rm", "checked");
removeClassById("xm", "checked");
addClassById("sm", "checked");
config["rhyme"] = false;
config["regex"] = false;
if (togglecme) {
toggleClassById("jvo-cme", "checked");
config["lujvo.cmevla"] = !config["lujvo.cmevla"];
}
id("jvo-x").disabled = !(config["lujvo.cmevla"]); // will expand later
dispatchSearchInputEvent();
}
function regexMode(togglei, toggletight) {
clearTimeout(timer);
removeClasses();
setBodyClass("regex");
removeClassById("sm", "checked");
removeClassById("rm", "checked");
addClassById("xm", "checked");
config["rhyme"] = false;
config["regex"] = true;
if (togglei) {
toggleClassById("regex-i", "checked");
config["regex.insensitive"] = !config["regex.insensitive"];
}
if (toggletight) {
toggleClassById("regex-tight", "checked");
config["regex.tight"] = !config["regex.tight"];
}
dispatchSearchInputEvent();
}
function rhymeMode(toggle) {
clearTimeout(timer);
removeClasses();
setBodyClass("rhyme");
removeClassById("sm", "checked");
removeClassById("xm", "checked");
addClassById("rm", "checked");
config["rhyme"] = true;
config["regex"] = false;
if (toggle) {
toggleClassById("rhyme-y", "checked");
config["rhyme.ignorey"] = !config["rhyme.ignorey"];
}
dispatchSearchInputEvent();
}
id("clear").addEventListener("click", function() {
id("search").value = "";
id("search").focus();
dispatchSearchInputEvent();
});
id("theme").addEventListener("click", function() {
setTheme(document.documentElement.className != "dark");
});