-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery_script.js
121 lines (101 loc) · 4.3 KB
/
gallery_script.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
setTimeout(() => {
if(db){
//video retrieval
let videoDBTransaction = db.transaction("video", "readonly");
let videoStore = videoDBTransaction.objectStore("video");
let videoRequest = videoStore.getAll(); //event driven
videoRequest.onsuccess = (e) => {
let videoResult = videoRequest.result;
let galleryCont = document.querySelector(".gallery-cont");
videoResult.forEach((videoObj) => {
let mediaElem = document.createElement("div");
mediaElem.setAttribute("class", "media-cont");
mediaElem.setAttribute("id", videoObj.id);
let url = URL.createObjectURL(videoObj.blobData);
mediaElem.innerHTML = `
<div class="media">
<video autoplay loop src="${url}"></video>
</div>
<div class="download action-btn">Download</div>
<div class="delete action-btn">Delete</div>
`;
galleryCont.appendChild(mediaElem);
let deleteBtn = mediaElem.querySelector(".delete");
deleteBtn.addEventListener("click", deleteListener);
let downloadBtn = mediaElem.querySelector(".download");
downloadBtn.addEventListener("click", downloadListener);
})
}
//image retrieval
let imageDBTransaction = db.transaction("image", "readonly");
let imageStore = imageDBTransaction.objectStore("image");
let imageRequest = imageStore.getAll(); //event driven
imageRequest.onsuccess = (e) => {
let imageResult = imageRequest.result;
let galleryCont = document.querySelector(".gallery-cont");
imageResult.forEach((imageObj) => {
let mediaElem = document.createElement("div");
mediaElem.setAttribute("class", "media-cont");
mediaElem.setAttribute("id", imageObj.id);
mediaElem.innerHTML = `
<div class="media">
<img src="${imageObj.url}"></img>
</div>
<div class="download action-btn">Download</div>
<div class="delete action-btn">Delete</div>
`;
galleryCont.appendChild(mediaElem);
let deleteBtn = mediaElem.querySelector(".delete");
deleteBtn.addEventListener("click", deleteListener);
let downloadBtn = mediaElem.querySelector(".download");
downloadBtn.addEventListener("click", downloadListener);
})
}
}
}, 100);
function deleteListener(e){
//db removal
let id = e.target.parentElement.getAttribute("id");
let type = id.slice(0, 3);
if(type === "vid"){
let videoDBTransaction = db.transaction("video", "readwrite");
let videoStore = videoDBTransaction.objectStore("video");
videoStore.delete(id);
}
else if(type === "img"){
let imageDBTransaction = db.transaction("image", "readwrite");
let imageStore = imageDBTransaction.objectStore("image");
imageStore.delete(id);
}
//ui removal
e.target.parentElement.remove();
}
function downloadListener(e){
let id = e.target.parentElement.getAttribute("id");
let type = id.slice(0, 3);
if(type === "vid"){
let videoDBTransaction = db.transaction("video", "readwrite");
let videoStore = videoDBTransaction.objectStore("video");
let videoRequest = videoStore.get(id);
videoRequest.onsuccess = (e) => {
let videoResult = videoRequest.result;
let videoURL = URL.createObjectURL(videoResult.blobData);
let a = document.createElement("a");
a.href = videoURL;
a.download = "stream.mp4"
a.click();
}
}
else if(type === "img"){
let imageDBTransaction = db.transaction("image", "readonly");
let imageStore = imageDBTransaction.objectStore("image");
let imageRequest = imageStore.get(id);
imageRequest.onsuccess = (e) => {
let imageResult = imageRequest.result;
let a = document.createElement("a");
a.href = imageResult.url;
a.download = "image.jpg"
a.click();
}
}
}