import md from 'snarkdown'; import { removeTags, slugify } from '../helpers'; import { getWordsStats, wordExists } from './utilities'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search'; import { showSection } from './displayToggles'; import { setupSearchFilters, setupWordOptionButtons, setupPagination, setupWordOptionSelections, setupEditFormButtons } from './setupListeners'; import { DEFAULT_PAGE_SIZE } from '../constants'; export function renderAll() { renderDictionaryDetails(); renderPartsOfSpeech(); renderWords(); } 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); document.getElementById('dictionaryName').innerHTML = dictionaryName; } export function renderDescription() { const descriptionHTML = md(removeTags(window.currentDictionary.description)); detailsPanel.innerHTML = descriptionHTML; } export function renderDetails() { const { partsOfSpeech, alphabeticalOrder } = window.currentDictionary; const { phonology, 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, phonotactics } = 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 phonologyHTML = `

Phonology

${consonantHTML}
${vowelHTML}
${blendHTML}`; const { onset, nucleus, coda, exceptions } = 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 exceptionsHTML = exceptions.trim().length > 0 ? '

Exceptions:

' + md(removeTags(exceptions)) + '
' : ''; const phonotacticsHTML = `

Phonotactics

${onsetHTML}
${nucleusHTML}
${codaHTML}
${exceptionsHTML}`; const orthographyHTML = '

Orthography

Notes:

' + md(removeTags(orthography.notes)) + '
'; const grammarHTML = '

Grammar

Notes:

' + md(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() { let optionsHTML = '', searchHTML = ''; window.currentDictionary.partsOfSpeech.forEach(partOfSpeech => { partOfSpeech = removeTags(partOfSpeech); optionsHTML += ``; searchHTML += ``; }); Array.from(document.getElementsByClassName('part-of-speech-select')).forEach(select => { const selectedValue = select.value; select.innerHTML = optionsHTML; select.value = selectedValue; }); document.getElementById('searchPartsOfSpeech').innerHTML = searchHTML; setupSearchFilters(); } export function renderWords() { const words = getMatchingSearchWords(); let wordsHTML = ''; const pageSize = window.localStorage.getItem('pageSize') ? parseInt(window.localStorage.getItem('pageSize')) : DEFAULT_PAGE_SIZE; const currentPage = window.hasOwnProperty('currentPage') ? window.currentPage : 0; const start = currentPage * pageSize; const end = typeof words[start + pageSize] !== 'undefined' ? start + pageSize : words.length - 1; words.slice(start, end).forEach(originalWord => { let detailsMarkdown = removeTags(originalWord.longDefinition); const references = detailsMarkdown.match(/\{\{.+?\}\}/g); if (references && Array.isArray(references)) { new Set(references).forEach(reference => { const wordToFind = reference.replace(/\{\{|\}\}/g, ''); const existingWordId = wordExists(wordToFind, true); if (existingWordId !== false) { const wordMarkdownLink = `[${wordToFind}](#${existingWordId})`; detailsMarkdown = detailsMarkdown.replace(new RegExp(reference, 'g'), wordMarkdownLink); } }); } const word = highlightSearchTerm({ name: removeTags(originalWord.name), pronunciation: removeTags(originalWord.pronunciation), partOfSpeech: removeTags(originalWord.partOfSpeech), simpleDefinition: removeTags(originalWord.simpleDefinition), longDefinition: detailsMarkdown, wordId: originalWord.wordId, }); wordsHTML += `

${word.name}

${word.pronunciation} ${word.partOfSpeech} Options
${word.simpleDefinition}
${md(word.longDefinition)}
`; }); document.getElementById('entries').innerHTML = wordsHTML; setupWordOptionButtons(); setupWordOptionSelections(); // Show Search Results const searchTerm = getSearchTerm(); const filters = getSearchFilters(); let resultsText = searchTerm !== '' || !filters.allPartsOfSpeechChecked ? words.length.toString() + ' Results' : ''; resultsText += !filters.allPartsOfSpeechChecked ? ' (Filtered)' : ''; document.getElementById('searchResults').innerHTML = resultsText; renderPagination(); } export function renderPagination() { const numWords = window.currentDictionary.words.length; const pageSize = window.localStorage.getItem('pageSize') ? parseInt(window.localStorage.getItem('pageSize')) : DEFAULT_PAGE_SIZE; const pages = Math.floor(numWords / pageSize); const currentPage = window.hasOwnProperty('currentPage') ? window.currentPage : 0; if (pages > 0) { let paginationHTML = (currentPage > 0 ? '« Previous' : '') + '' + (currentPage < pages - 1 ? 'Next »' : ''); Array.from(document.getElementsByClassName('pagination')).forEach(pagination => { pagination.innerHTML = paginationHTML; }); setupPagination(); } } export function renderEditForm() { const wordId = parseInt(this.id.replace('edit_', '')); const word = window.currentDictionary.words.find(w => w.wordId === wordId); if (word) { const editForm = `
Save Changes Cancel Edit
`; document.getElementById(wordId.toString()).innerHTML = editForm; setupEditFormButtons(); } }