import md from 'marked'; import { removeTags, slugify } from '../../helpers'; import { getWordsStats, hasToken } from '../utilities'; import { showSection } from '../displayToggles'; import { setupSearchFilters } from '../setupListeners/search'; import { parseReferences } from '../wordManagement'; import { getPublicLink } from '../account/utilities'; export function renderDictionaryDetails() { renderName(); const tabs = document.querySelectorAll('#detailsSection nav li'); const shownTab = Array.from(tabs).find(tab => tab.classList.contains('active')); if (shownTab) { const tabName = shownTab.innerText.toLowerCase(); showSection(tabName); } } export function renderName() { const dictionaryName = removeTags(window.currentDictionary.name) + ' ' + removeTags(window.currentDictionary.specification); const name = document.getElementById('dictionaryName'); name.innerHTML = dictionaryName; const isPublic = hasToken() && window.currentDictionary.settings.isPublic; const shareLinkElement = document.getElementById('dictionaryShare'); if (isPublic && !shareLinkElement) { const shareLink = document.createElement('a'); shareLink.id = 'dictionaryShare'; shareLink.classList.add('button'); shareLink.style.float = 'right'; shareLink.href = getPublicLink(); shareLink.target = '_blank'; shareLink.title = 'Public Link to Dictionary'; shareLink.innerHTML = '➦'; name.parentElement.insertBefore(shareLink, name); } else if (isPublic && shareLinkElement) { shareLinkElement.href = getPublicLink(); } else if (!isPublic && shareLinkElement) { shareLinkElement.parentElement.removeChild(shareLinkElement); } } export function renderDescription() { const descriptionHTML = md(parseReferences(removeTags(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(parseReferences(removeTags(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(parseReferences(removeTags(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(parseReferences(removeTags(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(parseReferences(removeTags(grammar.notes))) : '') + '
' : ''; detailsPanel.innerHTML = generalHTML + phonologyHTML + phonotacticsHTML + orthographyHTML + grammarHTML; } export function renderStats() { const wordStats = getWordsStats(); 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(); }