Skip to content

Commit 20e66b2

Browse files
authored
Fix url replacement in cheat-sheet (#258)
Fixes issues where the href and the product selector weren't correctly updated on staging or prod when a new value was selected.
1 parent 9cfe738 commit 20e66b2

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

src/js/50-cheat-sheet-toggle.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ document.addEventListener('DOMContentLoaded', function () {
193193
const prodSelector = document.querySelector(prodSelectorID)
194194
prodSelector.dataset.current = prodSelector.options[prodSelector.selectedIndex].value
195195

196+
var versionSelector = document.querySelector('body.cheat-sheet .version-selector')
197+
196198
prodSelector.addEventListener('change', function (e) {
197199
e.stopPropagation()
198200

@@ -202,41 +204,42 @@ document.addEventListener('DOMContentLoaded', function () {
202204
return
203205
}
204206

205-
const currentProd = Object.keys(prodMatrix).find((key) => prodMatrix[key] === e.target.dataset.current)
206-
const newProd = Object.keys(prodMatrix).find((key) => prodMatrix[key] === e.target.value)
207-
const re = new RegExp(`/${currentProd}/`)
207+
const currentProd = (e.target.dataset.current === 'all') ? 'all' : Object.keys(prodMatrix).find((key) => prodMatrix[key] === e.target.dataset.current)
208+
// console.log(currentProd)
209+
const newProd = (e.target.value === 'all') ? 'all' : Object.keys(prodMatrix).find((key) => prodMatrix[key] === e.target.value)
210+
// console.log(newProd)
211+
212+
const re = new RegExp(`/${currentProd}`)
208213
let newURL
209214

210215
// if we're using a proxied path, just load the new url
211216
if (selectionFromPath) {
212-
newURL = newProd ? curURL.href.replace(re, `/${newProd}/`) : curURL.href.replace(re, '')
217+
// console.log(`using selection from path: ${selectionFromPath}`)
218+
// console.log(`current URL: ${curURL.href}`)
219+
// console.log(`regex: ${re}`)
220+
newURL = newProd ? curURL.href.replace(re, `/${newProd}`) : curURL.href.replace(re, '')
213221
} else {
222+
// console.log('no selectionFromPath')
214223
newURL = curURL.href.split('#')[0].concat(newProd).concat(curURL.hash)
215224
}
216225

217-
if (newURL) document.location.replace(newURL)
226+
// console.log(newURL)
227+
228+
if (newURL) {
229+
// console.log('replacing url with ' + newURL)
230+
document.location.replace(newURL)
231+
}
218232
})
219233

220-
var versionSelector = document.querySelector('body.cheat-sheet .version-selector')
221234
if (versionSelector) {
222235
versionSelector.addEventListener('change', function (e) {
223-
const target = e.target
224-
225-
const selectedProduct = prodSelector.selectedIndex
226-
const current = target.dataset.current
227-
const next = target.selectedOptions[0].dataset.version
228-
let newUrl
229-
if (selectionFromPath) {
230-
const re = new RegExp(`/${current}/`)
231-
newUrl = document.URL.replace(re, `/${next}/`)
232-
} else {
233-
newUrl = `${target.value}?product=${prodSelector.options[selectedProduct].value}`
234-
}
235-
236+
const current = e.target.dataset.current
237+
const next = e.target.selectedOptions[0].dataset.version
238+
const re = new RegExp(`/${current}/`)
239+
const newUrl = document.URL.replace(re, `/${next}/`)
236240
if (window.ga) {
237241
window.ga('send', 'event', 'version-select', 'From: ' + current + ';To:' + next + ';')
238242
}
239-
240243
document.location.replace(newUrl)
241244
})
242245
}
@@ -416,6 +419,9 @@ function fixURL () {
416419
// eg /docs/cypher-cheat-sheet/current/where
417420
// or /docs/cypher-cheat-sheet/5/auradb-free/
418421
// or /docs/cypher-cheat-sheet/5/auradb-free/where
422+
// or (special case) /docs/cypher-cheat-sheet/5/all
423+
424+
// console.log(`checking url ${href} for product name`)
419425

420426
const pathArr = stripTrailingSlash(url.pathname).split('/')
421427
if (pathArr[0] === '') pathArr.shift()
@@ -428,6 +434,8 @@ function fixURL () {
428434
// let version = values[0]
429435
// the second item in values should be the product
430436
let product = values[1]
437+
438+
// console.log(`product is ${product}`)
431439
// the third is a page that can be turned into a section id
432440
let possibleID = values[2]
433441
let id = ''
@@ -438,7 +446,7 @@ function fixURL () {
438446

439447
if (possibleID) {
440448
id = checkHashVariations(possibleID)
441-
console.log(id)
449+
// console.log(id)
442450
}
443451

444452
// update window.location.href
@@ -459,13 +467,17 @@ function fixURL () {
459467
if (id && Object.keys(prodMatrix).includes(product)) {
460468
window.location.hash = '#' + id
461469
const reHash = new RegExp(`/${possibleID}/?`)
462-
href = href.replace(reHash, `#${id}`)
470+
href = stripTrailingSlash(href).replace(reHash, `/#${id}`)
463471
}
464472

465473
if (href !== url.href) {
466474
window.location.replace(href)
467475
}
468476

477+
if (product === 'all') {
478+
return product
479+
}
480+
469481
return prodMatrix[product]
470482
}
471483

src/partials/nav-selectors.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<div class="dropdown">
1616
<span class="dropdown-label">{{#with (or page.attributes.cheatsheet-product 'Product')}}{{this}}{{/with}} Version</span>
1717

18-
<select data-current="{{@root.page.version}}" class="version-selector dropdown-styles">
18+
<select id="selector-version" data-current="{{@root.page.version}}" class="version-selector dropdown-styles">
1919
{{#each page.versions}}
2020
{{#unless this.prerelease}}
2121
<option

0 commit comments

Comments
 (0)