-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.js
127 lines (106 loc) · 4.42 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
121
122
123
124
125
126
127
setTimeout(() => {
if (db) {
// video retieval
let dbVideoTransaction = db.transaction("video", "readonly");
let videoStore = dbVideoTransaction.objectStore("video");
let videoRequest = videoStore.getAll();
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 src="${url}"></video>
</div>
<div class="delete action-btn">DELETE</div>
<div class="download action-btn">DOWNLOAD</div>
`;
galleryCont.appendChild(mediaElem);
//Listners
let deleteBtn = mediaElem.querySelector(".delete");
deleteBtn.addEventListener("click", deleteListner);
let downloadBtn = mediaElem.querySelector(".download");
downloadBtn.addEventListener("click", downloadListner);
});
}
// image retiaval
let dbImageTransaction = db.transaction("image", "readonly");
let imageStore = dbImageTransaction.objectStore("image");
let imageRequest = imageStore.getAll();
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);
//Listners
let deleteBtn = mediaElem.querySelector(".delete");
deleteBtn.addEventListener("click", deleteListner);
let downloadBtn = mediaElem.querySelector(".download");
downloadBtn.addEventListener("click", downloadListner);
});
}
}
}, 100);
// UI remove, DB renmove
function deleteListner(e) {
// DB removal
let id = e.target.parentElement.getAttribute("id");
let type = id.slice(0, 3);
if (type === "vid") {
let dbVideoTransaction = db.transaction("video", "readwrite");
let videoStore = dbVideoTransaction.objectStore("video");
videoStore.delete(id);
}
else if (type === "img") {
let dbImageTransaction = db.transaction("image", "readwrite");
let imageStore = dbImageTransaction.objectStore("image");
imageStore.delete(id);
}
//UI removal
e.target.parentElement.remove();
}
function downloadListner(e) {
let id = e.target.parentElement.getAttribute("id");
let type = id.slice(0, 3);
if (type === "vid") {
let dbVideoTransaction = db.transaction("video", "readwrite");
let videoStore = dbVideoTransaction.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 dbImageTransaction = db.transaction("image", "readwrite");
let imageStore = dbImageTransaction.objectStore("image");
let imageRequest = imageStore.get(id);
imageRequest.onsuccess = (e) => {
let imageResult = imageRequest.result;
let imageURL = imageResult.url;
let a = document.createElement("a");
a.href = imageURL;
a.download = "image.jpg";
a.click();
}
}
}