diff --git a/main.js b/main.js index 7d38ea7..057da94 100644 --- a/main.js +++ b/main.js @@ -13,7 +13,7 @@ // ==/UserScript== (function () { - 'use strict'; + 'use strict' let fileInputs = { "attachment1": { @@ -33,6 +33,20 @@ "h": "для ист. справки" } + let mapsCenterByCompetition = { + "msk2025": { + lat: 55.839808, + ifSouthern: { + lat: 55.798531, + lon: 37.690380, + }, + ifEverywhere: { + lat: 55.839759, + lon: 37.706577 + } + } + } + const localStorageItems = { NEED_UPDATE_ID: "needUpdateId", JUST_CREATED: "justCreated", @@ -49,9 +63,9 @@ let removedFilesLinks = [] class Property { - name; - content; - desc; + name + content + desc constructor(row = null) { if (row == null) return @@ -142,20 +156,55 @@ 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() { - let params = new URLSearchParams(document.location.search) - return params.get("action") === "edit" + return isCpManagement() && getAction() === "edit" } function isDeleteCpPage() { - let params = new URLSearchParams(document.location.search) - return params.get("action") === "delete" + return isCpManagement() && getAction() === "delete" } 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', body: formData }) @@ -170,13 +219,13 @@ function copyCoordinates() { let copyButton = document.createElement("button") copyButton.addEventListener("click", async event => { - event.preventDefault(); + event.preventDefault() let lat = document.querySelector(`input[name="cp[lattitude]"]`).value let lon = document.querySelector(`input[name="cp[longitude]"]`).value - const text = new Blob([`${lat}, ${lon}`], { type: "text/plain" }); - const data = new ClipboardItem({ "text/plain": text }); - await navigator.clipboard.write([data]); + const text = new Blob([`${lat}, ${lon}`], { type: "text/plain" }) + const data = new ClipboardItem({ "text/plain": text }) + await navigator.clipboard.write([data]) }) copyButton.classList.add("copy-button") @@ -742,7 +791,7 @@ 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) { let parent = prop.parentElement let propName @@ -1295,6 +1344,31 @@ 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 = ` #props caption { position: sticky; @@ -1646,14 +1720,20 @@ if (isDeleteCpPage()) { bindDeleteButton() - return } initMapbox() - addStickyHeaderToMainList() addFullscreenButton() + if (isListCpPage()) { + addStickyHeaderToMainList() + } + + if (isRouteBuildPage() || isRouteMapPage()) { + centerMap() + } + if (isEditCpPage()) { prettifyEditCpPage() }