-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
64 lines (60 loc) · 2.22 KB
/
Copy pathscript.js
File metadata and controls
64 lines (60 loc) · 2.22 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
const IMAGE_LIMIT_COUNT=500;
const SERVER_PORT=55000;
function is_Enter(e){
//TODO:codeとkeyどっちがいいかな?
return e.code=="Enter";
}
function html_escape(s){
const escape_map={"&": "&","<": "<",">": ">"};
return s.replace(/[&<>]/g,(x)=>{return escape_map[x]});
}
function fetch_images(){
const xhr = new XMLHttpRequest();
xhr.responseType = "json";
xhr.open("GET", `http://localhost:${SERVER_PORT}/images?sql=${encodeURIComponent(document.getElementById("sql").value)}`);
xhr.onload=()=>{
document.getElementById("message").innerText="";
const image_list=document.getElementById("image_list");
if(xhr.status=="400"){
document.getElementById("message").innerText=xhr.response;
return;
}
if(xhr.response.length>IMAGE_LIMIT_COUNT){
document.getElementById("message").innerText=`${IMAGE_LIMIT_COUNT}件越えは省略したぞう`;
}
document.getElementById("search_time").innerText=`🐘「${xhr.response.length}件 ${(new Date()-start_date)/1e3}秒でみつけたぞう」`;
image_list.innerHTML="";
for(const image of xhr.response.slice(0,IMAGE_LIMIT_COUNT)){
image_list.insertAdjacentHTML("beforeend",
`<div>
<div class="author_name">${image.author_name}</div>
<div>
<a href="${image.url}" target="_blank">
<div class="image_name">${image.name}</div>
</a>
</div>
<div>
<a href="file:///${image.path}" target="_blank">
<img src="file:///${image.path}">
</a>
</div>
<div class="tags">${image.tags.map(t=>`<button class="tag">${t}</button>`).join("")}</div>
<button data-id="${image.id}" onclick="add_tag(event)">タグ追加</button>
</div>`);
}
};
const start_date=new Date();
xhr.send();
}
function add_tag(e){
const xhr = new XMLHttpRequest();
xhr.open("POST", `http://localhost:${SERVER_PORT}/add_tag`);
xhr.onload = ()=>{
fetch_images();
};
const tag=document.getElementById("tag").value;
if(tag==""){
return;
}
xhr.send(JSON.stringify({id:e.target.dataset.id,tag:tag}));
}