summaryrefslogtreecommitdiff
path: root/player-server/web/js/lightbox.js
blob: b8cda541d3016848df5a96efeefd62884d257621 (plain)
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
let currentMediaList = [];
let currentIndex = -1;
let zoomScale = 1;
let panX = 0;
let panY = 0;
let isPanning = false;
let panStart = { x: 0, y: 0 };
let slideshowTimer = null;
let slideshowPausedUntil = 0;
let onNavigateCallback = null;

export function initLightbox({ onNavigate }) {
  onNavigateCallback = onNavigate;

  document.getElementById('lb-close')?.addEventListener('click', close);
  document.getElementById('lb-prev')?.addEventListener('click', prev);
  document.getElementById('lb-next')?.addEventListener('click', next);
  document.getElementById('lb-zoom-in')?.addEventListener('click', zoomIn);
  document.getElementById('lb-zoom-out')?.addEventListener('click', zoomOut);
  document.getElementById('lb-slideshow')?.addEventListener('click', toggleSlideshow);

  // Close on backdrop click (clicking outside the image/toolbar)
  document.getElementById('image-lightbox')?.addEventListener('click', (e) => {
    if (e.target === e.currentTarget) close();
  });

  const img = document.getElementById('lightbox-image');
  if (img) {
    img.addEventListener('mousedown', (e) => {
      if (zoomScale > 1) {
        isPanning = true;
        panStart = { x: e.clientX - panX, y: e.clientY - panY };
        img.style.cursor = 'grabbing';
        e.preventDefault();
      }
    });
    img.addEventListener('wheel', (e) => {
      e.preventDefault();
      if (e.deltaY < 0) zoomIn();
      else zoomOut();
    }, { passive: false });
  }
  window.addEventListener('mousemove', (e) => {
    if (!isPanning) return;
    panX = e.clientX - panStart.x;
    panY = e.clientY - panStart.y;
    applyTransform();
  });
  window.addEventListener('mouseup', () => {
    if (isPanning) {
      isPanning = false;
      const img = document.getElementById('lightbox-image');
      if (img) img.style.cursor = zoomScale > 1 ? 'grab' : 'zoom-in';
    }
  });
}

export function open(mediaArray, startMediaId) {
  currentMediaList = mediaArray.filter((m) => m.type === 'image');
  currentIndex = currentMediaList.findIndex((m) => m.id === startMediaId);
  if (currentIndex === -1) currentIndex = 0;
  render();
  document.getElementById('image-lightbox')?.classList.add('open');
}

export function close() {
  stopSlideshow();
  document.getElementById('image-lightbox')?.classList.remove('open');
  resetZoom();
  currentMediaList = [];
  currentIndex = -1;
}

export function isOpen() {
  return document.getElementById('image-lightbox')?.classList.contains('open');
}

export function next() {
  if (!currentMediaList.length) return;
  currentIndex = (currentIndex + 1) % currentMediaList.length;
  render();
  pauseSlideshow();
}

export function prev() {
  if (!currentMediaList.length) return;
  currentIndex = (currentIndex - 1 + currentMediaList.length) % currentMediaList.length;
  render();
  pauseSlideshow();
}

export function zoomIn() {
  setZoom(zoomScale * 1.25);
  pauseSlideshow();
}

export function zoomOut() {
  setZoom(zoomScale / 1.25);
  pauseSlideshow();
}

export function toggleSlideshow() {
  if (slideshowTimer) stopSlideshow();
  else startSlideshow();
}

export function isSlideshowActive() {
  return !!slideshowTimer;
}

function render() {
  const media = currentMediaList[currentIndex];
  const img = document.getElementById('lightbox-image');
  const meta = document.getElementById('lb-meta');
  const counter = document.getElementById('lb-counter');
  if (!media || !img) return;
  img.src = `/api/media/${media.id}/stream`;
  resetZoom();
  if (meta) {
    const size = media.file_size_bytes ? fmtSize(media.file_size_bytes) : '';
    const res = media.resolution || '';
    meta.textContent = `${media.file_name}${res ? ' — ' + res : ''}${size ? ' — ' + size : ''}`;
  }
  if (counter) {
    counter.textContent = `${currentIndex + 1} / ${currentMediaList.length}`;
  }
}

function setZoom(s) {
  zoomScale = Math.max(0.5, Math.min(s, 5));
  applyTransform();
}

function applyTransform() {
  const img = document.getElementById('lightbox-image');
  if (!img) return;
  img.style.transform = `translate(${panX}px, ${panY}px) scale(${zoomScale})`;
  img.style.cursor = zoomScale > 1 ? 'grab' : 'zoom-in';
}

function resetZoom() {
  zoomScale = 1;
  panX = 0;
  panY = 0;
  applyTransform();
}

function startSlideshow() {
  if (slideshowTimer) clearInterval(slideshowTimer);
  slideshowTimer = setInterval(() => {
    if (Date.now() < slideshowPausedUntil) return;
    if (!isOpen()) { stopSlideshow(); return; }
    next();
  }, 5000);
  const btn = document.getElementById('lb-slideshow');
  if (btn) btn.textContent = '⏸';
}

function stopSlideshow() {
  clearInterval(slideshowTimer);
  slideshowTimer = null;
  const btn = document.getElementById('lb-slideshow');
  if (btn) btn.textContent = '⏵';
}

function pauseSlideshow() {
  slideshowPausedUntil = Date.now() + 10000;
}

function fmtSize(bytes) {
  if (!bytes || bytes <= 0) return '';
  const kb = bytes / 1024;
  if (kb < 1024) return Math.round(kb) + ' KB';
  const mb = kb / 1024;
  if (mb < 1024) return Math.round(mb * 10) / 10 + ' MB';
  return Math.round((mb / 1024) * 10) / 10 + ' GB';
}