Skip to content

Commit

Permalink
vue: FormLegacyValuespec: reduced needless registration of event list…
Browse files Browse the repository at this point in the history
…eners

Change-Id: Ibd92c1b422379cc4529a1e0320605e17596af939
  • Loading branch information
schnetzzz committed Jan 16, 2025
1 parent a7038a7 commit 39ab6ca
Showing 1 changed file with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ onMounted(() => {
window['cmk'].forms.enable_dynamic_form_elements(legacyDOM.value!)
// @ts-expect-error comes from different javascript file
window['cmk'].valuespecs.initialize_autocompleters(legacyDOM.value!)
addEventListeners()
updateEventListeners()

const observer = new MutationObserver(() => {
collectData()
// On every mutation, we need to remove and re-add the event listeners
removeEventListeners()
addEventListeners()
updateEventListeners()
})

observer.observe(legacyDOM.value!, {
Expand All @@ -68,18 +66,18 @@ onMounted(() => {
})

onBeforeUnmount(() => {
removeEventListeners()
})

function addEventListeners() {
legacyDOM.value!.querySelectorAll(QUERY_INPUT_OBSERVER).forEach((element) => {
element.addEventListener('input', collectData)
element.removeEventListener('input', collectData)
})
}
})

function removeEventListeners() {
function updateEventListeners() {
legacyDOM.value!.querySelectorAll(QUERY_INPUT_OBSERVER).forEach((element) => {
element.removeEventListener('input', collectData)
if (element.getAttribute('data-has-event-listener') === 'true') {
return
}
element.setAttribute('data-has-event-listener', 'true')
element.addEventListener('input', collectData)
})
}

Expand Down

0 comments on commit 39ab6ca

Please sign in to comment.