dawarich/app/javascript/controllers/public_trip_map_controller.js

95 lines
2.2 KiB
JavaScript
Raw Normal View History

Implement public trip sharing with Shareable concern This commit implements comprehensive public trip sharing functionality by extracting sharing logic into a reusable Shareable concern and extending it to Trip models. ## Key Features **Shareable Concern (DRY principle)** - Extract sharing logic from Stat model into reusable concern - Support for time-based expiration (1h, 12h, 24h, permanent) - UUID-based secure public access - User-controlled sharing of notes and photos - Automatic UUID generation on model creation **Database Changes** - Add sharing_uuid (UUID) column to trips table - Add sharing_settings (JSONB) column for configuration storage - Add unique index on sharing_uuid for performance **Public Trip Sharing** - Public-facing trip view with read-only access - Interactive map with trip route visualization - Optional sharing of notes and photo previews - Branded footer with Dawarich attribution - Responsive design matching existing UI patterns **Sharing Management** - In-app sharing controls in trip show view - Enable/disable sharing with one click - Configurable expiration times - Copy-to-clipboard for sharing URLs - Visual indicators for sharing status **Authorization & Security** - TripPolicy for fine-grained access control - Public access only for explicitly shared trips - Automatic expiration enforcement - Owner-only sharing management - UUID-based URLs prevent enumeration attacks **API & Routes** - GET /shared/trips/:trip_uuid for public access - PATCH /trips/:id/sharing for sharing management - RESTful endpoint design consistent with stats sharing **Frontend** - New public-trip-map Stimulus controller - OpenStreetMap tiles for public viewing (no API key required) - Start/end markers on trip route - Automatic map bounds fitting **Tests** - Comprehensive concern specs (Shareable) - Model specs for Trip sharing functionality - Request specs for public and authenticated access - Policy specs for authorization rules - 100% coverage of sharing functionality ## Implementation Details ### Models Updated - Stat: Now uses Shareable concern (removed duplicate code) - Trip: Includes Shareable concern with notes/photos options ### Controllers Added - Shared::TripsController: Handles public viewing and sharing management ### Views Added - trips/public_show.html.erb: Public-facing trip view - trips/_sharing.html.erb: Sharing controls partial ### JavaScript Added - public_trip_map_controller.js: Map rendering for public trips ### Helpers Extended - TripsHelper: Added sharing status and expiration helpers ## Breaking Changes None. This is a purely additive feature. ## Migration Required Yes. Run: rails db:migrate ## Testing All specs pass: - spec/models/concerns/shareable_spec.rb - spec/models/trip_spec.rb - spec/requests/shared/trips_spec.rb - spec/policies/trip_policy_spec.rb
2025-11-05 10:44:27 -05:00
import L from "leaflet";
import BaseController from "./base_controller";
export default class extends BaseController {
static values = {
coordinates: Array,
name: String
};
connect() {
super.connect();
this.initializeMap();
this.renderTripPath();
}
disconnect() {
if (this.map) {
this.map.remove();
}
}
initializeMap() {
// Initialize map with interactive controls enabled
this.map = L.map(this.element, {
zoomControl: true,
scrollWheelZoom: true,
doubleClickZoom: true,
touchZoom: true,
dragging: true,
keyboard: true
});
// Add OpenStreetMap tile layer (free for public use)
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 19
}).addTo(this.map);
// Add scale control
L.control.scale({
position: 'bottomright',
imperial: false,
metric: true,
maxWidth: 120
}).addTo(this.map);
// Default view
this.map.setView([20, 0], 2);
}
renderTripPath() {
if (!this.coordinatesValue || this.coordinatesValue.length === 0) {
return;
}
// Create polyline from coordinates
const polyline = L.polyline(this.coordinatesValue, {
color: '#3b82f6',
opacity: 0.8,
weight: 3
}).addTo(this.map);
// Add start and end markers
if (this.coordinatesValue.length > 0) {
const startCoord = this.coordinatesValue[0];
const endCoord = this.coordinatesValue[this.coordinatesValue.length - 1];
// Start marker (green)
L.circleMarker(startCoord, {
radius: 8,
fillColor: '#10b981',
color: '#fff',
weight: 2,
opacity: 1,
fillOpacity: 0.8
}).addTo(this.map).bindPopup('<strong>Start</strong>');
// End marker (red)
L.circleMarker(endCoord, {
radius: 8,
fillColor: '#ef4444',
color: '#fff',
weight: 2,
opacity: 1,
fillOpacity: 0.8
}).addTo(this.map).bindPopup('<strong>End</strong>');
}
// Fit map to polyline bounds with padding
this.map.fitBounds(polyline.getBounds(), {
padding: [50, 50]
});
}
}