refactor: global click listener and cleanup

breaking up the global click callback to make it easier in the future
to move some parts into ui modules such as settings.ts.
pull/72/head
OFF0 2 years ago
parent 52e2a31421
commit 9a34d4f31e
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -483,22 +483,6 @@ writeInput.addEventListener('input', () => {
}); });
writeInput.addEventListener('blur', () => sendStatus.textContent = ''); writeInput.addEventListener('blur', () => sendStatus.textContent = '');
document.body.onload = () => console.log('------------ pageload ------------')
// subscribe and change view // subscribe and change view
function route(path) { function route(path) {
if (path === '/') { if (path === '/') {
@ -532,62 +516,57 @@ window.addEventListener('popstate', (event) => {
const publishView = document.querySelector('#newNote'); const publishView = document.querySelector('#newNote');
document.body.addEventListener('click', (e) => { const handleLink = (e, a) => {
const a = e.target.closest('a'); if ('nav' in a.dataset) {
const pubkey = e.target.closest('[data-pubkey]')?.dataset.pubkey; e.preventDefault();
const id = e.target.closest('[data-id]')?.dataset.id; closeSettingsView();
if (a) { if (!publishView.hidden) {
if ('nav' in a.dataset) { publishView.hidden = true;
e.preventDefault();
closeSettingsView();
if (!publishView.hidden) {
publishView.hidden = true;
}
const href = a.getAttribute('href');
route(href);
history.pushState({}, null, href);
e.preventDefault();
} }
return; const href = a.getAttribute('href');
route(href);
history.pushState({}, null, href);
e.preventDefault();
} }
const button = e.target.closest('button'); };
if (button) {
switch(button.name) { const handleButton = (e, button) => {
case 'reply': const id = e.target.closest('[data-id]')?.dataset.id;
if (localStorage.getItem('reply_to') === id) { switch(button.name) {
writeInput.blur(); case 'reply':
return; if (localStorage.getItem('reply_to') === id) {
} writeInput.blur();
appendReplyForm(button.closest('.buttons')); return;
localStorage.setItem('reply_to', id); }
break; appendReplyForm(button.closest('.buttons'));
case 'star': localStorage.setItem('reply_to', id);
const note = replyList.find(r => r.id === id) || textNoteList.find(n => n.id === (id)); break;
handleUpvote(note); case 'star':
break; const note = replyList.find(r => r.id === id) || textNoteList.find(n => n.id === (id));
case 'settings': handleUpvote(note);
toggleSettingsView(); break;
break; case 'settings':
case 'new-note': toggleSettingsView();
if (publishView.hidden) { break;
localStorage.removeItem('reply_to'); // should it forget old replyto context? case 'new-note':
publishView.append(writeForm); if (publishView.hidden) {
if (writeInput.value.trimRight()) { localStorage.removeItem('reply_to'); // should it forget old replyto context?
writeInput.style.removeProperty('height'); publishView.append(writeForm);
} if (writeInput.value.trimRight()) {
requestAnimationFrame(() => { writeInput.style.removeProperty('height');
updateElemHeight(writeInput);
writeInput.focus();
});
publishView.removeAttribute('hidden');
} else {
publishView.hidden = true;
} }
break; requestAnimationFrame(() => {
case 'back': updateElemHeight(writeInput);
writeInput.focus();
});
publishView.removeAttribute('hidden');
} else {
publishView.hidden = true; publishView.hidden = true;
break; }
} break;
case 'back':
publishView.hidden = true;
break;
} }
// const container = e.target.closest('[data-append]'); // const container = e.target.closest('[data-append]');
// if (container) { // if (container) {
@ -595,6 +574,18 @@ document.body.addEventListener('click', (e) => {
// delete container.dataset.append; // delete container.dataset.append;
// return; // return;
// } // }
};
document.body.addEventListener('click', (e) => {
const a = e.target.closest('a');
if (a) {
handleLink(e, a);
return;
}
const button = e.target.closest('button');
if (button) {
handleButton(e, button);
}
}); });
// document.body.addEventListener('keyup', (e) => { // document.body.addEventListener('keyup', (e) => {

Loading…
Cancel
Save