// Version history for a Seafile file: see earlier versions, download one, or restore it. // Uses sf, fmtSize, openSheet, confirmSheet, toast, esc and FORM from app.js. async function fileHistory(repo, path, name, readOnly) { const base = `/api2/repos/${encodeURIComponent(repo)}/file`; const el = openSheet('Version history', name, '

Loading…

'); el.querySelector('.sheet').classList.add('wide'); try { let commits = [], next = ''; for (let page = 0; page < 3; page++) { // up to three pages of history const r = await sf.req(`${base}/history/?p=${encodeURIComponent(path)}${next ? '&commit_id=' + encodeURIComponent(next) : ''}`); commits = commits.concat(r.commits || []); next = r.next_start_commit; if (!next) break; } if (!document.body.contains(el)) return; if (!commits.length) { el.querySelector('.empty').textContent = 'No earlier versions are kept for this file.'; return; } const body = el.querySelector('.empty'); body.outerHTML = `
${commits.map((c, i) => `
${esc(new Date((c.ctime || 0) * 1000).toLocaleString([], { dateStyle: 'medium', timeStyle: 'short' }))}${i === 0 ? ' · current' : ''} ${esc(fmtSize(c.rev_file_size || 0))}${c.creator_name ? ' · ' + esc(c.creator_name) : ''} ${i === 0 ? '' : `${readOnly ? '' : ``}`}
`).join('')}
`; el.querySelector('#sx').onclick = closeSheet; el.querySelectorAll('[data-dl]').forEach(b => b.onclick = async () => { const c = commits[+b.dataset.dl]; try { window.open(await sf.req(`${base}/revision/?p=${encodeURIComponent(path)}&commit_id=${encodeURIComponent(c.id)}`), '_blank', 'noopener'); } catch (e) { toast(e.message); } }); el.querySelectorAll('[data-rs]').forEach(b => b.onclick = () => { const c = commits[+b.dataset.rs], when = new Date((c.ctime || 0) * 1000).toLocaleString([], { dateStyle: 'medium', timeStyle: 'short' }); confirmSheet('Restore this version?', name + ' goes back to how it was on ' + when + '. The current version stays in the history.', 'Restore', async () => { try { await sf.req(`${base}/revert/`, { method: 'POST', headers: FORM, body: new URLSearchParams({ p: path, commit_id: c.id }) }); toast('Restored'); if (typeof route === 'function') route(); } catch (e) { toast(e.message); } }); }); } catch (e) { const b = el.querySelector('.empty'); if (b) b.textContent = e.message; } }