-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·155 lines (128 loc) · 4.77 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const entryContainer = document.getElementById("entry-container");
const entryTemplate = document.getElementById("entry-template");
const mapFrame = document.querySelector(".map");
const osmLinkHref = document.getElementById("osm-link");
// Initialize the Leaflet map
const map = L.map(mapFrame, {
center: [39.17294, -86.52328],
zoom: 17,
attributionControl: false
});
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(map);
// Variable to store the current location marker
let currentLocationMarker = null;
let inputCircle = null;
let longitude = null;
let latitude = null;
// Adds a new blank entry
function addEntry() {
const newEntry = entryTemplate.content.cloneNode(true);
entryContainer.appendChild(newEntry);
}
function updateFileName(event) {
var input = event.target;
var fileName = input.files[0].name;
var fileNameElement = input.parentElement.parentElement.parentElement.querySelector(".name");
fileNameElement.innerHTML = fileName;
fileNameElement.classList.add('active');
}
function deleteEntry(button) {
const entry = button.parentNode.parentNode.parentNode.parentNode;
// If no input boxes, probably should remove the input circle
if (inputCircle && entry.parentNode.querySelectorAll(".entry").length < 2) {
map.removeLayer(inputCircle);
}
entry.parentNode.removeChild(entry);
}
function toggleMenu(fileContainer) {
const menu = fileContainer.querySelector(".menu");
menu.classList.toggle("active");
}
// Function to generate a random color
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function drawCircle(latitude, longitude, radius, color = getRandomColor()) {
const circleOptions = {
color: color,
fillColor: color,
fillOpacity: 0.5,
radius: radius
};
return circle = L.circle([latitude, longitude], circleOptions).addTo(map);
}
// While the user types in the input radius box,
// this draws a temporary circle to show the file share radius live
function inputRadius(event) {
const inputText = event.target.value;
// Check if the input is a valid integer between 0 and 100000
const isValidInteger = /^(0|[1-9]\d{0,4}|100000)$/.test(inputText);
if (inputCircle) {
map.removeLayer(inputCircle);
}
if (isValidInteger) {
if (longitude && latitude) {
inputCircle = drawCircle(latitude, longitude, inputText, "#6666ff");
}
} else {
// Cut off the last character (assuming it was the invalid one)
event.target.value = inputText.slice(0, -1);
}
}
function updateMapLocation(latitude, longitude) {
const userLocation = [latitude, longitude];
// Update location display bar
const location = document.getElementById("location");
location.innerHTML = `Current location: Latitude: ${latitude} | Longitude: ${longitude}`;
// Remove any existing marker
if (currentLocationMarker) {
map.removeLayer(currentLocationMarker);
}
// Add a new marker for the current location
currentLocationMarker = L.marker(userLocation).addTo(map);
// Relocate file radius input circle
if (inputCircle) {
inputCircle.setLatLng(userLocation);
}
map.setView(userLocation, 17);
// Update "View Larger Map" link
const newHref = `https://www.openstreetmap.org/#map=18/${latitude}/${longitude}`;
osmLinkHref.setAttribute("href", newHref);
}
// Function to handle adding a pin to the map
function onMapClick(event) {
const userLocation = event.latlng;
latitude = userLocation.lat;
longitude = userLocation.lng;
updateMapLocation(latitude, longitude);
// Turn off Spoof Mode
toggleSpoofMode();
}
function toggleSpoofMode() {
const spoofPin = document.getElementById("spoof-pin");
const isPinAddingMode = spoofPin.classList.toggle("active");
if (isPinAddingMode) {
spoofPin.textContent = "Cancel adding pin";
map.on("click", onMapClick);
} else {
spoofPin.textContent = "Set Location";
map.off("click", onMapClick);
}
}
function getLocation() {
const location = document.getElementById("location");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
updateMapLocation(latitude, longitude);
});
} else {
location.innerHTML = "Geolocation is not supported by this browser.";
}
}