From ab00863b732c702510ea4fbd020d56bbc1188fdb Mon Sep 17 00:00:00 2001 From: Hans5958 Date: Thu, 14 Apr 2022 21:03:17 +0700 Subject: [PATCH] Make exporting and view work --- web/_js/draw.js | 43 +++++++++++++++++++++++++++------ web/_js/time.js | 64 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 84 insertions(+), 23 deletions(-) diff --git a/web/_js/draw.js b/web/_js/draw.js index 3e9da5eb..5a5958d9 100644 --- a/web/_js/draw.js +++ b/web/_js/draw.js @@ -223,15 +223,32 @@ function initDraw(){ description: document.getElementById("descriptionField").value, website: document.getElementById("websiteField").value, subreddit: document.getElementById("subredditField").value, - center: calculateCenter(path), - path: path, + center: {}, + path: {}, }; - if (startPeriodField.value === endPeriodField.value) { - exportObject.period = [startPeriodField.value] - } else if (startPeriodField.value * 1 < endPeriodField.value * 1) { - exportObject.period = [startPeriodField.value + "-" + endPeriodField.value] + pathWithPeriodsTemp = pathWithPeriods.concat() + + console.log(pathWithPeriodsTemp) + + // calculateCenter(path) + + for (let i = pathWithPeriodsTemp.length - 1; i > 0; i--) { + for (let j = 0; j < i; j++) { + if (JSON.stringify(pathWithPeriodsTemp[i][1]) === JSON.stringify(pathWithPeriodsTemp[j][1])) { + pathWithPeriodsTemp[j][0] = pathWithPeriodsTemp[i][0] + ', ' + pathWithPeriodsTemp[j][0] + pathWithPeriodsTemp.splice(i, 1) + break + } + } } + + pathWithPeriodsTemp.forEach(([key, value]) => { + // TODO: Compress periods on something like 0-13, 14. + exportObject.path[key] = value + exportObject.center[key] = calculateCenter(value) + }) + var jsonString = JSON.stringify(exportObject, null, "\t"); var textarea = document.getElementById("exportString"); jsonString = jsonString.split("\n"); @@ -632,4 +649,16 @@ function updatePath(newPath = path) { undoHistory = [] // TODO: Able to click finish when one period has it. finishButton.disabled = path.length < 3; -} \ No newline at end of file +} + +// function compressPeriod(periodsString) { +// let periodStrings = periodsString.split(", ") +// let validPeriods = new Set() +// periodStrings.forEach(periodString => { +// let [start, end] = parsePeriod(periodString) +// for (var i = start; i <= end; i++) { +// validPeriods.add(i) +// } +// }) +// validPeriods = [...validPeriods].sort() +// } \ No newline at end of file diff --git a/web/_js/time.js b/web/_js/time.js index d519a04e..bb1aea3b 100644 --- a/web/_js/time.js +++ b/web/_js/time.js @@ -145,25 +145,41 @@ async function updateTime(currentPeriod) { // TEMP ATLAS ONLY ON LAST TIMESTAMP atlas = [] for ( var atlasIndex in atlasAll ) { - var validPeriods = atlasAll[atlasIndex].period - var isValid = false - if (validPeriods === undefined) validPeriods = defaultPeriod + "" - if (typeof validPeriods === "string") { - validPeriods.split(', ').some(period => { - if (period.search('-') + 1) { - var [before, after] = period.split('-') - if (currentPeriod >= before && currentPeriod <= after) { - return atlas.push(atlasAll[atlasIndex]) - } - } else { - var single = period - if (single == currentPeriod) { - return atlas.push(atlasAll[atlasIndex]) + var pathChosen, centerChosen + + if (Array.isArray(atlasAll[atlasIndex].path)) { + if (currentPeriod !== defaultPeriod) continue + pathChosen = atlasAll[atlasIndex].path + centerChosen = atlasAll[atlasIndex].center + } else { + var validPeriods2 = Object.keys(atlasAll[atlasIndex].path) + var chosenIndex + + for (let i in validPeriods2) { + let validPeriods = validPeriods2[i].split(', ') + for (let j in validPeriods) { + let [start, end] = parsePeriod(validPeriods[j]) + if (isOnPeriod(start, end, currentPeriod)) { + chosenIndex = i + break } } - }) + if (chosenIndex) break + } + + if (chosenIndex === undefined) continue + pathChosen = Object.values(atlasAll[atlasIndex].path)[chosenIndex] + centerChosen = Object.values(atlasAll[atlasIndex].center)[chosenIndex] } - if (isValid) atlas.push(atlasAll[atlasIndex]) + + console.log(pathChosen) + if (pathChosen === undefined) continue + + atlas.push({ + ...atlasAll[atlasIndex], + path: pathChosen, + center: centerChosen, + }) } dispatchTimeUpdateEvent(currentPeriod, atlas) if (typeof configObject.timestamp === "number") tooltip.querySelector('p').textContent = new Date(configObject.timestamp*1000).toUTCString() @@ -175,3 +191,19 @@ tooltip.parentElement.addEventListener('mouseenter', () => tooltip.style.left = window.addEventListener('resize', () => tooltip.style.left = (((timelineSlider.offsetWidth)*(timelineSlider.value-1)/(timelineSlider.max-1)) - tooltip.offsetWidth/2) + "px") +function isOnPeriod(start, end, current = period) { + console.log(start, end, current, current >= start && current <= end) + return current >= start && current <= end +} + +function parsePeriod(periodString) { + periodString = periodString + "" + // TODO: Support for multiple/alternative types of canvas + if (periodString.search('-') + 1) { + var [start, end] = periodString.split('-').map(i => parseInt(i)) + return [start, end] + } else { + let periodNew = parseInt(periodString) + return [periodNew, periodNew] + } +} \ No newline at end of file