-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removes click handler before adding in cx.js
- Loading branch information
Showing
1 changed file
with
15 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
// Additional utilties in the vein of HTMX, driven by HTML element attributes. | ||
|
||
// cx-toggle : CSS class to toggle on click, either on clicked element, or on cx-target | ||
|
||
function handle_cx_toggle(e) { | ||
[...(document.querySelectorAll('[cx-toggle]'))].forEach((el) => { | ||
let klass = el.getAttribute('cx-toggle') | ||
let selector = el.getAttribute('cx-target') | ||
let target = selector ? el.closest(selector) : el; | ||
el.addEventListener("click", (_) => target.classList.toggle(klass)) | ||
}) | ||
let klass = e.currentTarget.getAttribute('cx-toggle'); | ||
let selector = e.currentTarget.getAttribute('cx-target'); | ||
let target = selector ? e.currentTarget.closest(selector) : e.currentTarget; | ||
target.classList.toggle(klass); | ||
} | ||
addEventListener("DOMContentLoaded", handle_cx_toggle); | ||
addEventListener("htmx:afterSwap", handle_cx_toggle); | ||
|
||
function add_handle_cx_toggle() { | ||
document.querySelectorAll('[cx-toggle]').forEach((el) => { | ||
el.removeEventListener('click', handle_cx_toggle); | ||
el.addEventListener('click', handle_cx_toggle); | ||
}); | ||
} | ||
|
||
addEventListener('DOMContentLoaded', add_handle_cx_toggle); | ||
addEventListener('htmx:afterSwap', add_handle_cx_toggle); |