HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vm8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: afleverb (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/drakkar_site_dev/wp-content/themes/blocksy/static/js/frontend/generic-accordion.js
import { whenTransitionEnds } from './helpers/when-transition-ends'

const computeHasMarginManipulation = (childrenWrap, el) => {
	const m = getComputedStyle(el).marginBottom

	// return parseFloat(m) > 0 && el.nextElementSibling === childrenWrap
	return el.nextElementSibling === childrenWrap
}

// support 3 strategies:
// 1. data-target on el
// 2. when no data-target, find aria-hidden element on the same level
// 3. when no data-target attribute is present, find aria-hidden element on
//    upper level.
//
// If you can't find target — do nothing.
const findTargetForEl = (el) => {
	// case 1
	if (el.hasAttribute('data-target')) {
		return document.querySelector(el.getAttribute('data-target'))
	}

	// case 2
	const maybeCurrentLevel = el.parentNode.querySelector('[aria-hidden]')

	if (maybeCurrentLevel) {
		return maybeCurrentLevel
	}

	// case 3
	const maybeUpperLevel =
		el.parentNode.parentNode.querySelector('[aria-hidden]')

	if (maybeUpperLevel) {
		return maybeUpperLevel
	}

	// When nothing found -- do nothing
	return null
}

const showContent = (childrenWrap, el) => {
	const hasMarginManipulation = computeHasMarginManipulation(childrenWrap, el)

	if (hasMarginManipulation) {
		el.setAttribute('aria-expanded', 'true')
	}

	requestAnimationFrame(() => {
		if (hasMarginManipulation) {
			const m = getComputedStyle(el).marginBottom

			// Revert back aria-expanded
			el.setAttribute('aria-expanded', 'false')

			childrenWrap.firstElementChild.prevStyle =
				childrenWrap.firstElementChild.getAttribute('style')

			childrenWrap.firstElementChild.style.marginTop = m
		}

		childrenWrap.setAttribute('aria-hidden', 'false')

		const actualHeight = childrenWrap.getBoundingClientRect().height

		childrenWrap.style.height = '0px'
		childrenWrap.style.opacity = '0'

		requestAnimationFrame(() => {
			childrenWrap.classList.add('is-animating')

			requestAnimationFrame(() => {
				childrenWrap.style.height = `${actualHeight}px`
				childrenWrap.style.opacity = '1'

				whenTransitionEnds(childrenWrap, () => {
					childrenWrap.classList.remove('is-animating')
					childrenWrap.removeAttribute('style')

					if (hasMarginManipulation) {
						if (childrenWrap.firstElementChild.prevStyle) {
							childrenWrap.firstElementChild.style =
								childrenWrap.firstElementChild.prevStyle
						} else {
							childrenWrap.firstElementChild.removeAttribute(
								'style'
							)
						}
					}

					el.setAttribute('aria-expanded', 'true')
				})
			})
		})
	})
}

const hideContent = (childrenWrap, el, cb) => {
	const hasMarginManipulation = computeHasMarginManipulation(childrenWrap, el)

	if (hasMarginManipulation) {
		const m = getComputedStyle(el).marginBottom

		childrenWrap.firstElementChild.prevStyle =
			childrenWrap.firstElementChild.getAttribute('style')

		childrenWrap.firstElementChild.style.marginTop = m

		if (el.getAttribute('style')) {
			el.prevStyle = el.getAttribute('style')
		}

		el.style.marginBottom = '0px'
	}

	requestAnimationFrame(() => {
		const actualHeight = childrenWrap.getBoundingClientRect().height

		childrenWrap.style.height = `${actualHeight}px`
		childrenWrap.style.opacity = '1'
		childrenWrap.classList.add('is-animating')

		requestAnimationFrame(() => {
			childrenWrap.style.height = '0px'
			childrenWrap.style.opacity = '0'

			whenTransitionEnds(childrenWrap, () => {
				childrenWrap.classList.remove('is-animating')

				childrenWrap.removeAttribute('style')

				if (hasMarginManipulation) {
					if (childrenWrap.firstElementChild.prevStyle) {
						childrenWrap.firstElementChild.style =
							childrenWrap.firstElementChild.prevStyle
					} else {
						childrenWrap.firstElementChild.removeAttribute('style')
					}

					if (el.prevStyle) {
						el.style = el.prevStyle
					} else {
						el.removeAttribute('style')
					}
				}

				cb()
			})
		})
	})
}

export const mount = (el, { event }) => {
	event.stopPropagation()
	event.preventDefault()

	const targetEl = findTargetForEl(el)

	if (!targetEl) {
		return
	}

	const isExpanded = targetEl.getAttribute('aria-hidden') === 'false'

	if (isExpanded) {
		hideContent(targetEl, el, () => {
			el.setAttribute('aria-expanded', 'false')
			targetEl.setAttribute('aria-hidden', 'true')
		})

		return
	}

	if (typeof el.dataset.closeOthers !== 'undefined') {
		const parent = el.closest('.ct-accordion-tab').parentNode
		const toggles = parent.querySelectorAll(
			'.ct-expandable-trigger[aria-expanded="true"]'
		)

		toggles.forEach((toggle) => {
			const targetEl = findTargetForEl(toggle)

			if (targetEl) {
				hideContent(targetEl, el, () => {
					toggle.setAttribute('aria-expanded', 'false')
					targetEl.setAttribute('aria-hidden', 'true')
				})
			}
		})
	}

	showContent(targetEl, el)
}