-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththesaurus1.html
389 lines (371 loc) · 14.9 KB
/
thesaurus1.html
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href='https://fonts.googleapis.com/css?family=Lexend' rel='stylesheet' type='text/css'>
<style>
body { margin:0;padding:0;font-size:18px;font-family:Lexend,"Helvetica Neue", Optima, Verdana, sans-serif; font-weight:300; background-color:#fff; display: flex; height: 100vh; width: 100vw; }
h1, h2, h3, h4, h5, input, select, textarea, button { font-family: Lexend, "Helvetica Neue", Optima, Verdana, sans-serif; }
a { text-decoration:none;color:#00c; }
a:hover { text-decoration:underline; }
a:visited { color:#006; }
a:active { color:#e00;display:inline-block;top:2px; }
.antonym { color: #c00; }
.synonym { color: #00c; }
#synonyms {
display: flex;
flex-direction: column;
width: 100vw;
height: 100vw;
}
.word {
display: flex;
padding: 0.5rem 1rem;
border: 1px solid #000;
border-radius: 1rem;
position: absolute;
min-width: 5rem;
height: 2rem;
text-align: center;
box-shadow: 0.5rem 0.5rem 0.5rem #0005;
background:#eef;
z-index:999;
align-items:center;
justify-content:center;
}
.root {
position: relative;
background-color: #fef;
z-index: 999;
}
.node {
position: relative;
}
.title {
position: relative;
padding: 1rem;
border: 1px solid #000;
border-radius: 1rem;
}
h1.title {
position: fixed;
top:50%;
left:50%;
}
.category {
position: absolute;
display: flex;
z-index: 999;
}
.link {
background-color: #000;
height: 4px;
position: absolute;
transform-origin: 0px 0px;
z-index: 1;
}
.info-icon {
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512"><path d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zM216 336h24V272H216c-13.3 0-24-10.7-24-24s10.7-24 24-24h48c13.3 0 24 10.7 24 24v88h8c13.3 0 24 10.7 24 24s-10.7 24-24 24H216c-13.3 0-24-10.7-24-24s10.7-24 24-24zm40-208a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>');
background-size: contain;
background-repeat: no-repeat;
filter: invert(1);
display: inline-block;
height: 1.5rem;
width: 1.5rem;
padding-right: 1rem;
margin-left: 1rem;
}
form#thesaurus {
display: flex;
align-items: center;
justify-content: flex-start;
background-color: #222;
color: #eee;
height: 3rem;
}
input, select, button {
font-size: 18px;
font-weight: 300;
border-radius: 4px;
}
input[type='text'] {
height: 2rem;
}
button {
border-radius: 0.75rem;
height: 2rem;
background-color: #333;
border: 1px solid #fff;
color: #eee;
}
.app-title {
display: inline-block;
font-size: 24px;
font-weight: bold;
}
dialog {
padding: 0px;
position: absolute;
top: 3rem;
}
#info {
width: 50rem;
font-size: 18px;
font-weight: 300;
display:flex;
flex-direction: column;
}
.dialog-header {
background-color: #060;
color: #fff;
display: flex;
flex-direction:row;
justify-content: space-between;
padding: 0 0 0 1rem;
height: 3rem;
}
.dialog-body {
padding: 0 1rem;
overflow-y: scroll;
}
h2 { padding: 0; margin: 0; padding-top: 4px; }
button.close-button {
height: 2rem;
width: 2rem;
border: 0;
font-size: 1.5rem;
background: none;
color: #fff;
text-shadow: 1px 1px 0px #0009;
padding-top: 8px;
}
.dialog-footer {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.close {
font-size: 22px;
}
#info .dialog-body {
display: flex;
flex-direction: column;
align-items:center;
justify-content: center;
background-color: #fff;
color: #000;
overflow: hidden;
}
dialog {
font-size: 18px;
}
h1, h2, h3, h4 {
margin:0;
padding:0;
}
.dialog-foot {
display: flex;
justify-content:center;
}
.dialog-foot button {
border-radius: 1rem;
margin: 0.5rem;
padding: 0.5rem;
height: 2.5rem;
}
#info .dialog-foot {
background-color:#000;
}
#info p {
}
#info {
display: none;
}
#info[open] {
border: 4px solid #eee;
border-radius: 1rem;
display: flex;
}
button:active {
transform: translateY(2px);
background-color: #fff;
}
</style>
</head>
<body>
<div id='main'>
<form id="thesaurus">
<div class='app-title'>Thesaurus</div> <a href="#" class="info-icon" onclick="showInfo(); return false;"></a> Word: <input type='text' id='word' name='word'><button>Lookup</button> <a style="color:#9cf;position:absolute;right:1rem;text-decoration: underline;" href="thesaurus.html">Original Thesaurus</a>
</form>
<div id='synonyms'></div>
</div>
<dialog id="info">
<form style="flex-direction:column;" method="dialog">
<div class='dialog-header'>
<h2>Online Thesaurus</h2>
<button class='close-button' onclick="closeInfo(); return false;">x</button>
</div>
<div class='dialog-body'>
<h3>About</h3>
<p>I've been using AlterVista for thesaurus lookups for years and up until now, I had been content with my boring lists of returned words. It was better than -any- of the mainstream alternatives which bombard you with ads and brings your computer to its knees with its web pages heavy with external libraries and trackers. But I recently wanted a way to visualize my lookups and be able to tunnel from word to word while still retaining my previous lookups. I've used D3.js in the past and so I quickly wrote a force directed graph for my thesaurus using D3.js. But still I was not satisfied. It still felt heavy. I know it's all probably in my head because I -know- there's a bunch of external code being loaded; but there were long pauses while before rendereing which I chalked up to all the bells and whistles D3.js provides. So I wrote my own.</p>
<p>I wrote a bare-bones JavaScript directed graph thesaurus library that is pretty unique in that it doesn't use canvas OR SVG (yet!), it only uses DOM elements, JavaScript and CSS. I know, the node layout algorithm needs a little attention but the basics are there and work. The connecting lines are simply DIV tags with a 4px height that I calculate the length and rotation using some basic trigonometry and apply using CSS. The only trick was changing the transform-origin to [0, 0] instead of the default of center.</p>
<p>So try it out! Enter in a few words, drag a few bubbles around and be amazed at the clever use of CSS and math to get something you would normally need to do with canvas.</p>
</div>
<div class='dialog-foot'>
<button onclick="closeInfo(); return false;">Return to Thesaurus</button>
</div>
</form>
</dialog>
</body>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type='text/javascript'>
function showInfo() {
document.querySelector("#info").setAttribute("open", true);
}
function closeInfo() {
document.querySelector("#info").removeAttribute("open");
}
function beginDrag(e) {
console.log('begin dragging');
console.dir(e);
if (e.target.tagName === "BUTTON") {
return true;
}
let tgt = e.target.closest(".word");
if (!tgt) tgt = e.target.closest(".category");
if (tgt && (tgt.classList.contains('word') || tgt.classList.contains('category'))) {
tgt.style.zIndex = 99999;
window.dragging = tgt;
document.addEventListener("mousemove", doDrag);
document.addEventListener("mouseup", endDrag);
}
}
function doDrag(e) {
e.preventDefault();
e.stopPropagation();
if (window.dragging) {
window.dragging.style.top = (e.y -50) + 'px';
window.dragging.style.left = (e.x -50) + 'px';
updateLinks();
}
}
function endDrag(e) {
if (window.dragging && e.target.tagName !== "BUTTON") {
e.preventDefault();
e.stopPropagation();
window.dragging = '';
updateLinks();
}
}
$(document).ready(function() {
$("form").submit(function() {
let el = document.querySelector("#myword");
if (el) {
el.parentElement.removeChild(el);
}
lookup($("#word").val());
return false;
});
document.querySelector("#synonyms").addEventListener("mousedown", beginDrag);
});
var result;
function lookup(word) {
window.word = word;
var s = document.createElement("script");
s.src = "https://thesaurus.altervista.org/service.php?word=" + encodeURIComponent(word) + "&language=en_US&output=json&key=uPg9e4w3ikOcjkaruu5P&callback=process";
document.getElementsByTagName("head")[0].appendChild(s);
}
conf = {};
if (window.location.search) {
var str = window.location.search.replace(/^\?/, '');
var pairs = str.split(/\&/), pc = pairs.length;
for (var i=0; i<pc; i++) {
args = pairs[i].split(/=/);
conf[args[0]] = args[1];
}
if (conf['word']) {
$("#word").val(conf['word']);
lookup(conf['word']);
}
}
function process(data) {
result = data;
let colors = ['#cceeff','#ffcccc','#ccffcc','#ccccff','#ffccff','#ffffcc','#ffffcc'];
var output = "", items, list, key, words, i, antonym, css, arr=[], types, typ, disp;
let catidx = 0;
let myword = document.createElement("div");
myword.id = "myword";
myword.className = 'root';
myword.innerHTML = "<h1 class='title'>"+window.word+"</h1>";
let centerX = window.innerWidth / 2;
let centerY = window.innerHeight / 2;
let pcenterX, pcenterY;
let names = {};
for (key in result.response) {
list = result.response[key].list;
items = list.synonyms.split(/\|/);
let angle = catidx * Math.PI / 4;
let pcenterY = centerY - (Math.sin(angle) * 300);
let pcenterX = centerX - (Math.cos(angle) * 300);
let ctop = pcenterY + 'px';
let cleft = pcenterX + 'px';
let name = list.category.replace(/\W/g, '');
if (!names[name]) {
names[name] = 0;
}
names[name]++;
output += `<div style='top:${ctop};left:${cleft};background-color:${colors[catidx]}' class='child category' id='${name}${names[name]}' data-parent='${window.word}'><span class='title'>${list.category}</span></div>`;
arr = renderWords(window.word, items, pcenterX, pcenterY, name + names[name]);
output += arr.join("");
++catidx;
}
if (output) {
myword.innerHTML += output;
document.getElementById("synonyms").append(myword);
}
updateLinks();
}
function renderWords(myword, items, x, y, category) {
let idx = 0, arr = [], typ = '', disp;;
for (i in items) {
antonym = 0;
types = items[i].match(/\s*\((.+?)\)/);
typ = (types && types[1]) ? types[1].replace(/\s*term/,'') : 'synonym';
disp = items[i].replace(/\s*\(.+?\)/, '');
if (items[i].match(/\(antonym\)/)) {
items[i] = '(' + items[i].replace(/\s*\(.*/, '') + ')';
antonym = 1;
}
css = (antonym) ? 'antonym' : 'synonym';
title = (antonym) ? 'Antonym' : 'Synonym';
angle = idx * (Math.PI / (items.length / 2));
let top = y - (Math.sin(angle) * 200 );
let left = x - (Math.cos(angle) * 200);
let y1 = top - y, x1 = left - x;
let rot = Math.atan2(y1, x1) * 180 / Math.PI;
let len = Math.sqrt(x1*x1 + y1*y1);
arr[arr.length] = `<div style="top:${top}px;left:${left}px" class='word ${css}' id='word_${disp.replace(/\W/g, '')}'><a title='${title}' class='${typ} ${css}' href='#' onclick="lookup('${disp}')">${disp}</a></div><div data-link1="word_${disp.replace(/\W/g, '')}" data-link2="${category.replace(/\W/g, '')}" class='link' style="top:${y-25}px;left:${x+40}px;rotate:${rot}deg;width:${len}px;" id="link_${myword}_${disp}"></div>`;
++idx;
}
return arr;
}
function updateLinks(dragObj='') {
document.querySelectorAll(".link").forEach((link, idx) => {
let obj1, obj2, y1, x1, rot, len;
let el1 = document.querySelector(`#${link.dataset.link1}`);
let el2 = document.querySelector(`#${link.dataset.link2}`);
obj1 = el1.getBoundingClientRect();
obj2 = el2.getBoundingClientRect();
y1 = obj1.y - obj2.y, x1 = obj1.x - obj2.x;
rot = Math.floor(Math.atan2(y1, x1) * 180 / Math.PI);
len = Math.floor(Math.sqrt(x1*x1 + y1*y1));
link.style.top = (obj2.top - 25)+ 'px';
link.style.left = (obj2.left + 40)+ 'px';
link.style.rotate = `${rot}deg`;
link.style.width = `${len}px`;
});
}
</script>
</html>