-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
386 lines (331 loc) · 11.1 KB
/
script.js
File metadata and controls
386 lines (331 loc) · 11.1 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
var jsonD = [],
menu = [],
jsonD_filtered = null,
currentVideoIndex = -1,
currentVideoList = [];
var LS = localStorage;
// Manual YYYY-MM-DD to avoid any locale/timezone weirdness
var d = new Date();
var tday = d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, '0') + "-" + String(d.getDate()).padStart(2, '0');
var lc = 0;
function inIframe() {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
function dtest() {
let info = "iFrame:" + inIframe() + " userAgent:" + navigator.userAgent;
let dbg = document.getElementById("debug");
if (dbg) dbg.innerHTML = info;
}
var displayedTotal = 0;
const PAGE_SIZE = 12;
function loaddata() {
fetch("./data.json")
.then((response) => response.json())
.then((json) => {
jsonD = json;
resetAndLoad(jsonD);
});
}
function resetAndLoad(data) {
let mn = document.getElementById("main");
if (mn) mn.innerHTML = "";
displayedTotal = 0;
// Load initial batches until the scrollbar appears or data runs out
loadInitialBatches();
}
function loadInitialBatches() {
loadMore();
// If the window is still not scrollable and we have more data, load another batch
if (displayedTotal < jsonD.length && document.documentElement.scrollHeight <= window.innerHeight) {
// We use a tiny timeout to let the browser render the previous batch and update scrollHeight
setTimeout(loadInitialBatches, 50);
}
}
function loadMore() {
if (displayedTotal >= jsonD.length) return;
let end = jsonD.length - 1 - displayedTotal;
let start = Math.max(0, end - PAGE_SIZE + 1);
for (let i = end; i >= start; i--) {
let thm = "thumbs/aqara_video/" + jsonD[i].camera + "/" + jsonD[i].path + ".png";
let vid = "files/aqara_video/" + jsonD[i].camera + "/" + jsonD[i].path + ".mp4";
let caption = jsonD[i].dt;
let text = `
<div class="column is-one-quarter-widescreen is-one-third-desktop is-half-tablet">
<div class="card">
<div class="card-image">
<a href="javascript:void(0)" onclick="openVideoModal(${i})">
<figure class="image is-16by9">
<img src="${thm}" alt="Thumbnail" style="object-fit: cover;">
</figure>
</a>
</div>
<div class="card-content p-3 has-text-centered">
<p class="is-size-7 has-text-grey-light" style="letter-spacing: 0.05em;">${caption}</p>
</div>
</div>
</div>`;
addli(text);
}
displayedTotal += (end - start + 1);
navbar(); // Update navbar info if needed
}
// Scroll listener for infinite scroll
window.onscroll = function () {
// Use documentElement.scrollHeight for better desktop compatibility
const scrollHeight = document.documentElement.scrollHeight;
const scrollPos = window.innerHeight + window.scrollY;
// If we are within 800px of the bottom, load more
if (scrollPos >= scrollHeight - 800) {
loadMore();
}
};
function schoose(val) {
let a = val.split("*")[0];
let b = val.split("*")[1];
choose(a, b);
}
function navbar() {
const navline = document.getElementById("navigate");
if (!navline) return;
// Re-generate menu from full jsonD for navigation
const tempMenu = [];
let showhour = -1;
let showdate = "";
for (let i = jsonD.length - 1; i >= 0; i--) {
let da = new Date(jsonD[i].dt);
let date = da.toString().substring(0, 10);
let vdate = da.getDate();
let hour = da.getHours();
if (hour != showhour || date != showdate) {
tempMenu.push({ d: date, h: hour });
showhour = hour;
showdate = date;
}
}
const days = {};
tempMenu.forEach(item => {
if (!days[item.d]) days[item.d] = [];
if (!days[item.d].includes(item.h)) days[item.d].push(item.h);
});
const sortedDays = Object.keys(days).sort((a, b) => new Date(b).getTime() - new Date(a).getTime());
let startHtml = '';
for (const dateStr of sortedDays) {
const hours = days[dateStr].sort((a, b) => b - a);
let options = `<option disabled selected>${dateStr}</option>`;
for (const hh of hours) {
const dayNum = dateStr.split(" ")[2];
options += `<option value="${dayNum}*${hh}">${hh}:00</option>`;
}
startHtml += `
<div class="navbar-item py-1 px-1">
<div class="select is-small is-rounded">
<select onchange="schoose(this.value)">${options}</select>
</div>
</div>`;
}
const data = `
<div class="navbar-brand">
<a class="navbar-item title is-5 mb-0" href="./">Camera</a>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" onclick="this.classList.toggle('is-active'); document.getElementById('navbarMenu').classList.toggle('is-active');">
<span aria-hidden="true"></span><span aria-hidden="true"></span><span aria-hidden="true"></span>
</a>
</div>
<div id="navbarMenu" class="navbar-menu">
<div class="navbar-start" style="flex-wrap: wrap; align-items: center; justify-content: flex-start; padding: 0.5rem;">
${startHtml}
</div>
<div class="navbar-end">
<div class="navbar-item px-2">
<div class="buttons field is-grouped mb-0">
<div class="control"><a class="button is-small is-primary is-outlined" href="javascript:location.reload()">REFRESH</a></div>
</div>
</div>
</div>
</div>`;
navline.innerHTML = data;
navline.style.display = "flex";
}
function choose(d, h) {
let filterJ = jsonD.filter((obj) => {
let dt = new Date(obj.dt);
return dt.getHours() == h && dt.getDate() == d;
});
jsonD_filtered = filterJ;
let mn = document.getElementById("main");
if (mn) mn.innerHTML = "";
for (let i = filterJ.length - 1; i >= 0; i--) {
let item = filterJ[i];
let thm = "thumbs/aqara_video/" + item.camera + "/" + item.path + ".png";
let vid = "files/aqara_video/" + item.camera + "/" + item.path + ".mp4";
let text = `
<div class="column is-one-quarter-widescreen is-one-third-desktop is-half-tablet">
<div class="card">
<div class="card-image">
<a href="javascript:void(0)" onclick="openVideoModal(${i})">
<figure class="image is-16by9">
<img src="${thm}" alt="Thumbnail" style="object-fit: cover;">
</figure>
</a>
</div>
<div class="card-content p-3 has-text-centered">
<p class="is-size-7 has-text-grey-light" style="letter-spacing: 0.05em;">${item.dt}</p>
</div>
</div>
</div>`;
addli(text);
}
window.scrollTo({ top: 0, behavior: 'smooth' });
}
function addli(data) {
let mn = document.getElementById("main");
if (mn) mn.innerHTML += data;
}
// Video Modal Functions
function openVideoModal(index) {
const data = jsonD_filtered || jsonD;
currentVideoIndex = index;
currentVideoList = data;
const item = data[index];
const videoSrc = "files/aqara_video/" + item.camera + "/" + item.path + ".mp4";
const modal = document.getElementById("videoModal");
const player = document.getElementById("videoPlayer");
player.src = videoSrc;
modal.classList.add("is-active");
player.play().catch(() => {});
updateTimeDisplay();
showControls();
}
function closeVideoModal() {
const modal = document.getElementById("videoModal");
const player = document.getElementById("videoPlayer");
player.pause();
player.src = "";
modal.classList.remove("is-active");
}
function togglePlay() {
const player = document.getElementById("videoPlayer");
const icon = document.getElementById("playIcon");
if (player.paused) {
player.play();
icon.innerHTML = "❚❚";
} else {
player.pause();
icon.innerHTML = "►";
}
}
function toggleMute() {
const player = document.getElementById("videoPlayer");
const icon = document.getElementById("volumeIcon");
player.muted = !player.muted;
icon.innerHTML = player.muted ? "🔇" : "🔊";
}
function setVolume() {
const player = document.getElementById("videoPlayer");
const slider = document.getElementById("volumeSlider");
player.volume = slider.value;
}
function seekVideo() {
const player = document.getElementById("videoPlayer");
const slider = document.getElementById("progressBar");
player.currentTime = slider.value;
}
function updateTimeDisplay() {
const player = document.getElementById("videoPlayer");
const progress = document.getElementById("progressBar");
const timeDisplay = document.getElementById("timeDisplay");
const playIcon = document.getElementById("playIcon");
if (!player.src) return;
const current = formatTime(player.currentTime);
const duration = formatTime(player.duration || 0);
timeDisplay.textContent = current + " / " + duration;
if (!isNaN(player.duration)) {
progress.max = player.duration;
progress.value = player.currentTime;
}
playIcon.innerHTML = player.paused ? "►" : "❚❚";
}
function formatTime(seconds) {
if (isNaN(seconds)) return "0:00";
const m = Math.floor(seconds / 60);
const s = Math.floor(seconds % 60);
return m + ":" + s.toString().padStart(2, '0');
}
function prevVideo() {
if (currentVideoIndex < currentVideoList.length - 1) {
openVideoModal(currentVideoIndex + 1);
}
}
function nextVideo() {
if (currentVideoIndex > 0) {
openVideoModal(currentVideoIndex - 1);
}
}
let controlsTimeout;
function showControls() {
const controls = document.querySelector('.video-controls');
if (!controls) return;
controls.style.opacity = '1';
clearTimeout(controlsTimeout);
controlsTimeout = setTimeout(() => {
controls.style.opacity = '0';
}, 3000);
}
// Video player event listeners
document.addEventListener("DOMContentLoaded", function() {
const player = document.getElementById("videoPlayer");
if (player) {
player.addEventListener("timeupdate", updateTimeDisplay);
player.addEventListener("loadedmetadata", updateTimeDisplay);
player.addEventListener("ended", function() {
document.getElementById("playIcon").innerHTML = "►";
});
player.addEventListener("dblclick", function(e) {
e.preventDefault();
toggleFit();
});
}
const container = document.querySelector('.video-container');
if (container) {
container.addEventListener('mousemove', showControls);
}
// Keyboard shortcuts
document.addEventListener("keydown", function(e) {
const modal = document.getElementById("videoModal");
if (!modal.classList.contains("is-active")) return;
switch(e.key) {
case " ":
e.preventDefault();
togglePlay();
break;
case "ArrowLeft":
prevVideo();
break;
case "ArrowRight":
nextVideo();
break;
case "Escape":
closeVideoModal();
break;
case "m":
case "M":
toggleMute();
break;
case "c":
case "C":
toggleFit();
break;
}
});
});
function toggleFit() {
const container = document.querySelector('.video-container');
container.classList.toggle('video-fit-contain');
const btn = document.getElementById('fitBtn');
if (btn) {
btn.innerHTML = container.classList.contains('video-fit-contain') ? '📷' : '🖼';
}
}