Files
cv-site/static/hyperscript/utils._hs
T
juanatsap d2330f5d48 refactor: migrate toggle and hover sync functions from JavaScript to Hyperscript
BREAKING: Removed JavaScript toggle functions in favor of organized Hyperscript architecture

Changes:
- Created organized Hyperscript file structure (no def limit with latest version):
  • static/hyperscript/utils._hs (utility functions)
  • static/hyperscript/toggles._hs (CV length, icons, theme toggles)
  • static/hyperscript/hover-sync._hs (PDF/Print hover sync + zoom highlight)

- Removed functions._hs (renamed to utils._hs for better organization)

- Emptied static/js/cv-functions.js (kept file with migration notice)
  • toggleCVLength, toggleIcons, toggleTheme → toggles._hs
  • syncPdfHover, syncPrintHover, highlightZoomControl → hover-sync._hs

- Updated templates/index.html to load all 3 new hyperscript files

- Updated tests/mjs/1-toggles.test.mjs for responsive design
  • Added viewport detection for desktop vs mobile toggles
  • Tests now adapt to screen size

Rationale:
- Test 9 confirmed NO def limit with latest hyperscript (tested up to 5 defs)
- Better separation of concerns with category-based file organization
- Aligns with server-side hypermedia pattern (HTMX + Hyperscript)
- Eliminates workaround JavaScript duplication
- 9 total def statements across 3 files (proving no limit)

Verified:
 All hyperscript files load successfully (HTTP 200)
 Hyperscript library loads without errors
 Functions work correctly in browser
 No console errors
 Test 9 (def limit) passes with 5 def statements

Related: Test 9 verification (tests/mjs/9-hyperscript-def-limit.test.mjs)
2025-11-17 16:28:52 +00:00

134 lines
3.9 KiB
Plaintext

-- ==============================================================================
-- CV Site - Hyperscript Functions
-- ==============================================================================
-- Global hyperscript functions for CV interactive features
-- Keeps HTML templates clean and behavior organized
-- ==============================================================================
-- PRINT FUNCTIONS
-- ==============================================================================
def printFriendly()
-- Store current state
set container to the first .cv-container
set paper to the first .cv-paper
set wasClean to container.classList.contains('theme-clean')
set wasLong to paper.classList.contains('cv-long')
set currentZoom to localStorage.getItem('cv-zoom') or '100'
-- Apply print-friendly settings
if wasClean is false
add .theme-clean to container
end
remove .cv-long from paper
add .cv-short to paper
-- Reset zoom for consistent printing
set #zoom-wrapper's *zoom to 1
-- Let CSS apply, then print
wait 50ms
call window.print()
-- Wait for print dialog to close, then restore
wait 100ms
-- Restore original theme
if wasClean is false
remove .theme-clean from container
end
-- Restore original length
if wasLong is true
remove .cv-short from paper
add .cv-long to paper
end
-- Restore original zoom by triggering slider input event
if currentZoom is not '100'
set #zoom-slider's value to currentZoom
send input to #zoom-slider
end
end
-- ==============================================================================
-- SCROLL BEHAVIOR
-- ==============================================================================
def initScrollBehavior()
set :lastScroll to 0
set :scrollThreshold to 100
set :keepHeaderVisible to false
end
def handleScroll()
set currentScroll to window.pageYOffset
set menu to the first .navigation-menu
set isMenuOpen to menu.classList.contains('menu-open')
-- Calculate if at bottom (within 50px)
set scrollHeight to document.documentElement.scrollHeight
set clientHeight to document.documentElement.clientHeight
set isAtBottom to (scrollHeight - currentScroll - clientHeight) < 50
-- Reset keepHeaderVisible when scrolling up
if currentScroll < :lastScroll
set :keepHeaderVisible to false
end
-- Header visibility: Scrolling down past threshold
if currentScroll > :scrollThreshold and currentScroll > :lastScroll and :keepHeaderVisible is false
add .header-hidden to .action-bar
if isMenuOpen is true
add .header-hidden to menu
end
end
-- Header visibility: Scrolling up past threshold
if currentScroll > :scrollThreshold and (currentScroll <= :lastScroll or :keepHeaderVisible is true)
remove .header-hidden from .action-bar
if isMenuOpen is true
remove .header-hidden from menu
end
end
-- Header visibility: At top
if currentScroll <= :scrollThreshold
remove .header-hidden from .action-bar
if isMenuOpen is true
remove .header-hidden from menu
end
end
-- Back to top button visibility
if currentScroll > 300
set #back-to-top's *display to 'flex'
end
if currentScroll <= 300
set #back-to-top's *display to 'none'
end
-- At-bottom class for fixed buttons
if isAtBottom
add .at-bottom to #back-to-top
add .at-bottom to #info-button
add .at-bottom to #shortcuts-button
end
if not isAtBottom
remove .at-bottom from #back-to-top
remove .at-bottom from #info-button
remove .at-bottom from #shortcuts-button
end
-- Update last scroll position
set :lastScroll to currentScroll
end
-- ==============================================================================
-- KEYBOARD SHORTCUTS
-- ==============================================================================
-- Note: Keyboard event handlers are now defined inline in the body tag
-- because hyperscript 0.9.12 doesn't support nested event handlers (on ... inside def)