-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinbox.html
More file actions
126 lines (105 loc) · 3.35 KB
/
inbox.html
File metadata and controls
126 lines (105 loc) · 3.35 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
<meta charset="UTF-8">
<style>
body {
margin: 20px 40px;
}
a {
width: 500px;
color: dodgerblue;
}
img {
width: 500px;
height: auto;
object-fit: contain;
border: 1px solid black;
margin-bottom: 11px;
}
input {
border: 1px solid black;
padding: 5px;
margin: 0px;
}
#home {
margin-right: 30px;
}
</style>
<a href="/">hyperpixel</a>
<div style="margin-top: 10px; display: flex; align-items: center;">
<input id="search" type="text" placeholder="Search"></input>
<button id="shuffle" style="margin-left: 10px;">
Shuffle
</button>
</div>
<div id="rows" style="display: flex; flex-direction: row;">
<div id="home" style="display: flex; flex-direction: column"></div>
<div id="inbox" style="display: flex; flex-direction: column"></div>
</div>
<script>
const defaultDict = () => new Proxy({}, { get: (d, k) => d[k] || 0 });
// Reactive data flow
let db = {};
let num_annotations = defaultDict();
const query = new URLSearchParams(window.location.search).get("query");
const ws = new WebSocket("ws://localhost:1234/ws")
const send = (msg) => ws.send(JSON.stringify(msg));
ws.onopen = () => {
send({op: "SUBSCRIBE", path: []});
}
ws.onmessage = (e) => {
db = JSON.parse(e.data);
num_annotations = defaultDict();
for (const annotation of Object.values(db["annotations"])) {
num_annotations[annotation["to"]] += 1;
num_annotations[annotation["from"]] += 1;
}
render();
}
const load = (imageIds) => {
imageIds.forEach(imageId => {
if (query) {
const text = db["images"][imageId]["text"].toLowerCase();
const q = query.toLowerCase();
if (!(imageId.includes(q) || text.includes(q))) {
return;
}
}
const path = db["images"][imageId]["path"];
const label = document.createElement("p");
label.innerHTML = `/${imageId}`;
const output = document.createElement("img");
output.src = `/files/${path}`;
const link = document.createElement("a");
link.href = `/${imageId}`;
link.appendChild(output);
if (num_annotations[imageId] > 0) {
document.getElementById("home").appendChild(label);
document.getElementById("home").appendChild(link);
} else {
document.getElementById("inbox").appendChild(label);
document.getElementById("inbox").appendChild(link);
}
})
}
const shuffle = document.getElementById("shuffle");
shuffle.onclick = () => {
document.getElementById("home").innerHTML = "";
document.getElementById("inbox").innerHTML = "";
const imageIds = Object.keys(db["images"]).sort(() => Math.random() - 0.5)
load(imageIds);
}
const render = () => {
document.getElementById("home").innerHTML = "";
document.getElementById("inbox").innerHTML = "";
const search = document.getElementById("search");
search.value = query;
search.addEventListener("change", e => {
window.location = `/?query=${e.target.value}`;
})
const imageIds = Object.keys(db["images"]).sort((a, b) => {
const _a = a.replace(/[^a-z0-9]/gi,'');
const _b = b.replace(/[^a-z0-9]/gi,'');
return _a.localeCompare(_b);
});
load(imageIds);
}
</script>