-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinterface.html
164 lines (139 loc) · 4.48 KB
/
interface.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Selfie Cam Viewer</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
background: #181818;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
grid-gap: 1em;
}
div {
position: relative
}
.image {
width: 100%;
height: auto;
}
.delete {
background: #ff3034;
border-radius: 100px;
color: #fff;
text-align: center;
line-height: 18px;
cursor: pointer;
position: absolute;
z-index: 100;
top: 10px;
right: 10px;
}
</style>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var baseHost = document.location.origin;
const WS_URL = "ws://" + window.location.host + "/ws";
const ws = new WebSocket(WS_URL);
ws.onopen = () => {
console.log(`Connected to ${WS_URL}`);
};
ws.onmessage = message => {
if (typeof message.data === "string") {
if (message.data.substr(0, 8) == "removed:") { // file removed from SPIFFS
removeSelfieFromScreen(message.data.substr(8));
} else if (message.data.substr(0, 6) == "added:") { // file added to SPIFFS
addSelfieToScreen(message.data.substr(6));
populateImgtag("img_"+message.data.substr(6)); // just add the new one not all again
} else {
const parent = document.body;
while (parent.firstChild) {
parent.firstChild.remove(); // clean the page
}
var filelistFromESP32 = message.data; // list of files from ESP32 like /img1.jpg/img2.jpg/img3.jpg
var fileIDs = filelistFromESP32.substring(1).split("/"); // remove first / and then split on subsequent /
// var fileIDs = filelistFromESP32.split("/");
fileIDs.forEach(function(item){
//if (item.substr(0, 1) != "_"){ // ignore files starting with underscore
if (item.includes("_t_")){ // thumnail images
addSelfieToScreen(item);
}
});
populateImgtags();
}
}
if (message.data instanceof Blob) {
// var urlObject = URL.createObjectURL(message.data);
// imageItem.src = urlObject;
}
};
function addSelfieToScreen(selfieID) {
const selfieList = document.querySelector("body");
let listItem = document.createElement("div");
let ahrefItem = document.createElement("a");
let imageItem = document.createElement("img");
let deleteItem = document.createElement("span");
var downloadId = selfieID.replace("_t_", "_f_"); //download file is named xxxx_f_123.jpg
ahrefItem.setAttribute('href', "/image/?id=img_"+downloadId);
ahrefItem.setAttribute("download", downloadId);
imageItem.classList.add("image");
imageItem.id = "img_"+selfieID;
imageItem.src = "";
deleteItem.classList.add("delete");
deleteItem.id = selfieID;
deleteItem.addEventListener("click", function() {
ws.send("delete:" + selfieID);
});
ahrefItem.appendChild(imageItem); // <a><img></a>
listItem.appendChild(ahrefItem); // <div><a><img></a></div>
listItem.appendChild(deleteItem).textContent = "X"; // <div><a><img></a><span></span></div>
selfieList.appendChild(listItem); // <body>...</body>
}
function removeSelfieFromScreen(imageid){
var imageItem = document.getElementById(imageid);
imageItem.parentElement.remove(); // remove parent div and contents
}
function populateImgtag(newImageId){ // Single Image
var imageItem = document.getElementById(newImageId);
console.log(imageItem);
const myHeaders = new Headers();
const myRequest = new Request('/image/?id='+newImageId, {
method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default',
});
fetch(myRequest)
.then((response) => response.blob())
.then((myBlob) => {
imageItem.src = URL.createObjectURL(myBlob);
});
}
function populateImgtags(){ // All images
var imageIDs = Array.from(document.getElementsByClassName("image"));
imageIDs.forEach(function(item){
var imageItem = [];
imageItem[item.id] = document.getElementById(item.id);
console.log(imageItem);
const myHeaders = new Headers();
const myRequest = new Request('/image/?id='+item.id, {
method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default',
});
fetch(myRequest)
.then((response) => response.blob())
.then((myBlob) => {
imageItem[item.id].src = URL.createObjectURL(myBlob);
});
});
}
});
</script>
</head>
<body>
</body>
</html>