2019-05-04 00:10:41 +02:00
|
|
|
import { renderWords } from "./render";
|
2019-05-10 21:08:03 +02:00
|
|
|
import { wordExists, addMessage, getNextId } from "./utilities";
|
2019-05-07 00:28:51 +02:00
|
|
|
import removeDiacritics from "./StackOverflow/removeDiacritics";
|
2019-05-21 23:21:09 +02:00
|
|
|
import { removeTags, getTimestampInSeconds } from "../helpers";
|
2019-05-08 23:23:46 +02:00
|
|
|
import { saveDictionary } from "./dictionaryManagement";
|
2019-05-04 00:10:41 +02:00
|
|
|
|
|
|
|
export function validateWord(word, wordId = false) {
|
|
|
|
const errorElementId = wordId === false ? 'wordErrorMessage' : 'wordErrorMessage_' + wordId,
|
|
|
|
errorElement = document.getElementById(errorElementId);
|
|
|
|
let errorMessage = '';
|
|
|
|
|
|
|
|
if (word.name === '') {
|
|
|
|
errorMessage += '<p class="bold red">Word field must not be blank.</p>';
|
|
|
|
}
|
2019-05-10 18:08:49 +02:00
|
|
|
if (word.definition === '' && word.details === '') {
|
2019-05-04 00:10:41 +02:00
|
|
|
errorMessage += '<p class="bold red">You must enter Definition or Details.</p>';
|
|
|
|
}
|
|
|
|
|
|
|
|
const { allowDuplicates, caseSensitive } = window.currentDictionary.settings;
|
|
|
|
if (!allowDuplicates) {
|
|
|
|
const foundDuplicate = wordExists(word.name, true);
|
|
|
|
if (foundDuplicate !== false) {
|
2019-05-07 19:08:58 +02:00
|
|
|
if (wordId !== false && foundDuplicate !== wordId) {
|
|
|
|
errorMessage += `<p class="bold red">"<a href="#${foundDuplicate}">${word.name}</a>" already exists, and "Prevent Duplicate Words" is turned on.${!caseSensitive ? ' <em>(Case sensitivity is turned off)</em>' : ''}</p>`;
|
|
|
|
}
|
2019-05-04 00:10:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorElement.innerHTML = errorMessage;
|
|
|
|
return errorMessage === '';
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:08:58 +02:00
|
|
|
export function sortWords(render) {
|
2019-05-04 00:10:41 +02:00
|
|
|
const { sortByDefinition } = window.currentDictionary.settings;
|
2019-05-10 18:08:49 +02:00
|
|
|
const sortBy = sortByDefinition ? 'definition' : 'name';
|
2019-05-04 00:10:41 +02:00
|
|
|
|
|
|
|
window.currentDictionary.words.sort((wordA, wordB) => {
|
2019-05-07 00:28:51 +02:00
|
|
|
if (removeDiacritics(wordA[sortBy]).toLowerCase() === removeDiacritics(wordB[sortBy]).toLowerCase()) return 0;
|
|
|
|
return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1;
|
2019-05-04 00:10:41 +02:00
|
|
|
});
|
2019-05-08 23:23:46 +02:00
|
|
|
|
|
|
|
saveDictionary();
|
2019-05-04 00:10:41 +02:00
|
|
|
|
2019-05-07 00:28:51 +02:00
|
|
|
if (render) {
|
|
|
|
renderWords();
|
|
|
|
}
|
2019-05-04 00:10:41 +02:00
|
|
|
}
|
2019-05-07 19:08:58 +02:00
|
|
|
|
2019-05-10 21:08:03 +02:00
|
|
|
export function submitWordForm() {
|
|
|
|
const name = document.getElementById('wordName').value,
|
|
|
|
pronunciation = document.getElementById('wordPronunciation').value,
|
|
|
|
partOfSpeech = document.getElementById('wordPartOfSpeech').value,
|
|
|
|
definition = document.getElementById('wordDefinition').value,
|
|
|
|
details = document.getElementById('wordDetails').value;
|
|
|
|
|
|
|
|
const word = {
|
|
|
|
name: removeTags(name).trim(),
|
|
|
|
pronunciation: removeTags(pronunciation).trim(),
|
|
|
|
partOfSpeech: removeTags(partOfSpeech).trim(),
|
|
|
|
definition: removeTags(definition).trim(),
|
|
|
|
details: removeTags(details).trim(),
|
|
|
|
wordId: getNextId(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (validateWord(word)) {
|
|
|
|
addWord(word);
|
2019-05-10 21:23:30 +02:00
|
|
|
clearWordForm();
|
2019-05-10 21:08:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 21:23:30 +02:00
|
|
|
export function clearWordForm() {
|
|
|
|
document.getElementById('wordName').value = '';
|
|
|
|
document.getElementById('wordPronunciation').value = '';
|
|
|
|
document.getElementById('wordPartOfSpeech').value = '';
|
|
|
|
document.getElementById('wordDefinition').value = '';
|
|
|
|
document.getElementById('wordDetails').value = '';
|
2019-05-10 23:31:01 +02:00
|
|
|
|
2019-05-10 21:23:30 +02:00
|
|
|
document.getElementById('wordName').focus();
|
|
|
|
}
|
|
|
|
|
2019-05-10 23:31:01 +02:00
|
|
|
export function addWord(word, render = true, message = true) {
|
2019-05-21 23:21:09 +02:00
|
|
|
const timestamp = getTimestampInSeconds();
|
|
|
|
word.lastUpdated = timestamp;
|
|
|
|
word.createdOn = timestamp;
|
2019-05-07 19:08:58 +02:00
|
|
|
window.currentDictionary.words.push(word);
|
2019-05-10 23:31:01 +02:00
|
|
|
if (message) {
|
|
|
|
addMessage(`<a href="#${word.wordId}">${word.name}</a> Created Successfully`, 10000);
|
|
|
|
}
|
2019-05-07 19:08:58 +02:00
|
|
|
sortWords(render);
|
|
|
|
}
|
|
|
|
|
2019-05-08 01:50:48 +02:00
|
|
|
export function deleteWord(wordId) {
|
|
|
|
const wordIndex = window.currentDictionary.words.findIndex(word => word.wordId === wordId);
|
2019-05-08 23:23:46 +02:00
|
|
|
if (wordIndex < 0) {
|
|
|
|
console.error('Could not find word to delete');
|
|
|
|
} else {
|
2019-05-08 01:50:48 +02:00
|
|
|
window.currentDictionary.words.splice(wordIndex, 1);
|
2019-05-09 22:34:32 +02:00
|
|
|
addMessage('Word Deleted Successfully');
|
2019-05-08 23:23:46 +02:00
|
|
|
sortWords(true);
|
2019-05-08 01:50:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:08:58 +02:00
|
|
|
export function updateWord(word, wordId) {
|
|
|
|
const wordIndex = window.currentDictionary.words.findIndex(word => word.wordId === wordId);
|
|
|
|
|
|
|
|
if (wordIndex < 0) {
|
|
|
|
console.error('Could not find word to update');
|
|
|
|
} else {
|
2019-05-21 23:21:09 +02:00
|
|
|
word.lastUpdated = getTimestampInSeconds();
|
2019-05-07 19:08:58 +02:00
|
|
|
window.currentDictionary.words[wordIndex] = word;
|
2019-05-09 22:34:32 +02:00
|
|
|
addMessage('Word Updated Successfully');
|
2019-05-07 19:08:58 +02:00
|
|
|
sortWords(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-10 21:08:03 +02:00
|
|
|
export function confirmEditWord(id) {
|
|
|
|
const wordId = typeof id.target !== 'undefined' ? parseInt(this.id.replace('editWordButton_', '')) : id;
|
|
|
|
console.log(wordId);
|
2019-05-07 19:08:58 +02:00
|
|
|
const name = document.getElementById('wordName_' + wordId).value,
|
|
|
|
pronunciation = document.getElementById('wordPronunciation_' + wordId).value,
|
|
|
|
partOfSpeech = document.getElementById('wordPartOfSpeech_' + wordId).value,
|
|
|
|
definition = document.getElementById('wordDefinition_' + wordId).value,
|
|
|
|
details = document.getElementById('wordDetails_' + wordId).value;
|
|
|
|
|
|
|
|
const word = {
|
|
|
|
name: removeTags(name).trim(),
|
|
|
|
pronunciation: removeTags(pronunciation).trim(),
|
|
|
|
partOfSpeech: removeTags(partOfSpeech).trim(),
|
2019-05-10 18:08:49 +02:00
|
|
|
definition: removeTags(definition).trim(),
|
|
|
|
details: removeTags(details).trim(),
|
2019-05-07 19:08:58 +02:00
|
|
|
wordId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (validateWord(word, wordId)) {
|
2019-05-07 20:55:47 +02:00
|
|
|
if (confirm(`Are you sure you want to save changes to "${word.name}"?`)) {
|
|
|
|
document.getElementById('editForm_' + wordId).classList.add('done');
|
2019-05-07 19:08:58 +02:00
|
|
|
updateWord(word, wordId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 20:55:47 +02:00
|
|
|
|
|
|
|
export function cancelEditWord() {
|
|
|
|
const wordId = parseInt(this.parentElement.id.replace('editForm_', ''));
|
|
|
|
if (confirm(`Are you sure you want to cancel?\n(Any changes will be lost!)`)) {
|
|
|
|
document.getElementById('editForm_' + wordId).classList.add('done');
|
|
|
|
renderWords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 01:50:48 +02:00
|
|
|
export function confirmDeleteWord(wordId) {
|
|
|
|
wordId = typeof wordId.target === 'undefined' ? wordId : parseInt(wordId.target.id.replace('delete_', ''));
|
|
|
|
const word = window.currentDictionary.words.find(w => w.wordId === wordId);
|
|
|
|
|
|
|
|
if (!word) {
|
|
|
|
console.error('Something went wrong! Couldn\'t find word with id of ' + wordId);
|
|
|
|
} else {
|
|
|
|
if (confirm(`Are you sure you want to delete "${word.name}"?`)) {
|
|
|
|
if (confirm(`Just to double-check:\nDo you really want to delete "${word.name}"?\n\nYou won't be able to undo it!`)) {
|
|
|
|
deleteWord(wordId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-07 20:55:47 +02:00
|
|
|
export function getOpenEditForms() {
|
|
|
|
const openEditForms = document.getElementsByClassName('edit-form');
|
|
|
|
const formsToReopen = [];
|
|
|
|
Array.from(openEditForms).forEach(form => {
|
|
|
|
if (!form.classList.contains('done')) {
|
|
|
|
formsToReopen.push(parseInt(form.id.replace('editForm_', '')));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return formsToReopen;
|
|
|
|
}
|