frontblocks-back-button.js
135 lines
| 1 | /** |
| 2 | * FrontBlocks Back Button |
| 3 | * |
| 4 | * @package FrontBlocks |
| 5 | * @version 1.0.0 |
| 6 | */ |
| 7 | |
| 8 | (function() { |
| 9 | 'use strict'; |
| 10 | |
| 11 | // Wait for DOM to be ready. |
| 12 | if (document.readyState === 'loading') { |
| 13 | document.addEventListener('DOMContentLoaded', init); |
| 14 | } else { |
| 15 | init(); |
| 16 | } |
| 17 | |
| 18 | function init() { |
| 19 | const backButton = document.getElementById('frbl-back-button'); |
| 20 | |
| 21 | if (!backButton) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | // Get or initialize navigation counter. |
| 26 | const STORAGE_KEY = 'frbl_nav_count'; |
| 27 | const ENTRY_URL_KEY = 'frbl_entry_url'; |
| 28 | |
| 29 | // Get current navigation count. |
| 30 | let navCount = parseInt(sessionStorage.getItem(STORAGE_KEY) || '0', 10); |
| 31 | |
| 32 | // Store entry URL if this is the first page. |
| 33 | if (navCount === 0) { |
| 34 | sessionStorage.setItem(ENTRY_URL_KEY, window.location.href); |
| 35 | } |
| 36 | |
| 37 | // Increment navigation counter. |
| 38 | navCount++; |
| 39 | sessionStorage.setItem(STORAGE_KEY, navCount.toString()); |
| 40 | |
| 41 | // Get entry URL. |
| 42 | const entryUrl = sessionStorage.getItem(ENTRY_URL_KEY); |
| 43 | const currentUrl = window.location.href; |
| 44 | |
| 45 | /** |
| 46 | * Check if button should be visible. |
| 47 | * |
| 48 | * @return {boolean} True if button should be visible. |
| 49 | */ |
| 50 | function shouldShowButton() { |
| 51 | // Don't show if this is the first page visited. |
| 52 | if (navCount <= 1) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // Don't show if we're back at the entry page. |
| 57 | if (currentUrl === entryUrl) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Don't show if there's no history. |
| 62 | if (window.history.length <= 1) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | // Initial visibility check. |
| 70 | const canShowButton = shouldShowButton(); |
| 71 | |
| 72 | if (canShowButton) { |
| 73 | // Show button after a short delay. |
| 74 | setTimeout(function() { |
| 75 | checkScrollAndShow(); |
| 76 | }, 500); |
| 77 | } |
| 78 | |
| 79 | // Handle button click. |
| 80 | backButton.addEventListener('click', function(e) { |
| 81 | e.preventDefault(); |
| 82 | |
| 83 | // Decrement counter when going back. |
| 84 | navCount = Math.max(0, navCount - 1); |
| 85 | sessionStorage.setItem(STORAGE_KEY, navCount.toString()); |
| 86 | |
| 87 | // Go back in history. |
| 88 | window.history.back(); |
| 89 | }); |
| 90 | |
| 91 | // Scroll threshold for showing/hiding button. |
| 92 | const scrollThreshold = 100; |
| 93 | |
| 94 | /** |
| 95 | * Check scroll position and show/hide button accordingly. |
| 96 | */ |
| 97 | function checkScrollAndShow() { |
| 98 | if (!canShowButton) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | const scrollTop = window.pageYOffset || document.documentElement.scrollTop; |
| 103 | |
| 104 | // Only show button if scrolled down. |
| 105 | if (scrollTop < scrollThreshold) { |
| 106 | backButton.classList.remove('frbl-show'); |
| 107 | } else { |
| 108 | backButton.classList.add('frbl-show'); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Listen to scroll events. |
| 113 | window.addEventListener('scroll', checkScrollAndShow, { passive: true }); |
| 114 | |
| 115 | // Listen to popstate (browser back/forward buttons). |
| 116 | window.addEventListener('popstate', function() { |
| 117 | // Update navigation count when using browser buttons. |
| 118 | navCount = parseInt(sessionStorage.getItem(STORAGE_KEY) || '0', 10); |
| 119 | |
| 120 | // Hide button if we're back at entry page. |
| 121 | if (window.location.href === entryUrl || navCount <= 1) { |
| 122 | backButton.classList.remove('frbl-show'); |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | // Clean up when leaving the site. |
| 127 | window.addEventListener('beforeunload', function() { |
| 128 | // Check if we're navigating to an external URL. |
| 129 | // Note: We can't always detect this, but sessionStorage will be cleared |
| 130 | // when the user closes the tab/window anyway. |
| 131 | }); |
| 132 | } |
| 133 | })(); |
| 134 | |
| 135 |