import md from 'marked'; import { removeTags, slugify } from '../../helpers'; import { getHomonymnNumber } from './utilities'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search'; import { showSection } from './displayToggles'; import { setupInfoModal } from '../setupListeners/modals'; import { setupSearchFilters } from '../setupListeners/search'; export function renderAll() { renderTheme(); renderCustomCSS(); renderDictionaryDetails(); renderPartsOfSpeech(); renderWords(); } export function renderTheme() { const { theme } = window.currentDictionary.settings; document.body.id = theme + 'Theme'; } export function renderCustomCSS() { const { customCSS } = window.currentDictionary.settings; const stylingId = 'customCSS'; const stylingElement = document.getElementById(stylingId); if (!stylingElement) { const styling = document.createElement('style'); styling.id = stylingId; styling.innerHTML = customCSS; document.body.appendChild(styling); } else { stylingElement.innerHTML = customCSS; } } export function renderDictionaryDetails() { renderName(); showSection('description'); } export function renderName() { const dictionaryName = removeTags(window.currentDictionary.name) + ' ' + removeTags(window.currentDictionary.specification); document.getElementById('dictionaryName').innerHTML = dictionaryName; const shareLink = window.location.pathname.match(new RegExp(window.currentDictionary.externalID + '$')) ? window.location.pathname : window.location.pathname.substring(0, window.location.pathname.indexOf(window.currentDictionary.externalID)) + window.currentDictionary.externalID; document.getElementById('dictionaryShare').href = shareLink; } export function renderDescription() { const descriptionHTML = md(window.currentDictionary.description); document.getElementById('detailsPanel').innerHTML = '
' + descriptionHTML + '
'; } export function renderDetails() { const { partsOfSpeech, alphabeticalOrder } = window.currentDictionary; const { phonology, phonotactics, orthography, grammar } = window.currentDictionary.details; const partsOfSpeechHTML = `

Parts of Speech
${partsOfSpeech.map(partOfSpeech => '' + partOfSpeech + '').join(' ')}`; const alphabeticalOrderHTML = `

Alphabetical Order
${ (alphabeticalOrder.length > 0 ? alphabeticalOrder : ['English Alphabet']).map(letter => `${letter}`).join(' ') }`; const generalHTML = `

General

${partsOfSpeechHTML}${alphabeticalOrderHTML}`; const { consonants, vowels, blends } = phonology const consonantHTML = `

Consonants
${consonants.map(letter => `${letter}`).join(' ')}

`; const vowelHTML = `

Vowels
${vowels.map(letter => `${letter}`).join(' ')}

`; const blendHTML = blends.length > 0 ? `

Polyphthongs / Blends
${blends.map(letter => `${letter}`).join(' ')}

` : ''; const phonologyNotesHTML = phonology.notes.trim().length > 0 ? '

Notes

' + md(phonology.notes) + '
' : ''; const phonologyHTML = `

Phonology

${consonantHTML}
${vowelHTML}
${blendHTML} ${phonologyNotesHTML}`; const { onset, nucleus, coda } = phonotactics; const onsetHTML = `

Onset
${onset.map(letter => `${letter}`).join(' ')}

`; const nucleusHTML = `

Nucleus
${nucleus.map(letter => `${letter}`).join(' ')}

`; const codaHTML = `

Coda
${coda.map(letter => `${letter}`).join(' ')}

`; const phonotacticsNotesHTML = phonotactics.notes.trim().length > 0 ? '

Notes

' + md(phonotactics.notes) + '
' : ''; const phonotacticsHTML = onset.length + nucleus.length + coda.length + phonotacticsNotesHTML.length > 0 ? `

Phonotactics

${onset.length > 0 || nucleus.length > 0 || coda.length > 0 ? `
${onsetHTML}
${nucleusHTML}
${codaHTML}
` : ''} ${phonotacticsNotesHTML}` : ''; const { translations } = orthography; const translationsHTML = translations.length > 0 ? `

Translations
${translations.map(translation => { translation = translation.split('=').map(value => value.trim()); if (translation.length > 1 && translation[0] !== '' && translation[1] !== '') { return `${translation[0]}${translation[1]}`; } return false; }).filter(html => html !== false).join(' ')}

` : ''; const orthographyNotesHTML = orthography.notes.trim().length > 0 ? '

Notes
' + md(orthography.notes) + '' : ''; const orthographyHTML = translations.length + orthographyNotesHTML.length > 0 ? `

Orthography

${translationsHTML} ${orthographyNotesHTML}` : ''; const grammarHTML = grammar.notes.trim().length > 0 ? '

Grammar

' + (grammar.notes.trim().length > 0 ? md(grammar.notes) : '') + '
' : ''; detailsPanel.innerHTML = generalHTML + phonologyHTML + phonotacticsHTML + orthographyHTML + grammarHTML; } export function renderStats() { const { wordStats } = window.currentDictionary; const numberOfWordsHTML = `

Number of Words
${wordStats.numberOfWords.map(stat => `${stat.name}${stat.value}`).join(' ')}

`; const wordLengthHTML = `

Word Length
Shortest${wordStats.wordLength.shortest} Longest${wordStats.wordLength.longest} Average${wordStats.wordLength.average}

`; const letterDistributionHTML = `

Letter Distribution
${wordStats.letterDistribution.map(stat => `${stat.letter}${stat.percentage.toFixed(2)}`).join(' ')}

`; const totalLettersHTML = `

${wordStats.totalLetters} Total Letters

`; detailsPanel.innerHTML = numberOfWordsHTML + wordLengthHTML + letterDistributionHTML + totalLettersHTML; } export function renderPartsOfSpeech(onlyOptions = false) { let optionsHTML = '', searchHTML = ''; window.currentDictionary.partsOfSpeech.forEach(partOfSpeech => { partOfSpeech = removeTags(partOfSpeech); optionsHTML += ``; searchHTML += ``; }); searchHTML += `Check All Uncheck All`; Array.from(document.getElementsByClassName('part-of-speech-select')).forEach(select => { const selectedValue = select.value; select.innerHTML = optionsHTML; select.value = selectedValue; }); if (!onlyOptions) { document.getElementById('searchPartsOfSpeech').innerHTML = searchHTML; } setupSearchFilters(); } export function renderWords() { let wordsHTML = ''; let words = false; if (window.currentDictionary.words.length === 0) { wordsHTML = `

No Words Found

Either this dictionary has not yet been started, or something prevented words from downloading.
`; } else { words = getMatchingSearchWords(); if (words.length === 0) { wordsHTML = `

No Search Results

Edit your search or filter to show words.
`; } words.forEach(originalWord => { const word = highlightSearchTerm({ name: removeTags(originalWord.name), pronunciation: removeTags(originalWord.pronunciation), partOfSpeech: removeTags(originalWord.partOfSpeech), definition: removeTags(originalWord.definition), details: originalWord.details, etymology: typeof originalWord.etymology === 'undefined' || originalWord.etymology.length < 1 ? null : originalWord.etymology.join(', '), related: typeof originalWord.related === 'undefined' || originalWord.related.length < 1 ? null : originalWord.related.join(', '), principalParts: typeof originalWord.principalParts === 'undefined' || originalWord.principalParts.length < 1 ? null : originalWord.principalParts.join(', '), wordId: originalWord.wordId, }); const homonymnNumber = getHomonymnNumber(originalWord); const shareLink = window.location.pathname + (window.location.pathname.match(new RegExp(word.wordId + '$')) ? '' : '/' + word.wordId); wordsHTML += `

${word.name}${homonymnNumber > 0 ? ' ' + homonymnNumber.toString() + '' : ''}

${word.principalParts === null ? '' : `(${word.principalParts})`} ${word.pronunciation} ${word.partOfSpeech}
${word.definition}
${md(word.details)}
${word.etymology === null && word.related === null ? '' : `
`} ${word.etymology === null ? '' : `
Etymology (Root Word${originalWord.etymology.length !== 1 ? 's' : ''})
${md(word.etymology).replace(/<\/?p>/g, '')}
`} ${word.related === null ? '' : `
Related Word${originalWord.related.length !== 1 ? 's' : ''}
`}
`; }); } document.getElementById('entries').innerHTML = wordsHTML; // Show Search Results const searchTerm = getSearchTerm(); const filters = getSearchFilters(); let resultsText = searchTerm !== '' || !filters.allPartsOfSpeechChecked ? (words ? words.length : 0).toString() + ' Results' : ''; resultsText += !filters.allPartsOfSpeechChecked ? ' (Filtered)' : ''; document.getElementById('searchResults').innerHTML = resultsText; } export function renderInfoModal(content) { const modalElement = document.createElement('section'); modalElement.classList.add('modal', 'info-modal'); modalElement.innerHTML = ` `; document.body.appendChild(modalElement); setupInfoModal(modalElement); }