|
1 | | -constenterpriseDocumentationCookie="cfdoctype=enterprise"; |
2 | | -constARGOHUB_MAIN_PATH=`/${SITE_GITOPS_COLLECTION}/`; |
3 | | -constenterpriseDocTypeLockKey="enterpriseDocTypeLock"; |
| 1 | +constGITOPS_DOC_COOKIE='cfdoctype=gitops' |
| 2 | +constIS_GITOPS_DOC_COOKIE_SET=document.cookie.includes(GITOPS_DOC_COOKIE) |
4 | 3 |
|
5 | | -functioncheckIfEnterpriseDocumentationCookieSet(){ |
6 | | -returndocument.cookie.includes(enterpriseDocumentationCookie); |
7 | | -} |
| 4 | +handleRedirect() |
8 | 5 |
|
9 | 6 | asyncfunctiongetArgoHubRedirectURL(currentPath){ |
10 | | -currentPath=currentPath.replace(SITE_BASE_URL,""); |
| 7 | +currentPath=currentPath.replace(SITE_BASE_URL,'') |
11 | 8 |
|
12 | | -constredirectMap=awaitfetchRedirectMap(); |
| 9 | +constredirectMap=awaitfetchRedirectMap() |
13 | 10 |
|
14 | | -constnewPath=redirectMap[currentPath]; |
15 | | -if(!newPath)returnnull; |
| 11 | +constnewPath=redirectMap[currentPath] |
| 12 | +if(!newPath)returnnull |
16 | 13 |
|
17 | 14 | constnewURL= |
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 | | -returnnewURL; |
| 19 | +returnnewURL |
23 | 20 | } |
24 | 21 |
|
25 | 22 | asyncfunctionhandleRedirect(){ |
26 | | -if(shouldSkipRedirect())return; |
| 23 | +if(SITE_IS_GITOPS_COLLECTION||!IS_GITOPS_DOC_COOKIE_SET)return |
27 | 24 |
|
28 | | -constargoHubRedirectURL=awaitgetArgoHubRedirectURL(location.pathname); |
29 | | -if(!argoHubRedirectURL)return; |
| 25 | +constargoHubRedirectURL=awaitgetArgoHubRedirectURL(location.pathname) |
| 26 | +if(!argoHubRedirectURL)return |
30 | 27 |
|
31 | | -location.href=argoHubRedirectURL; |
| 28 | +location.href=argoHubRedirectURL |
32 | 29 | } |
33 | 30 |
|
34 | 31 | asyncfunctionfetchRedirectMap(){ |
35 | | -constresponse=awaitfetch( |
36 | | -`${SITE_BASE_URL}/assets/js/src/argohub-redirect-mapping.json` |
37 | | -); |
| 32 | +constresponse=awaitfetch(`${SITE_BASE_URL}/assets/js/src/argohub-redirect-mapping.json`) |
38 | 33 | if(!response.ok){ |
39 | | -thrownewError("Failed to fetch the collections redirect map."); |
| 34 | +thrownewError('Failed to fetch the collections redirect map.') |
| 35 | +} |
| 36 | +returnresponse.json() |
| 37 | +} |
| 38 | + |
| 39 | +functionsetGitOpsDocumentationCookie(){ |
| 40 | +constmaxAge=2592000 |
| 41 | +configureGitOpsDocumentationCookie(maxAge) |
| 42 | +} |
| 43 | + |
| 44 | +functionremoveGitOpsDocumentationCookie(){ |
| 45 | +configureGitOpsDocumentationCookie(0) |
| 46 | +} |
| 47 | + |
| 48 | +functionconfigureGitOpsDocumentationCookie(maxAge){ |
| 49 | +letcookie=`${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 | +functiontoggleSegmentDropdown(){ |
| 63 | +constselect=document.querySelector('.custom-select') |
| 64 | +select.classList.toggle('open') |
| 65 | +} |
| 66 | + |
| 67 | +functionhandleDropdownKeydown(event){ |
| 68 | +constselect=document.querySelector('.custom-select') |
| 69 | +constoptions=select.querySelectorAll('.option') |
| 70 | +constisOpen=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 | | -returnresponse.json(); |
42 | 87 | } |
43 | 88 |
|
44 | | -functionisEnterpriseLockPresent(){ |
45 | | -constenterpriseDocTypeLock=localStorage.getItem(enterpriseDocTypeLockKey); |
46 | | -return!!enterpriseDocTypeLock; |
| 89 | +functionhandleOptionKeydown(event,option,selectedValue){ |
| 90 | +constselect=document.querySelector('.custom-select') |
| 91 | +constoptions=select.querySelectorAll('.option') |
| 92 | +constcurrentIndex=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 | | -functionshouldSkipRedirect(){ |
50 | | -returntrue;// 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 | +asyncfunctionselectSegmentOption(option,selectedValue){ |
| 120 | +constselectDisplay=document.querySelector('.select-display') |
| 121 | + |
| 122 | +selectDisplay.textContent=option.textContent |
| 123 | + |
| 124 | +constredirectMap=awaitfetchRedirectMap() |
| 125 | + |
| 126 | +constpathname=window.location.pathname |
| 127 | +constcurrentPath=pathname.replace(SITE_BASE_URL,'') |
| 128 | + |
| 129 | +if(selectedValue==='enterprise'){ |
| 130 | +removeGitOpsDocumentationCookie() |
| 131 | + |
| 132 | +constenterprisePath=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 | +}elseif(selectedValue==='gitops'){ |
| 140 | +setGitOpsDocumentationCookie() |
| 141 | + |
| 142 | +constgitOpsPath=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 | +constselect=document.querySelector('.custom-select') |
| 154 | +if(!select.contains(e.target)){ |
| 155 | +select.classList.remove('open') |
| 156 | +} |
| 157 | +}) |