atlas/web/_js/main/view.js

852 lines
23 KiB
JavaScript
Raw Normal View History

/*!
* The 2022 r/place Atlas
* Copyright (c) 2017 Roland Rytz <roland@draemm.li>
* Copyright (c) 2022 Place Atlas contributors
* Licensed under AGPL-3.0 (https://place-atlas.stefanocoding.me/license.txt)
*/
2022-05-06 09:41:22 +02:00
const linesCanvas = document.getElementById("linesCanvas")
const linesContext = linesCanvas.getContext("2d")
let hovered = []
let previousScaleZoomOrigin
let previousZoom
2022-05-06 09:41:22 +02:00
const backgroundCanvas = document.createElement("canvas")
2023-03-19 07:03:49 +01:00
backgroundCanvas.width = canvasSize.x
backgroundCanvas.height = canvasSize.y
2022-05-06 09:41:22 +02:00
const backgroundContext = backgroundCanvas.getContext("2d")
2022-05-06 09:41:22 +02:00
const wrapper = document.getElementById("wrapper")
const bottomBar = document.getElementById("bottomBar")
2022-04-09 11:27:14 +02:00
const showListButton = document.getElementById("showListButton")
const offcanvasList = document.getElementById("offcanvasList")
const bsOffcanvasList = new bootstrap.Offcanvas(offcanvasList)
const offcanvasDraw = document.getElementById("offcanvasDraw")
const bsOffcanvasDraw = new bootstrap.Offcanvas(offcanvasDraw)
2022-05-06 09:41:22 +02:00
const objectsContainer = document.getElementById("objectsList")
const closeObjectsListButton = document.getElementById("closeObjectsListButton")
const objectsListOverflowNotice = document.getElementById("objectsListOverflowNotice")
2022-04-09 11:27:14 +02:00
2022-05-06 09:41:22 +02:00
const filterInput = document.getElementById("searchList")
2022-04-09 11:27:14 +02:00
2022-05-06 09:41:22 +02:00
const entriesList = document.getElementById("entriesList")
2022-05-06 09:41:22 +02:00
let entriesListShown = false
2022-04-09 11:27:14 +02:00
2023-03-19 03:19:37 +01:00
const drawButton = document.getElementById("drawLink")
const objectEditNav = document.createElement("a")
objectEditNav.className = "btn btn-outline-primary"
objectEditNav.id = "objectEditNav"
2023-03-19 05:56:37 +01:00
objectEditNav.textContent = "Edit"
2023-03-19 03:19:37 +01:00
let atlasDisplay
2022-05-06 09:41:22 +02:00
const entriesLimit = 50
let entriesOffset = 0
const moreEntriesButton = document.createElement("button")
moreEntriesButton.innerHTML = "Show " + entriesLimit + " more"
2022-04-20 10:54:58 +02:00
moreEntriesButton.type = "button"
moreEntriesButton.className = "btn btn-primary d-block mb-2 mx-auto"
2022-05-06 09:41:22 +02:00
moreEntriesButton.id = "moreEntriesButton"
moreEntriesButton.addEventListener('click', () => {
2022-05-06 09:41:22 +02:00
buildObjectsList(null, null)
renderBackground(atlas)
2022-05-06 09:41:22 +02:00
render()
})
const moreEntriesObserver = new IntersectionObserver((entries, observer) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue
moreEntriesButton.click()
break
}
})
moreEntriesObserver.observe(moreEntriesButton)
2022-04-09 11:27:14 +02:00
2022-05-06 09:41:22 +02:00
let defaultSort = "shuffle"
document.getElementById("sort").value = defaultSort
2022-04-09 11:27:14 +02:00
2022-05-06 09:41:22 +02:00
let lastPos = [0, 0]
2022-04-09 11:27:14 +02:00
let fixed = false; // Fix hovered items in place, so that clicking on links is possible
2022-04-09 11:27:14 +02:00
filterInput.addEventListener("input", function () {
2022-05-06 09:41:22 +02:00
entriesOffset = 0
entriesList.replaceChildren()
2022-05-06 09:41:22 +02:00
entriesList.appendChild(moreEntriesButton)
2022-04-09 11:27:14 +02:00
if (this.value === "") {
2022-05-06 09:41:22 +02:00
document.getElementById("relevantOption").disabled = true
atlasDisplay = atlas.slice(0)
2022-05-06 09:41:22 +02:00
buildObjectsList(null, null)
2022-04-09 11:27:14 +02:00
} else {
2022-05-06 09:41:22 +02:00
document.getElementById("relevantOption").disabled = false
buildObjectsList(this.value.toLowerCase(), "relevant")
2022-04-09 11:27:14 +02:00
}
2022-05-06 09:41:22 +02:00
})
2022-04-09 11:27:14 +02:00
document.getElementById("sort").addEventListener("input", function () {
2023-03-17 18:30:33 +01:00
if (this.value !== "relevant") {
2022-05-06 09:41:22 +02:00
defaultSort = this.value
2022-04-09 11:27:14 +02:00
}
2022-05-06 09:41:22 +02:00
resetEntriesList(filterInput.value.toLowerCase(), this.value)
})
2022-04-09 11:27:14 +02:00
2023-04-08 04:10:22 +02:00
offcanvasDraw.addEventListener('show.bs.offcanvas', () => {
2022-05-06 09:41:22 +02:00
wrapper.classList.remove('listHidden')
wrapper.classList.add('listTransitioning')
applyView()
})
2023-04-08 04:10:22 +02:00
offcanvasDraw.addEventListener('shown.bs.offcanvas', () => {
2022-05-06 09:41:22 +02:00
wrapper.classList.remove('listTransitioning')
applyView()
2022-04-20 10:54:58 +02:00
})
2023-04-08 04:10:22 +02:00
offcanvasDraw.addEventListener('hide.bs.offcanvas', () => {
2022-05-06 09:41:22 +02:00
wrapper.classList.add('listHidden')
wrapper.classList.add('listTransitioning')
applyView()
})
2023-04-08 04:10:22 +02:00
offcanvasDraw.addEventListener('hidden.bs.offcanvas', () => {
2022-05-06 09:41:22 +02:00
wrapper.classList.remove('listTransitioning')
applyView()
2022-04-20 10:54:58 +02:00
})
2023-04-08 04:10:22 +02:00
offcanvasList.addEventListener('show.bs.offcanvas', () => {
2022-05-06 09:41:22 +02:00
wrapper.classList.remove('listHidden')
wrapper.classList.add('listTransitioning')
applyView()
})
2023-04-07 11:44:33 +02:00
offcanvasList.addEventListener('shown.bs.offcanvas', e => {
2022-05-06 09:41:22 +02:00
entriesListShown = true
wrapper.classList.remove('listTransitioning')
updateHovering(e)
applyView()
render()
updateLines()
})
2022-04-09 11:27:14 +02:00
2023-04-08 04:10:22 +02:00
offcanvasList.addEventListener('hide.bs.offcanvas', () => {
2022-05-06 09:41:22 +02:00
wrapper.classList.add('listHidden')
wrapper.classList.add('listTransitioning')
applyView()
})
2023-04-07 11:44:33 +02:00
offcanvasList.addEventListener('hidden.bs.offcanvas', e => {
2022-05-06 09:41:22 +02:00
entriesListShown = false
wrapper.classList.remove('listTransitioning')
updateHovering(e)
applyView()
render()
updateLines()
})
closeObjectsListButton.addEventListener("click", clearObjectsList)
2023-04-08 04:10:22 +02:00
bottomBar.addEventListener("mouseover", () => {
if (!fixed) clearObjectsList()
})
function clearObjectsList() {
closeObjectsListButton.classList.add("d-none")
objectsListOverflowNotice.classList.add("d-none")
entriesList.classList.remove("disableHover")
2022-05-06 09:41:22 +02:00
hovered = []
objectsContainer.replaceChildren()
updateLines()
fixed = false
render()
objectEditNav.remove()
2023-03-19 07:03:49 +01:00
document.title = pageTitle
}
2022-04-09 11:27:14 +02:00
function toggleFixed(e, tapped) {
2023-03-17 18:30:33 +01:00
if (!fixed && hovered.length === 0) {
entriesList.classList.remove("disableHover")
2022-05-06 09:41:22 +02:00
return 0
2022-04-09 11:27:14 +02:00
}
2022-05-06 09:41:22 +02:00
fixed = !fixed
if (!fixed) {
2022-05-06 09:41:22 +02:00
updateHovering(e, tapped)
render()
2022-04-09 11:27:14 +02:00
}
entriesList.classList.add("disableHover")
2022-05-06 09:41:22 +02:00
objectsListOverflowNotice.classList.add("d-none")
2022-04-09 11:27:14 +02:00
}
2022-05-06 09:41:22 +02:00
window.addEventListener("resize", updateLines)
window.addEventListener("mousemove", updateLines)
window.addEventListener("dblClick", updateLines)
window.addEventListener("wheel", updateLines)
2022-04-09 11:27:14 +02:00
2023-04-08 04:10:22 +02:00
objectsContainer.addEventListener("scroll", () => {
2022-05-06 09:41:22 +02:00
updateLines()
})
2022-04-09 11:27:14 +02:00
2023-04-08 04:10:22 +02:00
window.addEventListener("resize", () => {
2022-05-06 09:41:22 +02:00
applyView()
render()
updateLines()
2022-05-06 09:41:22 +02:00
})
2022-04-09 11:27:14 +02:00
function updateLines() {
// Line border
2022-05-06 09:41:22 +02:00
linesCanvas.width = linesCanvas.clientWidth
linesCanvas.height = linesCanvas.clientHeight
linesContext.lineCap = "round"
linesContext.lineWidth = Math.max(Math.min(zoom * 1.5, 16 * 1.5), 6)
linesContext.strokeStyle = "#222"
for (let i = 0; i < hovered.length; i++) {
2022-05-06 09:41:22 +02:00
const element = hovered[i].element
2023-03-17 18:30:33 +01:00
if (element.getBoundingClientRect().left !== 0) {
2017-04-09 02:08:35 +02:00
2022-05-06 09:41:22 +02:00
linesContext.beginPath()
// Align line based on which side the card is on
if ((element.getBoundingClientRect().left + element.clientWidth / 2) < (document.documentElement.clientWidth / 2)) {
linesContext.moveTo(
2023-03-19 07:10:21 +01:00
element.getBoundingClientRect().left + document.documentElement.scrollLeft + element.clientWidth - 5,
element.getBoundingClientRect().top + document.documentElement.scrollTop + 20
)
} else {
linesContext.moveTo(
2023-03-19 07:10:21 +01:00
element.getBoundingClientRect().left + document.documentElement.scrollLeft + 5,
element.getBoundingClientRect().top + document.documentElement.scrollTop + 20
)
}
2017-04-09 02:08:35 +02:00
linesContext.lineTo(
2023-03-19 07:10:21 +01:00
~~(hovered[i].center[0] * zoom) + innerContainer.offsetLeft,
~~(hovered[i].center[1] * zoom) + innerContainer.offsetTop
2022-05-06 09:41:22 +02:00
)
linesContext.stroke()
2017-04-09 02:08:35 +02:00
}
}
// Line body
2022-05-06 09:41:22 +02:00
linesContext.lineWidth = Math.max(Math.min(zoom, 16), 4)
linesContext.strokeStyle = "#FFFFFF"
for (let i = 0; i < hovered.length; i++) {
2022-05-06 09:41:22 +02:00
const element = hovered[i].element
2023-03-17 18:30:33 +01:00
if (element.getBoundingClientRect().left !== 0) {
2022-05-06 09:41:22 +02:00
linesContext.beginPath()
// Align line based on which side the card is on
if ((element.getBoundingClientRect().left + element.clientWidth / 2) < (document.documentElement.clientWidth / 2)) {
linesContext.moveTo(
2023-03-19 07:10:21 +01:00
element.getBoundingClientRect().left + document.documentElement.scrollLeft + element.clientWidth - 5,
element.getBoundingClientRect().top + document.documentElement.scrollTop + 20
)
} else {
linesContext.moveTo(
2023-03-19 07:10:21 +01:00
element.getBoundingClientRect().left + document.documentElement.scrollLeft + 5,
element.getBoundingClientRect().top + document.documentElement.scrollTop + 20
)
}
2017-04-09 02:08:35 +02:00
linesContext.lineTo(
2023-03-19 07:10:21 +01:00
~~(hovered[i].center[0] * zoom) + innerContainer.offsetLeft,
~~(hovered[i].center[1] * zoom) + innerContainer.offsetTop
2022-05-06 09:41:22 +02:00
)
linesContext.stroke()
2017-04-09 02:08:35 +02:00
}
}
}
function renderBackground(atlas) {
2023-04-07 11:44:33 +02:00
backgroundContext.clearRect(0, 0, highlightCanvas.width, highlightCanvas.height)
2022-05-06 09:41:22 +02:00
//backgroundCanvas.width = 1000 * zoom
//backgroundCanvas.height = 1000 * zoom
2022-05-06 09:41:22 +02:00
//backgroundContext.lineWidth = zoom
2022-05-06 09:41:22 +02:00
backgroundContext.fillStyle = "rgba(0, 0, 0, 0.6)"
backgroundContext.fillRect(0, 0, backgroundCanvas.width, backgroundCanvas.height)
for (let i = 0; i < atlas.length; i++) {
2022-05-06 09:41:22 +02:00
const path = atlas[i].path
2022-05-06 09:41:22 +02:00
backgroundContext.beginPath()
if (path[0]) {
2022-05-06 09:41:22 +02:00
//backgroundContext.moveTo(path[0][0]*zoom, path[0][1]*zoom)
backgroundContext.moveTo(path[0][0], path[0][1])
}
for (let p = 1; p < path.length; p++) {
2022-05-06 09:41:22 +02:00
//backgroundContext.lineTo(path[p][0]*zoom, path[p][1]*zoom)
backgroundContext.lineTo(path[p][0], path[p][1])
}
2022-05-06 09:41:22 +02:00
backgroundContext.closePath()
2022-05-06 09:41:22 +02:00
let bgStrokeStyle
2022-04-08 06:06:16 +02:00
switch (atlas[i].diff) {
case "add":
2022-05-06 09:41:22 +02:00
bgStrokeStyle = "rgba(0, 255, 0, 1)"
backgroundContext.lineWidth = 2
break
2022-04-08 06:06:16 +02:00
case "edit":
2022-05-06 09:41:22 +02:00
bgStrokeStyle = "rgba(255, 255, 0, 1)"
backgroundContext.lineWidth = 2
break
2022-04-08 22:02:14 +02:00
case "delete":
2022-05-06 09:41:22 +02:00
bgStrokeStyle = "rgba(255, 0, 0, 1)"
backgroundContext.lineWidth = 2
break
2022-04-08 06:06:16 +02:00
default:
2022-05-06 09:41:22 +02:00
bgStrokeStyle = "rgba(255, 255, 255, 0.8)"
break
2022-04-08 06:06:16 +02:00
}
2022-05-06 09:41:22 +02:00
backgroundContext.strokeStyle = bgStrokeStyle
backgroundContext.stroke()
backgroundContext.lineWidth = 1
}
}
function buildObjectsList(filter, sort = defaultSort) {
if (entriesList.contains(moreEntriesButton)) {
2022-05-06 09:41:22 +02:00
entriesList.removeChild(moreEntriesButton)
2022-04-08 01:11:29 +02:00
}
atlasDisplay ||= atlas.slice()
if (filter) {
atlasDisplay = atlas.filter(entry => {
2022-04-09 11:27:14 +02:00
return (
2023-03-19 10:26:45 +01:00
entry.name.toLowerCase().includes(filter.toLowerCase())
|| entry.description?.toLowerCase().includes(filter.toLowerCase())
2023-03-19 10:26:45 +01:00
|| Object.values(entry.links).flat().some(str => str.toLowerCase().includes(filter))
|| entry.id === filter
2022-05-06 09:41:22 +02:00
)
})
document.getElementById("atlasSize").innerHTML = "Found " + atlasDisplay.length + " entries."
2022-04-09 11:27:14 +02:00
} else {
document.getElementById("atlasSize").innerHTML = "The Atlas contains " + atlasDisplay.length + " entries."
}
sort ||= defaultSort
renderBackground(atlasDisplay)
2022-05-06 09:41:22 +02:00
render()
2022-05-06 09:41:22 +02:00
document.getElementById("sort").value = sort
2022-05-06 09:41:22 +02:00
//console.log(sort)
2017-04-06 00:43:45 +02:00
2022-05-06 09:41:22 +02:00
let sortFunction
2022-05-06 09:41:22 +02:00
//console.log(sort)
switch (sort) {
2022-04-09 11:27:14 +02:00
case "shuffle":
2022-05-06 09:41:22 +02:00
sortFunction = null
2023-03-17 18:30:33 +01:00
if (entriesOffset === 0) {
2022-05-06 09:41:22 +02:00
shuffle()
2022-04-09 11:27:14 +02:00
}
2022-05-06 09:41:22 +02:00
break
2022-04-09 11:27:14 +02:00
case "alphaAsc":
2023-03-19 10:26:45 +01:00
sortFunction = (a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase())
2022-05-06 09:41:22 +02:00
break
2022-04-09 11:27:14 +02:00
case "alphaDesc":
2023-03-19 10:26:45 +01:00
sortFunction = (a, b) => b.name.toLowerCase().localeCompare(a.name.toLowerCase())
2022-05-06 09:41:22 +02:00
break
2022-04-09 11:27:14 +02:00
case "newest":
2023-03-19 10:26:45 +01:00
sortFunction = (a, b) => b.id.length - a.id.length || b.id.localeCompare(a.id)
2022-05-06 09:41:22 +02:00
break
2022-04-09 11:27:14 +02:00
case "oldest":
2023-03-19 10:26:45 +01:00
sortFunction = (a, b) => a.id.length - b.id.length || a.id.localeCompare(b.id)
2022-05-06 09:41:22 +02:00
break
2022-04-09 11:27:14 +02:00
case "area":
2023-03-19 10:26:45 +01:00
sortFunction = (a, b) => calcPolygonArea(b.path) - calcPolygonArea(a.path)
2022-05-06 09:41:22 +02:00
break
2022-04-09 11:27:14 +02:00
case "relevant":
2023-03-19 10:26:45 +01:00
sortFunction = (a, b) => {
if (a.name.toLowerCase().includes(filter) && b.name.toLowerCase().includes(filter)) {
return a.name.toLowerCase().indexOf(filter) - b.name.toLowerCase().indexOf(filter) || a.name.toLowerCase().localeCompare(b.name.toLowerCase())
} else if (a.name.toLowerCase().includes(filter)) {
2022-05-06 09:41:22 +02:00
return -1
2023-03-19 10:26:45 +01:00
} else if (b.name.toLowerCase().includes(filter)) {
2022-05-06 09:41:22 +02:00
return 1
2022-04-09 11:27:14 +02:00
} else {
2023-03-19 10:26:45 +01:00
return a.description.toLowerCase().indexOf(filter) - b.description.toLowerCase().indexOf(filter) || a.name.toLowerCase().localeCompare(b.name.toLowerCase())
2022-04-09 11:27:14 +02:00
}
}
2022-05-06 09:41:22 +02:00
break
2022-04-09 11:27:14 +02:00
}
2017-04-06 00:43:45 +02:00
if (sortFunction) {
atlasDisplay.sort(sortFunction)
2022-04-09 11:27:14 +02:00
}
for (let i = entriesOffset; i < entriesOffset + entriesLimit; i++) {
if (i >= atlasDisplay.length) break
const element = createInfoBlock(atlasDisplay[i])
const entry = atlasDisplay[i]
2017-04-09 02:08:35 +02:00
2023-03-18 02:05:40 +01:00
element.addEventListener("mouseenter", function () {
if (fixed || dragging) return
objectsContainer.replaceChildren()
2017-04-10 12:25:04 +02:00
previousScaleZoomOrigin ??= [...scaleZoomOrigin]
previousZoom ??= zoom
setView(entry.center[0], entry.center[1], setZoomByPath(entry.path))
hovered = [entry]
render()
hovered[0].element = this
updateLines()
2022-05-06 09:41:22 +02:00
})
2023-04-07 11:44:33 +02:00
element.addEventListener("click", e => {
2022-05-06 09:41:22 +02:00
toggleFixed(e)
if (!fixed) return
previousScaleZoomOrigin ??= [...scaleZoomOrigin]
previousZoom ??= zoom
applyView()
2022-05-06 09:41:22 +02:00
})
2023-04-08 04:10:22 +02:00
element.addEventListener("mouseleave", () => {
if (fixed || dragging) return
scaleZoomOrigin = [...previousScaleZoomOrigin]
zoom = previousZoom
previousScaleZoomOrigin = undefined
previousZoom = undefined
applyView()
hovered = []
updateLines()
render()
2022-05-06 09:41:22 +02:00
})
2022-05-06 09:41:22 +02:00
entriesList.appendChild(element)
2022-04-09 11:27:14 +02:00
}
2017-04-09 02:08:35 +02:00
2022-05-06 09:41:22 +02:00
entriesOffset += entriesLimit
2017-04-06 00:43:45 +02:00
if (atlasDisplay.length > entriesOffset) {
moreEntriesButton.innerHTML = "Show " + Math.min(entriesLimit, atlasDisplay.length - entriesOffset) + " more"
2022-05-06 09:41:22 +02:00
entriesList.appendChild(moreEntriesButton)
}
2022-04-09 11:27:14 +02:00
}
function shuffle() {
2022-05-06 09:41:22 +02:00
//console.log("shuffled atlas")
for (let i = atlasDisplay.length - 1; i > 0; i--) {
2022-05-06 09:41:22 +02:00
const j = Math.floor(Math.random() * (i + 1))
const temp = atlasDisplay[i]
atlasDisplay[i] = atlasDisplay[j]
atlasDisplay[j] = temp
2022-04-09 11:27:14 +02:00
}
}
2022-04-10 11:11:34 +02:00
function resetEntriesList() {
2022-05-06 09:41:22 +02:00
entriesOffset = 0
entriesList.replaceChildren()
entriesList.appendChild(moreEntriesButton)
2022-04-10 11:11:34 +02:00
buildObjectsList(filter = null, sort = null)
}
async function render() {
2023-04-07 11:44:33 +02:00
highlightContext.clearRect(0, 0, highlightCanvas.width, highlightCanvas.height)
2022-05-06 09:41:22 +02:00
//canvas.width = 1000*zoom
//canvas.height = 1000*zoom
2023-04-07 11:44:33 +02:00
highlightContext.globalCompositeOperation = "source-over"
highlightContext.clearRect(0, 0, highlightCanvas.width, highlightCanvas.height)
if (hovered.length > 0) {
2022-05-06 09:41:22 +02:00
container.style.cursor = "pointer"
2022-04-09 11:27:14 +02:00
} else {
2022-05-06 09:41:22 +02:00
container.style.cursor = "default"
2022-04-09 11:27:14 +02:00
}
for (let i = 0; i < hovered.length; i++) {
2022-05-06 09:41:22 +02:00
const path = hovered[i].path
2023-04-07 11:44:33 +02:00
highlightContext.beginPath()
2017-04-06 19:44:09 +02:00
if (path[0]) {
2022-05-06 09:41:22 +02:00
//context.moveTo(path[0][0]*zoom, path[0][1]*zoom)
2023-04-07 11:44:33 +02:00
highlightContext.moveTo(path[0][0], path[0][1])
}
for (let p = 1; p < path.length; p++) {
2022-05-06 09:41:22 +02:00
//context.lineTo(path[p][0]*zoom, path[p][1]*zoom)
2023-04-07 11:44:33 +02:00
highlightContext.lineTo(path[p][0], path[p][1])
2017-04-10 12:25:04 +02:00
}
2017-04-06 00:43:45 +02:00
2023-04-07 11:44:33 +02:00
highlightContext.closePath()
2017-04-10 15:16:34 +02:00
2023-04-07 11:44:33 +02:00
highlightContext.globalCompositeOperation = "source-over"
2023-04-07 11:44:33 +02:00
highlightContext.fillStyle = "rgba(0, 0, 0, 1)"
highlightContext.fill()
2022-04-09 11:27:14 +02:00
}
2017-04-06 00:43:45 +02:00
2023-04-07 11:44:33 +02:00
highlightContext.globalCompositeOperation = "source-out"
highlightContext.drawImage(backgroundCanvas, 0, 0)
2022-04-09 11:27:14 +02:00
if (hovered.length === 1 && hovered[0].path.length && hovered[0].overrideImage) {
2022-05-06 09:41:22 +02:00
const undisputableHovered = hovered[0]
2022-04-09 11:27:14 +02:00
// Find the left-topmost point of all the paths
2022-05-06 09:41:22 +02:00
const entryPosition = getPositionOfEntry(undisputableHovered)
if (entryPosition) {
2022-05-06 09:41:22 +02:00
const [startX, startY] = entryPosition
const overrideImage = new Image()
2022-04-09 11:27:14 +02:00
const loadingPromise = new Promise((res, rej) => {
2022-05-06 09:41:22 +02:00
overrideImage.onerror = rej
overrideImage.onload = res
})
overrideImage.src = "imageOverrides/" + undisputableHovered.overrideImage
try {
2022-05-06 09:41:22 +02:00
await loadingPromise
2023-04-07 11:44:33 +02:00
highlightContext.globalCompositeOperation = "source-over"
highlightContext.drawImage(overrideImage, startX, startY)
} catch (ex) {
2022-05-06 09:41:22 +02:00
console.error("Cannot override image.", ex)
2022-04-09 11:27:14 +02:00
}
}
}
for (let i = 0; i < hovered.length; i++) {
2017-04-10 12:25:04 +02:00
2022-05-06 09:41:22 +02:00
const path = hovered[i].path
2023-04-07 11:44:33 +02:00
highlightContext.beginPath()
if (path[0]) {
2022-05-06 09:41:22 +02:00
//context.moveTo(path[0][0]*zoom, path[0][1]*zoom)
2023-04-07 11:44:33 +02:00
highlightContext.moveTo(path[0][0], path[0][1])
}
for (let p = 1; p < path.length; p++) {
2022-05-06 09:41:22 +02:00
//context.lineTo(path[p][0]*zoom, path[p][1]*zoom)
2023-04-07 11:44:33 +02:00
highlightContext.lineTo(path[p][0], path[p][1])
}
2023-04-07 11:44:33 +02:00
highlightContext.closePath()
2023-04-07 11:44:33 +02:00
highlightContext.globalCompositeOperation = "source-over"
2022-05-06 09:41:22 +02:00
let hoverStrokeStyle
2022-04-09 11:27:14 +02:00
switch (hovered[i].diff) {
case "add":
2022-05-06 09:41:22 +02:00
hoverStrokeStyle = "rgba(0, 155, 0, 1)"
break
2022-04-09 11:27:14 +02:00
case "edit":
2022-05-06 09:41:22 +02:00
hoverStrokeStyle = "rgba(155, 155, 0, 1)"
break
2022-04-09 11:27:14 +02:00
default:
2022-05-06 09:41:22 +02:00
hoverStrokeStyle = "rgba(0, 0, 0, 1)"
break
}
2023-04-07 11:44:33 +02:00
highlightContext.strokeStyle = hoverStrokeStyle
2022-05-06 09:41:22 +02:00
//context.lineWidth = zoom
2023-04-07 11:44:33 +02:00
highlightContext.stroke()
2022-04-09 11:27:14 +02:00
}
2017-04-06 00:43:45 +02:00
2022-04-09 11:27:14 +02:00
}
function updateHovering(e, tapped) {
if (dragging || (fixed && !tapped)) return
2022-04-20 10:54:58 +02:00
const pos = [
(e.clientX - (container.clientWidth / 2 - innerContainer.clientWidth / 2 + zoomOrigin[0] + container.offsetLeft)) / zoom,
(e.clientY - (container.clientHeight / 2 - innerContainer.clientHeight / 2 + zoomOrigin[1] + container.offsetTop)) / zoom
]
const coordsEl = document.getElementById("coords_p")
2022-04-09 11:27:14 +02:00
// Displays coordinates as zero instead of NaN
if (isNaN(pos[0])) {
coordsEl.textContent = "0, 0"
} else {
coordsEl.textContent = Math.ceil(pos[0]) + ", " + Math.ceil(pos[1])
}
if (!(pos[0] <= 2200 && pos[0] >= -100 && pos[0] <= 2200 && pos[0] >= -100)) return
const newHovered = []
for (const entry of atlasDisplay) {
if (pointIsInPolygon(pos, entry.path)) newHovered.push(entry)
}
let changed = false
if (hovered.length === newHovered.length) {
for (let i = 0; i < hovered.length; i++) {
if (hovered[i].id !== newHovered[i].id) {
2022-05-06 09:41:22 +02:00
changed = true
break
}
}
} else {
changed = true
}
if (!changed) return
2017-04-06 00:43:45 +02:00
hovered = newHovered.sort(function (a, b) {
return calcPolygonArea(a.path) - calcPolygonArea(b.path)
})
objectsContainer.replaceChildren()
2017-04-06 00:43:45 +02:00
for (const entry of hovered) {
const element = createInfoBlock(entry)
objectsContainer.appendChild(element)
entry.element = element
}
if (hovered.length) {
document.getElementById("timeControlsSlider").blur()
closeObjectsListButton.classList.remove("d-none")
if ((objectsContainer.scrollHeight > objectsContainer.clientHeight) && !tapped) {
objectsListOverflowNotice.classList.remove("d-none")
} else {
objectsListOverflowNotice.classList.add("d-none")
}
} else {
closeObjectsListButton.classList.add("d-none")
objectsListOverflowNotice.classList.add("d-none")
entriesList.classList.remove("disableHover")
}
render()
2022-04-09 11:27:14 +02:00
}
2022-05-06 09:41:22 +02:00
window.addEventListener("hashchange", highlightEntryFromUrl)
2022-04-09 21:09:57 +02:00
function highlightEntryFromUrl() {
const hash = window.location.hash.substring(1); //Remove hash prefix
2023-03-26 14:35:26 +02:00
let [id, period] = hash.split('/')
// Handle zzz and 0.. prefix
let newId = id.replace(/^zzz([a-z0-9]{8,})$/g, "$1").replace(/^0+/, '')
if (id !== newId) {
id = newId
const newLocation = new URL(window.location)
newLocation.hash = '#' + [newId, period].join('/')
history.replaceState({}, "", newLocation)
}
2023-03-26 14:35:26 +02:00
2023-03-19 05:56:37 +01:00
let targetPeriod, targetVariation
2022-04-09 21:09:57 +02:00
if (period) {
[targetPeriod, , targetVariation] = parsePeriod(period)
} else {
2023-03-19 05:56:37 +01:00
targetPeriod = defaultPeriod
targetVariation = defaultVariation
}
2023-03-19 05:56:37 +01:00
updateTime(targetPeriod, targetVariation, true)
if (!id) return
2023-04-07 11:44:33 +02:00
const entries = atlas.filter(e => {
return e.id === id
})
2022-05-06 09:41:22 +02:00
if (entries.length !== 1) return
const entry = entries[0]
2022-05-06 09:41:22 +02:00
2023-03-19 07:03:49 +01:00
document.title = entry.name + " on " + pageTitle
if ((!entry.diff || entry.diff !== "delete")) {
2023-03-19 03:19:37 +01:00
objectEditNav.href = "./?mode=draw&id=" + id
objectEditNav.title = "Edit " + entry.name
if (!objectEditNav.isConnected) {
showListButton.parentElement.appendChild(objectEditNav)
}
} else if (entry.diff === "delete" && document.getElementById("objectEditNav")) {
2023-03-19 03:19:37 +01:00
objectEditNav.remove()
}
2022-05-06 09:41:22 +02:00
const infoElement = createInfoBlock(entry)
objectsContainer.replaceChildren()
objectsContainer.appendChild(infoElement)
2022-05-06 09:41:22 +02:00
renderBackground(atlas)
applyView()
2023-03-26 13:58:17 +02:00
setView(entry.center[0], entry.center[1], setZoomByPath(entry.path))
closeObjectsListButton.classList.remove("d-none")
entriesList.classList.add("disableHover")
hovered = [entry]
render()
hovered[0].element = infoElement
updateLines()
2017-04-06 00:43:45 +02:00
}
2023-03-26 13:58:17 +02:00
function setZoomByPath(path) {
let boundingBox = [canvasSize.x, 0, canvasSize.y, 0]
path?.forEach(([x, y]) => {
boundingBox[0] = Math.min(boundingBox[0], x)
boundingBox[1] = Math.max(boundingBox[1], x)
boundingBox[2] = Math.min(boundingBox[2], y)
boundingBox[3] = Math.max(boundingBox[3], y)
})
const boundingBoxSize = [boundingBox[1] - boundingBox[0], boundingBox[3] - boundingBox[2]]
const clientSize = [
Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0),
Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0)
]
zoom = Math.min(clientSize[0] / boundingBoxSize[0], clientSize[1] / boundingBoxSize[1])
zoom = Math.min(4, zoom/2)
}
function initView() {
2022-05-06 09:41:22 +02:00
buildObjectsList(null, null)
renderBackground(atlas)
render()
2023-04-07 04:44:06 +02:00
2023-03-18 02:05:40 +01:00
document.addEventListener('timeupdate', () => {
atlasDisplay = atlas.slice()
2022-04-10 11:11:34 +02:00
resetEntriesList(null, null)
})
2022-04-09 11:27:14 +02:00
// parse linked atlas entry id from link hash
/*if (window.location.hash.substring(3)){
2022-05-06 09:41:22 +02:00
zoom = 4
applyView()
highlightEntryFromUrl()
2022-04-09 11:27:14 +02:00
}*/
2022-05-06 09:41:22 +02:00
applyView()
render()
updateLines()
2022-04-09 11:27:14 +02:00
}
function initExplore() {
2022-04-09 11:27:14 +02:00
window.updateHovering = updateHovering
2023-04-08 04:10:22 +02:00
window.render = () => { }
function updateHovering(e, tapped) {
2023-04-07 11:44:33 +02:00
if (dragging || (fixed && !tapped)) return
const pos = [
(e.clientX - (container.clientWidth / 2 - innerContainer.clientWidth / 2 + zoomOrigin[0] + container.offsetLeft)) / zoom,
(e.clientY - (container.clientHeight / 2 - innerContainer.clientHeight / 2 + zoomOrigin[1] + container.offsetTop)) / zoom
]
const coordsEl = document.getElementById("coords_p")
// Displays coordinates as zero instead of NaN
if (isNaN(pos[0])) {
coordsEl.textContent = "0, 0"
} else {
coordsEl.textContent = Math.ceil(pos[0]) + ", " + Math.ceil(pos[1])
}
}
2022-05-06 09:41:22 +02:00
renderBackground(atlas)
2022-04-09 11:27:14 +02:00
2022-05-06 09:41:22 +02:00
applyView()
2022-04-09 11:27:14 +02:00
}
function initGlobal() {
2023-04-07 11:44:33 +02:00
container.addEventListener("mousemove", e => {
if (e.sourceCapabilities) {
if (!e.sourceCapabilities.firesTouchEvents) {
2022-05-06 09:41:22 +02:00
updateHovering(e)
2022-04-09 11:27:14 +02:00
}
} else {
2022-05-06 09:41:22 +02:00
updateHovering(e)
}
2022-05-06 09:41:22 +02:00
})
2023-04-07 11:44:33 +02:00
document.addEventListener('timeupdate', event => {
let hashData = window.location.hash.substring(1).split('/')
const newLocation = new URL(window.location)
newLocation.hash = formatHash(hashData[0], event.detail.period, event.detail.period, event.detail.variation)
if (location.hash !== newLocation.hash) history.replaceState({}, "", newLocation)
})
2022-04-09 11:27:14 +02:00
}
2022-04-09 11:27:14 +02:00
function initViewGlobal() {
2023-04-07 11:44:33 +02:00
container.addEventListener("mousedown", e => {
lastPos = [
2023-03-19 07:10:21 +01:00
e.clientX,
e.clientY
2022-05-06 09:41:22 +02:00
]
})
2023-04-07 11:44:33 +02:00
container.addEventListener("touchstart", e => {
2023-03-17 18:30:33 +01:00
if (e.touches.length === 1) {
lastPos = [
2023-03-19 07:10:21 +01:00
e.touches[0].clientX,
e.touches[0].clientY
2022-05-06 09:41:22 +02:00
]
}
2022-05-06 09:41:22 +02:00
}, { passive: true })
2023-04-07 11:44:33 +02:00
container.addEventListener("mouseup", e => {
if (Math.abs(lastPos[0] - e.clientX) + Math.abs(lastPos[1] - e.clientY) <= 4) {
2022-05-06 09:41:22 +02:00
toggleFixed(e)
}
2022-05-06 09:41:22 +02:00
})
2023-04-07 11:44:33 +02:00
container.addEventListener("touchend", e => {
e.preventDefault()
2022-05-06 09:41:22 +02:00
//console.log(e)
//console.log(e.changedTouches[0].clientX)
if (e.changedTouches.length !== 1) return
e = e.changedTouches[0]
//console.log(lastPos[0] - e.clientX)
if (Math.abs(lastPos[0] - e.clientX) + Math.abs(lastPos[1] - e.clientY) > 4)
//console.log("Foo!!")
dragging = false
fixed = false
2023-04-07 11:14:07 +02:00
setTimeout(() => updateHovering(e, true), 0)
2022-05-06 09:41:22 +02:00
})
if (window.location.hash) { // both "/" and just "/#" will be an empty hash string
2022-05-06 09:41:22 +02:00
highlightEntryFromUrl()
}
2023-04-07 11:44:33 +02:00
document.addEventListener('timeupdate', event => {
drawButton.href = "./?mode=draw" + formatHash(undefined, event.detail.period, event.detail.period, event.detail.variation)
})
2022-04-09 17:49:27 +02:00
}