Notiz-Medien & Sprachnachrichten: Fotos/Videos/Dateien + Audio an Notizen
Wiederverwendbarer UI.noteMediaAttacher für beide Notiz-Stellen (UI.noteModal
+ Notizblock-Seite). note_media-Tabelle + POST/DELETE /api/notes/{id}/media
(vor der gierigen /{parent_type}/{parent_id}-Route). Audio per MediaRecorder,
serverseitig nach m4a/AAC transkodiert (ffmpeg) — iOS spielt Chrome-Opus-webm
nicht ab. UI.lightbox global eingeführt. Mikrofon-Policy microphone=(self) +
CSP media-src 'self' blob:, Datenschutz v6. Disk-Cleanup für note_media bei
Notiz-, Account- und Admin-User-Delete. Reine Medien-Notiz ohne Text erlaubt.
noteModal-Bug gefixt: notes.get() liefert Array -> existing[0] statt
existing?.id (verhinderte Bearbeiten, erzeugte Duplikate). 12 neue Tests.
admin.py enthält außerdem KI-Vision-Statusfelder aus paralleler Arbeit
(nicht sauber trennbar ohne interaktives Staging).
This commit is contained in:
parent
203da50e1d
commit
e86d89f3d9
12 changed files with 947 additions and 59 deletions
|
|
@ -675,6 +675,12 @@ const API = (() => {
|
|||
delete(id) {
|
||||
return del(`/notes/${id}`);
|
||||
},
|
||||
uploadMedia(noteId, formData) {
|
||||
return upload(`/notes/${noteId}/media`, formData);
|
||||
},
|
||||
deleteMedia(noteId, mediaId) {
|
||||
return del(`/notes/${noteId}/media/${mediaId}`);
|
||||
},
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------
|
||||
|
|
|
|||
|
|
@ -305,6 +305,34 @@ window.Page_notes = (() => {
|
|||
`;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Medien-Helfer: Preview-URL ableiten + Indikator-Strip für die Karte
|
||||
// ----------------------------------------------------------
|
||||
function _notePreview(url) {
|
||||
if (!url) return url;
|
||||
const dot = url.lastIndexOf('.');
|
||||
if (dot < 0) return url;
|
||||
if (/\.(mp4|webm|mov|avi|m4v)$/i.test(url)) return url.slice(0, dot) + '_thumb.jpg';
|
||||
if (/\.(m4a|aac|mp3|ogg|oga|wav|pdf)$/i.test(url)) return url;
|
||||
return url.slice(0, dot) + '_preview.webp';
|
||||
}
|
||||
|
||||
function _noteMediaStrip(note) {
|
||||
const items = note.media_items || [];
|
||||
if (!items.length) return '';
|
||||
const n = { image: 0, video: 0, audio: 0, pdf: 0, file: 0 };
|
||||
items.forEach(m => { n[m.media_type] = (n[m.media_type] || 0) + 1; });
|
||||
const parts = [];
|
||||
if (n.image) parts.push(['image', n.image]);
|
||||
if (n.video) parts.push(['video-camera', n.video]);
|
||||
if (n.audio) parts.push(['microphone', n.audio]);
|
||||
if (n.pdf + n.file) parts.push(['paperclip', n.pdf + n.file]);
|
||||
if (!parts.length) return '';
|
||||
return `<div class="notes-media-strip" style="display:flex;align-items:center;gap:12px;margin-top:6px;font-size:12px;color:var(--c-text-secondary)">
|
||||
${parts.map(([icon, count]) => `<span style="display:inline-flex;align-items:center;gap:4px">${UI.icon(icon)} ${count}</span>`).join('')}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// Notiz-Karte HTML
|
||||
// ----------------------------------------------------------
|
||||
|
|
@ -342,7 +370,9 @@ window.Page_notes = (() => {
|
|||
</div>
|
||||
|
||||
<!-- Notiztext -->
|
||||
<p class="list-item-text notes-card-text">${UI.escape(_truncate(note.text))}</p>
|
||||
${note.text ? `<p class="list-item-text notes-card-text">${UI.escape(_truncate(note.text))}</p>` : ''}
|
||||
|
||||
${_noteMediaStrip(note)}
|
||||
|
||||
<!-- Micro-Badges -->
|
||||
${microBadges.length ? `
|
||||
|
|
@ -495,7 +525,20 @@ window.Page_notes = (() => {
|
|||
${note.parent_label
|
||||
? `<div class="text-sm-secondary"><strong>${UI.escape(note.parent_label)}</strong></div>` : ''}
|
||||
|
||||
<p class="notes-detail-text">${UI.escape(note.text || '')}</p>
|
||||
${note.text ? `<p class="notes-detail-text">${UI.escape(note.text)}</p>` : ''}
|
||||
|
||||
${(note.media_items && note.media_items.length) ? `
|
||||
<div style="display:flex;flex-direction:column;gap:8px">
|
||||
${note.media_items.map(m => {
|
||||
if (m.media_type === 'image')
|
||||
return `<img class="notes-detail-media-img" data-full="${m.url}" src="${_notePreview(m.url)}" alt="" style="width:100%;max-height:320px;object-fit:cover;border-radius:var(--radius-md);cursor:pointer">`;
|
||||
if (m.media_type === 'video')
|
||||
return `<video src="${m.url}" controls playsinline style="width:100%;max-height:320px;border-radius:var(--radius-md);background:#000"></video>`;
|
||||
if (m.media_type === 'audio')
|
||||
return `<audio controls src="${m.url}" style="width:100%"></audio>`;
|
||||
return `<a href="${m.url}" target="_blank" rel="noopener" class="btn btn-secondary" style="justify-content:flex-start;gap:8px">${UI.icon('file-text')} ${m.media_type === 'pdf' ? 'PDF öffnen' : 'Datei öffnen'}</a>`;
|
||||
}).join('')}
|
||||
</div>` : ''}
|
||||
|
||||
${microBadges.length ? `
|
||||
<div class="list-item-micro-badges">
|
||||
|
|
@ -523,6 +566,16 @@ window.Page_notes = (() => {
|
|||
UI.modal.close();
|
||||
_openEditModal(note);
|
||||
});
|
||||
|
||||
// Bild-Thumbnails → Lightbox; Preview→Original-Fallback (CSP-konform)
|
||||
const _imgItems = (note.media_items || []).filter(m => m.media_type === 'image').map(m => ({ url: m.url, type: 'image' }));
|
||||
document.querySelectorAll('.notes-detail-media-img').forEach(img => {
|
||||
img.addEventListener('error', () => { if (img.src !== img.dataset.full) img.src = img.dataset.full; }, { once: true });
|
||||
img.addEventListener('click', () => {
|
||||
const idx = _imgItems.findIndex(it => it.url === img.dataset.full);
|
||||
UI.lightbox?.show(_imgItems, Math.max(0, idx));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
|
|
@ -587,6 +640,12 @@ window.Page_notes = (() => {
|
|||
box-sizing:border-box"></textarea>
|
||||
</div>
|
||||
|
||||
<!-- Medien -->
|
||||
<div class="mb-4">
|
||||
<label style="display:block;font-size:var(--text-sm);font-weight:600;color:var(--c-text);margin-bottom:var(--space-2)">Medien</label>
|
||||
<div id="nc-media"></div>
|
||||
</div>
|
||||
|
||||
<div class="flex-gap-3">
|
||||
<button id="nc-cancel" class="btn btn-ghost flex-1">Abbrechen</button>
|
||||
<button id="nc-save" class="btn btn-primary flex-1">Speichern</button>
|
||||
|
|
@ -597,40 +656,52 @@ window.Page_notes = (() => {
|
|||
overlay.innerHTML = _buildContent();
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
const _rebind = () => {
|
||||
overlay.querySelectorAll('.nc-cat').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
_selType = btn.dataset.type;
|
||||
overlay.innerHTML = _buildContent();
|
||||
_rebind();
|
||||
overlay.querySelector('#nc-text')?.focus();
|
||||
const _media = UI.noteMediaAttacher({ containerId: 'nc-media' });
|
||||
const _remove = () => { _media.destroy(); overlay.remove(); };
|
||||
|
||||
// Kategorie-Wechsel: nur Auswahl + Button-Styles aktualisieren — KEIN
|
||||
// innerHTML-Rebuild, sonst gingen eingegebener Text & angehängte Medien
|
||||
// (und eine laufende Sprachaufnahme) verloren.
|
||||
overlay.querySelectorAll('.nc-cat').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
_selType = btn.dataset.type;
|
||||
overlay.querySelectorAll('.nc-cat').forEach(b => {
|
||||
const r = _rubrik(b.dataset.type);
|
||||
const active = b.dataset.type === _selType;
|
||||
b.style.borderColor = active ? r.color : 'var(--c-border)';
|
||||
b.style.background = active ? r.color + '22' : 'var(--c-surface-2)';
|
||||
b.style.color = active ? r.color : 'var(--c-text-secondary)';
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
overlay.querySelector('#nc-cancel')?.addEventListener('click', () => overlay.remove());
|
||||
overlay.addEventListener('click', e => { if (e.target === overlay) overlay.remove(); });
|
||||
overlay.querySelector('#nc-cancel')?.addEventListener('click', _remove);
|
||||
overlay.addEventListener('click', e => { if (e.target === overlay) _remove(); });
|
||||
|
||||
overlay.querySelector('#nc-save')?.addEventListener('click', async () => {
|
||||
const text = overlay.querySelector('#nc-text')?.value?.trim();
|
||||
if (!text) { UI.toast.warning('Bitte einen Text eingeben.'); return; }
|
||||
const btn = overlay.querySelector('#nc-save');
|
||||
await UI.asyncButton(btn, async () => {
|
||||
const rb = _rubrik(_selType);
|
||||
await API.notes.create(_selType, 'standalone', {
|
||||
text,
|
||||
parent_label: rb.label,
|
||||
});
|
||||
overlay.remove();
|
||||
_filterType = _selType;
|
||||
await _reload();
|
||||
UI.toast.success('Notiz gespeichert.');
|
||||
overlay.querySelector('#nc-save')?.addEventListener('click', async () => {
|
||||
const text = overlay.querySelector('#nc-text')?.value?.trim();
|
||||
if (!text && !_media.hasPending()) {
|
||||
UI.toast.warning('Bitte einen Text eingeben oder ein Medium anhängen.');
|
||||
return;
|
||||
}
|
||||
const btn = overlay.querySelector('#nc-save');
|
||||
await UI.asyncButton(btn, async () => {
|
||||
const rb = _rubrik(_selType);
|
||||
const created = await API.notes.create(_selType, 'standalone', {
|
||||
text: text || '',
|
||||
parent_label: rb.label,
|
||||
});
|
||||
if (created?.id && _media.hasPending()) {
|
||||
await _media.uploadAll(created.id, (d, t) => { btn.textContent = `${d}/${t} hochgeladen…`; });
|
||||
}
|
||||
_remove();
|
||||
_filterType = _selType;
|
||||
await _reload();
|
||||
UI.toast.success('Notiz gespeichert.');
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(() => overlay.querySelector('#nc-text')?.focus(), 100);
|
||||
};
|
||||
|
||||
_rebind();
|
||||
setTimeout(() => overlay.querySelector('#nc-text')?.focus(), 100);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
|
|
@ -732,6 +803,13 @@ window.Page_notes = (() => {
|
|||
</div>
|
||||
` : ''}
|
||||
|
||||
<!-- Medien -->
|
||||
<div>
|
||||
<label style="display:block;font-size:var(--text-sm);font-weight:var(--weight-semibold);
|
||||
color:var(--c-text);margin-bottom:var(--space-2)">Medien</label>
|
||||
<div id="notes-edit-media"></div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Buttons -->
|
||||
|
|
@ -760,6 +838,12 @@ window.Page_notes = (() => {
|
|||
|
||||
document.body.appendChild(overlay);
|
||||
|
||||
const _media = UI.noteMediaAttacher({
|
||||
containerId: 'notes-edit-media',
|
||||
noteId: note.id,
|
||||
existingMedia: note.media_items || [],
|
||||
});
|
||||
|
||||
let selErfolgsquote = meta.erfolgsquote || null;
|
||||
let selUmgebung = meta.umgebung || null;
|
||||
let selStimmung = meta.hund_stimmung || null;
|
||||
|
|
@ -796,14 +880,17 @@ window.Page_notes = (() => {
|
|||
});
|
||||
});
|
||||
|
||||
function _close() { overlay.remove(); }
|
||||
function _close() { _media.destroy(); overlay.remove(); }
|
||||
overlay.addEventListener('click', e => { if (e.target === overlay) _close(); });
|
||||
overlay.querySelector('#notes-edit-cancel').addEventListener('click', _close);
|
||||
|
||||
// Speichern
|
||||
overlay.querySelector('#notes-edit-save').addEventListener('click', async () => {
|
||||
const text = overlay.querySelector('#notes-edit-text').value.trim();
|
||||
if (!text) { UI.toast.warning('Notiz darf nicht leer sein.'); return; }
|
||||
if (!text && !_media.hasPending() && !(note.media_items || []).length) {
|
||||
UI.toast.warning('Bitte einen Text eingeben oder ein Medium anhängen.');
|
||||
return;
|
||||
}
|
||||
|
||||
const saveBtn = overlay.querySelector('#notes-edit-save');
|
||||
saveBtn.disabled = true;
|
||||
|
|
@ -819,6 +906,10 @@ window.Page_notes = (() => {
|
|||
text,
|
||||
meta_json: Object.keys(metaObj).length > 0 ? metaObj : null,
|
||||
});
|
||||
if (_media.hasPending()) {
|
||||
const { uploaded } = await _media.uploadAll(note.id, (d, t) => { saveBtn.textContent = `${d}/${t} hochgeladen…`; });
|
||||
updated.media_items = (updated.media_items || []).concat(uploaded);
|
||||
}
|
||||
const idx = _notes.findIndex(n => n.id === note.id);
|
||||
if (idx >= 0) _notes[idx] = updated;
|
||||
_render();
|
||||
|
|
|
|||
|
|
@ -1706,6 +1706,270 @@ const UI = (() => {
|
|||
});
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// NOTE-MEDIA-ATTACHER — wiederverwendbarer Medien-Anhang für Notizen.
|
||||
// Buttons (Mediathek/Aufnehmen/Datei/Sprachnachricht), Liste anhängiger
|
||||
// Dateien, Sprachaufnahme (MediaRecorder) und bereits gespeicherte Medien
|
||||
// (mit Löschen). Genutzt von noteModal (ui.js) UND der Notizblock-Seite
|
||||
// (pages/notes.js) — eine Quelle statt Duplikat.
|
||||
// const m = UI.noteMediaAttacher({ containerId, noteId, existingMedia });
|
||||
// …im Submit nach create/update: await m.uploadAll(noteId, onProgress);
|
||||
// ----------------------------------------------------------
|
||||
function noteMediaAttacher({ containerId, noteId = null, existingMedia = [] } = {}) {
|
||||
const container = document.getElementById(containerId);
|
||||
const _noop = { uploadAll: async () => ({ uploaded: [], failed: 0 }), hasPending: () => false, destroy: () => {} };
|
||||
if (!container) return _noop;
|
||||
|
||||
const p = containerId.replace(/[^a-z0-9]/gi, '-');
|
||||
const ids = {
|
||||
bar: `${p}-bar`, pending: `${p}-pending`, existing: `${p}-existing`,
|
||||
rec: `${p}-rec`, recTimer: `${p}-rec-timer`, recStop: `${p}-rec-stop`,
|
||||
recCancel: `${p}-rec-cancel`, mic: `${p}-mic`,
|
||||
};
|
||||
|
||||
let _noteId = noteId;
|
||||
const _pending = []; // [{ file, url }]
|
||||
let _existing = (existingMedia || []).slice();
|
||||
const RECORDER_OK = !!(window.MediaRecorder && navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
|
||||
let _stream = null, _rec = null, _chunks = [], _tick = null, _recStart = 0, _recMax = null, _recCancelled = false;
|
||||
|
||||
const BTN = 'display:inline-flex;align-items:center;gap:6px;font-size:var(--text-xs);font-weight:600;' +
|
||||
'padding:7px 12px;border-radius:var(--radius-full);border:1.5px solid var(--c-border);' +
|
||||
'background:var(--c-surface-2);color:var(--c-text-secondary);cursor:pointer';
|
||||
const DEL = 'flex-shrink:0;width:26px;height:26px;border-radius:50%;border:none;background:rgba(0,0,0,.08);' +
|
||||
'color:var(--c-text-secondary);cursor:pointer;display:flex;align-items:center;justify-content:center;padding:0;font-size:14px;line-height:1';
|
||||
const ROW = 'display:flex;align-items:center;gap:10px;padding:6px;border:1px solid var(--c-border);border-radius:var(--radius-md)';
|
||||
|
||||
container.innerHTML = `
|
||||
<div id="${ids.existing}"></div>
|
||||
<div id="${ids.bar}" style="display:flex;flex-wrap:wrap;gap:8px;margin:6px 0">
|
||||
<button type="button" id="${p}-gallery" style="${BTN}">${_svgIcon('paperclip')} Foto / Video / Datei</button>
|
||||
${RECORDER_OK ? `<button type="button" id="${ids.mic}" style="${BTN}">${_svgIcon('microphone')} Sprachnachricht</button>` : ''}
|
||||
</div>
|
||||
<div id="${ids.rec}" style="display:none;align-items:center;gap:10px;margin:6px 0;padding:8px 12px;border-radius:var(--radius-md);background:var(--c-danger-subtle,rgba(220,53,53,.12))">
|
||||
<span style="width:10px;height:10px;border-radius:50%;background:var(--c-danger,#dc3535);flex-shrink:0"></span>
|
||||
<span id="${ids.recTimer}" style="font-variant-numeric:tabular-nums;font-weight:600;color:var(--c-text)">0:00</span>
|
||||
<span style="flex:1"></span>
|
||||
<button type="button" id="${ids.recCancel}" style="${BTN}">Verwerfen</button>
|
||||
<button type="button" id="${ids.recStop}" style="${BTN};border-color:var(--c-danger,#dc3535);color:var(--c-danger,#dc3535)">${_svgIcon('stop')} Stop</button>
|
||||
</div>
|
||||
<div id="${ids.pending}" style="display:flex;flex-direction:column;gap:6px;margin-top:6px"></div>
|
||||
`;
|
||||
|
||||
// ---- Datei-Picker (nativer Browser-Dialog) ----
|
||||
function _openPicker(opts = {}) {
|
||||
const tmp = document.createElement('input');
|
||||
tmp.type = 'file';
|
||||
tmp.multiple = true;
|
||||
tmp.accept = 'image/*,video/*';
|
||||
tmp.style.display = 'none';
|
||||
if (opts.capture) tmp.setAttribute('capture', opts.capture);
|
||||
if (opts.noAccept) tmp.removeAttribute('accept');
|
||||
tmp.addEventListener('change', () => { if (tmp.files.length) _addFiles(tmp.files); tmp.remove(); });
|
||||
document.body.appendChild(tmp);
|
||||
tmp.click();
|
||||
}
|
||||
|
||||
function _addFiles(list) {
|
||||
for (const f of list) _pending.push({ file: f, url: URL.createObjectURL(f) });
|
||||
_renderPending();
|
||||
}
|
||||
|
||||
function _renderPending() {
|
||||
const grid = document.getElementById(ids.pending);
|
||||
if (!grid) return;
|
||||
grid.innerHTML = _pending.map((it, i) => {
|
||||
const f = it.file, t = f.type || '';
|
||||
let preview, label = '';
|
||||
if (t.startsWith('image/')) {
|
||||
preview = `<img src="${it.url}" alt="" style="width:48px;height:48px;border-radius:6px;object-fit:cover;flex-shrink:0">`;
|
||||
label = `<span style="flex:1;min-width:0;font-size:var(--text-xs);color:var(--c-text-secondary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap">${escape(f.name || 'Bild')}</span>`;
|
||||
} else if (t.startsWith('video/')) {
|
||||
preview = `<video src="${it.url}" style="width:48px;height:48px;border-radius:6px;object-fit:cover;flex-shrink:0" muted playsinline></video>`;
|
||||
label = `<span style="flex:1;min-width:0;font-size:var(--text-xs);color:var(--c-text-secondary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap">${escape(f.name || 'Video')}</span>`;
|
||||
} else if (t.startsWith('audio/')) {
|
||||
preview = `<audio controls src="${it.url}" style="height:36px;flex:1;min-width:0"></audio>`;
|
||||
} else {
|
||||
preview = `<svg class="ph-icon" style="width:30px;height:30px;color:var(--c-text-secondary);flex-shrink:0" aria-hidden="true"><use href="/icons/phosphor.svg#file-text"></use></svg>`;
|
||||
label = `<span style="flex:1;min-width:0;font-size:var(--text-xs);color:var(--c-text-secondary);overflow:hidden;text-overflow:ellipsis;white-space:nowrap">${escape(f.name || 'Datei')}</span>`;
|
||||
}
|
||||
return `<div style="${ROW}" data-pidx="${i}">${preview}${label}<button type="button" data-pidx="${i}" aria-label="Entfernen" style="${DEL}">✕</button></div>`;
|
||||
}).join('');
|
||||
grid.querySelectorAll('button[data-pidx]').forEach(btn => {
|
||||
btn.addEventListener('click', () => {
|
||||
const i = parseInt(btn.dataset.pidx, 10);
|
||||
if (_pending[i]) { try { URL.revokeObjectURL(_pending[i].url); } catch (_) {} _pending.splice(i, 1); }
|
||||
_renderPending();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function _previewOf(url) {
|
||||
if (!url) return url;
|
||||
if (/\.(mp4|webm|mov|avi|m4a|aac|mp3|ogg|oga|wav|pdf)$/i.test(url)) return url;
|
||||
const dot = url.lastIndexOf('.');
|
||||
return dot > 0 ? url.slice(0, dot) + '_preview.webp' : url;
|
||||
}
|
||||
|
||||
function _renderExisting() {
|
||||
const wrap = document.getElementById(ids.existing);
|
||||
if (!wrap) return;
|
||||
if (!_existing.length) { wrap.innerHTML = ''; return; }
|
||||
wrap.innerHTML = `<div style="display:flex;flex-direction:column;gap:6px;margin-bottom:4px">${_existing.map(m => {
|
||||
let preview, label = '';
|
||||
if (m.media_type === 'image') {
|
||||
preview = `<img src="${_previewOf(m.url)}" data-full="${m.url}" alt="" style="width:48px;height:48px;border-radius:6px;object-fit:cover;flex-shrink:0;cursor:pointer" data-full-open="${m.url}">`;
|
||||
} else if (m.media_type === 'video') {
|
||||
preview = `<video src="${m.url}" controls playsinline style="height:48px;border-radius:6px;flex:1;min-width:0"></video>`;
|
||||
} else if (m.media_type === 'audio') {
|
||||
preview = `<audio controls src="${m.url}" style="height:36px;flex:1;min-width:0"></audio>`;
|
||||
} else {
|
||||
preview = `<a href="${m.url}" target="_blank" rel="noopener" style="display:flex;align-items:center;gap:8px;flex:1;min-width:0;color:var(--c-text-secondary);text-decoration:none"><svg class="ph-icon" style="width:28px;height:28px;flex-shrink:0" aria-hidden="true"><use href="/icons/phosphor.svg#file-text"></use></svg><span style="font-size:var(--text-xs);overflow:hidden;text-overflow:ellipsis;white-space:nowrap">${escape((m.media_type === 'pdf' ? 'PDF' : 'Datei'))}</span></a>`;
|
||||
}
|
||||
return `<div style="${ROW}" data-mid="${m.id}">${preview}${label}<button type="button" data-mid="${m.id}" aria-label="Löschen" style="${DEL}">✕</button></div>`;
|
||||
}).join('')}</div>`;
|
||||
// CSP-konformer Preview→Original-Fallback
|
||||
wrap.querySelectorAll('img[data-full]').forEach(img => {
|
||||
img.addEventListener('error', () => { if (img.src !== img.dataset.full) img.src = img.dataset.full; }, { once: true });
|
||||
});
|
||||
wrap.querySelectorAll('img[data-full-open]').forEach(img => {
|
||||
img.addEventListener('click', () => {
|
||||
const imgs = _existing.filter(m => m.media_type === 'image').map(m => ({ url: m.url, type: 'image' }));
|
||||
const idx = imgs.findIndex(it => it.url === img.dataset.fullOpen);
|
||||
if (UI.lightbox) UI.lightbox.show(imgs, Math.max(0, idx));
|
||||
});
|
||||
});
|
||||
wrap.querySelectorAll('button[data-mid]').forEach(btn => {
|
||||
btn.addEventListener('click', async () => {
|
||||
const mid = parseInt(btn.dataset.mid, 10);
|
||||
if (_noteId == null) { _existing = _existing.filter(m => m.id !== mid); _renderExisting(); return; }
|
||||
btn.disabled = true;
|
||||
try {
|
||||
await API.notes.deleteMedia(_noteId, mid);
|
||||
} catch (e) {
|
||||
if (e?.status !== 404) { btn.disabled = false; toast.error(e.message || 'Löschen fehlgeschlagen.'); return; }
|
||||
}
|
||||
_existing = _existing.filter(m => m.id !== mid);
|
||||
_renderExisting();
|
||||
toast.success('Medium entfernt.');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ---- Sprachaufnahme ----
|
||||
function _stopTracks() { if (_stream) { _stream.getTracks().forEach(t => t.stop()); _stream = null; } }
|
||||
|
||||
function _setRecState(on) {
|
||||
const rec = document.getElementById(ids.rec);
|
||||
const bar = document.getElementById(ids.bar);
|
||||
if (rec) rec.style.display = on ? 'flex' : 'none';
|
||||
if (bar) { bar.style.opacity = on ? '.4' : ''; bar.querySelectorAll('button').forEach(b => b.disabled = on); }
|
||||
}
|
||||
|
||||
function _updateTimer() {
|
||||
const el = document.getElementById(ids.recTimer);
|
||||
if (!el) return;
|
||||
const s = Math.floor((Date.now() - _recStart) / 1000);
|
||||
el.textContent = `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
async function _startRecording() {
|
||||
try {
|
||||
_stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
} catch (_) {
|
||||
toast.error('Kein Mikrofon-Zugriff. Bitte in den Geräte-Einstellungen erlauben.');
|
||||
return;
|
||||
}
|
||||
const cands = ['audio/mp4', 'audio/webm;codecs=opus', 'audio/webm', 'audio/ogg;codecs=opus'];
|
||||
const mime = cands.find(t => { try { return MediaRecorder.isTypeSupported(t); } catch (_) { return false; } }) || '';
|
||||
try { _rec = mime ? new MediaRecorder(_stream, { mimeType: mime }) : new MediaRecorder(_stream); }
|
||||
catch (_) { _rec = new MediaRecorder(_stream); }
|
||||
_chunks = [];
|
||||
_recCancelled = false;
|
||||
_rec.addEventListener('dataavailable', e => { if (e.data && e.data.size) _chunks.push(e.data); });
|
||||
_rec.addEventListener('stop', () => {
|
||||
_stopTracks();
|
||||
if (_tick) { clearInterval(_tick); _tick = null; }
|
||||
if (_recMax) { clearTimeout(_recMax); _recMax = null; }
|
||||
_setRecState(false);
|
||||
if (_recCancelled) { _chunks = []; return; }
|
||||
const type = _rec.mimeType || mime || 'audio/webm';
|
||||
const ext = type.includes('mp4') ? '.m4a' : type.includes('ogg') ? '.ogg' : '.webm';
|
||||
const blob = new Blob(_chunks, { type });
|
||||
_chunks = [];
|
||||
if (blob.size > 0) _addFiles([new File([blob], `sprachnachricht${ext}`, { type })]);
|
||||
});
|
||||
_rec.start();
|
||||
_recStart = Date.now();
|
||||
_setRecState(true);
|
||||
_updateTimer();
|
||||
_tick = setInterval(_updateTimer, 250);
|
||||
_recMax = setTimeout(() => { if (_rec && _rec.state === 'recording') { _recCancelled = false; try { _rec.stop(); } catch (_) {} } }, 5 * 60 * 1000);
|
||||
}
|
||||
|
||||
function _stopRecording(cancel) {
|
||||
_recCancelled = !!cancel;
|
||||
if (_rec && _rec.state !== 'inactive') { try { _rec.stop(); } catch (_) {} }
|
||||
else { _stopTracks(); _setRecState(false); }
|
||||
}
|
||||
|
||||
// ---- Event-Bindung ----
|
||||
// Ein Button ohne accept: iOS zeigt im Aktionsblatt von sich aus Mediathek,
|
||||
// Kamera UND Datei-Auswahl — separate Buttons dafür sind überflüssig.
|
||||
document.getElementById(`${p}-gallery`)?.addEventListener('click', () => _openPicker({ noAccept: true }));
|
||||
if (RECORDER_OK) document.getElementById(ids.mic)?.addEventListener('click', _startRecording);
|
||||
document.getElementById(ids.recStop)?.addEventListener('click', () => _stopRecording(false));
|
||||
document.getElementById(ids.recCancel)?.addEventListener('click', () => _stopRecording(true));
|
||||
|
||||
_renderExisting();
|
||||
_renderPending();
|
||||
|
||||
// ---- Öffentliche API ----
|
||||
async function uploadAll(noteIdArg, onProgress) {
|
||||
if (noteIdArg != null) _noteId = noteIdArg;
|
||||
if (!_pending.length) return { uploaded: [], failed: 0 };
|
||||
const total = _pending.length;
|
||||
let done = 0;
|
||||
onProgress?.(0, total);
|
||||
const results = await Promise.all(_pending.map(async (it) => {
|
||||
try {
|
||||
const toUpload = await API.compressImage(it.file); // nur Bilder werden komprimiert
|
||||
const fd = new FormData();
|
||||
fd.append('file', toUpload);
|
||||
const m = await API.notes.uploadMedia(_noteId, fd);
|
||||
onProgress?.(++done, total);
|
||||
return { ok: true, m };
|
||||
} catch (_) {
|
||||
onProgress?.(++done, total);
|
||||
return { ok: false };
|
||||
}
|
||||
}));
|
||||
const uploaded = results.filter(r => r.ok).map(r => r.m);
|
||||
const failed = results.filter(r => !r.ok).length;
|
||||
if (failed) toast.warning(`${failed} Medi${failed > 1 ? 'en' : 'um'} konnte${failed > 1 ? 'n' : ''} nicht hochgeladen werden.`);
|
||||
_pending.forEach(it => { try { URL.revokeObjectURL(it.url); } catch (_) {} });
|
||||
_pending.length = 0;
|
||||
_renderPending();
|
||||
_existing = _existing.concat(uploaded);
|
||||
return { uploaded, failed };
|
||||
}
|
||||
|
||||
function destroy() {
|
||||
_stopRecording(true);
|
||||
_stopTracks();
|
||||
if (_tick) { clearInterval(_tick); _tick = null; }
|
||||
if (_recMax) { clearTimeout(_recMax); _recMax = null; }
|
||||
_pending.forEach(it => { try { URL.revokeObjectURL(it.url); } catch (_) {} });
|
||||
_pending.length = 0;
|
||||
}
|
||||
|
||||
return {
|
||||
uploadAll,
|
||||
hasPending: () => _pending.length > 0 || (_rec && _rec.state === 'recording'),
|
||||
destroy,
|
||||
};
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// NOTE-MODAL — Notiz zu einem beliebigen Objekt (parentType/parentId)
|
||||
// erstellen/bearbeiten. Zentral, damit nicht jede Seite eine eigene Kopie hat.
|
||||
|
|
@ -1735,6 +1999,7 @@ const UI = (() => {
|
|||
placeholder="Notiz eingeben…"
|
||||
style="width:100%;resize:vertical"></textarea>
|
||||
</form>
|
||||
<div id="by-note-media" style="margin-top:var(--space-3)"></div>
|
||||
</div>
|
||||
<div style="padding:var(--space-3) var(--space-5);border-top:1px solid var(--c-border);
|
||||
display:flex;gap:var(--space-2);flex-shrink:0">
|
||||
|
|
@ -1752,17 +2017,27 @@ const UI = (() => {
|
|||
const closeBtn = document.getElementById('by-note-close');
|
||||
|
||||
let existingNoteId = null;
|
||||
let existingNote = null;
|
||||
try {
|
||||
const existing = await API.notes.get(parentType, parentId);
|
||||
if (existing?.id) {
|
||||
existingNoteId = existing.id;
|
||||
textarea.value = existing.text || '';
|
||||
const res = await API.notes.get(parentType, parentId);
|
||||
// GET /notes/{type}/{id} liefert ein Array (neueste zuerst) — die jüngste
|
||||
// Notiz bearbeiten statt bei jedem Öffnen eine neue anzulegen (Duplikate).
|
||||
existingNote = Array.isArray(res) ? res[0] : res;
|
||||
if (existingNote?.id) {
|
||||
existingNoteId = existingNote.id;
|
||||
textarea.value = existingNote.text || '';
|
||||
}
|
||||
} catch (_) { /* keine Notiz vorhanden — ok */ }
|
||||
|
||||
const _media = noteMediaAttacher({
|
||||
containerId: 'by-note-media',
|
||||
noteId: existingNoteId,
|
||||
existingMedia: existingNote?.media_items || [],
|
||||
});
|
||||
|
||||
setTimeout(() => textarea.focus(), 100);
|
||||
|
||||
const _close = () => overlay.remove();
|
||||
const _close = () => { _media.destroy(); overlay.remove(); };
|
||||
closeBtn.addEventListener('click', _close);
|
||||
cancelBtn.addEventListener('click', _close);
|
||||
overlay.addEventListener('click', e => { if (e.target === overlay) _close(); });
|
||||
|
|
@ -1770,13 +2045,24 @@ const UI = (() => {
|
|||
document.getElementById('by-note-form').addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
const text = textarea.value.trim();
|
||||
// Reine Medien-Notiz (nur Foto/Sprachnachricht) ist erlaubt — nur ganz
|
||||
// leer (kein Text, keine Medien) verhindern.
|
||||
if (!text && !_media.hasPending() && !existingNoteId) {
|
||||
toast.warning('Bitte einen Text eingeben oder ein Medium anhängen.');
|
||||
return;
|
||||
}
|
||||
setLoading(saveBtn, true);
|
||||
try {
|
||||
const payload = { text, parent_label: parentLabel, location_name: locationName || null, client_time: API.clientNow() };
|
||||
if (existingNoteId) {
|
||||
await API.notes.update(existingNoteId, payload);
|
||||
let noteId = existingNoteId;
|
||||
if (noteId) {
|
||||
await API.notes.update(noteId, payload);
|
||||
} else {
|
||||
await API.notes.create(parentType, parentId, payload);
|
||||
const created = await API.notes.create(parentType, parentId, payload);
|
||||
noteId = created?.id;
|
||||
}
|
||||
if (noteId && _media.hasPending()) {
|
||||
await _media.uploadAll(noteId, (d, t) => { saveBtn.textContent = `${d}/${t} hochgeladen…`; });
|
||||
}
|
||||
toast.success('Notiz gespeichert.');
|
||||
_close();
|
||||
|
|
@ -1787,10 +2073,62 @@ const UI = (() => {
|
|||
});
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------
|
||||
// LIGHTBOX — Vollbild-Viewer für Bilder/Videos mit Vor/Zurück.
|
||||
// items: [{ url, type }] (type 'image'|'video', Default 'image') oder
|
||||
// reine URL-Strings. Global, damit Notizen, Tagebuch & Co. EINEN Viewer
|
||||
// teilen (app.js erwartet UI.lightbox.show bereits).
|
||||
// ----------------------------------------------------------
|
||||
const lightbox = (() => {
|
||||
function show(items, startIdx = 0) {
|
||||
const list = (Array.isArray(items) ? items : [items])
|
||||
.map(it => (typeof it === 'string' ? { url: it } : it))
|
||||
.filter(it => it && it.url);
|
||||
if (!list.length) return;
|
||||
let idx = Math.min(Math.max(0, startIdx | 0), list.length - 1);
|
||||
|
||||
const lb = document.createElement('div');
|
||||
lb.id = 'by-lightbox';
|
||||
lb.style.cssText = 'position:fixed;inset:0;z-index:3000;background:#000;display:flex;flex-direction:column';
|
||||
|
||||
const render = () => {
|
||||
const m = list[idx];
|
||||
const media = (m.type === 'video')
|
||||
? `<video src="${escape(m.url)}" controls autoplay playsinline style="max-width:100%;max-height:100%;display:block"></video>`
|
||||
: `<img src="${escape(m.url)}" alt="" style="max-width:100%;max-height:100%;object-fit:contain;display:block">`;
|
||||
lb.innerHTML = `
|
||||
<div style="flex:1;display:flex;align-items:center;justify-content:center;overflow:hidden;position:relative">${media}</div>
|
||||
<div style="display:grid;grid-template-columns:1fr auto 1fr;align-items:center;padding-top:10px;
|
||||
padding-bottom:calc(env(safe-area-inset-bottom,0px) + 12px);
|
||||
padding-left:calc(env(safe-area-inset-left,0px) + 16px);
|
||||
padding-right:calc(env(safe-area-inset-right,0px) + 16px);
|
||||
flex-shrink:0;background:rgba(0,0,0,.5);gap:8px">
|
||||
<button id="by-lb-close" style="background:rgba(255,255,255,.15);border:none;border-radius:24px;height:48px;
|
||||
color:#fff;cursor:pointer;display:flex;align-items:center;justify-content:center;gap:6px;padding:0 16px;
|
||||
font-size:15px;font-weight:500">${_svgIcon('arrow-left')} Schließen</button>
|
||||
<span style="color:rgba(255,255,255,.7);font-size:14px;text-align:center;white-space:nowrap">${list.length > 1 ? `${idx + 1} / ${list.length}` : ''}</span>
|
||||
${list.length > 1 ? `
|
||||
<div style="display:flex;gap:8px;justify-content:flex-end">
|
||||
<button id="by-lb-prev" style="background:rgba(255,255,255,.15);border:none;border-radius:50%;width:48px;height:48px;
|
||||
color:#fff;font-size:24px;cursor:pointer;display:flex;align-items:center;justify-content:center${idx === 0 ? ';opacity:.3;pointer-events:none' : ''}">‹</button>
|
||||
<button id="by-lb-next" style="background:rgba(255,255,255,.15);border:none;border-radius:50%;width:48px;height:48px;
|
||||
color:#fff;font-size:24px;cursor:pointer;display:flex;align-items:center;justify-content:center${idx === list.length - 1 ? ';opacity:.3;pointer-events:none' : ''}">›</button>
|
||||
</div>` : '<div></div>'}
|
||||
</div>`;
|
||||
lb.querySelector('#by-lb-close').addEventListener('click', () => lb.remove());
|
||||
lb.querySelector('#by-lb-prev')?.addEventListener('click', () => { if (idx > 0) { idx--; render(); } });
|
||||
lb.querySelector('#by-lb-next')?.addEventListener('click', () => { if (idx < list.length - 1) { idx++; render(); } });
|
||||
};
|
||||
render();
|
||||
document.body.appendChild(lb);
|
||||
}
|
||||
return { show };
|
||||
})();
|
||||
|
||||
// Öffentliche API
|
||||
return {
|
||||
toast, modal,
|
||||
noteModal,
|
||||
noteModal, noteMediaAttacher, lightbox,
|
||||
setLoading, asyncButton,
|
||||
formData, setFormError, clearFormErrors,
|
||||
emptyState, errorState, time, text, money,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue