|
1 | | -(()=>{ |
2 | | -/* global SITE_BASE_URL, SITE_GITOPS_COLLECTION, SITE_IS_GITOPS_COLLECTION */ |
| 1 | +/* eslint-disable no-implicit-globals, strict */ |
| 2 | +/* global SITE_BASE_URL, SITE_GITOPS_COLLECTION, SITE_IS_GITOPS_COLLECTION */ |
3 | 3 |
|
4 | | -'use strict' |
| 4 | +constGITOPS_DOC_COOKIE='cfdoctype=gitops' |
| 5 | +constIS_GITOPS_DOC_COOKIE_SET=document.cookie.includes(GITOPS_DOC_COOKIE) |
5 | 6 |
|
6 | | -constGITOPS_DOC_COOKIE='cfdoctype=gitops' |
7 | | -constIS_GITOPS_DOC_COOKIE_SET=document.cookie.includes(GITOPS_DOC_COOKIE) |
| 7 | +handleRedirect() |
8 | 8 |
|
9 | | -handleRedirect() |
| 9 | +asyncfunctiongetArgoHubRedirectURL(currentPath){ |
| 10 | +currentPath=currentPath.replace(SITE_BASE_URL,'') |
10 | 11 |
|
11 | | -asyncfunctiongetArgoHubRedirectURL(currentPath){ |
12 | | -currentPath=currentPath.replace(SITE_BASE_URL,'') |
13 | | - |
14 | | -constredirectMap=awaitfetchRedirectMap() |
15 | | -constnewPath=redirectMap[currentPath] |
16 | | -if(!newPath){ |
17 | | -returnnull |
18 | | -} |
19 | | - |
20 | | -constnewURL= |
21 | | -newPath===`/${SITE_GITOPS_COLLECTION}/` |
22 | | - ?`${location.href}${SITE_GITOPS_COLLECTION}` |
23 | | - :location.href.replace(currentPath,newPath) |
24 | | - |
25 | | -returnnewURL |
| 12 | +constredirectMap=awaitfetchRedirectMap() |
| 13 | +constnewPath=redirectMap[currentPath] |
| 14 | +if(!newPath){ |
| 15 | +returnnull |
26 | 16 | } |
27 | 17 |
|
28 | | -asyncfunctionhandleRedirect(){ |
29 | | -if(SITE_IS_GITOPS_COLLECTION||!IS_GITOPS_DOC_COOKIE_SET){ |
30 | | -return |
31 | | -} |
| 18 | +constnewURL= |
| 19 | +newPath===`/${SITE_GITOPS_COLLECTION}/` |
| 20 | +?`${location.href}${SITE_GITOPS_COLLECTION}` |
| 21 | + :location.href.replace(currentPath,newPath) |
32 | 22 |
|
33 | | -constargoHubRedirectURL=awaitgetArgoHubRedirectURL(location.pathname) |
34 | | -if(!argoHubRedirectURL){ |
35 | | -return |
36 | | -} |
| 23 | +returnnewURL |
| 24 | +} |
37 | 25 |
|
38 | | -location.href=argoHubRedirectURL |
| 26 | +asyncfunctionhandleRedirect(){ |
| 27 | +if(SITE_IS_GITOPS_COLLECTION||!IS_GITOPS_DOC_COOKIE_SET){ |
| 28 | +return |
39 | 29 | } |
40 | 30 |
|
41 | | -asyncfunctionfetchRedirectMap(){ |
42 | | -constresponse=awaitfetch(`${SITE_BASE_URL}/assets/js/src/argohub-redirect-mapping.json`) |
43 | | -if(!response.ok){ |
44 | | -thrownewError('Failed to fetch the collections redirect map.') |
45 | | -} |
46 | | -returnresponse.json() |
| 31 | +constargoHubRedirectURL=awaitgetArgoHubRedirectURL(location.pathname) |
| 32 | +if(!argoHubRedirectURL){ |
| 33 | +return |
47 | 34 | } |
48 | 35 |
|
49 | | -functionsetGitOpsDocumentationCookie(){ |
50 | | -constmaxAge=2592000 |
51 | | -configureGitOpsDocumentationCookie(maxAge) |
52 | | -} |
| 36 | +location.href=argoHubRedirectURL |
| 37 | +} |
53 | 38 |
|
54 | | -functionremoveGitOpsDocumentationCookie(){ |
55 | | -configureGitOpsDocumentationCookie(0) |
| 39 | +asyncfunctionfetchRedirectMap(){ |
| 40 | +constresponse=awaitfetch(`${SITE_BASE_URL}/assets/js/src/argohub-redirect-mapping.json`) |
| 41 | +if(!response.ok){ |
| 42 | +thrownewError('Failed to fetch the collections redirect map.') |
56 | 43 | } |
| 44 | +returnresponse.json() |
| 45 | +} |
57 | 46 |
|
58 | | -functionconfigureGitOpsDocumentationCookie(maxAge){ |
59 | | -letcookie=`${GITOPS_DOC_COOKIE}; Max-Age=${maxAge}; Path=/; SameSite=Strict` |
| 47 | +functionsetGitOpsDocumentationCookie(){ |
| 48 | +constmaxAge=2592000 |
| 49 | +configureGitOpsDocumentationCookie(maxAge) |
| 50 | +} |
60 | 51 |
|
61 | | -if(location.protocol==='https:'){ |
62 | | -cookie+='; Secure' |
63 | | -} |
| 52 | +functionremoveGitOpsDocumentationCookie(){ |
| 53 | +configureGitOpsDocumentationCookie(0) |
| 54 | +} |
64 | 55 |
|
65 | | -if(location.hostname==='codefresh.io'){ |
66 | | -cookie+='; Domain=.codefresh.io' |
67 | | -} |
| 56 | +functionconfigureGitOpsDocumentationCookie(maxAge){ |
| 57 | +letcookie=`${GITOPS_DOC_COOKIE}; Max-Age=${maxAge}; Path=/; SameSite=Strict` |
68 | 58 |
|
69 | | -document.cookie=cookie |
| 59 | +if(location.protocol==='https:'){ |
| 60 | +cookie+='; Secure' |
70 | 61 | } |
71 | 62 |
|
72 | | -functiontoggleSegmentDropdown(){ |
73 | | -constselect=document.querySelector('.custom-select') |
74 | | -select.classList.toggle('open') |
| 63 | +if(location.hostname==='codefresh.io'){ |
| 64 | +cookie+='; Domain=.codefresh.io' |
75 | 65 | } |
76 | 66 |
|
77 | | -// eslint-disable-next-line no-unused-vars |
78 | | -functionhandleDropdownKeydown(event){ |
79 | | -constselect=document.querySelector('.custom-select') |
80 | | -constoptions=select.querySelectorAll('.option') |
81 | | -constisOpen=select.classList.contains('open') |
82 | | - |
83 | | -switch(event.key){ |
84 | | -case'Enter': |
85 | | -case' ': |
86 | | -event.preventDefault() |
| 67 | +document.cookie=cookie |
| 68 | +} |
| 69 | + |
| 70 | +functiontoggleSegmentDropdown(){ |
| 71 | +constselect=document.querySelector('.custom-select') |
| 72 | +select.classList.toggle('open') |
| 73 | +} |
| 74 | + |
| 75 | +// eslint-disable-next-line no-unused-vars |
| 76 | +functionhandleDropdownKeydown(event){ |
| 77 | +constselect=document.querySelector('.custom-select') |
| 78 | +constoptions=select.querySelectorAll('.option') |
| 79 | +constisOpen=select.classList.contains('open') |
| 80 | + |
| 81 | +switch(event.key){ |
| 82 | +case'Enter': |
| 83 | +case' ': |
| 84 | +event.preventDefault() |
| 85 | +toggleSegmentDropdown() |
| 86 | +break |
| 87 | +case'ArrowDown': |
| 88 | +event.preventDefault() |
| 89 | +if(!isOpen){ |
87 | 90 | toggleSegmentDropdown() |
88 | | -break |
89 | | -case'ArrowDown': |
90 | | -event.preventDefault() |
91 | | -if(!isOpen){ |
92 | | -toggleSegmentDropdown() |
93 | | -} |
94 | | -options[0].focus() |
95 | | -break |
96 | | -case'Escape': |
97 | | -if(isOpen){ |
98 | | -toggleSegmentDropdown() |
99 | | -} |
100 | | -break |
101 | | -default: |
102 | | -break |
103 | | -} |
104 | | -} |
105 | | - |
106 | | -// eslint-disable-next-line no-unused-vars |
107 | | -functionhandleOptionKeydown(event,option,selectedValue){ |
108 | | -constselect=document.querySelector('.custom-select') |
109 | | -constoptions=select.querySelectorAll('.option') |
110 | | -constcurrentIndex=Array.from(options).indexOf(option) |
111 | | - |
112 | | -switch(event.key){ |
113 | | -case'Enter': |
114 | | -case' ': |
115 | | -event.preventDefault() |
116 | | -selectSegmentOption(option,selectedValue) |
117 | | -break |
118 | | -case'ArrowDown': |
119 | | -event.preventDefault() |
120 | | -if(currentIndex<options.length-1){ |
121 | | -options[currentIndex+1].focus() |
122 | | -} |
123 | | -break |
124 | | -case'ArrowUp': |
125 | | -event.preventDefault() |
126 | | -if(currentIndex>0){ |
127 | | -options[currentIndex-1].focus() |
128 | | -} |
129 | | -break |
130 | | -case'Escape': |
131 | | -select.classList.remove('open') |
132 | | -select.querySelector('.select-display').focus() |
133 | | -break |
134 | | -default: |
135 | | -break |
136 | | -} |
| 91 | +} |
| 92 | +options[0].focus() |
| 93 | +break |
| 94 | +case'Escape': |
| 95 | +if(isOpen){ |
| 96 | +toggleSegmentDropdown() |
| 97 | +} |
| 98 | +break |
| 99 | +default: |
| 100 | +break |
137 | 101 | } |
| 102 | +} |
138 | 103 |
|
139 | | -asyncfunctionselectSegmentOption(option,selectedValue){ |
140 | | -constselectDisplay=document.querySelector('.select-display') |
141 | | -selectDisplay.textContent=option.textContent |
| 104 | +asyncfunctionselectSegmentOption(option,selectedValue){ |
| 105 | +constselectDisplay=document.querySelector('.select-display') |
| 106 | +selectDisplay.textContent=option.textContent |
142 | 107 |
|
143 | | -constredirectMap=awaitfetchRedirectMap() |
144 | | -constpathname=window.location.pathname |
145 | | -constcurrentPath=pathname.replace(SITE_BASE_URL,'') |
| 108 | +constredirectMap=awaitfetchRedirectMap() |
| 109 | +constpathname=window.location.pathname |
| 110 | +constcurrentPath=pathname.replace(SITE_BASE_URL,'') |
146 | 111 |
|
147 | | -if(selectedValue==='enterprise'){ |
148 | | -removeGitOpsDocumentationCookie() |
| 112 | +if(selectedValue==='enterprise'){ |
| 113 | +removeGitOpsDocumentationCookie() |
149 | 114 |
|
150 | | -constenterprisePath=Object.keys(redirectMap).find( |
151 | | -(key)=>redirectMap[key]===currentPath |
152 | | -) |
| 115 | +constenterprisePath=Object.keys(redirectMap).find( |
| 116 | +(key)=>redirectMap[key]===currentPath |
| 117 | +) |
153 | 118 |
|
154 | | -if(enterprisePath){ |
155 | | -window.location.href=`${SITE_BASE_URL}${enterprisePath}` |
156 | | -}else{ |
157 | | -window.location.href=`${SITE_BASE_URL}/` |
158 | | -} |
159 | | -}elseif(selectedValue==='gitops'){ |
160 | | -setGitOpsDocumentationCookie() |
| 119 | +if(enterprisePath){ |
| 120 | +window.location.href=`${SITE_BASE_URL}${enterprisePath}` |
| 121 | +}else{ |
| 122 | +window.location.href=`${SITE_BASE_URL}/` |
| 123 | +} |
| 124 | +}elseif(selectedValue==='gitops'){ |
| 125 | +setGitOpsDocumentationCookie() |
161 | 126 |
|
162 | | -constgitOpsPath=redirectMap[currentPath] |
| 127 | +constgitOpsPath=redirectMap[currentPath] |
163 | 128 |
|
164 | | -if(gitOpsPath){ |
165 | | -window.location.href=`${SITE_BASE_URL}${gitOpsPath}` |
166 | | -}else{ |
167 | | -window.location.href=`${SITE_BASE_URL}/${SITE_GITOPS_COLLECTION}/` |
168 | | -} |
| 129 | +if(gitOpsPath){ |
| 130 | +window.location.href=`${SITE_BASE_URL}${gitOpsPath}` |
| 131 | +}else{ |
| 132 | +window.location.href=`${SITE_BASE_URL}/${SITE_GITOPS_COLLECTION}/` |
169 | 133 | } |
170 | 134 | } |
171 | | - |
172 | | -document.addEventListener('click',(e)=>{ |
173 | | -constselect=document.querySelector('.custom-select') |
174 | | -if(!select.contains(e.target)){ |
| 135 | +} |
| 136 | + |
| 137 | +// eslint-disable-next-line no-unused-vars |
| 138 | +functionhandleOptionKeydown(event,option,selectedValue){ |
| 139 | +constselect=document.querySelector('.custom-select') |
| 140 | +constoptions=select.querySelectorAll('.option') |
| 141 | +constcurrentIndex=Array.from(options).indexOf(option) |
| 142 | + |
| 143 | +switch(event.key){ |
| 144 | +case'Enter': |
| 145 | +case' ': |
| 146 | +event.preventDefault() |
| 147 | +selectSegmentOption(option,selectedValue) |
| 148 | +break |
| 149 | +case'ArrowDown': |
| 150 | +event.preventDefault() |
| 151 | +if(currentIndex<options.length-1){ |
| 152 | +options[currentIndex+1].focus() |
| 153 | +} |
| 154 | +break |
| 155 | +case'ArrowUp': |
| 156 | +event.preventDefault() |
| 157 | +if(currentIndex>0){ |
| 158 | +options[currentIndex-1].focus() |
| 159 | +} |
| 160 | +break |
| 161 | +case'Escape': |
175 | 162 | select.classList.remove('open') |
176 | | -} |
177 | | -}) |
178 | | -})() |
| 163 | +select.querySelector('.select-display').focus() |
| 164 | +break |
| 165 | +default: |
| 166 | +break |
| 167 | +} |
| 168 | +} |
| 169 | + |
| 170 | +document.addEventListener('click',(e)=>{ |
| 171 | +constselect=document.querySelector('.custom-select') |
| 172 | +if(!select.contains(e.target)){ |
| 173 | +select.classList.remove('open') |
| 174 | +} |
| 175 | +}) |