-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.js
120 lines (98 loc) · 3.89 KB
/
gallery.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
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="delete action-btn">Delete</div>
<div class="download action-btn">Download</div>`;
galleryCont.appendChild(mediaElem);
// Listeners
let deleteBtn = mediaElem.querySelector(".delete");
deleteBtn.addEventListener("click", deleteListener);
let downloadBtn = mediaElem.querySelector(".download");
downloadBtn.addEventListener("click", downloadListener);
});
};
// Images 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);
let url = imageObj.url;
mediaElem.innerHTML = ` <div class="media">
<img src="${url}"></img>
</div>
<div class="delete action-btn">Delete</div>
<div class="download action-btn">Download</div>`;
galleryCont.appendChild(mediaElem);
// Listeners
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", "readwrite");
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();
};
}
}