Moved dialog handling into a JS class for some state management.
Translation selection requires localStorage support for state to be remembered between sessions.
This commit is contained in:
parent
fb03cb3872
commit
639dba4520
1 changed files with 188 additions and 3 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { aliases, meta, translations } from './meta.js';
|
||||
|
||||
import { hasLocalStorage } from './utils.js';
|
||||
|
||||
export const modal = (el) => {
|
||||
|
||||
|
|
@ -151,10 +151,195 @@ function render(dialog, response, refs) {
|
|||
article.insertAdjacentElement('beforeend', section);
|
||||
}
|
||||
|
||||
class RenderScripture {
|
||||
translation = 'BSB';
|
||||
dialog = null;
|
||||
ls = false;
|
||||
|
||||
constructor(dialog) {
|
||||
this.dialog = dialog;
|
||||
|
||||
if (hasLocalStorage()) {
|
||||
this.ls = true;
|
||||
this.translation = localStorage.getItem('__translation') || 'BSB';
|
||||
}
|
||||
|
||||
const elems = document.querySelectorAll('a[data-scripture]');
|
||||
elems.forEach((e) => {
|
||||
e.addEventListener('click', (evt) => {
|
||||
this.anchorClick(evt)
|
||||
});
|
||||
});
|
||||
const sel = this.dialog[0]?.querySelector('select.translation');
|
||||
this.buildTranslationList(sel);
|
||||
sel?.addEventListener('change', (evt) => {
|
||||
this.txnOnChange(evt)
|
||||
});
|
||||
|
||||
this.dialog[0]?.addEventListener('click', (evt) => {
|
||||
if (evt.target === this.dialog[0]) {
|
||||
this.closeDialog()
|
||||
}
|
||||
});
|
||||
this.dialog[0]?.querySelector('button[data-close]')?.addEventListener('click', (evt) => {
|
||||
evt.preventDefault();
|
||||
this.closeDialog();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
anchorClick(evt) {
|
||||
const scripture = evt.target.attributes.getNamedItem('data-scripture');
|
||||
const refs = parseRef(scripture.value);
|
||||
const tx = refs.meta?.tx || 'BSB';
|
||||
evt.preventDefault();
|
||||
this.dialog[0]?.showModal();
|
||||
this.dialog[0]?.setAttribute('data-scripture', scripture.value);
|
||||
this.dialog[0]?.classList.remove("hidden");
|
||||
this.dialog[0]?.querySelector('.spinner svg')
|
||||
.classList.remove('hidden');
|
||||
this.readScripture(refs);
|
||||
}
|
||||
|
||||
buildTranslationList(sel) {
|
||||
sel?.replaceChildren();
|
||||
for (const tx in translations) {
|
||||
const opt = document.createElement('option');
|
||||
opt.setAttribute('value', tx);
|
||||
opt.insertAdjacentText('beforeend', translations[tx][0]);
|
||||
if (tx == this.translation) {
|
||||
opt.setAttribute('selected', 'selected');
|
||||
}
|
||||
sel?.insertAdjacentElement('beforeend', opt);
|
||||
}
|
||||
}
|
||||
|
||||
closeDialog() {
|
||||
this.dialog[0]?.close();
|
||||
this.dialog[0]?.classList.add('hidden');
|
||||
this.dialog[0]?.querySelector('article')?.replaceChildren();
|
||||
}
|
||||
|
||||
txnOnChange(evt) {
|
||||
const tx = evt.target.selectedOptions[0].value;
|
||||
this.translation = tx;
|
||||
console.log(tx, this.ls);
|
||||
if (this.ls) {
|
||||
localStorage.setItem('__translation', tx);
|
||||
}
|
||||
console.log(this.translation);
|
||||
console.log(this.dialog[0])
|
||||
console.log(this.dialog[0]?.getAttribute('data-scripture'))
|
||||
const refs = parseRef(this.dialog[0]?.getAttribute('data-scripture'));
|
||||
this.dialog[0]?.querySelector('article')?.replaceChildren();
|
||||
this.dialog[0]?.querySelector('.spinner svg')
|
||||
.classList.remove('hidden');
|
||||
console.log(refs);
|
||||
this.readScripture(refs);
|
||||
}
|
||||
|
||||
readScripture(refs) {
|
||||
const uri = `https://bible.helloao.org/api/${this.translation}/${refs.book.toUpperCase()}/${refs.chapter}.json`;
|
||||
fetch(uri)
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
console.error('query failed');
|
||||
console.log(response);
|
||||
return;
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then((response) => {
|
||||
this.render(response, refs);
|
||||
this.dialog[0]?.querySelector('.spinner svg')
|
||||
.classList.add('hidden');
|
||||
});
|
||||
}
|
||||
|
||||
render(response, refs) {
|
||||
const content = response?.chapter?.content;
|
||||
let inSpan = false;
|
||||
|
||||
if (this.dialog === null || this.dialog === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const section = document.createElement('section');
|
||||
const article = this.dialog[0]?.querySelector('article');
|
||||
let para = document.createElement('p');
|
||||
|
||||
const heading = document.createElement('h1');
|
||||
heading.insertAdjacentText('beforeend',
|
||||
`${refs.fullTitle()}`);
|
||||
article.insertAdjacentElement('beforeend', heading);
|
||||
|
||||
for (const c of content) {
|
||||
if (c.type === 'heading') {
|
||||
continue;
|
||||
}
|
||||
|
||||
let verse = document.createElement('span');
|
||||
|
||||
switch (c.type) {
|
||||
case 'line_break':
|
||||
if (inSpan) {
|
||||
section.insertAdjacentElement('beforeend', para);
|
||||
para = document.createElement('p');
|
||||
}
|
||||
break;
|
||||
case 'verse':
|
||||
if (!('number' in c)) {
|
||||
continue;
|
||||
}
|
||||
if (!('verses' in refs) || (c.number >= refs.verses.start && c.number <= refs.verses.end)) {
|
||||
inSpan = true;
|
||||
} else {
|
||||
inSpan = false;
|
||||
}
|
||||
if (inSpan) {
|
||||
verse.insertAdjacentText('beforeend', c.number);
|
||||
verse.classList.add('verse');
|
||||
para.insertAdjacentElement('beforeend', verse);
|
||||
verse = document.createElement('span');
|
||||
for (const v of c.content) {
|
||||
if (typeof(v) === 'string') {
|
||||
para.insertAdjacentText('beforeend', v+' ');
|
||||
continue
|
||||
}
|
||||
if ('lineBreak' in v) {
|
||||
if (para.innerHTML.length != 0 || para.innerText.length != 0) {
|
||||
section.insertAdjacentElement('beforeend', para);
|
||||
para = document.createElement('p');
|
||||
}
|
||||
}
|
||||
if ('text' in v) {
|
||||
para.insertAdjacentText('beforeend', v.text);
|
||||
if ('poem' in v) {
|
||||
para.classList.add('poem-'+v.poem);
|
||||
if (para.innerHTML.length != 0 || para.innerText.length != 0) {
|
||||
section.insertAdjacentElement('beforeend',
|
||||
para);
|
||||
}
|
||||
para = document.createElement('p');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.error('unhandled type:', c.type);
|
||||
}
|
||||
}
|
||||
section.insertAdjacentElement('beforeend', para);
|
||||
article.insertAdjacentElement('beforeend', section);
|
||||
}
|
||||
}
|
||||
|
||||
export const bindModals = () => {
|
||||
const elems = document.querySelectorAll('a[data-scripture]');
|
||||
const dialog = document.querySelectorAll('dialog[data-scripture]');
|
||||
let translation = 'BSB';
|
||||
const renderer = new RenderScripture(dialog);
|
||||
/*let translation = 'BSB';
|
||||
elems.forEach((e) => {
|
||||
e.addEventListener('click', (evt) => {
|
||||
const scripture = e.attributes.getNamedItem('data-scripture');
|
||||
|
|
@ -208,5 +393,5 @@ export const bindModals = () => {
|
|||
console.log('onchange');
|
||||
console.log(evt);
|
||||
console.log(evt.target.selectedOptions[0].value);
|
||||
});
|
||||
});*/
|
||||
};
|
||||
Loading…
Reference in a new issue