diff --git a/README.md b/README.md index bb0da50..e89429e 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ GetBible loader is an intuitive and lightweight JavaScript solution for embeddin ```html - + ``` 2. **Markup Your Scripture References:** diff --git a/dist/js/getBible.js b/dist/js/getBible.js index 57dc77d..28b91d0 100644 --- a/dist/js/getBible.js +++ b/dist/js/getBible.js @@ -1,5 +1,5 @@ /** - * getBible Loader v3.0.2 + * getBible Loader v3.0.3 * https://getbible.net * (c) 2014 - 2023 Llewellyn van der Merwe * MIT License @@ -155,8 +155,38 @@ * Initializes the BibleVerse object with verse data. * * @param {Object} data - The JSON data containing verse information. + * @param {string} data.translation - The name of the translation. + * @param {string} data.abbreviation - The abbreviation of the translation. + * @param {string} data.language - The full language name. + * @param {string} data.lang - The language code. + * @param {string} data.direction - The text direction (LTR or RTL). + * @param {string} data.encoding - The encoding format (e.g., UTF-8). + * @param {number} data.book_nr - The book number. + * @param {string} data.book_name - The name of the book. + * @param {number} data.chapter - The chapter number. + * @param {string} data.name - The name of the chapter. + * @param {Array} data.verses - An array of objects representing each verse. + * @param {string|Array} data.ref - The local reference string or array of strings. */ constructor(data) { + // Simple validation to check if essential properties are present + const requiredProperties = [ + 'translation', 'abbreviation', 'language', 'lang', + 'direction', 'encoding', 'book_nr', 'book_name', + 'chapter', 'name', 'verses', 'ref' + ]; + + if (!data || typeof data !== 'object') { + throw new Error('Data must be a valid object.'); + } + + requiredProperties.forEach(prop => { + if (data[prop] === undefined || data[prop] === null) { + throw new Error(`Missing required property: '${prop}'.`); + } + }); + + // Assign the data after validation this.#data = data; } @@ -165,7 +195,7 @@ * * @returns {string} The name of the translation. */ - getTranslation() { + get translation() { return this.#data.translation; } @@ -174,26 +204,26 @@ * * @returns {string} The abbreviation of the translation. */ - getAbbreviation() { + get abbreviation() { return this.#data.abbreviation; } - /** - * Retrieves the language code. - * - * @returns {string} The language code. - */ - getLanguage() { - return this.#data.lang; - } - /** * Retrieves the full language name. * + * @returns {string} The language code. + */ + get language() { + return this.#data.language; + } + + /** + * Retrieves the language code. + * * @returns {string} The full name of the language. */ - getLanguageName() { - return this.#data.language; + get languageCode() { + return this.#data.lang; } /** @@ -201,7 +231,7 @@ * * @returns {string} The direction of the text (LTR or RTL). */ - getTextDirection() { + get textDirection() { return this.#data.direction; } @@ -210,7 +240,7 @@ * * @returns {string} The encoding format (e.g., UTF-8). */ - getEncoding() { + get encoding() { return this.#data.encoding; } @@ -219,7 +249,7 @@ * * @returns {number} The book number. */ - getBookNumber() { + get bookNumber() { return this.#data.book_nr; } @@ -228,7 +258,7 @@ * * @returns {string} The name of the book. */ - getBookName() { + get bookName() { return this.#data.book_name; } @@ -237,7 +267,7 @@ * * @returns {number} The chapter number. */ - getChapter() { + get chapter() { return this.#data.chapter; } @@ -246,16 +276,17 @@ * * @returns {string} The name of the chapter. */ - getChapterName() { + get chapterName() { return this.#data.name; } /** * Retrieves all verses of the chapter. * - * @returns {Array} An array of objects representing each verse. + * @returns {Array<{chapter: number, verse: number, name: string, text: string}>} + * An array of objects representing each verse. */ - getVerses() { + get verses() { return this.#data.verses; } @@ -280,12 +311,22 @@ return this.#data.verses.filter(verse => verse.verse >= startVerse && verse.verse <= endVerse); } + /** + * Get the local reference string set in the website. + * + * @returns {string} The reference string. + */ + get localReference() { + // Ensure that this.#data.ref is treated as an array. + return Array.isArray(this.#data.ref) ? this.#data.ref.join('; ') : this.#data.ref; + } + /** * Generates a reference string for the verses. * * @returns {string} The reference string. */ - getReference() { + get reference() { const verseNumbers = this.#data.verses.map(verse => verse.verse).sort((a, b) => a - b); let refString = `${this.#data.name}:`; let ranges = {}; @@ -331,26 +372,6 @@ this.#references = Object.values(data).map(reference => new Reference(reference)); } - /** - * Gets a reference by its numerical index. - * - * @param {number} index - The index of the reference. - * @returns {Reference|null} The Reference instance or null if out of bounds. - */ - getReference(index) { - return (index >= 0 && index < this.#references.length) ? this.#references[index] : null; - } - - /** - * Gets the translation. - * - * @param {number} index - The index of the reference. - * @returns {Reference|null} The Reference instance or null if out of bounds. - */ - getReference(index) { - return (index >= 0 && index < this.#references.length) ? this.#references[index] : null; - } - /** * Iterates over all references and performs a callback function. * @@ -370,9 +391,11 @@ #translations; #showBookName; #showReference; + #showLocalReference; #showTranslation; #showAbbreviation; #showLanguage; + #showLanguageCode; /** * Initializes the Actions object with a DOM element and its data attributes. @@ -390,9 +413,15 @@ this.#translations = (element.dataset.translation || 'kjv').toLowerCase().split(';').map(translation => translation.trim()); this.#showBookName = element.dataset.showBookName ? parseInt(element.dataset.showBookName, 10) : 0; this.#showReference = element.dataset.showReference ? parseInt(element.dataset.showReference, 10) : 1; + this.#showLocalReference = element.dataset.showLocalReference ? parseInt(element.dataset.showLocalReference, 10) : 0; this.#showTranslation = element.dataset.showTranslation ? parseInt(element.dataset.showTranslation, 10) : 0; this.#showAbbreviation = element.dataset.showAbbreviation ? parseInt(element.dataset.showAbbreviation, 10) : 0; this.#showLanguage = element.dataset.showLanguage ? parseInt(element.dataset.showLanguage, 10) : 0; + this.#showLanguageCode = element.dataset.showLanguageCode ? parseInt(element.dataset.showLanguageCode, 10) : 0; + + if (this.#showLocalReference){ + this.#showReference = 0; + } } /** @@ -400,7 +429,7 @@ * * @returns {Array} An array of translation strings. */ - getTranslations() { + get translations() { return this.#translations; } @@ -409,7 +438,7 @@ * * @returns {number} The show book name flag (0 or 1). */ - showBookName() { + get bookName() { return this.#showBookName; } @@ -418,16 +447,25 @@ * * @returns {number} The show reference flag (0 or 1). */ - showReference() { + get reference() { return this.#showReference; } + /** + * Retrieves the show local reference flag. + * + * @returns {number} The show reference flag (0 or 1). + */ + get localReference() { + return this.#showLocalReference; + } + /** * Retrieves the show translation flag. * * @returns {number} The show translation flag (0 or 1). */ - showTranslation() { + get translation() { return this.#showTranslation; } @@ -436,7 +474,7 @@ * * @returns {number} The show abbreviation flag (0 or 1). */ - showAbbreviation() { + get abbreviation() { return this.#showAbbreviation; } @@ -445,16 +483,25 @@ * * @returns {number} The show language flag (0 or 1). */ - showLanguage() { + get language() { return this.#showLanguage; } + /** + * Retrieves the show language code flog. + * + * @returns {number} The show language flag (0 or 1). + */ + get languageCode() { + return this.#showLanguageCode; + } + /** * Retrieves the element format. * * @returns {string} The element format. */ - getFormat() { + get format() { return this.#format; } @@ -463,7 +510,7 @@ * * @returns {HTMLElement} The DOM element associated with this object. */ - getElement() { + get element() { return this.#element; } } @@ -485,7 +532,7 @@ * * @returns {Action} The current actions. */ - action() { + get action() { return this.#action; } @@ -516,27 +563,33 @@ let display = []; scripture.forEachReference((reference) => { let header = []; - display.push(`
`); - if (this.action().showBookName()) { - header.push(`${reference.getBookName()}`); + display.push(`
`); + if (this.action.bookName) { + header.push(`${reference.bookName}`); } - if (this.action().showReference()) { - header.push(`${reference.getReference()}`); + if (this.action.reference) { + header.push(`${reference.reference}`); } - if (this.action().showTranslation()) { - header.push(`${reference.getTranslation()}`); + if (this.action.localReference) { + header.push(`${reference.localReference}`); } - if (this.action().showAbbreviation()) { - header.push(`${reference.getAbbreviation()}`); + if (this.action.translation) { + header.push(`${reference.translation}`); } - if (this.action().showLanguage()) { - header.push(`${reference.getLanguage()}`); + if (this.action.abbreviation) { + header.push(`${reference.abbreviation}`); + } + if (this.action.language) { + header.push(`${reference.language}`); + } + if (this.action.languageCode) { + header.push(`${reference.languageCode}`); } // Construct the header if (header.length > 0) { display.push(`${header.join(' - ')}`); } - const verses = reference.getVerses() + const verses = reference.verses .map(verse => `
${verse.verse}. ${verse.text}
`) .join("\n"); display.push(`
${verses}
`); @@ -562,23 +615,29 @@ let display = []; scripture.forEachReference((reference) => { let footer = []; - display.push(`
`); - if (this.action().showBookName()) { - footer.push(`${reference.getBookName()}`); + display.push(`
`); + if (this.action.bookName) { + footer.push(`${reference.bookName}`); } - if (this.action().showReference()) { - footer.push(`${reference.getReference()}`); + if (this.action.reference) { + footer.push(`${reference.reference}`); } - if (this.action().showTranslation()) { - footer.push(`${reference.getTranslation()}`); + if (this.action.localReference) { + footer.push(`${reference.localReference}`); } - if (this.action().showAbbreviation()) { - footer.push(`${reference.getAbbreviation()}`); + if (this.action.translation) { + footer.push(`${reference.translation}`); } - if (this.action().showLanguage()) { - footer.push(`${reference.getLanguage()}`); + if (this.action.abbreviation) { + footer.push(`${reference.abbreviation}`); } - const verses = reference.getVerses() + if (this.action.language) { + footer.push(`${reference.language}`); + } + if (this.action.languageCode) { + footer.push(`${reference.languageCode}`); + } + const verses = reference.verses .map(verse => `${verse.verse}. ${verse.text}`) .join("\n"); display.push(`${verses}`); @@ -608,27 +667,33 @@ let display = []; scripture.forEachReference((reference) => { let header = []; - if (this.action().showBookName()) { - header.push(`${reference.getBookName()}`); + if (this.action.bookName) { + header.push(`${reference.bookName}`); } - if (this.action().showReference()) { - header.push(`${reference.getReference()}`); + if (this.action.reference) { + header.push(`${reference.reference}`); } - if (this.action().showTranslation()) { - header.push(`${reference.getTranslation()}`); + if (this.action.localReference) { + header.push(`${reference.localReference}`); } - if (this.action().showAbbreviation()) { - header.push(`${reference.getAbbreviation()}`); + if (this.action.translation) { + header.push(`${reference.translation}`); } - if (this.action().showLanguage()) { - header.push(`${reference.getLanguage()}`); + if (this.action.abbreviation) { + header.push(`${reference.abbreviation}`); + } + if (this.action.language) { + header.push(`${reference.language}`); + } + if (this.action.languageCode) { + header.push(`${reference.languageCode}`); } // Construct the header if (header.length > 0) { display.push(`[${header.join(' - ')}]`); } display.push( - reference.getVerses() + reference.verses .map(verse => `${verse.verse}. ${verse.text}`) .join("\n") ); @@ -655,7 +720,7 @@ 'tooltip': PlainFormat }; - const format = action.getFormat(); + const format = action.format; const FormatType = formatTypes[format] || InlineFormat; this.format = new FormatType(action); } @@ -683,7 +748,7 @@ constructor(action) { this.#modalId = `modal-${Math.random().toString(36).slice(2, 11)}`; this.#action = action; - this.getElement().style.cursor = 'pointer'; + this.element.style.cursor = 'pointer'; this.initializeTrigger(); } @@ -693,11 +758,11 @@ * @param {string} content - The content to load into the modal. */ load(content) { - const existingModal = document.getElementById(this.getModalId()); + const existingModal = document.getElementById(this.id); // Check if modal already exists if (existingModal) { // Update the content of the existing modal - const contentDiv = document.getElementById(`${this.getModalId()}-content`); + const contentDiv = document.getElementById(`${this.id}-content`); if (contentDiv) { contentDiv.innerHTML += content; } @@ -723,17 +788,17 @@ */ create(content) { const modalHtml = ` -