55 lines
4.4 KiB
JavaScript
55 lines
4.4 KiB
JavaScript
(() => {
|
|
'use strict';
|
|
const app = document.getElementById('contacts-app'); if (!app) return;
|
|
const ja = document.documentElement.lang.startsWith('ja'), t = (en, jp) => ja ? jp : en;
|
|
const api = '/_/api/v1/extension/organizer.contacts/', editor = document.getElementById('contact-editor');
|
|
const el = (tag, text) => { const n = document.createElement(tag); if (text !== undefined) n.textContent = text; return n; };
|
|
const exportLink = el('a', t('Export address book', 'アドレス帳を書き出す')); exportLink.href = api + 'export.vcf';
|
|
document.getElementById('contacts-controls').append(exportLink);
|
|
async function request(path, options = {}) {
|
|
const r = await fetch(api + path, { credentials: 'same-origin', signal: AbortSignal.timeout(15000), ...options, headers: { 'Content-Type': 'application/json', ...options.headers } });
|
|
const data = await r.json().catch(() => ({}));
|
|
if (!r.ok || r.redirected) throw Error(r.status === 412 ? t('Changed elsewhere. Reload before restoring.', '他で変更されています。再読込してから復元してください。') : t('Unable to load/restore. Check login and permissions.', '読込・復元できません。ログインと権限を確認してください。'));
|
|
return data;
|
|
}
|
|
function attach() {
|
|
const form = editor.querySelector('form'), link = form?.querySelector('a[href$=".vcf"]');
|
|
if (!link || form.dataset.historyAttached) return; form.dataset.historyAttached = 'true';
|
|
const id = new URL(link.href).pathname.split('/').pop().slice(0, -4);
|
|
const details = el('details'); details.className = 'contact-history';
|
|
details.append(el('summary', t('Revision history', '変更履歴')));
|
|
const status = el('p'); status.role = 'status'; const rows = el('div'), preview = el('pre'); details.append(status, rows, preview); editor.append(details);
|
|
let opened = false, busy = false, version = 0;
|
|
async function load(before = '') {
|
|
if (busy) return; busy = true;
|
|
try {
|
|
if (!version) version = (await request('items/' + id)).item.version;
|
|
const data = await request('items/' + id + '/history' + (before ? '?before=' + before : ''));
|
|
if (!details.isConnected) return;
|
|
rows.querySelector('[data-more]')?.remove();
|
|
for (const revision of data.items) {
|
|
const row = el('div'); row.className = 'contact-history-row';
|
|
row.append(el('span', '#' + revision.version + ' · ' + new Date(revision.created_ms).toLocaleString() + (revision.deleted ? t(' · deleted', ' · 削除') : '')));
|
|
const view = el('button', t('Preview', '確認')); view.type = 'button';
|
|
view.onclick = async () => { try { const d = await request('items/' + id + '/history/' + revision.version); if (details.isConnected) preview.textContent = d.item.vcard; } catch (e) { status.textContent = e.message; } };
|
|
row.append(view);
|
|
if (app.dataset.writable === 'true') {
|
|
const restore = el('button', t('Restore', '復元')); restore.type = 'button';
|
|
restore.onclick = async () => {
|
|
if (busy || !confirm(t('Restore this revision as a new version? Unsaved form edits will be discarded. The contact will be active.', 'この履歴を新しい版として復元しますか?フォームの未保存変更は破棄し、連絡先を有効な状態に戻します。'))) return;
|
|
busy = true; restore.disabled = true;
|
|
try { const d = await request('items/' + id + '/restore', { method: 'POST', headers: { 'If-Match': '"' + version + '"' }, body: JSON.stringify({ version, revision: revision.version }) }); if (details.isConnected) app.dispatchEvent(new CustomEvent('contact-restored', { detail: d.item })); }
|
|
catch (e) { status.textContent = e.message; } finally { busy = false; restore.disabled = false; }
|
|
};
|
|
row.append(restore);
|
|
}
|
|
rows.append(row);
|
|
}
|
|
if (data.next) { const more = el('button', t('Earlier revisions', '以前の履歴')); more.type = 'button'; more.dataset.more = 'true'; more.onclick = () => load(data.next); rows.append(more); }
|
|
} catch (e) { status.textContent = e.message; opened = false; } finally { busy = false; }
|
|
}
|
|
details.ontoggle = () => { if (details.open && !opened) { opened = true; load(); } };
|
|
}
|
|
new MutationObserver(attach).observe(editor, { childList: true }); attach();
|
|
})();
|