Save and load from localStorage; fix csv arrays in edit modal

This commit is contained in:
Robbie Antenesse 2019-05-08 15:23:46 -06:00
parent 29488f2ed5
commit 22f8c0885a
5 changed files with 43 additions and 22 deletions

View File

@ -50,4 +50,6 @@ export const DEFAULT_DICTIONARY = {
createdOn: 0,
};
export const DEFAULT_PAGE_SIZE = 50;
export const DEFAULT_PAGE_SIZE = 50;
export const LOCAL_STORAGE_KEY = 'dictionary';

View File

@ -1,14 +1,13 @@
import './main.scss';
import { DEFAULT_DICTIONARY } from './constants';
import setupListeners from './js/setupListeners';
import { renderAll } from './js/render';
import { cloneObject } from './helpers';
import { generateRandomWords } from './js/utilities';
import { loadDictionary } from './js/dictionaryManagement';
function initialize() {
console.log('initializing');
window.currentDictionary = cloneObject(DEFAULT_DICTIONARY);
loadDictionary();
// generateRandomWords(100);
setupListeners();
renderAll();

View File

@ -1,5 +1,6 @@
import { renderDictionaryDetails, renderPartsOfSpeech } from "./render";
import { removeTags } from "../helpers";
import { removeTags, cloneObject } from "../helpers";
import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY } from "../constants";
export function updateDictionary () {
@ -38,18 +39,18 @@ export function openEditModal() {
document.getElementById('editModal').style.display = '';
}
export function save() {
export function saveEditModal() {
window.currentDictionary.name = removeTags(document.getElementById('editName').value.trim());
window.currentDictionary.specification = removeTags(document.getElementById('editSpecification').value.trim());
window.currentDictionary.description = removeTags(document.getElementById('editDescription').value.trim());
window.currentDictionary.partsOfSpeech = document.getElementById('editPartsOfSpeech').value.split(',').map(val => val.trim());
window.currentDictionary.partsOfSpeech = document.getElementById('editPartsOfSpeech').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.consonants = document.getElementById('editConsonants').value.split(',').map(val => val.trim());
window.currentDictionary.details.phonology.vowels = document.getElementById('editVowels').value.split(',').map(val => val.trim());
window.currentDictionary.details.phonology.blends = document.getElementById('editBlends').value.split(',').map(val => val.trim());
window.currentDictionary.details.phonology.phonotactics.onset = document.getElementById('editOnset').value.split(',').map(val => val.trim());
window.currentDictionary.details.phonology.phonotactics.nucleus = document.getElementById('editNucleus').value.split(',').map(val => val.trim());
window.currentDictionary.details.phonology.phonotactics.coda = document.getElementById('editCoda').value.split(',').map(val => val.trim());
window.currentDictionary.details.phonology.consonants = document.getElementById('editConsonants').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.vowels = document.getElementById('editVowels').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.blends = document.getElementById('editBlends').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.phonotactics.onset = document.getElementById('editOnset').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.phonotactics.nucleus = document.getElementById('editNucleus').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.phonotactics.coda = document.getElementById('editCoda').value.split(',').map(val => val.trim()).filter(val => val !== '');
window.currentDictionary.details.phonology.phonotactics.exceptions = removeTags(document.getElementById('editExceptions').value.trim());
window.currentDictionary.details.orthography.notes = removeTags(document.getElementById('editOrthography').value.trim());
@ -61,15 +62,29 @@ export function save() {
window.currentDictionary.settings.isComplete = document.getElementById('editIsComplete').checked;
window.currentDictionary.settings.isPublic = document.getElementById('editIsPublic').checked;
saveDictionary();
renderDictionaryDetails();
renderPartsOfSpeech();
}
export function saveAndClose() {
save();
export function saveAndCloseEditModal() {
saveEditModal();
document.getElementById('editModal').style.display = 'none';
}
export function updateGeneralDetails() {
export function saveDictionary() {
window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(window.currentDictionary));
}
}
export function loadDictionary() {
const storedDictionary = window.localStorage.getItem(LOCAL_STORAGE_KEY);
if (storedDictionary) {
window.currentDictionary = JSON.parse(storedDictionary);
} else {
clearDictionary();
}
}
export function clearDictionary() {
window.currentDictionary = cloneObject(DEFAULT_DICTIONARY);
}

View File

@ -3,7 +3,7 @@ import { renderWords, renderEditForm, renderMaximizedTextbox, renderInfoModal }
import { validateWord, addWord, confirmEditWord, cancelEditWord, confirmDeleteWord } from './wordManagement';
import { removeTags } from '../helpers';
import { getNextId } from './utilities';
import { openEditModal, save, saveAndClose } from './dictionaryManagement';
import { openEditModal, saveEditModal, saveAndCloseEditModal } from './dictionaryManagement';
import { goToNextPage, goToPreviousPage, goToPage } from './pagination';
export default function setupListeners() {
@ -74,8 +74,8 @@ function setupEditFormInteractions() {
}
function setupEditFormButtons() {
document.getElementById('editSave').addEventListener('click', () => save());
document.getElementById('editSaveAndClose').addEventListener('click', () => saveAndClose());
document.getElementById('editSave').addEventListener('click', () => saveEditModal());
document.getElementById('editSaveAndClose').addEventListener('click', () => saveAndCloseEditModal());
setupMaximizeButtons();
}

View File

@ -2,6 +2,7 @@ import { renderWords } from "./render";
import { wordExists } from "./utilities";
import removeDiacritics from "./StackOverflow/removeDiacritics";
import { removeTags } from "../helpers";
import { saveDictionary } from "./dictionaryManagement";
export function validateWord(word, wordId = false) {
const errorElementId = wordId === false ? 'wordErrorMessage' : 'wordErrorMessage_' + wordId,
@ -37,6 +38,8 @@ export function sortWords(render) {
if (removeDiacritics(wordA[sortBy]).toLowerCase() === removeDiacritics(wordB[sortBy]).toLowerCase()) return 0;
return removeDiacritics(wordA[sortBy]).toLowerCase() > removeDiacritics(wordB[sortBy]).toLowerCase() ? 1 : -1;
});
saveDictionary();
if (render) {
renderWords();
@ -50,10 +53,12 @@ export function addWord(word, render = true) {
export function deleteWord(wordId) {
const wordIndex = window.currentDictionary.words.findIndex(word => word.wordId === wordId);
if (wordIndex > -1) {
if (wordIndex < 0) {
console.error('Could not find word to delete');
} else {
window.currentDictionary.words.splice(wordIndex, 1);
sortWords(true);
}
sortWords(true);
}
export function updateWord(word, wordId) {