2019-07-08 15:49:46 -06:00
|
|
|
import md from 'marked';
|
|
|
|
import { removeTags } from '../../helpers';
|
|
|
|
import { getHomonymnNumber, hasToken } from '../utilities';
|
|
|
|
import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from '../search';
|
|
|
|
import {
|
|
|
|
setupWordOptionButtons,
|
2020-08-03 22:58:29 -06:00
|
|
|
// setupPagination,
|
2019-07-08 15:49:46 -06:00
|
|
|
setupWordOptionSelections,
|
|
|
|
setupWordEditFormButtons,
|
2019-07-10 16:13:01 -06:00
|
|
|
} from '../setupListeners/words';
|
2020-08-03 22:58:29 -06:00
|
|
|
// import { getPaginationData } from '../pagination';
|
2020-05-03 22:28:31 -06:00
|
|
|
import { getOpenEditForms, translateOrthography, parseReferences, getWordReferenceMarkdown } from '../wordManagement';
|
2019-07-08 15:49:46 -06:00
|
|
|
import { getPublicLink } from '../account/utilities';
|
|
|
|
import { renderPartsOfSpeech } from './details';
|
2020-07-31 15:14:41 -06:00
|
|
|
import { renderTemplateSelectOptions } from './settings';
|
2019-07-08 15:49:46 -06:00
|
|
|
|
2020-07-31 12:20:06 -06:00
|
|
|
export function renderWord(savedWord, isPublic) {
|
|
|
|
const word = highlightSearchTerm({
|
|
|
|
name: removeTags(savedWord.name),
|
|
|
|
pronunciation: removeTags(savedWord.pronunciation),
|
|
|
|
partOfSpeech: removeTags(savedWord.partOfSpeech),
|
|
|
|
definition: removeTags(savedWord.definition),
|
|
|
|
details: parseReferences(removeTags(savedWord.details)),
|
|
|
|
etymology: typeof savedWord.etymology === 'undefined' || savedWord.etymology.length < 1 ? null
|
|
|
|
: savedWord.etymology.map(root => getWordReferenceMarkdown(removeTags(root))).join(', '),
|
|
|
|
related: typeof savedWord.related === 'undefined' || savedWord.related.length < 1 ? null
|
|
|
|
: savedWord.related.map(relatedWord => getWordReferenceMarkdown(removeTags(relatedWord))).join(', '),
|
|
|
|
principalParts: typeof savedWord.principalParts === 'undefined' || savedWord.principalParts.length < 1 ? null
|
|
|
|
: savedWord.principalParts.join(', '),
|
|
|
|
wordId: savedWord.wordId,
|
|
|
|
});
|
|
|
|
const homonymnNumber = getHomonymnNumber(savedWord);
|
|
|
|
const shareLink = window.currentDictionary.hasOwnProperty('externalID') ? getPublicLink() + '/' + word.wordId : '';
|
|
|
|
|
|
|
|
let wordNameDisplay = translateOrthography(word.name);
|
|
|
|
|
|
|
|
return `<article class="entry" id="${word.wordId}">
|
|
|
|
<header>
|
|
|
|
<h4 class="word"><span class="orthographic-translation">${wordNameDisplay}</span>${homonymnNumber > 0 ? ' <sub>' + homonymnNumber.toString() + '</sub>' : ''}</h4>
|
|
|
|
${word.principalParts === null ? '' : `<span class="principalParts">(${word.principalParts})</span>`}
|
|
|
|
<span class="pronunciation">${word.pronunciation}</span>
|
|
|
|
<span class="part-of-speech">${word.partOfSpeech}</span>
|
|
|
|
${isPublic ? `<a class="small button share-link" href="${shareLink}" target="_blank" title="Public Link to Word">➦</a>` : ''}
|
|
|
|
<span class="small button word-option-button">Options</span>
|
|
|
|
<div class="word-option-list" style="display:none;">
|
|
|
|
<div class="word-option" id="edit_${word.wordId}">Edit</div>
|
|
|
|
<div class="word-option" id="delete_${word.wordId}">Delete</div>
|
|
|
|
</div>
|
|
|
|
</header>
|
|
|
|
<dl>
|
|
|
|
<dt class="definition">${word.definition}</dt>
|
|
|
|
<dd class="details">
|
|
|
|
${md(word.details)}
|
|
|
|
</dd>
|
|
|
|
${word.etymology === null && word.related === null ? '' : `<hr>`}
|
|
|
|
${word.etymology === null ? '' : `<dt>Etymology <small>(Root Word${savedWord.etymology.length !== 1 ? 's' : ''})</small></dt>
|
|
|
|
<dd class="etymology">
|
|
|
|
${md(word.etymology).replace(/<\/?p>/g, '')}
|
|
|
|
</dd>`}
|
|
|
|
${word.related === null ? '' : `<dt>Related Word${savedWord.related.length !== 1 ? 's' : ''}</dt>
|
|
|
|
<dd class="related">
|
|
|
|
${md(word.related).replace(/<\/?p>/g, '')}
|
|
|
|
</dd>`}
|
|
|
|
</dl>
|
|
|
|
</article>`;
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:49:46 -06:00
|
|
|
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 = `<article class="entry">
|
|
|
|
<header>
|
|
|
|
<h4 class="word">No Words Created</h4>
|
|
|
|
</header>
|
|
|
|
<dl>
|
|
|
|
<dt class="definition">Use the Word Form to create words or click the Help button below!</dt>
|
|
|
|
</dl>
|
|
|
|
</article>`;
|
|
|
|
} else {
|
|
|
|
words = getMatchingSearchWords();
|
|
|
|
|
|
|
|
if (words.length === 0) {
|
|
|
|
wordsHTML = `<article class="entry">
|
|
|
|
<header>
|
|
|
|
<h4 class="word">No Search Results</h4>
|
|
|
|
</header>
|
|
|
|
<dl>
|
|
|
|
<dt class="definition">Edit your search or filter to show words.</dt>
|
|
|
|
</dl>
|
|
|
|
</article>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-07-31 12:20:06 -06:00
|
|
|
// words.slice(pageStart, pageEnd).forEach(savedWord => {
|
2022-08-26 12:44:31 -06:00
|
|
|
words.forEach(savedWord => {
|
2020-07-31 12:20:06 -06:00
|
|
|
wordsHTML += renderWord(savedWord, isPublic);
|
2019-07-08 15:49:46 -06:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-08-03 22:58:29 -06:00
|
|
|
// export function renderPagination(filteredWords) {
|
|
|
|
// const paginationData = getPaginationData(filteredWords);
|
|
|
|
|
|
|
|
// if (paginationData.pages > 0) {
|
|
|
|
// let paginationHTML = (paginationData.currentPage > 0 ? '<span class="button prev-button">« Previous</span>' : '')
|
|
|
|
// + '<select class="page-selector">';
|
|
|
|
// for (let i = 0; i < paginationData.pages; i++) {
|
|
|
|
// paginationHTML += `<option value="${i}"${paginationData.currentPage === i ? ' selected' : ''}>Page ${i + 1}</option>`;
|
|
|
|
// }
|
|
|
|
// paginationHTML += '</select>'
|
|
|
|
// + (paginationData.currentPage < paginationData.pages - 1 ? '<span class="button next-button">Next »</span>' : '');
|
|
|
|
|
|
|
|
// Array.from(document.getElementsByClassName('pagination')).forEach(pagination => {
|
|
|
|
// pagination.innerHTML = paginationHTML;
|
|
|
|
// });
|
|
|
|
|
|
|
|
// setupPagination();
|
|
|
|
// }
|
|
|
|
// }
|
2019-07-08 15:49:46 -06:00
|
|
|
|
|
|
|
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) {
|
2020-10-18 14:33:15 -06:00
|
|
|
const escapeQuotes = (value) => value.replace(/"/g, '"');
|
2020-07-31 11:39:54 -06:00
|
|
|
const wordHasAdvancedFields = (word.hasOwnProperty('etymology') && word.etymology)
|
|
|
|
|| (word.hasOwnProperty('related') && word.related) || (word.hasOwnProperty('principalParts') && word.principalParts);
|
2020-10-18 14:33:15 -06:00
|
|
|
const ipaPronunciationField = `<input id="wordPronunciation_${wordId}" class="ipa-field" maxlength="200" value="${escapeQuotes(word.pronunciation)}"><br>
|
2019-07-08 15:49:46 -06:00
|
|
|
<a class="label-help-button ipa-field-help-button">Field Help</a>`;
|
2020-10-18 14:33:15 -06:00
|
|
|
const plainPronunciationField = `<input id="wordPronunciation_${wordId}" maxlength="200" value="${escapeQuotes(word.pronunciation)}">`;
|
2019-07-08 15:49:46 -06:00
|
|
|
const editForm = `<form id="editForm_${wordId}" class="edit-form">
|
|
|
|
<label>Word<span class="red">*</span><br>
|
2020-10-18 14:33:15 -06:00
|
|
|
<input id="wordName_${wordId}" maxlength="200" value="${escapeQuotes(word.name)}">
|
2019-07-08 15:49:46 -06:00
|
|
|
</label>
|
|
|
|
<label>Pronunciation<a class="label-button ipa-table-button">IPA Chart</a><br>
|
|
|
|
${window.settings.useIPAPronunciationField ? ipaPronunciationField : plainPronunciationField}
|
|
|
|
</label>
|
|
|
|
<label>Part of Speech<br>
|
|
|
|
<select id="wordPartOfSpeech_${wordId}" class="part-of-speech-select">
|
2020-10-18 14:33:15 -06:00
|
|
|
<option value="${escapeQuotes(word.partOfSpeech)}" selected>${word.partOfSpeech}</option>
|
2019-07-08 15:49:46 -06:00
|
|
|
</select>
|
|
|
|
</label>
|
|
|
|
<label>Definition<span class="red">*</span><br>
|
2020-10-18 14:33:15 -06:00
|
|
|
<input id="wordDefinition_${wordId}" maxlength="2500" value="${escapeQuotes(word.definition)}" placeholder="Equivalent words">
|
2019-07-08 15:49:46 -06:00
|
|
|
</label>
|
|
|
|
<label>Details<span class="red">*</span><a class="label-button maximize-button">Maximize</a><br>
|
|
|
|
<textarea id="wordDetails_${wordId}" placeholder="Markdown formatting allowed">${word.details}</textarea>
|
|
|
|
</label>
|
2020-04-30 00:38:45 -06:00
|
|
|
<label>
|
2020-07-31 11:39:54 -06:00
|
|
|
<a id="expandAdvancedForm_${wordId}" class="small button expand-advanced-form">${wordHasAdvancedFields || window.settings.showAdvanced ? 'Hide' : 'Show'} Advanced Fields</a>
|
2020-04-30 00:38:45 -06:00
|
|
|
</label>
|
2020-07-31 11:39:54 -06:00
|
|
|
<div id="advancedForm_${wordId}" class="advanced-word-form" style="display:${wordHasAdvancedFields || window.settings.showAdvanced ? 'block' : 'none'};">
|
2020-07-31 15:14:41 -06:00
|
|
|
<label>Details Field Templates
|
|
|
|
<select id="templateSelect_${wordId}" class="template-select">
|
|
|
|
</select>
|
|
|
|
<small>Choose one to fill the details field. (Note: Will erase anything currently there.)</small>
|
|
|
|
</label>
|
2019-10-01 14:41:09 -06:00
|
|
|
<label>Etymology / Root Words<br>
|
2020-10-18 14:33:15 -06:00
|
|
|
<input id="wordEtymology_${wordId}" maxlength="2500" placeholder="comma,separated,root,words" value="${word.hasOwnProperty('etymology') ? escapeQuotes(word.etymology.toString()) : ''}">
|
2019-10-01 14:41:09 -06:00
|
|
|
</label>
|
2020-05-04 00:16:52 -06:00
|
|
|
<label>Related Words<br>
|
2020-10-18 14:33:15 -06:00
|
|
|
<input id="wordRelated_${wordId}" maxlength="2500" placeholder="comma,separated,related,words" value="${word.hasOwnProperty('related') ? escapeQuotes(word.related.toString()) : ''}">
|
2020-05-04 00:16:52 -06:00
|
|
|
</label>
|
2020-05-07 23:57:27 -06:00
|
|
|
<label>Principal Parts<a href="https://en.wikipedia.org/wiki/Principal_parts" target="_blank" class="label-button">What's This?</a><br>
|
2020-10-18 14:33:15 -06:00
|
|
|
<input id="wordPrincipalParts_${wordId}" maxlength="2500" placeholder="comma,separated,principal,parts" value="${word.hasOwnProperty('principalParts') ? escapeQuotes(word.principalParts.toString()) : ''}">
|
2020-05-07 23:57:27 -06:00
|
|
|
</label>
|
2019-10-01 14:41:09 -06:00
|
|
|
</div>
|
2019-07-08 15:49:46 -06:00
|
|
|
<div id="wordErrorMessage_${wordId}"></div>
|
|
|
|
<a class="button edit-save-changes" id="editWordButton_${wordId}">Save Changes</a>
|
|
|
|
<a class="button edit-cancel">Cancel Edit</a>
|
|
|
|
</form>`;
|
|
|
|
|
|
|
|
document.getElementById(wordId.toString()).innerHTML = editForm;
|
|
|
|
setupWordEditFormButtons();
|
|
|
|
renderPartsOfSpeech(true);
|
2020-07-31 15:14:41 -06:00
|
|
|
renderTemplateSelectOptions();
|
2019-07-08 15:49:46 -06:00
|
|
|
}
|
|
|
|
}
|