diff --git a/src/utils/modal.js b/src/utils/modal.js index 74e8da0..d0bc9db 100644 --- a/src/utils/modal.js +++ b/src/utils/modal.js @@ -1,4 +1,4 @@ -import { aliases, meta, translations } from './meta.js'; +import { aliases, defaulttx, meta, translations } from './meta.js'; import { hasLocalStorage } from './utils.js'; export const modal = (el) => { @@ -73,7 +73,9 @@ function parseRef(ref) { class RenderScripture { translation = 'BSB'; dialog = null; + sel = null; ls = false; + ref = {}; constructor(dialog) { this.dialog = dialog; @@ -90,10 +92,10 @@ class RenderScripture { }); }); const sel = this.dialog[0]?.querySelector('select.translation'); - this.buildTranslationList(sel); sel?.addEventListener('change', (evt) => { this.txnOnChange(evt) }); + this.sel = sel; this.dialog[0]?.addEventListener('click', (evt) => { if (evt.target === this.dialog[0]) { @@ -111,18 +113,24 @@ class RenderScripture { const scripture = evt.target.attributes.getNamedItem('data-scripture'); const refs = parseRef(scripture.value); const tx = refs.meta?.tx || 'BSB'; + this.ref = refs; 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.buildTranslationList(this.sel); this.readScripture(refs); } buildTranslationList(sel) { sel?.replaceChildren(); for (const tx in translations) { + const testament = meta[this.ref.book].ts; + if (translations[tx][2].indexOf(testament) == -1) { + continue; + } const opt = document.createElement('option'); opt.setAttribute('value', tx); opt.insertAdjacentText('beforeend', translations[tx][0]); @@ -153,7 +161,23 @@ class RenderScripture { } readScripture(refs) { - const uri = `https://bible.helloao.org/api/${this.translation}/${refs.book.toUpperCase()}/${refs.chapter}.json`; + // Current translation. + let tx = this.translation; + // Previous translation. + const px = this.translation; + + const book = refs.book; + const chapter = refs.chapter; + const testament = meta[book].ts; + + // If the current translation doesn't contain the testament, switch to + // the default translation that does. + if (translations[tx][2].indexOf(testament) == -1) { + tx = defaulttx[testament]; + this.translation = tx; + } + + const uri = `https://bible.helloao.org/api/${tx}/${book.toUpperCase()}/${chapter}.json`; fetch(uri) .then((response) => { if (!response.ok) { @@ -167,6 +191,11 @@ class RenderScripture { this.render(response, refs); this.dialog[0]?.querySelector('.spinner svg') .classList.add('hidden'); + // Switch translation back if it's forced to a different + // translation from book availability. + if (this.translation != px) { + this.translation = px; + } }); }