Avoid another update on hash linking, allow code-only period on default

The code-only period is just "".
This commit is contained in:
Hans5958 2022-04-29 21:11:01 +07:00
parent 24f9c397f1
commit 49f0e631cb
3 changed files with 22 additions and 9 deletions

View file

@ -43,7 +43,7 @@ function createInfoBlock(entry, isPreview) {
else {
let targetPeriod = formatPeriod(currentPeriod, currentPeriod, currentVariation)
linkElement.href = "#" + entry.id
if (targetPeriod !== defaultPeriod) linkElement.href += "/" + targetPeriod
if (targetPeriod) linkElement.href += "/" + targetPeriod
};
const linkNameElement = document.createElement("span");
linkNameElement.className = "flex-grow-1 text-break";

View file

@ -111,7 +111,15 @@ async function init() {
}
}
await updateTime(currentPeriod, currentVariation)
const hash = window.location.hash.substring(1)
const [ , period] = hash.split('/')
if (period) {
const [ , targetPeriod, targetVariation] = parsePeriod(period)
await updateTime(targetPeriod, targetVariation)
} else {
await updateTime(currentPeriod, currentVariation)
}
//console.log(document.documentElement.clientWidth, document.documentElement.clientHeight);

View file

@ -156,8 +156,13 @@ async function updateBackground(newPeriod = currentPeriod, newVariation = curren
async function updateTime(newPeriod = currentPeriod, newVariation = currentVariation, forcePeriod = false) {
document.body.dataset.canvasLoading = ""
currentPeriod = newPeriod
if (!variationsConfig[newVariation]) newVariation = defaultVariation
const variationConfig = variationsConfig[newVariation]
if (newPeriod < 0) newPeriod = 0
else if (newPeriod > variationConfig.versions.length - 1) newPeriod = variationConfig.versions.length - 1
currentPeriod = newPeriod
if (currentVariation !== newVariation) {
currentVariation = newVariation
timelineSlider.max = variationConfig.versions.length - 1;
@ -245,7 +250,7 @@ function parsePeriod(periodString) {
periodString = split[1]
}
if (periodString.search('-') + 1) {
const [start, end] = periodString.split('-').map(i => parseInt(i))
let [start, end] = periodString.split('-').map(i => parseInt(i))
return [start, end, variation]
} else if (codeReference[periodString]) {
variation = codeReference[periodString]
@ -259,11 +264,11 @@ function parsePeriod(periodString) {
function formatPeriod(start, end, variation) {
let periodString
if (start === end) periodString = start
else periodString = start + "-" + end
if (variation !== "default") {
if (start === variationsConfig[variation].default) return variationsConfig[variation].code
else return variationsConfig[variation].code + ":" + periodString
if (start === end) {
if (start === variationsConfig[variation].default) periodString = ""
else periodString = start
}
else periodString = start + "-" + end
if (variation !== "default") return variationsConfig[variation].code + ":" + periodString
return periodString
}