|
1 |
| -const enterpriseDocumentationCookie = "cfdoctype=enterprise"; |
2 |
| -const ARGOHUB_MAIN_PATH = `/${SITE_GITOPS_COLLECTION}/`; |
3 |
| -const enterpriseDocTypeLockKey = "enterpriseDocTypeLock"; |
| 1 | +const GITOPS_DOC_COOKIE = 'cfdoctype=gitops' |
| 2 | +const IS_GITOPS_DOC_COOKIE_SET = document.cookie.includes(GITOPS_DOC_COOKIE) |
4 | 3 |
|
5 |
| -function checkIfEnterpriseDocumentationCookieSet() { |
6 |
| - return document.cookie.includes(enterpriseDocumentationCookie); |
7 |
| -} |
| 4 | +handleRedirect() |
8 | 5 |
|
9 | 6 | async function getArgoHubRedirectURL(currentPath) {
|
10 |
| - currentPath = currentPath.replace(SITE_BASE_URL, ""); |
| 7 | + currentPath = currentPath.replace(SITE_BASE_URL, '') |
11 | 8 |
|
12 |
| - const redirectMap = await fetchRedirectMap(); |
| 9 | + const redirectMap = await fetchRedirectMap() |
13 | 10 |
|
14 |
| - const newPath = redirectMap[currentPath]; |
15 |
| - if (!newPath) return null; |
| 11 | + const newPath = redirectMap[currentPath] |
| 12 | + if (!newPath) return null |
16 | 13 |
|
17 | 14 | const newURL =
|
18 |
| - newPath === ARGOHUB_MAIN_PATH |
| 15 | + newPath === `/${SITE_GITOPS_COLLECTION}/` |
19 | 16 | ? `${location.href}${SITE_GITOPS_COLLECTION}`
|
20 |
| - : location.href.replace(currentPath, newPath); |
| 17 | + : location.href.replace(currentPath, newPath) |
21 | 18 |
|
22 |
| - return newURL; |
| 19 | + return newURL |
23 | 20 | }
|
24 | 21 |
|
25 | 22 | async function handleRedirect() {
|
26 |
| - if (shouldSkipRedirect()) return; |
| 23 | + if (SITE_IS_GITOPS_COLLECTION || !IS_GITOPS_DOC_COOKIE_SET) return |
27 | 24 |
|
28 |
| - const argoHubRedirectURL = await getArgoHubRedirectURL(location.pathname); |
29 |
| - if (!argoHubRedirectURL) return; |
| 25 | + const argoHubRedirectURL = await getArgoHubRedirectURL(location.pathname) |
| 26 | + if (!argoHubRedirectURL) return |
30 | 27 |
|
31 |
| - location.href = argoHubRedirectURL; |
| 28 | + location.href = argoHubRedirectURL |
32 | 29 | }
|
33 | 30 |
|
34 | 31 | async function fetchRedirectMap() {
|
35 |
| - const response = await fetch( |
36 |
| - `${SITE_BASE_URL}/assets/js/src/argohub-redirect-mapping.json` |
37 |
| - ); |
| 32 | + const response = await fetch(`${SITE_BASE_URL}/assets/js/src/argohub-redirect-mapping.json`) |
38 | 33 | if (!response.ok) {
|
39 |
| - throw new Error("Failed to fetch the collections redirect map."); |
| 34 | + throw new Error('Failed to fetch the collections redirect map.') |
| 35 | + } |
| 36 | + return response.json() |
| 37 | +} |
| 38 | + |
| 39 | +function setGitOpsDocumentationCookie() { |
| 40 | + const maxAge = 2592000 |
| 41 | + configureGitOpsDocumentationCookie(maxAge) |
| 42 | +} |
| 43 | + |
| 44 | +function removeGitOpsDocumentationCookie() { |
| 45 | + configureGitOpsDocumentationCookie(0) |
| 46 | +} |
| 47 | + |
| 48 | +function configureGitOpsDocumentationCookie(maxAge) { |
| 49 | + let cookie = `${GITOPS_DOC_COOKIE}; Max-Age=${maxAge}; Path=/; SameSite=Strict` |
| 50 | + |
| 51 | + if (location.protocol === 'https:') { |
| 52 | + cookie += '; Secure' |
| 53 | + } |
| 54 | + |
| 55 | + if (location.hostname.endsWith('.codefresh.io')) { |
| 56 | + cookie += '; Domain=.codefresh.io' |
| 57 | + } |
| 58 | + |
| 59 | + document.cookie = cookie |
| 60 | +} |
| 61 | + |
| 62 | +function toggleSegmentDropdown() { |
| 63 | + const select = document.querySelector('.custom-select') |
| 64 | + select.classList.toggle('open') |
| 65 | +} |
| 66 | + |
| 67 | +function handleDropdownKeydown(event) { |
| 68 | + const select = document.querySelector('.custom-select') |
| 69 | + const options = select.querySelectorAll('.option') |
| 70 | + const isOpen = select.classList.contains('open') |
| 71 | + |
| 72 | + switch (event.key) { |
| 73 | + case 'Enter': |
| 74 | + case ' ': |
| 75 | + event.preventDefault() |
| 76 | + toggleSegmentDropdown() |
| 77 | + break |
| 78 | + case 'ArrowDown': |
| 79 | + event.preventDefault() |
| 80 | + if (!isOpen) toggleSegmentDropdown() |
| 81 | + options[0].focus() |
| 82 | + break |
| 83 | + case 'Escape': |
| 84 | + if (isOpen) toggleSegmentDropdown() |
| 85 | + break |
40 | 86 | }
|
41 |
| - return response.json(); |
42 | 87 | }
|
43 | 88 |
|
44 |
| -function isEnterpriseLockPresent() { |
45 |
| - const enterpriseDocTypeLock = localStorage.getItem(enterpriseDocTypeLockKey); |
46 |
| - return !!enterpriseDocTypeLock; |
| 89 | +function handleOptionKeydown(event, option, selectedValue) { |
| 90 | + const select = document.querySelector('.custom-select') |
| 91 | + const options = select.querySelectorAll('.option') |
| 92 | + const currentIndex = Array.from(options).indexOf(option) |
| 93 | + |
| 94 | + switch (event.key) { |
| 95 | + case 'Enter': |
| 96 | + case ' ': |
| 97 | + event.preventDefault() |
| 98 | + selectSegmentOption(option, selectedValue) |
| 99 | + break |
| 100 | + case 'ArrowDown': |
| 101 | + event.preventDefault() |
| 102 | + if (currentIndex < options.length - 1) { |
| 103 | + options[currentIndex + 1].focus() |
| 104 | + } |
| 105 | + break |
| 106 | + case 'ArrowUp': |
| 107 | + event.preventDefault() |
| 108 | + if (currentIndex > 0) { |
| 109 | + options[currentIndex - 1].focus() |
| 110 | + } |
| 111 | + break |
| 112 | + case 'Escape': |
| 113 | + select.classList.remove('open') |
| 114 | + select.querySelector('.select-display').focus() |
| 115 | + break |
| 116 | + } |
47 | 117 | }
|
48 | 118 |
|
49 |
| -function shouldSkipRedirect() { |
50 |
| - return true; // Redirect temporarily disabled. Will be re-enabled with a new cookie-based mechanism. |
51 |
| - // return ( |
52 |
| - // isEnterpriseLockPresent() || |
53 |
| - // SITE_IS_GITOPS_COLLECTION || |
54 |
| - // checkIfEnterpriseDocumentationCookieSet() |
55 |
| - // ); |
| 119 | +async function selectSegmentOption(option, selectedValue) { |
| 120 | + const selectDisplay = document.querySelector('.select-display') |
| 121 | + |
| 122 | + selectDisplay.textContent = option.textContent |
| 123 | + |
| 124 | + const redirectMap = await fetchRedirectMap() |
| 125 | + |
| 126 | + const pathname = window.location.pathname |
| 127 | + const currentPath = pathname.replace(SITE_BASE_URL, '') |
| 128 | + |
| 129 | + if (selectedValue === 'enterprise') { |
| 130 | + removeGitOpsDocumentationCookie() |
| 131 | + |
| 132 | + const enterprisePath = Object.keys(redirectMap).find((key) => redirectMap[key] === currentPath) |
| 133 | + |
| 134 | + if (enterprisePath) { |
| 135 | + window.location.href = `${SITE_BASE_URL}${enterprisePath}` |
| 136 | + } else { |
| 137 | + window.location.href = `${SITE_BASE_URL}/` |
| 138 | + } |
| 139 | + } else if (selectedValue === 'gitops') { |
| 140 | + setGitOpsDocumentationCookie() |
| 141 | + |
| 142 | + const gitOpsPath = redirectMap[currentPath] |
| 143 | + |
| 144 | + if (gitOpsPath) { |
| 145 | + window.location.href = `${SITE_BASE_URL}${gitOpsPath}` |
| 146 | + } else { |
| 147 | + window.location.href = `${SITE_BASE_URL}/${SITE_GITOPS_COLLECTION}/` |
| 148 | + } |
| 149 | + } |
56 | 150 | }
|
57 | 151 |
|
58 |
| -handleRedirect(); |
| 152 | +document.addEventListener('click', (e) => { |
| 153 | + const select = document.querySelector('.custom-select') |
| 154 | + if (!select.contains(e.target)) { |
| 155 | + select.classList.remove('open') |
| 156 | + } |
| 157 | +}) |
0 commit comments