Adjusting context menus

This commit is contained in:
Will Bradley 2025-07-16 13:03:10 -07:00
parent 54f83d68ee
commit 4dfc0fdf84

View File

@ -67,63 +67,14 @@
} }
.main-content { .main-content {
display: flex;
height: calc(100vh - 80px); height: calc(100vh - 80px);
} }
.sidebar {
width: 100%;
max-width: 400px;
background: white;
border-right: 1px solid #e1e1e1;
overflow-y: auto;
}
.map-container {
flex: 1;
position: relative;
}
#map { #map {
height: 100%; height: 100%;
width: 100%; width: 100%;
} }
.tab-buttons {
display: flex;
background: #f8f9fa;
border-bottom: 1px solid #e1e1e1;
}
.tab-button {
flex: 1;
padding: 1rem;
background: none;
border: none;
cursor: pointer;
font-size: 0.9rem;
color: #666;
border-bottom: 2px solid transparent;
}
.tab-button.active {
color: #667eea;
border-bottom-color: #667eea;
background: white;
}
.tab-content {
padding: 1rem;
}
.tab-panel {
display: none;
}
.tab-panel.active {
display: block;
}
.form-group { .form-group {
margin-bottom: 1rem; margin-bottom: 1rem;
} }
@ -189,83 +140,38 @@
background: #5a6268; background: #5a6268;
} }
.station-list {
max-height: 300px;
overflow-y: auto;
}
.station-item { .context-menu {
padding: 0.8rem;
border-bottom: 1px solid #e1e1e1;
cursor: pointer;
transition: background-color 0.3s;
display: flex;
}
.station-item:hover {
background: #f8f9fa;
}
.station-item.selected {
background: #e3f2fd;
border-left: 4px solid #667eea;
}
.station-name {
font-weight: 500;
color: #333;
margin-bottom: 0.3rem;
}
.station-status {
font-size: 0.8rem;
color: #666;
display: flex;
align-items: center;
margin-left: 2em;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 0.5rem;
}
.map-controls {
position: absolute; position: absolute;
top: 10px; background: white;
right: 10px; border: 1px solid #ccc;
border-radius: 4px;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
z-index: 1000; z-index: 1000;
display: none;
min-width: 150px;
} }
.add-pin-btn { .context-menu-item {
background: white; padding: 0.75rem 1rem;
border: 1px solid #ccc;
padding: 0.5rem;
margin-bottom: 0.5rem;
border-radius: 4px;
cursor: pointer; cursor: pointer;
box-shadow: 0 2px 4px rgba(0,0,0,0.1); font-size: 0.9rem;
font-size: 0.8rem; color: #333;
display: inline-block; border-bottom: 1px solid #eee;
width: 40%;
} }
input#coordinates { .context-menu-item:last-child {
display: inline-block; border-bottom: none;
width: 58%;
} }
.map-control-btn { .context-menu-item:hover {
background: white; background: #f5f5f5;
border: 1px solid #ccc; }
padding: 0.5rem;
margin-bottom: 0.5rem; .context-menu-item:before {
border-radius: 4px; content: '+ ';
cursor: pointer; font-weight: bold;
box-shadow: 0 2px 4px rgba(0,0,0,0.1); color: #667eea;
font-size: 0.8rem;
} }
.message { .message {
@ -287,113 +193,132 @@
border: 1px solid #f5c6cb; border: 1px solid #f5c6cb;
} }
.modal {
display: none;
position: fixed;
z-index: 2000;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
}
.modal-content {
background-color: white;
margin: 10% auto;
padding: 2rem;
border-radius: 8px;
width: 90%;
max-width: 500px;
position: relative;
}
.close {
position: absolute;
right: 1rem;
top: 1rem;
font-size: 1.5rem;
cursor: pointer;
color: #666;
}
.close:hover {
color: #000;
}
.modal h2 {
margin-bottom: 1rem;
color: #333;
}
.popup-btn {
background: #667eea;
color: white;
border: none;
padding: 0.5rem 1rem;
border-radius: 4px;
cursor: pointer;
font-size: 0.9rem;
margin-top: 0.5rem;
}
.popup-btn:hover {
background: #5a6fd8;
}
@media (max-width: 768px) { @media (max-width: 768px) {
.main-content { .modal-content {
flex-direction: column; margin: 5% auto;
} width: 95%;
padding: 1.5rem;
.sidebar {
max-width: none;
height: 55vh;
border-right: none;
border-bottom: 1px solid #e1e1e1;
}
.map-container {
height: 40vh;
}
.header {
flex-direction: column;
gap: 0.5rem;
text-align: center;
}
.header-right {
justify-content: center;
} }
} }
</style> </style>
</head> </head>
<body> <body>
<div class="header"> <div class="header">
<h1>💧 <span id="cityName">Loading...</span></h1>
<div class="header-right"> <div class="header-right">
<div class="user-info">
<h1>💧 Water Stations - <span id="cityName">Loading...</span></h1>
Welcome, <span id="username"></span>
</div>
<a href="/city-select?from=dashboard" class="city-selector">Change City</a> <a href="/city-select?from=dashboard" class="city-selector">Change City</a>
<button class="logout-btn" onclick="logout()">Logout</button> <button class="logout-btn" onclick="logout()">Logout</button>
</div> </div>
</div> </div>
<div class="main-content"> <div class="main-content">
<div class="sidebar"> <div id="map"></div>
<div class="tab-buttons"> <div id="contextMenu" class="context-menu">
<button class="tab-button active" onclick="switchTab('update')">Update Status</button> <div class="context-menu-item" id="addStationMenuItem">Add Station Here</div>
<button class="tab-button" onclick="switchTab('add')">Add Station</button>
</div>
<div class="tab-content">
<div id="update-tab" class="tab-panel active">
<div id="update-message"></div>
<div class="form-group">
<div class="station-list" id="stationList">
<!-- Stations will be populated here -->
</div>
</div>
<form id="updateStationForm" style="display: none;">
<div class="form-group">
<label for="updateDescription">Status Description</label>
<textarea id="updateDescription" name="description" placeholder="e.g., Refilled and working well"></textarea>
</div>
<div class="form-group">
<label for="estimatedHours">Estimated hours until empty</label>
<input type="number" id="estimatedHours" name="estimatedHours" min="1" max="48" value="6">
</div>
<button type="submit" class="btn btn-primary">Update Status</button>
<button type="button" class="btn btn-secondary" onclick="cancelUpdate()">Cancel</button>
</form>
</div>
<div id="add-tab" class="tab-panel">
<div id="add-message"></div>
<form id="addStationForm">
<div class="form-group">
<label for="stationName">Station Name</label>
<input type="text" id="stationName" name="name" required>
</div>
<div class="form-group">
<label for="stationDescription">Description</label>
<textarea id="stationDescription" name="description" placeholder="e.g., Public fountain in park"></textarea>
</div>
<div class="form-group">
<label>Location</label>
<!--<p style="font-size: 0.8rem; color: #666; margin-bottom: 0.5rem;">
Click on the map to set location
</p>-->
<button class="add-pin-btn" onclick="startAddMode()">
<span id="addModeText">Select Location</span>
</button>
<input type="text" id="coordinates" readonly>
</div>
<button type="submit" class="btn btn-primary">Add Station</button>
</form>
</div>
</div>
</div> </div>
</div>
<div class="map-container"> <!-- Update Status Modal -->
<div id="map"></div> <div id="updateModal" class="modal">
<!--<div class="map-controls"> <div class="modal-content">
<button class="map-control-btn" onclick="centerMap()">Center Map</button> <span class="close" onclick="closeUpdateModal()">&times;</span>
</div>--> <h2>Update Station Status</h2>
<div id="update-message"></div>
<form id="updateStationForm">
<div class="form-group">
<label for="updateDescription">Status Description</label>
<textarea id="updateDescription" name="description" placeholder="e.g., Refilled and working well"></textarea>
</div>
<div class="form-group">
<label for="estimatedHours">Estimated hours until empty</label>
<input type="number" id="estimatedHours" name="estimatedHours" min="1" max="48" value="6">
</div>
<button type="submit" class="btn btn-primary">Update Status</button>
<button type="button" class="btn btn-secondary" onclick="closeUpdateModal()">Cancel</button>
</form>
</div>
</div>
<!-- Add Station Modal -->
<div id="addModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeAddModal()">&times;</span>
<h2>Add New Station</h2>
<div id="add-message"></div>
<form id="addStationForm">
<div class="form-group">
<label for="stationName">Station Name</label>
<input type="text" id="stationName" name="name" required>
</div>
<div class="form-group">
<label for="stationDescription">Description</label>
<textarea id="stationDescription" name="description" placeholder="e.g., Public fountain in park"></textarea>
</div>
<div class="form-group">
<label>Location</label>
<input type="text" id="coordinates" readonly placeholder="Right-click or long-press map to select location">
</div>
<button type="submit" class="btn btn-primary">Add Station</button>
</form>
</div> </div>
</div> </div>
@ -402,10 +327,11 @@
let map; let map;
let stations = []; let stations = [];
let selectedStation = null; let selectedStation = null;
let addMode = false;
let tempMarker = null; let tempMarker = null;
let user = null; let user = null;
let currentCity = null; let currentCity = null;
let contextMenuPosition = null;
let longPressTimer = null;
function initDashboard() { function initDashboard() {
// Get city name from URL // Get city name from URL
@ -435,7 +361,6 @@
} }
user = data.user; user = data.user;
document.getElementById('username').textContent = user.display_name || user.username || 'User';
} catch (error) { } catch (error) {
window.location.href = '/login'; window.location.href = '/login';
} }
@ -448,33 +373,99 @@
attribution: '© OpenStreetMap contributors' attribution: '© OpenStreetMap contributors'
}).addTo(map); }).addTo(map);
map.on('click', function(e) { // Handle right-click context menu
if (addMode) { map.on('contextmenu', function(e) {
setStationLocation(e.latlng); console.log('contextmenu event:', e);
e.originalEvent.preventDefault();
const clientX = e.originalEvent.clientX || e.originalEvent.pageX;
const clientY = e.originalEvent.clientY || e.originalEvent.pageY;
console.log('Context menu coordinates:', clientX, clientY);
showContextMenu(clientX, clientY, e.latlng);
});
// Handle long-press for mobile
let longPressTimer = null;
let longPressStarted = false;
map.on('mousedown', function(e) {
if (e.originalEvent.button === 0) { // Left click only
longPressStarted = true;
longPressTimer = setTimeout(() => {
if (longPressStarted) {
showContextMenu(e.originalEvent.clientX, e.originalEvent.clientY, e.latlng);
}
}, 500); // 500ms for long press
} }
}); });
map.on('mouseup', function(e) {
if (longPressTimer) {
clearTimeout(longPressTimer);
longPressTimer = null;
}
longPressStarted = false;
});
map.on('mousemove', function(e) {
if (longPressTimer) {
clearTimeout(longPressTimer);
longPressTimer = null;
}
longPressStarted = false;
});
// Hide context menu on map click
map.on('click', function(e) {
hideContextMenu();
});
} }
function setStationLocation(latlng) { // Context menu functions
if (tempMarker) { function showContextMenu(x, y, latlng) {
map.removeLayer(tempMarker); console.log('showContextMenu called with:', x, y, latlng);
} hideContextMenu();
const contextMenu = document.getElementById('contextMenu');
contextMenuPosition = latlng;
console.log('contextMenuPosition set to:', contextMenuPosition);
tempMarker = L.marker(latlng).addTo(map); contextMenu.style.display = 'block';
document.getElementById('coordinates').value = `${latlng.lat.toFixed(6)}, ${latlng.lng.toFixed(6)}`; contextMenu.style.left = x + 'px';
stopAddMode(); contextMenu.style.top = y + 'px';
// Adjust position if menu goes off screen
const rect = contextMenu.getBoundingClientRect();
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
if (rect.right > viewportWidth) {
contextMenu.style.left = (x - rect.width) + 'px';
}
if (rect.bottom > viewportHeight) {
contextMenu.style.top = (y - rect.height) + 'px';
}
} }
function startAddMode() { function hideContextMenu() {
addMode = true; document.getElementById('contextMenu').style.display = 'none';
map.getContainer().style.cursor = 'crosshair'; contextMenuPosition = null;
} }
function stopAddMode() { function contextAddStation() {
addMode = false; console.log('contextAddStation called');
map.getContainer().style.cursor = ''; console.log('contextMenuPosition:', contextMenuPosition);
hideContextMenu();
if (contextMenuPosition) {
console.log('Opening modal with position:', contextMenuPosition);
openAddStationModal(contextMenuPosition);
} else {
console.log('No contextMenuPosition available');
}
} }
// Expose functions to global scope for onclick handlers (for modal close buttons)
window.closeUpdateModal = closeUpdateModal;
window.closeAddModal = closeAddModal;
async function loadStations() { async function loadStations() {
try { try {
const response = await fetch(`/api/cities/${currentCity}/stations`); const response = await fetch(`/api/cities/${currentCity}/stations`);
@ -484,7 +475,6 @@
document.getElementById('cityName').textContent = data[0].city_name; document.getElementById('cityName').textContent = data[0].city_name;
} }
displayStations(); displayStations();
populateStationList();
fitMapToStations(); fitMapToStations();
} catch (error) { } catch (error) {
console.error('Error loading stations:', error); console.error('Error loading stations:', error);
@ -554,76 +544,70 @@
<p><strong>Estimated Empty:</strong> ${estimatedEmpty}</p> <p><strong>Estimated Empty:</strong> ${estimatedEmpty}</p>
<p><strong>Last Updated:</strong> ${station.last_updated ? new Date(station.last_updated).toLocaleString() : 'Never'}</p> <p><strong>Last Updated:</strong> ${station.last_updated ? new Date(station.last_updated).toLocaleString() : 'Never'}</p>
<p><strong>Last Updated By:</strong> ${station.updated_by_name || 'Unknown'}</p> <p><strong>Last Updated By:</strong> ${station.updated_by_name || 'Unknown'}</p>
<button class="popup-btn" onclick="openUpdateModal(${station.id})">Update Status</button>
</div> </div>
`; `;
} }
function populateStationList() { // Modal functions
const stationList = document.getElementById('stationList'); function openUpdateModal(stationId) {
stationList.innerHTML = ''; const station = stations.find(s => s.id === stationId);
if (!station) return;
stations.forEach(station => {
const item = document.createElement('div');
item.className = 'station-item';
item.onclick = () => selectStation(station);
const color = getStationColor(station);
const statusText = getStatusText(station);
item.innerHTML = `
<span class="station-name">${station.name}</span>
<span class="station-status">
<span class="status-dot" style="background-color: ${color}"></span>
${statusText}
</span>
`;
stationList.appendChild(item);
});
}
function getStatusText(station) {
if (!station.last_refill_time) return 'Never updated';
const now = new Date();
const refillTime = new Date(station.last_refill_time);
const daysSinceRefill = (now - refillTime) / (1000 * 60 * 60 * 24);
if (daysSinceRefill > 7) return 'Old data (7+ days)';
if (!station.estimated_empty_time) return 'Recently refilled';
const emptyTime = new Date(station.estimated_empty_time);
const hoursUntilEmpty = (emptyTime - now) / (1000 * 60 * 60);
if (hoursUntilEmpty <= 0) return 'Empty';
if (hoursUntilEmpty <= 3) return 'Needs refill soon';
return 'Recently refilled';
}
function selectStation(station) {
selectedStation = station; selectedStation = station;
document.getElementById('updateModal').style.display = 'block';
// Update UI
document.querySelectorAll('.station-item').forEach(item => {
item.classList.remove('selected');
});
event.target.closest('.station-item').classList.add('selected');
// Show update form
document.getElementById('updateStationForm').style.display = 'block';
// Center map on selected station // Center map on selected station
map.setView([station.latitude, station.longitude], 16); map.setView([station.latitude, station.longitude], 16);
} }
function cancelUpdate() { function closeUpdateModal() {
selectedStation = null; selectedStation = null;
document.getElementById('updateStationForm').style.display = 'none'; document.getElementById('updateModal').style.display = 'none';
document.querySelectorAll('.station-item').forEach(item => { document.getElementById('updateStationForm').reset();
item.classList.remove('selected'); document.getElementById('update-message').innerHTML = '';
}); }
function openAddStationModal(latlng = null) {
console.log('openAddStationModal called with:', latlng);
document.getElementById('addModal').style.display = 'block';
if (latlng) {
console.log('Setting coordinates:', latlng.lat, latlng.lng);
document.getElementById('coordinates').value = `${latlng.lat.toFixed(6)}, ${latlng.lng.toFixed(6)}`;
if (tempMarker) {
map.removeLayer(tempMarker);
}
tempMarker = L.marker(latlng).addTo(map);
console.log('Marker added to map');
}
}
function closeAddModal() {
document.getElementById('addModal').style.display = 'none';
document.getElementById('addStationForm').reset();
document.getElementById('add-message').innerHTML = '';
document.getElementById('coordinates').value = '';
if (tempMarker) {
map.removeLayer(tempMarker);
tempMarker = null;
}
}
// Close modal when clicking outside
window.onclick = function(event) {
const updateModal = document.getElementById('updateModal');
const addModal = document.getElementById('addModal');
const contextMenu = document.getElementById('contextMenu');
if (event.target === updateModal) {
closeUpdateModal();
}
if (event.target === addModal) {
closeAddModal();
}
if (!contextMenu.contains(event.target)) {
hideContextMenu();
}
} }
function fitMapToStations() { function fitMapToStations() {
@ -641,19 +625,6 @@
fitMapToStations(); fitMapToStations();
} }
function switchTab(tabName) {
// Update tab buttons
document.querySelectorAll('.tab-button').forEach(btn => {
btn.classList.remove('active');
});
document.querySelectorAll('.tab-panel').forEach(panel => {
panel.classList.remove('active');
});
event.target.classList.add('active');
document.getElementById(tabName + '-tab').classList.add('active');
}
function showMessage(elementId, message, type = 'success') { function showMessage(elementId, message, type = 'success') {
const messageDiv = document.getElementById(elementId); const messageDiv = document.getElementById(elementId);
@ -670,7 +641,7 @@
const coordinates = document.getElementById('coordinates').value; const coordinates = document.getElementById('coordinates').value;
if (!coordinates) { if (!coordinates) {
showMessage('add-message', 'Please select a location on the map', 'error'); showMessage('add-message', 'Please right-click or long-press on the map to select a location', 'error');
return; return;
} }
@ -695,13 +666,10 @@
if (response.ok) { if (response.ok) {
showMessage('add-message', 'Station added successfully!'); showMessage('add-message', 'Station added successfully!');
e.target.reset(); setTimeout(() => {
document.getElementById('coordinates').value = ''; closeAddModal();
if (tempMarker) { loadStations();
map.removeLayer(tempMarker); }, 1500);
tempMarker = null;
}
loadStations();
} else { } else {
const result = await response.json(); const result = await response.json();
showMessage('add-message', result.error || 'Failed to add station', 'error'); showMessage('add-message', result.error || 'Failed to add station', 'error');
@ -736,9 +704,10 @@
if (response.ok) { if (response.ok) {
showMessage('update-message', 'Station updated successfully!'); showMessage('update-message', 'Station updated successfully!');
e.target.reset(); setTimeout(() => {
cancelUpdate(); closeUpdateModal();
loadStations(); loadStations();
}, 1500);
} else { } else {
const result = await response.json(); const result = await response.json();
showMessage('update-message', result.error || 'Failed to update station', 'error'); showMessage('update-message', result.error || 'Failed to update station', 'error');
@ -758,7 +727,18 @@
} }
// Initialize dashboard when page loads // Initialize dashboard when page loads
document.addEventListener('DOMContentLoaded', initDashboard); document.addEventListener('DOMContentLoaded', function() {
initDashboard();
// Add event listener for context menu item
const menuItem = document.getElementById('addStationMenuItem');
if (menuItem) {
menuItem.addEventListener('click', contextAddStation);
console.log('Context menu event listener attached');
} else {
console.error('Could not find addStationMenuItem element');
}
});
</script> </script>
</body> </body>
</html> </html>