import md from 'marked';
import { removeTags, slugify } from '../helpers';
import { getWordsStats, getHomonymnNumber, hasToken } from './utilities';
import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search';
import { showSection } from './displayToggles';
import {
setupSearchFilters,
setupWordOptionButtons,
setupPagination,
setupWordOptionSelections,
setupWordEditFormButtons,
setupMaximizeModal,
setupInfoModal,
setupIPATable,
setupIPAFields
} from './setupListeners';
import { getPaginationData } from './pagination';
import { getOpenEditForms, parseReferences } from './wordManagement';
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);
const name = document.getElementById('dictionaryName');
name.innerHTML = dictionaryName;
const isPublic = hasToken() && window.currentDictionary.settings.isPublic;
if (isPublic && !document.getElementById('dictionaryShare')) {
const shareLink = document.createElement('a');
shareLink.id = 'dictionaryShare';
shareLink.classList.add('button');
shareLink.style.float = 'right';
shareLink.href = 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;
shareLink.target = '_blank';
shareLink.title = 'Public Link to Dictionary';
shareLink.innerHTML = '➦';
name.parentElement.insertBefore(shareLink, name);
}
}
export function renderDescription() {
const descriptionHTML = md(removeTags(window.currentDictionary.description));
document.getElementById('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(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 openEditForms = getOpenEditForms();
let words = false;
const isPublic = hasToken() && window.currentDictionary.settings.isPublic;
if (window.currentDictionary.words.length === 0) {
wordsHTML = `
- Use the Word Form to create words or click the Help button below!
`;
} else {
words = getMatchingSearchWords();
if (words.length === 0) {
wordsHTML = `
- Edit your search or filter to show words.
`;
}
if (openEditForms.length > 0) {
// Clone the dom nodes
openEditForms.forEach((wordFormId, index) => {
openEditForms[index] = document.getElementById(wordFormId.toString()).cloneNode(true);
});
}
// const { pageStart, pageEnd } = getPaginationData(words);
// words.slice(pageStart, pageEnd).forEach(originalWord => {
words.forEach(originalWord => {
let detailsMarkdown = removeTags(originalWord.details);
const references = detailsMarkdown.match(/\{\{.+?\}\}/g);
if (references && Array.isArray(references)) {
detailsMarkdown = parseReferences(detailsMarkdown, references);
}
const word = highlightSearchTerm({
name: removeTags(originalWord.name),
pronunciation: removeTags(originalWord.pronunciation),
partOfSpeech: removeTags(originalWord.partOfSpeech),
definition: removeTags(originalWord.definition),
details: detailsMarkdown,
wordId: originalWord.wordId,
});
const homonymnNumber = getHomonymnNumber(originalWord);
const shareLink = window.currentDictionary.hasOwnProperty('externalID')
? window.location.pathname + window.currentDictionary.externalID + '/' + word.wordId : '';
wordsHTML += `
${word.name}${homonymnNumber > 0 ? ' ' + homonymnNumber.toString() + '' : ''}
${word.pronunciation}
${word.partOfSpeech}
${isPublic ? `➦` : ''}
Options
- ${word.definition}
-
${md(word.details)}
`;
});
}
document.getElementById('entries').innerHTML = wordsHTML;
if (openEditForms.length > 0) {
// Clone the dom nodes
openEditForms.forEach(editForm => {
const entryElement = document.getElementById(editForm.id);
entryElement.parentNode.replaceChild(editForm, entryElement);
});
setupWordEditFormButtons();
}
setupWordOptionButtons();
setupWordOptionSelections();
// 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;
// renderPagination(words);
}
export function renderPagination(filteredWords) {
const paginationData = getPaginationData(filteredWords);
if (paginationData.pages > 0) {
let paginationHTML = (paginationData.currentPage > 0 ? '« Previous' : '')
+ ''
+ (paginationData.currentPage < paginationData.pages - 1 ? 'Next »' : '');
Array.from(document.getElementsByClassName('pagination')).forEach(pagination => {
pagination.innerHTML = paginationHTML;
});
setupPagination();
}
}
export function renderEditForm(wordId = false) {
wordId = typeof wordId.target === 'undefined' ? wordId : parseInt(this.id.replace('edit_', ''));
const word = window.currentDictionary.words.find(w => w.wordId === wordId);
if (word) {
const ipaPronunciationField = ``;
const plainPronunciationField = ``;
const editForm = ``;
document.getElementById(wordId.toString()).innerHTML = editForm;
setupWordEditFormButtons();
renderPartsOfSpeech(true);
}
}
export function renderIPATable(ipaTableButton) {
ipaTableButton = typeof ipaTableButton.target === 'undefined' || ipaTableButton.target === '' ? ipaTableButton : ipaTableButton.target;
const label = ipaTableButton.parentElement.innerText.replace(/(Field Help|IPA Chart)/g, '').trim();
const textBox = ipaTableButton.parentElement.querySelector('input');
import('./KeyboardFire/phondue/ipa-table.html').then(html => {
const modalElement = document.createElement('section');
modalElement.classList.add('modal', 'ipa-table-modal');
modalElement.innerHTML = `
`;
document.body.appendChild(modalElement);
setupIPAFields();
setupIPATable(modalElement, textBox);
});
}
export function renderMaximizedTextbox(maximizeButton) {
maximizeButton = typeof maximizeButton.target === 'undefined' || maximizeButton.target === '' ? maximizeButton : maximizeButton.target;
console.log(maximizeButton.parentElement);
const label = maximizeButton.parentElement.innerText.replace(/(\*|Maximize)/g, '').trim();
const textBox = maximizeButton.parentElement.querySelector('textarea');
const modalElement = document.createElement('section');
modalElement.classList.add('modal', 'maximize-modal');
modalElement.innerHTML = `
`;
document.body.appendChild(modalElement);
setupMaximizeModal(modalElement, textBox);
}
export function renderInfoModal(content) {
const modalElement = document.createElement('section');
modalElement.classList.add('modal', 'info-modal');
modalElement.innerHTML = `
`;
document.body.appendChild(modalElement);
setupInfoModal(modalElement);
}