From 89fa069d4245aab2d63134da7fabd91027d4b144 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Fri, 13 Dec 2024 23:24:30 -0700 Subject: [PATCH] Fix event problem --- src/js/render/words.js | 4 ++-- src/js/setupListeners/index.js | 3 +-- src/js/wordManagement.js | 32 ++++++++++++++++++-------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/js/render/words.js b/src/js/render/words.js index 9388dff..061aa21 100644 --- a/src/js/render/words.js +++ b/src/js/render/words.js @@ -148,8 +148,8 @@ export function renderWords() { // } // } -export function renderEditForm(wordId = false) { - wordId = typeof wordId.target === 'undefined' ? wordId : parseInt(this.id.replace('edit_', '')); +export function renderEditForm(event = false) { + const wordId = typeof event.target === 'undefined' ? event : parseInt(event.target.id.replace('edit_', '')); const word = window.currentDictionary.words.find(w => w.wordId === wordId); if (word) { const escapeQuotes = (value) => value.replace(/"/g, '"'); diff --git a/src/js/setupListeners/index.js b/src/js/setupListeners/index.js index 4cf4347..15ab477 100644 --- a/src/js/setupListeners/index.js +++ b/src/js/setupListeners/index.js @@ -3,7 +3,7 @@ import { enableHotKeys } from '../hotkeys'; import { dismiss } from '../announcements'; import { handleDetailClicks, setupEditFormInteractions } from './details'; import { setupWordForm, handleWordFormClicks, handleWordOptionClicks, handleWordEditFormButtonClicks } from './words'; -import { setupInfoButtons, handleIPAButtonClicks, handleMaximizeButtonClicks, handleInfoButtonClicks, handleHeaderButtonClicks } from './buttons'; +import { handleIPAButtonClicks, handleMaximizeButtonClicks, handleInfoButtonClicks, handleHeaderButtonClicks } from './buttons'; import { setupTemplateChangeEvents, setupTemplateSelectOptions } from './settings'; import { setupSearchBarEvents } from './search'; @@ -15,7 +15,6 @@ export default function setupListeners() { setupSearchBarEvents(); setupWordForm(); - setupInfoButtons(); setupTemplateSelectOptions(); if (window.settings.useHotkeys) { enableHotKeys(); diff --git a/src/js/wordManagement.js b/src/js/wordManagement.js index d722bc3..e8e52f1 100644 --- a/src/js/wordManagement.js +++ b/src/js/wordManagement.js @@ -164,17 +164,21 @@ export function getWordReferenceMarkdown(reference) { return reference; } -export function expandAdvancedForm(id = false) { - const wordId = typeof id.target !== 'undefined' ? this.id.replace('expandAdvancedForm', '') : id; - const button = typeof id.target !== 'undefined' ? this : document.getElementById('expandAdvancedForm' + (!wordId ? '' : wordId)), +let expandAdvancedFormTimeout = null; +export function expandAdvancedForm(event = false) { + const wordId = typeof event.target !== 'undefined' ? event.target.id.replace('expandAdvancedForm', '') : event; + const button = typeof event.target !== 'undefined' ? event.target : document.getElementById('expandAdvancedForm' + (!wordId ? '' : wordId)), form = document.getElementById('advancedForm' + (!wordId ? '' : wordId)); - if (form.style.display !== 'block') { - button.innerText = 'Hide Advanced Fields'; - form.style.display = 'block'; - } else { - button.innerText = 'Show Advanced Fields'; - form.style.display = 'none'; - } + clearTimeout(expandAdvancedFormTimeout); + expandAdvancedFormTimeout = setTimeout(() => { + if (form.style.display !== 'block') { + button.innerText = 'Hide Advanced Fields'; + form.style.display = 'block'; + } else { + button.innerText = 'Show Advanced Fields'; + form.style.display = 'none'; + } + }, 10) } export function submitWordForm() { @@ -305,8 +309,8 @@ export function updateWord(word, wordId) { } } -export function confirmEditWord(id) { - const wordId = typeof id.target !== 'undefined' ? parseInt(this.id.replace('editWordButton_', '')) : id; +export function confirmEditWord(event) { + const wordId = typeof event.target !== 'undefined' ? parseInt(event.target.id.replace('editWordButton_', '')) : event; const name = document.getElementById('wordName_' + wordId).value, pronunciation = document.getElementById('wordPronunciation_' + wordId).value, partOfSpeech = document.getElementById('wordPartOfSpeech_' + wordId).value, @@ -345,8 +349,8 @@ export function confirmEditWord(id) { } } -export function cancelEditWord() { - const wordId = parseInt(this.parentElement.id.replace('editForm_', '')); +export function cancelEditWord(event) { + const wordId = parseInt(event.target.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();