2026-08-18 15:23:25 -06:00
|
|
|
|
import { aliases, meta, translations } from './meta.js';
|
2026-08-13 17:01:13 -06:00
|
|
|
|
|
2026-07-30 23:58:15 -06:00
|
|
|
|
|
2026-07-29 15:24:02 -06:00
|
|
|
|
export const modal = (el) => {
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-08-01 14:07:41 -06:00
|
|
|
|
function parseRef(ref) {
|
2026-08-03 15:34:10 -06:00
|
|
|
|
ref = ref.replace(/%3[Aa]/, ':').replace(/\+/, ' ');
|
|
|
|
|
|
const m = ref.match(/((\d+(\+|\s))?[a-zA-Z]+)(\+|\s)?([0-9]+)?\:?([0-9]+(-?[0-9]+)?)?/);
|
|
|
|
|
|
|
2026-08-17 19:43:54 -06:00
|
|
|
|
const book = m[1].toLowerCase();
|
2026-08-03 15:34:10 -06:00
|
|
|
|
const chapter = m[5];
|
2026-08-06 18:19:27 -06:00
|
|
|
|
const verses = m[6]?.replaceAll(/–/g, '-');
|
|
|
|
|
|
|
2026-08-03 15:34:10 -06:00
|
|
|
|
if (book === undefined) {
|
|
|
|
|
|
return {};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-06 18:19:27 -06:00
|
|
|
|
let refdata = {
|
|
|
|
|
|
ref: ref
|
|
|
|
|
|
};
|
2026-08-17 19:43:54 -06:00
|
|
|
|
if (book in aliases) {
|
|
|
|
|
|
refdata.book = aliases[book];
|
2026-08-05 11:17:39 -06:00
|
|
|
|
}
|
2026-08-17 19:43:54 -06:00
|
|
|
|
refdata.meta = meta[refdata.book];
|
2026-08-06 18:54:19 -06:00
|
|
|
|
refdata.fullTitle = () => {
|
|
|
|
|
|
let title = refdata.meta.title+' ';
|
|
|
|
|
|
title += refdata.chapter;
|
|
|
|
|
|
|
|
|
|
|
|
if (refdata.verses?.start > 0) {
|
|
|
|
|
|
title += ':'+refdata.verses.start;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (refdata.verses?.end > 0 && refdata.verses?.end > refdata.verses?.start) {
|
|
|
|
|
|
title += '-'+refdata.verses.end;
|
|
|
|
|
|
}
|
|
|
|
|
|
return title;
|
|
|
|
|
|
};
|
2026-08-05 11:17:39 -06:00
|
|
|
|
|
2026-08-03 15:34:10 -06:00
|
|
|
|
if (chapter !== undefined) {
|
2026-08-06 18:54:19 -06:00
|
|
|
|
refdata['chapter'] = parseInt(chapter);
|
2026-08-03 15:34:10 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (verses !== undefined) {
|
|
|
|
|
|
if (verses.indexOf('-') < 0) {
|
2026-08-06 15:41:25 -06:00
|
|
|
|
refdata['verses'] = {
|
2026-08-06 18:19:27 -06:00
|
|
|
|
start: parseInt(verses),
|
|
|
|
|
|
end: parseInt(verses)
|
2026-08-06 15:41:25 -06:00
|
|
|
|
};
|
2026-08-03 15:34:10 -06:00
|
|
|
|
} else {
|
|
|
|
|
|
refdata['verses'] = {
|
2026-08-06 18:19:27 -06:00
|
|
|
|
start: parseInt(verses.substring(0, verses.indexOf('-'))),
|
|
|
|
|
|
end: parseInt(verses.substring(verses.indexOf('-')+1))
|
2026-08-03 15:34:10 -06:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ('verses' in refdata) {
|
|
|
|
|
|
if (refdata.verses.start < 1) {
|
|
|
|
|
|
refdata.verses.start = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (refdata.verses.end < 1) {
|
|
|
|
|
|
refdata.verses.end = 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (refdata.verses.start > refdata.verses.end) {
|
|
|
|
|
|
refdata.verses.end = refdata.verses.start;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return refdata;
|
2026-08-01 14:07:41 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-06 18:19:27 -06:00
|
|
|
|
function render(dialog, response, refs) {
|
2026-08-05 11:17:39 -06:00
|
|
|
|
const content = response?.chapter?.content;
|
|
|
|
|
|
let inSpan = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (dialog === null || dialog === undefined) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const section = document.createElement('section');
|
2026-08-06 18:54:19 -06:00
|
|
|
|
const article = dialog?.querySelector('article');
|
2026-08-06 13:41:52 -06:00
|
|
|
|
let para = document.createElement('p');
|
2026-08-05 11:17:39 -06:00
|
|
|
|
|
2026-08-06 18:54:19 -06:00
|
|
|
|
const heading = document.createElement('h1');
|
|
|
|
|
|
heading.insertAdjacentText('beforeend',
|
|
|
|
|
|
`${refs.fullTitle()}`);
|
|
|
|
|
|
article.insertAdjacentElement('beforeend', heading);
|
|
|
|
|
|
|
2026-08-05 11:17:39 -06:00
|
|
|
|
for (const c of content) {
|
|
|
|
|
|
if (c.type === 'heading') {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-06 13:41:52 -06:00
|
|
|
|
let verse = document.createElement('span');
|
|
|
|
|
|
|
2026-08-05 11:17:39 -06:00
|
|
|
|
switch (c.type) {
|
|
|
|
|
|
case 'line_break':
|
|
|
|
|
|
if (inSpan) {
|
2026-08-06 13:41:52 -06:00
|
|
|
|
section.insertAdjacentElement('beforeend', para);
|
|
|
|
|
|
para = document.createElement('p');
|
2026-08-05 11:17:39 -06:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'verse':
|
|
|
|
|
|
if (!('number' in c)) {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2026-08-06 13:41:52 -06:00
|
|
|
|
if (!('verses' in refs) || (c.number >= refs.verses.start && c.number <= refs.verses.end)) {
|
2026-08-05 11:17:39 -06:00
|
|
|
|
inSpan = true;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
inSpan = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (inSpan) {
|
2026-08-06 13:41:52 -06:00
|
|
|
|
verse.insertAdjacentText('beforeend', c.number);
|
|
|
|
|
|
verse.classList.add('verse');
|
|
|
|
|
|
para.insertAdjacentElement('beforeend', verse);
|
|
|
|
|
|
verse = document.createElement('span');
|
2026-08-05 11:17:39 -06:00
|
|
|
|
for (const v of c.content) {
|
|
|
|
|
|
if (typeof(v) === 'string') {
|
2026-08-06 13:41:52 -06:00
|
|
|
|
para.insertAdjacentText('beforeend', v+' ');
|
2026-08-05 11:17:39 -06:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if ('lineBreak' in v) {
|
2026-08-06 15:41:25 -06:00
|
|
|
|
if (para.innerHTML.length != 0 || para.innerText.length != 0) {
|
|
|
|
|
|
section.insertAdjacentElement('beforeend', para);
|
|
|
|
|
|
para = document.createElement('p');
|
|
|
|
|
|
}
|
|
|
|
|
|
//para.insertAdjacentElement('beforeend', document.createElement('br'));
|
2026-08-06 13:41:52 -06:00
|
|
|
|
//para = document.createElement('p');
|
2026-08-05 11:17:39 -06:00
|
|
|
|
}
|
|
|
|
|
|
if ('text' in v) {
|
2026-08-06 15:41:25 -06:00
|
|
|
|
para.insertAdjacentText('beforeend', v.text);
|
2026-08-05 11:17:39 -06:00
|
|
|
|
if ('poem' in v) {
|
2026-08-06 15:41:25 -06:00
|
|
|
|
para.classList.add('poem-'+v.poem);
|
|
|
|
|
|
if (para.innerHTML.length != 0 || para.innerText.length != 0) {
|
2026-08-06 19:02:04 -06:00
|
|
|
|
section.insertAdjacentElement('beforeend',
|
2026-08-06 15:41:25 -06:00
|
|
|
|
para);
|
|
|
|
|
|
}
|
|
|
|
|
|
para = document.createElement('p');
|
2026-08-05 11:17:39 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2026-08-06 18:19:27 -06:00
|
|
|
|
console.error('unhandled type:', c.type);
|
2026-08-05 11:17:39 -06:00
|
|
|
|
}
|
2026-08-06 18:19:27 -06:00
|
|
|
|
//console.log(c);
|
2026-08-05 11:17:39 -06:00
|
|
|
|
}
|
2026-08-06 13:41:52 -06:00
|
|
|
|
section.insertAdjacentElement('beforeend', para);
|
2026-08-06 18:54:19 -06:00
|
|
|
|
article.insertAdjacentElement('beforeend', section);
|
2026-08-05 11:17:39 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-29 15:24:02 -06:00
|
|
|
|
export const bindModals = () => {
|
2026-07-29 22:55:24 -06:00
|
|
|
|
const elems = document.querySelectorAll('a[data-scripture]');
|
|
|
|
|
|
const dialog = document.querySelectorAll('dialog[data-scripture]');
|
2026-08-18 15:23:25 -06:00
|
|
|
|
let translation = 'BSB';
|
2026-07-29 15:24:02 -06:00
|
|
|
|
elems.forEach((e) => {
|
2026-07-29 22:55:24 -06:00
|
|
|
|
e.addEventListener('click', (evt) => {
|
|
|
|
|
|
const scripture = e.attributes.getNamedItem('data-scripture');
|
2026-08-06 18:19:27 -06:00
|
|
|
|
const refs = parseRef(scripture.value);
|
2026-08-17 19:43:54 -06:00
|
|
|
|
const tx = refs.meta?.tx || 'BSB';
|
2026-07-29 22:55:24 -06:00
|
|
|
|
evt.preventDefault();
|
2026-08-17 19:43:54 -06:00
|
|
|
|
const uri = `https://bible.helloao.org/api/${tx}/${refs.book.toUpperCase()}/${refs.chapter}.json`;
|
2026-07-29 22:55:24 -06:00
|
|
|
|
dialog[0]?.showModal();
|
|
|
|
|
|
dialog[0]?.setAttribute('data-scripture', scripture);
|
|
|
|
|
|
dialog[0]?.classList.remove("hidden");
|
2026-08-06 20:01:18 -06:00
|
|
|
|
dialog[0]?.querySelector('.spinner svg')
|
|
|
|
|
|
.classList.remove('hidden');
|
2026-08-03 15:34:10 -06:00
|
|
|
|
|
2026-08-06 18:19:27 -06:00
|
|
|
|
fetch(uri)
|
2026-08-03 15:34:10 -06:00
|
|
|
|
.then((response) => {
|
|
|
|
|
|
return response.json();
|
|
|
|
|
|
})
|
|
|
|
|
|
.then((response) => {
|
2026-08-06 18:19:27 -06:00
|
|
|
|
render(dialog[0], response, refs);
|
2026-08-06 20:01:18 -06:00
|
|
|
|
dialog[0]?.querySelector('.spinner svg')
|
|
|
|
|
|
.classList.add('hidden');
|
2026-08-03 15:34:10 -06:00
|
|
|
|
});
|
2026-07-29 22:55:24 -06:00
|
|
|
|
});
|
2026-07-29 15:24:02 -06:00
|
|
|
|
});
|
2026-07-30 16:17:31 -06:00
|
|
|
|
dialog[0]?.addEventListener('click', (evt) => {
|
2026-08-06 18:19:27 -06:00
|
|
|
|
if (evt.target === dialog[0]) {
|
|
|
|
|
|
dialog[0]?.close();
|
2026-08-06 20:01:18 -06:00
|
|
|
|
dialog[0]?.classList.add('hidden');
|
2026-08-06 18:19:27 -06:00
|
|
|
|
dialog[0]?.querySelector('article')?.replaceChildren();
|
|
|
|
|
|
}
|
2026-08-06 15:41:25 -06:00
|
|
|
|
});
|
|
|
|
|
|
dialog[0]?.querySelector('button[data-close]')?.addEventListener('click', (evt) => {
|
|
|
|
|
|
evt.preventDefault();
|
|
|
|
|
|
dialog[0]?.close();
|
2026-08-06 18:19:27 -06:00
|
|
|
|
dialog[0]?.classList.add('hidden');
|
2026-08-06 15:41:25 -06:00
|
|
|
|
dialog[0]?.querySelector('article')?.replaceChildren();
|
2026-07-30 16:17:31 -06:00
|
|
|
|
});
|
2026-08-18 15:23:25 -06:00
|
|
|
|
const sel = dialog[0]?.querySelector('select.translation');
|
|
|
|
|
|
console.log(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 == translation) {
|
|
|
|
|
|
opt.setAttribute('selected', 'selected');
|
|
|
|
|
|
}
|
|
|
|
|
|
sel?.insertAdjacentElement('beforeend', opt);
|
|
|
|
|
|
}
|
|
|
|
|
|
sel?.addEventListener('change', (evt) => {
|
|
|
|
|
|
console.log('onchange');
|
|
|
|
|
|
console.log(evt);
|
|
|
|
|
|
console.log(evt.target.selectedOptions[0].value);
|
|
|
|
|
|
});
|
2026-08-17 19:43:54 -06:00
|
|
|
|
};
|