Add Related Words field & display

This commit is contained in:
Robbie Antenesse 2020-05-04 00:16:52 -06:00
parent 69c0539370
commit e2d3164136
5 changed files with 39 additions and 10 deletions

View File

@ -36,6 +36,7 @@ export const DEFAULT_DICTIONARY = {
definition: '', definition: '',
details: '', details: '',
etymology: [], etymology: [],
related: [],
wordId: 0 wordId: 0
}, */ }, */
], ],

View File

@ -62,6 +62,8 @@ export function renderWords() {
details: parseReferences(removeTags(originalWord.details)), details: parseReferences(removeTags(originalWord.details)),
etymology: typeof originalWord.etymology === 'undefined' || originalWord.etymology.length < 1 ? null etymology: typeof originalWord.etymology === 'undefined' || originalWord.etymology.length < 1 ? null
: originalWord.etymology.map(root => getWordReferenceMarkdown(removeTags(root))).join(', '), : originalWord.etymology.map(root => getWordReferenceMarkdown(removeTags(root))).join(', '),
related: typeof originalWord.related === 'undefined' || originalWord.related.length < 1 ? null
: originalWord.related.map(relatedWord => getWordReferenceMarkdown(removeTags(relatedWord))).join(', '),
wordId: originalWord.wordId, wordId: originalWord.wordId,
}); });
const homonymnNumber = getHomonymnNumber(originalWord); const homonymnNumber = getHomonymnNumber(originalWord);
@ -88,11 +90,15 @@ export function renderWords() {
<dd class="details"> <dd class="details">
${md(word.details)} ${md(word.details)}
</dd> </dd>
${word.etymology === null ? '' : `<hr> ${word.etymology === null && word.related === null ? '' : `<hr>`}
<dt>Etymology <small>(Root Word${originalWord.etymology.length !== 1 ? 's' : ''})</small></dt> ${word.etymology === null ? '' : `<dt>Etymology <small>(Root Word${originalWord.etymology.length !== 1 ? 's' : ''})</small></dt>
<dd class="etymology"> <dd class="etymology">
${md(word.etymology).replace(/<\/?p>/g, '')} ${md(word.etymology).replace(/<\/?p>/g, '')}
</dd>`} </dd>`}
${word.related === null ? '' : `<dt>Related Word${originalWord.related.length !== 1 ? 's' : ''}</dt>
<dd class="related">
${md(word.related).replace(/<\/?p>/g, '')}
</dd>`}
</dl> </dl>
</article>`; </article>`;
}); });
@ -174,6 +180,9 @@ export function renderEditForm(wordId = false) {
<label>Etymology / Root Words<br> <label>Etymology / Root Words<br>
<input id="wordEtymology_${wordId}" maxlength="2500" placeholder="comma,separated,root,words" value="${word.hasOwnProperty('etymology') ? word.etymology : ''}"> <input id="wordEtymology_${wordId}" maxlength="2500" placeholder="comma,separated,root,words" value="${word.hasOwnProperty('etymology') ? word.etymology : ''}">
</label> </label>
<label>Related Words<br>
<input id="wordRelated_${wordId}" maxlength="2500" placeholder="comma,separated,related,words" value="${word.hasOwnProperty('related') ? word.related : ''}">
</label>
</div> </div>
<div id="wordErrorMessage_${wordId}"></div> <div id="wordErrorMessage_${wordId}"></div>
<a class="button edit-save-changes" id="editWordButton_${wordId}">Save Changes</a> <a class="button edit-save-changes" id="editWordButton_${wordId}">Save Changes</a>

View File

@ -182,7 +182,8 @@ export function submitWordForm() {
partOfSpeech = document.getElementById('wordPartOfSpeech').value, partOfSpeech = document.getElementById('wordPartOfSpeech').value,
definition = document.getElementById('wordDefinition').value, definition = document.getElementById('wordDefinition').value,
details = document.getElementById('wordDetails').value, details = document.getElementById('wordDetails').value,
etymology = document.getElementById('wordEtymology').value; etymology = document.getElementById('wordEtymology').value,
related = document.getElementById('wordRelated').value;
const word = { const word = {
name: removeTags(name).trim(), name: removeTags(name).trim(),
@ -197,6 +198,10 @@ export function submitWordForm() {
word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0); word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0);
} }
if (removeTags(related).trim() !== '') {
word.related = removeTags(related).split(',').map(w => w.trim()).filter(w => w.length > 0);
}
if (validateWord(word)) { if (validateWord(word)) {
addWord(word); addWord(word);
sortWords(true); sortWords(true);
@ -280,7 +285,8 @@ export function confirmEditWord(id) {
partOfSpeech = document.getElementById('wordPartOfSpeech_' + wordId).value, partOfSpeech = document.getElementById('wordPartOfSpeech_' + wordId).value,
definition = document.getElementById('wordDefinition_' + wordId).value, definition = document.getElementById('wordDefinition_' + wordId).value,
details = document.getElementById('wordDetails_' + wordId).value, details = document.getElementById('wordDetails_' + wordId).value,
etymology = document.getElementById('wordEtymology_' + wordId).value; etymology = document.getElementById('wordEtymology_' + wordId).value,
related = document.getElementById('wordRelated_' + wordId).value;
const word = { const word = {
name: removeTags(name).trim(), name: removeTags(name).trim(),
@ -295,6 +301,10 @@ export function confirmEditWord(id) {
word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0); word.etymology = removeTags(etymology).split(',').map(w => w.trim()).filter(w => w.length > 0);
} }
if (removeTags(related).trim() !== '') {
word.related = removeTags(related).split(',').map(w => w.trim()).filter(w => w.length > 0);
}
if (validateWord(word, wordId)) { if (validateWord(word, wordId)) {
if (confirm(`Are you sure you want to save changes to "${word.name}"?`)) { if (confirm(`Are you sure you want to save changes to "${word.name}"?`)) {
document.getElementById('editForm_' + wordId).classList.add('done'); document.getElementById('editForm_' + wordId).classList.add('done');

View File

@ -215,7 +215,7 @@ WHERE dictionary=$dictionary";
} }
public function getWords ($user, $dictionary) { public function getWords ($user, $dictionary) {
$query = "SELECT words.*, words_advanced.etymology FROM words $query = "SELECT words.*, words_advanced.etymology, words_advanced.related FROM words
LEFT JOIN words_advanced ON words_advanced.dictionary = words.dictionary AND words_advanced.word_id = words.word_id LEFT JOIN words_advanced ON words_advanced.dictionary = words.dictionary AND words_advanced.word_id = words.word_id
JOIN dictionaries ON dictionaries.id = words.dictionary JOIN dictionaries ON dictionaries.id = words.dictionary
WHERE words.dictionary=$dictionary AND dictionaries.user=$user"; WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
@ -233,10 +233,14 @@ WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
'wordId' => intval($row['word_id']), 'wordId' => intval($row['word_id']),
); );
if (!is_null($row['etymology'])) { if (!is_null($row['etymology']) && $row['etymology'] !== '') {
$word['etymology'] = explode(',', $row['etymology']); $word['etymology'] = explode(',', $row['etymology']);
} }
if (!is_null($row['related']) && $row['related'] !== '') {
$word['related'] = explode(',', $row['related']);
}
return $word; return $word;
}, $results); }, $results);
} }
@ -263,7 +267,7 @@ WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
} }
$query1 = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES '; $query1 = 'INSERT INTO words (dictionary, word_id, name, pronunciation, part_of_speech, definition, details, last_updated, created_on) VALUES ';
$query2 = 'INSERT INTO words_advanced (dictionary, word_id, etymology) VALUES '; $query2 = 'INSERT INTO words_advanced (dictionary, word_id, etymology, related) VALUES ';
$params1 = array(); $params1 = array();
$params2 = array(); $params2 = array();
$word_ids = array(); $word_ids = array();
@ -285,10 +289,11 @@ WHERE words.dictionary=$dictionary AND dictionaries.user=$user";
$params1[] = $last_updated; $params1[] = $last_updated;
$params1[] = $word['createdOn']; $params1[] = $word['createdOn'];
$query2 .= "(?, ?, ?), "; $query2 .= "(?, ?, ?, ?), ";
$params2[] = $dictionary; $params2[] = $dictionary;
$params2[] = $word['wordId']; $params2[] = $word['wordId'];
$params2[] = isset($word['etymology']) ? implode(',', $word['etymology']) : null; $params2[] = isset($word['etymology']) ? implode(',', $word['etymology']) : '';
$params2[] = isset($word['related']) ? implode(',', $word['related']) : '';
} }
$query1 = trim($query1, ', ') . ' ON DUPLICATE KEY UPDATE $query1 = trim($query1, ', ') . ' ON DUPLICATE KEY UPDATE
name=VALUES(name), name=VALUES(name),
@ -299,7 +304,8 @@ details=VALUES(details),
last_updated=VALUES(last_updated), last_updated=VALUES(last_updated),
created_on=VALUES(created_on)'; created_on=VALUES(created_on)';
$query2 = trim($query2, ', ') . ' ON DUPLICATE KEY UPDATE $query2 = trim($query2, ', ') . ' ON DUPLICATE KEY UPDATE
etymology=VALUES(etymology)'; etymology=VALUES(etymology),
related=VALUES(related)';
$results1 = $this->db->execute($query1, $params1); $results1 = $this->db->execute($query1, $params1);

View File

@ -146,6 +146,9 @@
<label>Etymology / Root Words<br> <label>Etymology / Root Words<br>
<input id="wordEtymology" maxlength="2500" placeholder="comma,separated,root,words"> <input id="wordEtymology" maxlength="2500" placeholder="comma,separated,root,words">
</label> </label>
<label>Related Words<br>
<input id="wordRelated" maxlength="2500" placeholder="comma,separated,related,words">
</label>
</div> </div>
<div id="wordErrorMessage"></div> <div id="wordErrorMessage"></div>
<a class="button" id="addWordButton">Add Word</a> <a class="button" id="addWordButton">Add Word</a>