notes/src/utils/modal.js

408 lines
14 KiB
JavaScript
Raw Normal View History

2026-07-29 15:24:02 -06:00
export const modal = (el) => {
};
2026-08-01 14:07:41 -06:00
function parseRef(ref) {
console.log(ref);
2026-08-03 15:34:10 -06:00
ref = ref.replace(/%3[Aa]/, ':').replace(/\+/, ' ');
console.log(ref);
const m = ref.match(/((\d+(\+|\s))?[a-zA-Z]+)(\+|\s)?([0-9]+)?\:?([0-9]+(-?[0-9]+)?)?/);
2026-08-01 14:07:41 -06:00
console.log(m);
2026-08-03 15:34:10 -06:00
const book = m[1];
const chapter = m[5];
const verses = m[6]?.replaceAll(/–/g, '-');
console.log(`parseRef: ${book}, ${chapter}, ${verses}`);
console.log(books);
2026-08-03 15:34:10 -06:00
if (book === undefined) {
return {};
}
let refdata = {
ref: ref
};
2026-08-05 11:17:39 -06:00
if (book in books) {
refdata.book = book;
} else if (book in _map) {
refdata.book = _map[book];
}
refdata.meta = books[refdata.book];
console.log('refdata:', refdata, book, _map);
2026-08-03 15:34:10 -06:00
if (chapter !== undefined) {
refdata['chapter'] = chapter;
}
if (verses !== undefined) {
if (verses.indexOf('-') < 0) {
refdata['verses'] = {
start: parseInt(verses),
end: parseInt(verses)
};
2026-08-03 15:34:10 -06:00
} else {
refdata['verses'] = {
start: parseInt(verses.substring(0, verses.indexOf('-'))),
end: parseInt(verses.substring(verses.indexOf('-')+1))
2026-08-03 15:34:10 -06:00
};
}
}
2026-08-05 11:17:39 -06:00
console.log(refdata, book);
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
}
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;
}
console.log('render refs:', refs);
2026-08-05 11:17:39 -06:00
const section = document.createElement('section');
2026-08-06 13:41:52 -06:00
let para = document.createElement('p');
2026-08-05 11:17:39 -06:00
dialog.querySelector('a.bg').href = `https://www.biblegateway.com/passage/?search=${refs.ref}&version=ESV`
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) {
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) {
para.insertAdjacentText('beforeend', v.text);
2026-08-05 11:17:39 -06:00
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');
2026-08-05 11:17:39 -06:00
}
}
}
}
break;
default:
console.error('unhandled type:', c.type);
2026-08-05 11:17:39 -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-05 11:17:39 -06:00
dialog?.querySelector('article').insertAdjacentElement('beforeend', section);
}
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-06 13:41:52 -06:00
console.log(dialog);
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');
const refs = parseRef(scripture.value);
2026-07-29 22:55:24 -06:00
evt.preventDefault();
console.log('scripture:', scripture);
console.log('refs:', refs);
const uri = `https://bible.helloao.org/api/BSB/${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");
//document.body.classList.add("overflow-hidden");
2026-08-03 15:34:10 -06:00
fetch(uri)
2026-08-03 15:34:10 -06:00
.then((response) => {
console.log(response.ok);
return response.json();
})
.then((response) => {
render(dialog[0], response, refs);
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) => {
if (evt.target === dialog[0]) {
dialog[0]?.close();
dialog[0]?.querySelector('article')?.replaceChildren();
}
});
dialog[0]?.querySelector('button[data-close]')?.addEventListener('click', (evt) => {
evt.preventDefault();
dialog[0]?.close();
dialog[0]?.classList.add('hidden');
dialog[0]?.querySelector('article')?.replaceChildren();
2026-07-30 16:17:31 -06:00
});
};
const books = {
"gen": {"title": "Genesis", "start": 1, "stop": 50, "count": 50,
"aliases": ["gn"],
},
"exo": {"title": "Exodus", "start": 1, "stop": 40, "count": 40,
"aliases": ["ex"],
},
"lev": {"title": "Leviticus", "start": 1, "stop": 27, "count": 27,
"aliases": ["lv"],
},
"num": {"title": "Numbers", "start": 1, "stop": 36, "count": 36,
"aliases": ["nm", "numb", "nmb", "nmbs"],
},
"deu": {"title": "Deuteronomy", "start": 1, "stop": 34, "count": 34,
"aliases": ["dt", "deut", "deuter"],
},
"jos": {"title": "Joshua", "start": 1, "stop": 24, "count": 24,
"aliases": ["js", "josh"],
},
"jdg": {"title": "Judges", "start": 1, "stop": 21, "count": 21,
"aliases": ["jd", "jdgs"],
},
"rut": {"title": "Ruth", "start": 1, "stop": 4, "count": 4,
"aliases": ["rt"],
},
"1sa": {"title": "1 Samuel", "start": 1, "stop": 31, "count": 31,
"aliases": ["1s", "1sam", "1 s", "1 sa", "1 sam"],
},
"2sa": {"title": "2 Samuel", "start": 1, "stop": 24, "count": 24,
"aliases": ["2s", "2sam", "2 s", "2 sa", "2 sam"],
},
"1ki": {"title": "1 Kings", "start": 1, "stop": 22, "count": 22,
"aliases": ["1k", "1kgs", "1king", "1 k", "1 ki", "1 kgs", "1 king"],
},
"2ki": {"title": "2 Kings", "start": 1, "stop": 25, "count": 25,
"aliases": ["2k", "2kgs", "2king", "2 k", "2 ki", "2 kgs", "2 king"],
},
"1ch": {"title": "1 Chronicles", "start": 1, "stop": 29, "count": 29,
"aliases": ["1c", "1chr", "1chron", "1 c", "1 ch", "1 chr", "1 chron"],
},
"2ch": {"title": "2 Chronicles", "start": 1, "stop": 36, "count": 36,
"aliases": ["2c", "2chr", "2chron", "2 c", "2 ch", "2 chr", "2 chron"],
},
"ezr": {"title": "Ezra", "start": 1, "stop": 10, "count": 10,
"aliases": ["ez"],
},
"neh": {"title": "Nehemiah", "start": 1, "stop": 13, "count": 13,
"aliases": ["nh"],
},
"est": {"title": "Esther", "start": 1, "stop": 10, "count": 10,
"aliases": ["es"],
},
"job": {"title": "Job", "start": 1, "stop": 42, "count": 42,
"aliases": ["jo"],
},
"psa": {"title": "Psalms", "start": 1, "stop": 150, "count": 150,
"aliases": ["ps", "psalm", "pss"],
},
"pro": {"title": "Proverbs", "start": 1, "stop": 31, "count": 31,
"aliases": ["pr", "prov"],
},
"ecc": {"title": "Ecclesiastes", "start": 1, "stop": 12, "count": 12,
"aliases": ["ec", "eccl", "eccle"],
},
"sng": {"title": "Song of Solomon", "start": 1, "stop": 8, "count": 8,
"aliases": ["sn", "son", "song", "sol"],
},
"isa": {"title": "Isaiah", "start": 1, "stop": 66, "count": 66,
"aliases": ["is", "isai"],
},
"jer": {"title": "Jeremiah", "start": 1, "stop": 52, "count": 52,
"aliases": ["jr", "jere", "jerem"],
},
"lam": {"title": "Lamentations", "start": 1, "stop": 5, "count": 5,
"aliases": ["lm", "lamen"],
},
"ezk": {"title": "Ezekiel", "start": 1, "stop": 48, "count": 48,
"aliases": ["eze", "ezek"],
},
"dan": {"title": "Daniel", "start": 1, "stop": 12, "count": 12,
"aliases": ["dn"],
},
"hos": {"title": "Hosea", "start": 1, "stop": 14, "count": 14,
"aliases": ["hs"],
},
"jol": {"title": "Joel", "start": 1, "stop": 3, "count": 3,
"aliases": ["jl"],
},
"amo": {"title": "Amos", "start": 1, "stop": 9, "count": 9,
"aliases": ["am"],
},
"oba": {"title": "Obadiah", "start": 1, "stop": 1, "count": 1,
"aliases": ["ob", "obd", "obad"],
},
"jon": {"title": "Jonah", "start": 1, "stop": 4, "count": 4,
"aliases": ["jo"],
},
"mic": {"title": "Micah", "start": 1, "stop": 7, "count": 7,
"aliases": ["mc"],
},
"nam": {"title": "Nahum", "start": 1, "stop": 3, "count": 3,
"aliases": ["na"],
},
"hab": {"title": "Habakkuk", "start": 1, "stop": 3, "count": 3,
"aliases": ["hb", "haba", "habak"],
},
"zep": {"title": "Zephaniah", "start": 1, "stop": 3, "count": 3,
"aliases": ["ze", "zf", "zef"],
},
"hag": {"title": "Haggai", "start": 1, "stop": 2, "count": 2,
"aliases": ["hg"],
},
"zec": {"title": "Zechariah", "start": 1, "stop": 14, "count": 14,
"aliases": ["ze", "zech"],
},
"mal": {"title": "Malachi", "start": 1, "stop": 4, "count": 4,
"aliases": ["ml", "mala"],
},
"mat": {"title": "Matthew", "start": 1, "stop": 28, "count": 28,
"aliases": ["mt", "matt"],
},
"mrk": {"title": "Mark", "start": 1, "stop": 16, "count": 16,
"aliases": ["mk"],
},
"luk": {"title": "Luke", "start": 1, "stop": 24, "count": 24,
"aliases": ["lk"],
},
"jhn": {"title": "John", "start": 1, "stop": 21, "count": 21,
"aliases": ["jn"],
},
"act": {"title": "Acts", "start": 1, "stop": 28, "count": 28,
"aliases": ["ac"],
},
"rom": {"title": "Romans", "start": 1, "stop": 16, "count": 16,
"aliases": ["rm", "rms"],
},
"1co": {"title": "1 Corinthians", "start": 1, "stop": 16, "count": 16,
"aliases": ["1cor", "1corin", "1 co", "1 cor", "1 corin"],
},
"2co": {"title": "2 Corinthians", "start": 1, "stop": 13, "count": 13,
"aliases": ["2cor", "2corin", "2 co", "2 cor", "2 corin"],
},
"gal": {"title": "Galatians", "start": 1, "stop": 6, "count": 6,
"aliases": ["gl"],
},
"eph": {"title": "Ephesians", "start": 1, "stop": 6, "count": 6,
"aliases": ["ep", "ef"],
},
"php": {"title": "Philippians", "start": 1, "stop": 4, "count": 4,
"aliases": ["ph", "phil"],
},
"col": {"title": "Colossians", "start": 1, "stop": 4, "count": 4,
"aliases": ["cl", "colo"],
},
"1th": {"title": "1 Thessalonians", "start": 1, "stop": 5, "count": 5,
"aliases": ["1thes", "1thess", "1 thes", "1 thess"],
},
"2th": {"title": "2 Thessalonians", "start": 1, "stop": 3, "count": 3,
"aliases": ["2thes", "2thess", "2 thes", "2 thess"],
},
"1ti": {"title": "1 Timothy", "start": 1, "stop": 6, "count": 6,
"aliases": ["1tim", "1 ti", "1 tim"],
},
"2ti": {"title": "2 Timothy", "start": 1, "stop": 4, "count": 4,
"aliases": ["2tim", "2 ti", "2 tim"],
},
"tit": {"title": "Titus", "start": 1, "stop": 3, "count": 3,
"aliases": ["ti"],
},
"phm": {"title": "Philemon", "start": 1, "stop": 1, "count": 1,
"aliases": ["pi", "philm", "philem"],
},
"heb": {"title": "Hebrews", "start": 1, "stop": 13, "count": 13,
"aliases": ["he"],
},
"jas": {"title": "James", "start": 1, "stop": 5, "count": 5,
"aliases": ["ja", "jam"],
},
"1pe": {"title": "1 Peter", "start": 1, "stop": 5, "count": 5,
"aliases": ["1p", "1pt", "1pet", "1 p", "1 pe", "1 pt", "1 pet"],
},
"2pe": {"title": "2 Peter", "start": 1, "stop": 3, "count": 3,
"aliases": ["2p", "2pt", "2pet", "2 p", "2 pe", "2 pt", "2 pet"],
},
"1jn": {"title": "1 John", "start": 1, "stop": 5, "count": 5,
"aliases": ["1jon", "1 jn", "1 jon"],
},
"2jn": {"title": "2 John", "start": 1, "stop": 1, "count": 1,
"aliases": ["2jon", "2 jn", "2 jon"],
},
"3jn": {"title": "3 John", "start": 1, "stop": 1, "count": 1,
"aliases": ["3jon", "3 jn", "3 jon"],
},
"jud": {"title": "Jude", "start": 1, "stop": 1, "count": 1,
"aliases": ["ju"],
},
"rev": {"title": "Revelation", "start": 1, "stop": 22, "count": 22,
"aliases": ["rv", "reve"],
},
}
function buildBookMap() {
let mp = {};
for (const book in books) {
if (!("aliases" in books[book])) {
continue;
}
for (const alias of books[book]?.aliases) {
mp[alias] = book;
}
mp[books[book]["title"].toLowerCase()] = book;
}
console.log(mp.length);
return mp;
2026-08-01 14:07:41 -06:00
}
const _map = buildBookMap();