2019-05-02 15:45:10 -06:00
|
|
|
import {showSection} from './displayToggles';
|
2019-05-08 11:57:08 -06:00
|
|
|
import { renderWords, renderEditForm, renderMaximizedTextbox, renderInfoModal } from './render';
|
2019-05-07 17:50:48 -06:00
|
|
|
import { validateWord, addWord, confirmEditWord, cancelEditWord, confirmDeleteWord } from './wordManagement';
|
2019-05-03 16:10:41 -06:00
|
|
|
import { removeTags } from '../helpers';
|
|
|
|
import { getNextId } from './utilities';
|
2019-05-04 00:21:55 -06:00
|
|
|
import { openEditModal, save, saveAndClose } from './dictionaryManagement';
|
2019-05-06 16:28:51 -06:00
|
|
|
import { goToNextPage, goToPreviousPage, goToPage } from './pagination';
|
2019-05-02 15:45:10 -06:00
|
|
|
|
|
|
|
export default function setupListeners() {
|
|
|
|
setupDetailsTabs();
|
2019-05-03 13:59:52 -06:00
|
|
|
setupSearchBar();
|
2019-05-08 13:01:39 -06:00
|
|
|
setupSettingsModal();
|
2019-05-03 16:10:41 -06:00
|
|
|
setupWordForm();
|
2019-05-07 17:34:07 -06:00
|
|
|
setupMobileWordFormButton();
|
2019-05-08 11:57:08 -06:00
|
|
|
setupInfoButtons();
|
2019-05-02 15:45:10 -06:00
|
|
|
}
|
|
|
|
|
2019-05-03 13:59:52 -06:00
|
|
|
function setupDetailsTabs() {
|
|
|
|
const tabs = document.querySelectorAll('#detailsSection nav li');
|
2019-05-02 15:45:10 -06:00
|
|
|
tabs.forEach(tab => {
|
|
|
|
tab.addEventListener('click', () => {
|
|
|
|
const section = tab.innerText.toLowerCase();
|
2019-05-03 21:52:19 -06:00
|
|
|
if (section === 'edit') {
|
|
|
|
openEditModal();
|
2019-05-04 00:51:44 -06:00
|
|
|
// import('../test.js').then(function (test) {
|
|
|
|
// // Render page
|
|
|
|
// test.aaa();
|
|
|
|
// });
|
2019-05-02 15:45:10 -06:00
|
|
|
} else {
|
2019-05-03 21:52:19 -06:00
|
|
|
const isActive = tab.classList.contains('active');
|
|
|
|
tabs.forEach(t => t.classList.remove('active'));
|
|
|
|
if (isActive) {
|
|
|
|
document.getElementById('detailsPanel').style.display = 'none';
|
|
|
|
} else {
|
|
|
|
tab.classList.add('active');
|
|
|
|
showSection(section);
|
|
|
|
}
|
2019-05-02 15:45:10 -06:00
|
|
|
}
|
|
|
|
});
|
2019-05-03 21:52:19 -06:00
|
|
|
});
|
2019-05-03 23:58:49 -06:00
|
|
|
setupEditFormTabs();
|
2019-05-06 14:30:57 -06:00
|
|
|
setupEditFormInteractions();
|
2019-05-03 23:58:49 -06:00
|
|
|
setupEditFormButtons();
|
2019-05-03 21:52:19 -06:00
|
|
|
}
|
|
|
|
|
2019-05-03 23:58:49 -06:00
|
|
|
function setupEditFormTabs() {
|
|
|
|
const tabs = document.querySelectorAll('#editModal nav li');
|
|
|
|
tabs.forEach(tab => {
|
|
|
|
tab.addEventListener('click', () => {
|
|
|
|
tabs.forEach(t => {
|
|
|
|
t.classList.remove('active');
|
|
|
|
document.getElementById('edit' + t.innerText + 'Tab').style.display = 'none';
|
|
|
|
});
|
|
|
|
tab.classList.add('active');
|
|
|
|
document.getElementById('edit' + tab.innerText + 'Tab').style.display = '';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-06 14:30:57 -06:00
|
|
|
function setupEditFormInteractions() {
|
|
|
|
const preventDuplicatesBox = document.getElementById('editPreventDuplicates');
|
|
|
|
preventDuplicatesBox.addEventListener('change', () => {
|
|
|
|
console.log('changed');
|
|
|
|
const caseSensitiveBox = document.getElementById('editCaseSensitive');
|
|
|
|
if (preventDuplicatesBox.checked) {
|
|
|
|
console.log('checked');
|
|
|
|
caseSensitiveBox.disabled = false;
|
|
|
|
} else {
|
|
|
|
console.log('unchecked');
|
|
|
|
caseSensitiveBox.disabled = true;
|
|
|
|
caseSensitiveBox.checked = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-03 23:58:49 -06:00
|
|
|
function setupEditFormButtons() {
|
2019-05-04 00:21:55 -06:00
|
|
|
document.getElementById('editSave').addEventListener('click', () => save());
|
|
|
|
document.getElementById('editSaveAndClose').addEventListener('click', () => saveAndClose());
|
2019-05-07 14:31:54 -06:00
|
|
|
|
|
|
|
setupMaximizeButtons();
|
2019-05-03 13:59:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function setupSearchBar() {
|
|
|
|
const searchBox = document.getElementById('searchBox'),
|
|
|
|
clearSearchButton = document.getElementById('clearSearchButton'),
|
2019-05-07 10:41:26 -06:00
|
|
|
openSearchModal = document.getElementById('openSearchModal'),
|
2019-05-08 14:09:00 -06:00
|
|
|
searchIgnoreDiacritics = document.getElementById('searchIgnoreDiacritics'),
|
2019-05-07 10:41:26 -06:00
|
|
|
searchExactWords = document.getElementById('searchExactWords'),
|
|
|
|
searchIncludeDetails = document.getElementById('searchIncludeDetails');
|
2019-05-03 13:59:52 -06:00
|
|
|
searchBox.addEventListener('change', () => {
|
|
|
|
renderWords();
|
|
|
|
});
|
|
|
|
searchBox.addEventListener('input', event => {
|
|
|
|
openSearchModal.value = event.target.value;
|
|
|
|
});
|
|
|
|
clearSearchButton.addEventListener('click', event => {
|
|
|
|
searchBox.value = '';
|
|
|
|
openSearchModal.value = '';
|
|
|
|
renderWords();
|
|
|
|
});
|
|
|
|
openSearchModal.addEventListener('click', () => {
|
|
|
|
document.getElementById('searchModal').style.display = 'block';
|
2019-05-07 10:45:25 -06:00
|
|
|
searchBox.focus();
|
2019-05-03 13:59:52 -06:00
|
|
|
});
|
2019-05-07 10:41:26 -06:00
|
|
|
|
2019-05-08 14:09:00 -06:00
|
|
|
const toggleDetailsCheck = function() {
|
2019-05-07 10:41:26 -06:00
|
|
|
if (searchExactWords.checked) {
|
|
|
|
searchIncludeDetails.checked = false;
|
|
|
|
searchIncludeDetails.disabled = true;
|
|
|
|
} else {
|
|
|
|
searchIncludeDetails.disabled = false;
|
|
|
|
searchIncludeDetails.checked = true;
|
|
|
|
}
|
2019-05-08 14:09:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
searchIgnoreDiacritics.addEventListener('change', () => {
|
|
|
|
if (searchIgnoreDiacritics.checked) {
|
|
|
|
searchExactWords.checked = false;
|
|
|
|
searchExactWords.disabled = true;
|
|
|
|
} else {
|
|
|
|
searchExactWords.disabled = false;
|
|
|
|
}
|
|
|
|
toggleDetailsCheck();
|
2019-05-07 10:41:26 -06:00
|
|
|
});
|
2019-05-08 14:09:00 -06:00
|
|
|
|
|
|
|
searchExactWords.addEventListener('change', () => toggleDetailsCheck());
|
2019-05-03 16:10:41 -06:00
|
|
|
}
|
|
|
|
|
2019-05-05 12:00:32 -06:00
|
|
|
export function setupSearchFilters() {
|
|
|
|
const searchFilters = document.querySelectorAll('#searchOptions input[type="checkbox"]');
|
|
|
|
Array.from(searchFilters).concat([searchBox]).forEach(filter => {
|
|
|
|
filter.removeEventListener('change', renderWords);
|
|
|
|
filter.addEventListener('change', renderWords);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-03 16:10:41 -06:00
|
|
|
function setupWordForm() {
|
|
|
|
const wordForm = document.getElementById('wordForm'),
|
|
|
|
addWordButton = document.getElementById('addWordButton');
|
|
|
|
wordForm.addEventListener('submit', event => {
|
|
|
|
// Allow semantic form and prevent it from getting submitted
|
|
|
|
event.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
addWordButton.addEventListener('click', () => {
|
|
|
|
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(),
|
|
|
|
simpleDefinition: removeTags(definition).trim(),
|
|
|
|
longDefinition: removeTags(details).trim(),
|
|
|
|
wordId: getNextId(),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (validateWord(word)) {
|
|
|
|
addWord(word);
|
|
|
|
}
|
|
|
|
});
|
2019-05-07 14:31:54 -06:00
|
|
|
|
|
|
|
setupMaximizeButtons();
|
2019-05-05 13:09:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function setupWordOptionButtons() {
|
|
|
|
const wordOptionButtons = document.getElementsByClassName('word-option-button');
|
|
|
|
const showWordOptions = function() {
|
|
|
|
this.parentElement.querySelector('.word-option-list').style.display = '';
|
|
|
|
}
|
|
|
|
const hideWordOptions = function(e) {
|
|
|
|
if (!e.target.classList.contains('word-option-button')) {
|
|
|
|
const allWordOptions = document.querySelectorAll('.word-option-list');
|
|
|
|
Array.from(allWordOptions).forEach(wordOptionList => {
|
|
|
|
wordOptionList.style.display = 'none';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Array.from(wordOptionButtons).forEach(button => {
|
|
|
|
button.removeEventListener('click', showWordOptions);
|
|
|
|
button.addEventListener('click', showWordOptions);
|
|
|
|
});
|
|
|
|
|
|
|
|
document.removeEventListener('click', hideWordOptions);
|
|
|
|
document.addEventListener('click', hideWordOptions);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupWordOptionSelections() {
|
|
|
|
const wordOptions = document.getElementsByClassName('word-option');
|
|
|
|
Array.from(wordOptions).forEach(option => {
|
|
|
|
switch (option.innerText) {
|
|
|
|
case 'Edit': {
|
|
|
|
option.removeEventListener('click', renderEditForm);
|
|
|
|
option.addEventListener('click', renderEditForm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Delete': {
|
2019-05-07 17:50:48 -06:00
|
|
|
option.removeEventListener('click', confirmDeleteWord);
|
|
|
|
option.addEventListener('click', confirmDeleteWord);
|
2019-05-05 13:09:38 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-05-06 16:28:51 -06:00
|
|
|
|
2019-05-08 13:01:39 -06:00
|
|
|
export function setupSettingsModal() {
|
|
|
|
document.getElementById('settingsButton').addEventListener('click', () => {
|
|
|
|
document.getElementById('settingsModal').style.display = '';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-07 10:22:32 -06:00
|
|
|
export function setupWordEditFormButtons() {
|
2019-05-06 16:50:52 -06:00
|
|
|
const saveChangesButtons = document.getElementsByClassName('edit-save-changes');
|
|
|
|
const cancelChangesButtons = document.getElementsByClassName('edit-cancel');
|
|
|
|
Array.from(saveChangesButtons).forEach(button => {
|
2019-05-07 11:08:58 -06:00
|
|
|
button.removeEventListener('click', confirmEditWord);
|
|
|
|
button.addEventListener('click', confirmEditWord);
|
2019-05-06 16:50:52 -06:00
|
|
|
});
|
|
|
|
Array.from(cancelChangesButtons).forEach(button => {
|
2019-05-07 12:55:47 -06:00
|
|
|
button.removeEventListener('click', cancelEditWord);
|
|
|
|
button.addEventListener('click', cancelEditWord);
|
2019-05-06 16:50:52 -06:00
|
|
|
});
|
2019-05-07 14:31:54 -06:00
|
|
|
|
|
|
|
setupMaximizeButtons();
|
2019-05-06 16:50:52 -06:00
|
|
|
}
|
|
|
|
|
2019-05-07 17:34:07 -06:00
|
|
|
export function setupMobileWordFormButton() {
|
|
|
|
const mobileButton = document.getElementById('mobileWordFormShow'),
|
|
|
|
wordForm = document.getElementById('wordForm');
|
|
|
|
|
|
|
|
mobileButton.addEventListener('click', () => {
|
|
|
|
if (mobileButton.innerText === '+') {
|
|
|
|
wordForm.style.display = 'block';
|
|
|
|
mobileButton.innerHTML = '×︎';
|
|
|
|
} else {
|
|
|
|
wordForm.style.display = '';
|
|
|
|
mobileButton.innerHTML = '+';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-06 16:28:51 -06:00
|
|
|
export function setupPagination() {
|
|
|
|
const nextButtons = document.getElementsByClassName('next-button'),
|
|
|
|
prevButtons = document.getElementsByClassName('prev-button'),
|
|
|
|
pageSelectors = document.getElementsByClassName('page-selector');
|
|
|
|
|
|
|
|
Array.from(nextButtons).forEach(nextButton => {
|
|
|
|
nextButton.removeEventListener('click', goToNextPage);
|
|
|
|
nextButton.addEventListener('click', goToNextPage);
|
|
|
|
});
|
|
|
|
Array.from(prevButtons).forEach(prevButton => {
|
|
|
|
prevButton.removeEventListener('click', goToPreviousPage);
|
|
|
|
prevButton.addEventListener('click', goToPreviousPage);
|
|
|
|
});
|
|
|
|
|
|
|
|
Array.from(pageSelectors).forEach(pageSelector => {
|
|
|
|
pageSelector.removeEventListener('change', goToPage);
|
|
|
|
pageSelector.addEventListener('change', goToPage);
|
|
|
|
});
|
|
|
|
}
|
2019-05-07 14:31:54 -06:00
|
|
|
|
|
|
|
export function setupMaximizeButtons() {
|
|
|
|
const maximizeButtons = document.getElementsByClassName('maximize-button');
|
|
|
|
Array.from(maximizeButtons).forEach(button => {
|
|
|
|
button.removeEventListener('click', renderMaximizedTextbox);
|
|
|
|
button.addEventListener('click', renderMaximizedTextbox);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupMaximizeModal(modal, textBox) {
|
|
|
|
const closeElements = modal.querySelectorAll('.modal-background, .close-button, .done-button'),
|
|
|
|
maximizedTextBox = modal.querySelector('textarea');
|
|
|
|
Array.from(closeElements).forEach(close => {
|
|
|
|
close.addEventListener('click', () => {
|
|
|
|
modal.parentElement.removeChild(modal);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
maximizedTextBox.addEventListener('change', () => {
|
|
|
|
textBox.value = maximizedTextBox.value;
|
|
|
|
})
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
modal.querySelector('textarea').focus();
|
|
|
|
}, 1);
|
|
|
|
}
|
2019-05-08 11:57:08 -06:00
|
|
|
|
|
|
|
export function setupInfoButtons() {
|
|
|
|
document.getElementById('helpInfoButton').addEventListener('click', () => {
|
|
|
|
import('../markdown/help.md').then(html => {
|
|
|
|
renderInfoModal(html);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
document.getElementById('termsInfoButton').addEventListener('click', () => {
|
|
|
|
import('../markdown/terms.md').then(html => {
|
|
|
|
renderInfoModal(html);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
document.getElementById('privacyInfoButton').addEventListener('click', () => {
|
|
|
|
import('../markdown/privacy.md').then(html => {
|
|
|
|
renderInfoModal(html);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setupInfoModal(modal) {
|
|
|
|
const closeElements = modal.querySelectorAll('.modal-background, .close-button');
|
|
|
|
Array.from(closeElements).forEach(close => {
|
|
|
|
close.addEventListener('click', () => {
|
|
|
|
modal.parentElement.removeChild(modal);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|