Files
pages/static/js/bookmark-manager.js
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

90 lines
7.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
(() => {
const form = document.getElementById('bookmark-form');
if (!form) return;
const msg = form.dataset, groups = [];
for (const row of document.querySelectorAll('#bookmark-source-rows tr')) {
const item = {label:row.querySelector('[name=bookmark_label]').value, href:row.querySelector('[name=bookmark_target]').value, kind:row.querySelector('[name=bookmark_kind]').value, children:row.querySelector('[data-bookmark-children]').checked};
if (item.kind === 'section') groups.push({section:item, links:[]});
else { if (!groups.length) groups.push({section:null,links:[]}); groups.at(-1).links.push(item); }
}
const legacy = form.querySelector('.bookmark-legacy');
legacy.hidden = true; legacy.querySelectorAll('input,select').forEach(input => input.disabled = true);
const el = (tag, cls, text) => { const node = document.createElement(tag); if (cls) node.className = cls; if (text) node.textContent = text; return node; };
const editor = el('div','bookmark-card-editor'); legacy.before(editor);
const toolbar = el('div','bookmark-editor-toolbar'); editor.before(toolbar);
const status = form.querySelector('[data-bookmark-status]');
const count = () => groups.reduce((n,g) => n + g.links.length + (g.section ? 1 : 0), 0);
const room = () => { if (count() < 64) return true; status.textContent = msg.limit; return false; };
const button = (text, action, title = text) => {const node = el('button','',text); node.type='button'; node.title=title; node.setAttribute('aria-label',title); node.addEventListener('click',action); return node;};
const field = (title, value, change, required = true) => {
const label=el('label','bookmark-field'), input=el('input'); label.append(el('span','',title),input);
input.value=value; input.required=required; input.maxLength=(title===msg.label || title===msg.section) ? 200 : 4096;
input.addEventListener('input',()=>change(input.value)); return label;
};
const addLink = group => { if (!room()) return; group.links.push({kind:'path', label:'', href:'', children:false}); render(); editor.querySelectorAll('.bookmark-link-row').item([...editor.querySelectorAll('.bookmark-link-row')].length-1)?.querySelector('input')?.focus(); };
toolbar.append(button(msg.addSection,()=>{
if (!room() || groups.filter(g=>g.section).length>=16) {status.textContent=msg.limit;return;}
groups.push({section:{kind:'section',label:'',href:'',children:false},links:[]});render();editor.lastElementChild.querySelector('input').focus();
}),button(msg.addLink,()=>{if(!groups.length)groups.push({section:null,links:[]});addLink(groups.at(-1));}));
function render() {
editor.replaceChildren();
groups.forEach((group,index) => {
const card=el('fieldset','bookmark-editor-card'), header=el('div','bookmark-card-head');
if (group.section) {
header.append(field(msg.section,group.section.label,v=>group.section.label=v));
const up=button('↑',()=>{[groups[index-1],groups[index]]=[groups[index],groups[index-1]];render();},msg.up);
up.disabled=index===0 || !groups[index-1].section;
const down=button('↓',()=>{[groups[index+1],groups[index]]=[groups[index],groups[index+1]];render();},msg.down);down.disabled=index===groups.length-1;
header.append(up,down,button('×',()=>{if(!confirm(msg.confirm))return;groups.splice(index,1);render();},msg.remove));
} else header.append(el('h3','',msg.default));
card.append(header);
if (group.section) {
const options=el('div','bookmark-section-options'), toggle=el('label','bookmark-toggle'), check=el('input');check.type='checkbox';check.role='switch';check.checked=group.section.children;
toggle.append(check,el('span','',msg.children));check.addEventListener('change',()=>{group.section.children=check.checked;render();});
options.append(toggle);
if (group.section.children) options.append(field('Path',group.section.href,v=>group.section.href=v),el('p','muted',msg.auto));
card.append(options);
}
group.links.forEach((link,li) => {
const row=el('div','bookmark-link-row');
row.append(field(msg.label,link.label,v=>link.label=v));
const kind=el('select');kind.setAttribute('aria-label',msg.target);
for (const value of ['path','url']) {const option=el('option','',value==='path'?'Path':'URL');option.value=value;kind.append(option);}kind.value=link.kind;kind.addEventListener('change',()=>link.kind=kind.value);
row.append(kind,field(msg.target,link.href,v=>link.href=v));
const move=el('select');move.setAttribute('aria-label',msg.section);
groups.forEach((g,i)=>{const option=el('option','',g.section?.label||msg.default);option.value=i;move.append(option);});move.value=index;
move.addEventListener('change',()=>{group.links.splice(li,1);groups[Number(move.value)].links.push(link);render();});row.append(move);
const up=button('↑',()=>{[group.links[li-1],group.links[li]]=[group.links[li],group.links[li-1]];render();},msg.up);up.disabled=li===0;
const down=button('↓',()=>{[group.links[li+1],group.links[li]]=[group.links[li],group.links[li+1]];render();},msg.down);down.disabled=li===group.links.length-1;
row.append(up,down,button('×',()=>{group.links.splice(li,1);render();},msg.remove));card.append(row);
});
card.append(button(msg.addLink,()=>addLink(group)));editor.append(card);
});
}
render();
const dialog=document.querySelector('[data-bookmark-dialog]'), open=document.querySelector('[data-bookmark-open]');
let dirty=false;
form.addEventListener('input',()=>dirty=true);form.addEventListener('change',()=>dirty=true);
toolbar.addEventListener('click',()=>dirty=true);editor.addEventListener('click',e=>{if(e.target.closest('button'))dirty=true;});
if (dialog) {
open.addEventListener('click',()=>dialog.showModal());
dialog.querySelector('[data-bookmark-close]').addEventListener('click',()=>dialog.close());
dialog.addEventListener('click',event=>{if(event.target===dialog){const r=dialog.getBoundingClientRect();if(event.clientX<r.left||event.clientX>r.right||event.clientY<r.top||event.clientY>r.bottom)dialog.close();}});
dialog.addEventListener('close',()=>open.focus());
}
window.addEventListener('beforeunload',event=>{if(dirty){event.preventDefault();event.returnValue='';}});
form.addEventListener('submit',async event=>{
event.preventDefault();const save=form.querySelector('[type=submit]');if(save.disabled)return;
const data=new URLSearchParams(new FormData(form));
for(const group of groups)for(const item of [...(group.section?[group.section]:[]),...group.links]){
data.append('bookmark_label',item.label);data.append('bookmark_kind',item.kind);data.append('bookmark_target',item.href);data.append('bookmark_children',String(Boolean(item.children)));
}
save.disabled=true;status.textContent='';
try{
const response=await fetch(form.action,{method:'POST',body:data,headers:{Accept:'application/json'},signal:AbortSignal.timeout(15000)});
if(!response.ok)throw new Error((await response.text()).trim());await response.json();dirty=false;status.textContent=msg.saved;
if(dialog){dialog.close();location.reload();}
}catch(error){status.textContent=error.message;}finally{save.disabled=false;}
});
})();