refactor & center map on route manager

This commit is contained in:
Zhora Shalyapin 2025-03-14 08:55:13 +00:00
parent 00fe5c2d4b
commit 4ac2f39868

114
main.js
View File

@ -13,7 +13,7 @@
// ==/UserScript== // ==/UserScript==
(function () { (function () {
'use strict'; 'use strict'
let fileInputs = { let fileInputs = {
"attachment1": { "attachment1": {
@ -33,6 +33,20 @@
"h": "для ист. справки" "h": "для ист. справки"
} }
let mapsCenterByCompetition = {
"msk2025": {
lat: 55.839808,
ifSouthern: {
lat: 55.798531,
lon: 37.690380,
},
ifEverywhere: {
lat: 55.839759,
lon: 37.706577
}
}
}
const localStorageItems = { const localStorageItems = {
NEED_UPDATE_ID: "needUpdateId", NEED_UPDATE_ID: "needUpdateId",
JUST_CREATED: "justCreated", JUST_CREATED: "justCreated",
@ -49,9 +63,9 @@
let removedFilesLinks = [] let removedFilesLinks = []
class Property { class Property {
name; name
content; content
desc; desc
constructor(row = null) { constructor(row = null) {
if (row == null) return if (row == null) return
@ -142,20 +156,55 @@
document.head.appendChild(styleSheet) document.head.appendChild(styleSheet)
} }
const sleep = ms => new Promise(res => setTimeout(res, ms)); const sleep = ms => new Promise(res => setTimeout(res, ms))
function pathNameSplit() {
return window.location.pathname.split('/')
}
function getCompetition() {
return pathNameSplit()[1]
}
function getPageType() {
return pathNameSplit()[2]
}
function getAction() {
let params = new URLSearchParams(document.location.search)
return params.get("action")
}
function isCpManagement() {
return getPageType() === "cp_mgmt"
}
function isRouteManagement() {
return getPageType() === "route_mgmt"
}
function isListCpPage() {
return isCpManagement() && getAction() === null
}
function isRouteBuildPage() {
return isRouteManagement() && getAction() === "build2"
}
function isRouteMapPage() {
return isRouteManagement() && getAction() === "map"
}
function isEditCpPage() { function isEditCpPage() {
let params = new URLSearchParams(document.location.search) return isCpManagement() && getAction() === "edit"
return params.get("action") === "edit"
} }
function isDeleteCpPage() { function isDeleteCpPage() {
let params = new URLSearchParams(document.location.search) return isCpManagement() && getAction() === "delete"
return params.get("action") === "delete"
} }
async function updatePoint(formData) { async function updatePoint(formData) {
return await fetch(`https://runcity.geo.rictum.ru/api/competitions/${window.location.pathname.split('/')[1]}/update`, { return await fetch(`https://runcity.geo.rictum.ru/api/competitions/${getCompetition()}/update`, {
method: 'POST', method: 'POST',
body: formData body: formData
}) })
@ -170,13 +219,13 @@
function copyCoordinates() { function copyCoordinates() {
let copyButton = document.createElement("button") let copyButton = document.createElement("button")
copyButton.addEventListener("click", async event => { copyButton.addEventListener("click", async event => {
event.preventDefault(); event.preventDefault()
let lat = document.querySelector(`input[name="cp[lattitude]"]`).value let lat = document.querySelector(`input[name="cp[lattitude]"]`).value
let lon = document.querySelector(`input[name="cp[longitude]"]`).value let lon = document.querySelector(`input[name="cp[longitude]"]`).value
const text = new Blob([`${lat}, ${lon}`], { type: "text/plain" }); const text = new Blob([`${lat}, ${lon}`], { type: "text/plain" })
const data = new ClipboardItem({ "text/plain": text }); const data = new ClipboardItem({ "text/plain": text })
await navigator.clipboard.write([data]); await navigator.clipboard.write([data])
}) })
copyButton.classList.add("copy-button") copyButton.classList.add("copy-button")
@ -742,7 +791,7 @@
localStorage.setItem(localStorageItems.NEED_UPDATE_ID, true) localStorage.setItem(localStorageItems.NEED_UPDATE_ID, true)
} }
let props = document.querySelectorAll("input[name^=\"prop_\"]"); let props = document.querySelectorAll("input[name^=\"prop_\"]")
for (let prop of props) { for (let prop of props) {
let parent = prop.parentElement let parent = prop.parentElement
let propName let propName
@ -1295,6 +1344,31 @@
checkIfAlwaysPrettify(form, oldTable, container, insertedFileRows) checkIfAlwaysPrettify(form, oldTable, container, insertedFileRows)
} }
function isAllPointsSouthOfLat(lat) {
map.eachLayer(function(layer) {
if (layer instanceof L.Marker) {
let latLng = layer.getLatLng()
if (latLng.lat > lat) {
return false
}
}
})
}
function centerMap() {
if (map !== undefined && L !== undefined) {
let coords = mapsCenterByCompetition[getCompetition()]
if (coords == null) return
if (isAllPointsSouthOfLat(lat)) {
map.setView(new L.LatLng(coords.ifSouthern.lat, coords.ifSouthern.lon), 13)
}
else {
map.setView(new L.LatLng(coords.ifEverywhere.lat, coords.ifEverywhere.lon), 12)
}
}
}
let styles = ` let styles = `
#props caption { #props caption {
position: sticky; position: sticky;
@ -1646,14 +1720,20 @@
if (isDeleteCpPage()) { if (isDeleteCpPage()) {
bindDeleteButton() bindDeleteButton()
return return
} }
initMapbox() initMapbox()
addStickyHeaderToMainList()
addFullscreenButton() addFullscreenButton()
if (isListCpPage()) {
addStickyHeaderToMainList()
}
if (isRouteBuildPage() || isRouteMapPage()) {
centerMap()
}
if (isEditCpPage()) { if (isEditCpPage()) {
prettifyEditCpPage() prettifyEditCpPage()
} }