signModal, click map 4 marker, draft loadmap

This commit is contained in:
user 2024-03-29 19:27:55 +01:00
parent e3811ff49d
commit 3c3f746084
8 changed files with 386 additions and 222 deletions

View file

@ -1,5 +1,5 @@
#modalAddMarker {
display: none; /* Hide the modal by default */
display: none;
position: fixed;
top: 50%;
left: 50%;
@ -7,7 +7,18 @@
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
z-index: 1002;
}
#modalOverlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1001;
}
#modalAddMarker input[type="file"] {
@ -20,3 +31,19 @@
margin-bottom: 10px;
}
#modalSign {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #fff;
padding: 20px;
border-radius: 5px;
z-index: 1002;
}
#registerForm {
display: none;
}

View file

@ -1,95 +1,133 @@
<html>
<head>
<title>RuscoMap</title>
<link rel="stylesheet" href="/leaflet.css" />
<link rel="stylesheet" href="/index.css" />
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
<head>
#map {
height: 90dvh;
max-height: calc(90dvh - 20px);
}
<title>RuscoMap</title>
<link rel="stylesheet" href="/leaflet.css" />
<link rel="stylesheet" href="/index.css" />
<style>
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
h1 {
height: 9dvh;
}
#map {
height: 90dvh;
max-height: calc(90dvh - 20px);
}
body {
height: 100dvh;
padding: 20px;
}
</style>
</head>
h1 {
height: 9dvh;
}
body {
height: 100dvh;
padding: 20px;
}
</style>
</head>
<body>
<div id='modalAddMarker'>
<div id='nameField'>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<body>
<div id="modalAddMarker">
<form id="addMarkerForm">
<div id="latitudeLongitudeField">
<label for="latitudeValue">Latitude:</label>
<p id="latitudeValue"></p>
<label for="longitudeValue">longitude:</label>
<p id="longitudeValue"></p>
</div>
<div id="nameField">
<label for="name">Name:</label>
<input type="text" id="markerName" name="name" required />
</div>
<div id="descriptionField">
<label for="description">Description:</label>
<textarea
id="markerDescription"
name="description"
required
></textarea>
</div>
<div id="photoField">
<label for="photo">Photo:</label>
<input type="file" id="markerPhoto" name="photo" />
</div>
<button type="submit" id="addMarker">Aggiungi marker</button>
</form>
</div>
<div id='descriptionField'>
<label for="description">Description:</label>
<textarea id="description" name="description"></textarea>
<div id="modalSign">
<form id="loginForm">
<h2>Login</h2>
<div class="container">
<label for="username"><b>Username</b></label>
<input
id="username"
type="text"
placeholder="Enter Username"
name="username"
required
/>
<label for="password"><b>Password</b></label>
<input
id="password"
type="password"
placeholder="Enter Password"
name="password"
required
/>
<button type="submit" name="action" value="loginModal">
Login
</button>
</div>
<button id="showRegisterForm" formnovalidate>
Non hai un account? Registrati
</button>
</form>
<form id="registerForm">
<h2>Registra</h2>
<div class="container">
<label for="username"><b>Username</b></label>
<input
id="username"
type="text"
placeholder="Enter Username"
name="username"
required
/>
<label for="password"><b>Password</b></label>
<input
id="password"
type="password"
placeholder="Enter Password"
name="password"
required
/>
<button type="submit" name="registerModal" value="register">
Registrati
</button>
<button id="showLoginForm" formnovalidate>
Hai un account? Loggati
</button>
</div>
</form>
</div>
<div id='photoField'>
<label for="photo">Photo:</label>
<input type="file" id="photo" name="photo">
</div>
<button id="addMarker"> Aggiungi marker </button>
</div>
<div id='modalOverlay'></div>
<h1>RuscoMap <button id="PMaggiore"> Aggiungi P.zza maggiore </button> </h1>
<div id="modalOverlay"></div>
<button id="showAddMarkerModal"> Aggiungi marker </button>
<h1>RuscoMap</h1>
<!-- LogIn/SignIn -->
<button id="showAddMarkerModal">Aggiungi marker</button>
<button id="showModalSign">Login / Registrazione</button>
<h2>User Registration</h2>
<form id="registerForm">
<div class="container">
<label for="username"><b>Username</b></label>
<input id="username" type="text" placeholder="Enter Username" name="username" required>
<script src="register.js"></script>
<script src="login.js"></script>
<label for="password"><b>Password</b></label>
<input id="password" type="password" placeholder="Enter Password" name="password" required>
<div id="map"></div>
<button type="submit">Register</button>
</div>
</form>
<script defer src="/leaflet.js"></script>
<h2>User Login</h2>
<form id="loginForm">
<div class="container">
<label for="loginUsername"><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="loginUsername" id="loginUsername" required>
<label for="loginPassword"><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="loginPassword" id="loginPassword" required>
<button type="submit">Login</button>
</div>
</form>
<script src="register.js"></script>
<script src="login.js"></script>
<div id="map"></div>
<script defer src="/leaflet.js"></script>
<script defer src="/index.js"></script>
</body>
</html>
<script defer src="/index.js"></script>
</body>
</html>

View file

@ -1,103 +1,118 @@
let map = null;
let points = [];
let map = null
let points = []
//BOLOGNA
const x = 11.343095841047235;
const y = 44.49373478876318;
const piazzaMaggiore = [x, y];
const x = 11.343095841047235
const y = 44.49373478876318
const piazzaMaggiore = [x, y]
var lat = 0
var lng = 0
// COORDS ESISTENTI
async function downloadRusco() {
const resp = await window.fetch("/rusco.geojson");
async function downloadRusco () {
const resp = await window.fetch('/rusco.geojson')
const rusco = await resp.json()
return rusco.features;
return rusco.features
}
// SET VIEW
function loadMap() {
map = L.map('map').setView([44.49593478876318, 11.343095841047235], 13.5);
async function initMap () {
map = L.map('map').setView([44.49593478876318, 11.343095841047235], 13.5)
// IMPOSTA MAPPA
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; OpenStreetMap contributors | RuscoMap'
}).addTo(map);
// LOAD JSON WITH nome, descrizione e fotoURl
fetch('rusco.geojson')
.then(response => response.json())
.then(data => {
L.geoJSON(data, {
onEachFeature: function (feature, layer) {
layer.bindPopup('<h2>' +
feature.properties.Nome + '</h2><p>' + feature.properties.Descrizione + '</p><img src="' + feature.properties.Foto + '" alt="Photo" style="width:200px;height:auto;">');
}
}).addTo(map);
});
}).addTo(map)
loadMarkers(map)
map.on('click', function (e) {
lat = e.latlng.lat
lng = e.latlng.lng
document.getElementById('latitudeValue').textContent = lat
document.getElementById('longitudeValue').textContent = lng
// Bind a popup to the marker
showModal('modalAddMarker')
})
}
//LOGICA BTN
function preparaDOM() {
const button = document.querySelector('#PMaggiore');
button.addEventListener('click', () => {
//const risposta = prompt("Dammi coordinates");
const puntoNuovo = {
"type": "Feature",
"properties": {
"Nome": "Coors Field",
"Descrizione": "Baseball Stadium",
},
"geometry": {
"type": "Point",
"coordinates": piazzaMaggiore
async function loadMarkers(map) {
fetch('http://localhost:3000/getRuschi')
.then(response => response.json())
.then(data => {
L.geoJSON(data, {
onEachFeature: function (feature, layer) {
layer.bindPopup('<h2>' +
feature.properties.Nome + '</h2><p>' + feature.properties.Descrizione + '</p><img src="' + feature.properties.Foto + '" alt="Photo" style="width:200px;height:auto;">');
}
}
//PUSH NUOVECOORD
points.push(puntoNuovo);
//RELOAD JSON
L.geoJSON(points).addTo(map);
button.innerHTML = "Aggiungi piu' ombra"
}).addTo(map);
});
}
// SEGUENZA EXEC
async function load() {
points = await downloadRusco();
//console.log (points.features)
preparaDOM();
loadMap();
async function load () {
points = await downloadRusco()
await initMap()
}
function showModalAddMarker(){
const addMarkerModal = document.getElementById('modalAddMarker');
const modalOverlay = document.getElementById('modalOverlay');
addMarkerModal.style.display = 'block'; // Show the modal
addMarkerModal.style.zIndex = '1002'; // Show the modal
modalOverlay.style.position = 'fixed';
modalOverlay.style.top = '0';
modalOverlay.style.left = '0';
modalOverlay.style.width = '100%';
modalOverlay.style.height = '100%';
modalOverlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)'; // Black semi-transparent background
modalOverlay.style.zIndex = '1001'; // Ensure it's on top of everything
function showModal (modalName) {
const selectedModal = document.getElementById(modalName)
const modalOverlay = document.getElementById('modalOverlay')
selectedModal.style.display = 'block' // Show the modal
modalOverlay.style.display = 'block' // Show the overlay
}
function closeModalAddMarker(){
const addMarkerModal = document.getElementById('modalAddMarker');
const modalOverlay = document.getElementById('modalOverlay');
addMarkerModal.style.display = 'none';
modalOverlay.style.display = 'none';
function detectCurrentShownModal(){
const modalAddMarker = document.getElementById('modalAddMarker')
const modalSign = document.getElementById('modalSign')
return modalAddMarker.style.display !== 'none' ? modalAddMarker : modalSign
}
function addMarker(){
const nameValue = document.getElementById('name').value;
const descriptionValue = document.getElementById('description').value;
const photoValue = document.getElementById('photo').value;
closeModalAddMarker()
function closeModal () {
const currentShownModal = detectCurrentShownModal()
const modalOverlay = document.getElementById('modalOverlay')
currentShownModal.style.display = 'none'
modalOverlay.style.display = 'none'
}
window.addEventListener('load', load);
function addMarker () {
const name = document.getElementById('name').value
const description = document.getElementById('description').value
const imageUrl = document.getElementById('photo').value
const latitude = document.getElementById('latitude').value
const longitude = document.getElementById('longitude').value
fetch('http://localhost:3000/addRuschi', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: name,
description: description,
imageUrl: imageUrl,
latitude: latitude,
longitude: longitude
})
}).then(response => {
if (response.ok) {
// Authentication successful
console.log('Added marker')
// Redirect the user to the authenticated area or perform other actions
} else {
// Authentication failed
console.error('Error adding marker')
}
})
.catch(error => {
console.error('Error adding marker', error)
})
closeModalAddMarker()
}
const addMarkerModal = document.getElementById('showAddMarkerModal');
addMarkerModal.addEventListener('click', showModalAddMarker);
window.addEventListener('load', load)
const modalOverlay = document.getElementById('modalOverlay');
modalOverlay.addEventListener('click', closeModalAddMarker);
const showAddMarkerModal = document.getElementById('showAddMarkerModal')
showAddMarkerModal.addEventListener('click', () => showModal('modalAddMarker'))
const addMarkerBtn = document.getElementById('addMarker');
addMarkerBtn.addEventListener('click', addMarker);
const showModalSign = document.getElementById('showModalSign')
showModalSign.addEventListener('click', () => showModal('modalSign'))
const modalOverlay = document.getElementById('modalOverlay')
modalOverlay.addEventListener('click', closeModal)

View file

@ -1,6 +1,11 @@
// login.js
document.getElementById('loginForm').addEventListener('submit', function (event) {
event.preventDefault();
if(event.submitter.id == 'showRegisterForm') {
return showRegisterForm();
}
const username = document.getElementById('loginUsername').value;
const password = document.getElementById('loginPassword').value;
// Make a POST request to the server to authenticate the user
@ -25,4 +30,10 @@ document.getElementById('loginForm').addEventListener('submit', function (event)
console.error('Error authenticating user', error);
});
});
function showRegisterForm(){
const registerForm = document.getElementById('registerForm')
registerForm.style.display = 'block'
const loginForm = document.getElementById('loginForm')
loginForm.style.display = 'none'
}

View file

View file

@ -1,6 +1,12 @@
// register.js
document.getElementById('registerForm').addEventListener('submit', function (event) {
event.preventDefault();
if(event.submitter.id == 'showLoginForm') {
return showLoginModal();
}
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
const data = { username: username, password: password };
@ -26,4 +32,10 @@ document.getElementById('registerForm').addEventListener('submit', function (eve
console.error('Error registering new user', error);
});
});
function showLoginModal(){
const loginForm = document.getElementById('loginForm')
loginForm.style.display = 'block'
const registerForm = document.getElementById('registerForm')
registerForm.style.display = 'none'
}

23
ruscoapp/public/unused.js Normal file
View file

@ -0,0 +1,23 @@
//LOGICA BTN
function preparaDOM () {
const button = document.querySelector('#PMaggiore')
button.addEventListener('click', () => {
//const risposta = prompt("Dammi coordinates");
const puntoNuovo = {
type: 'Feature',
properties: {
Nome: 'Coors Field',
Descrizione: 'Baseball Stadium'
},
geometry: {
type: 'Point',
coordinates: piazzaMaggiore
}
}
//PUSH NUOVECOORD
points.push(puntoNuovo)
//RELOAD JSON
L.geoJSON(points).addTo(map)
button.innerHTML = "Aggiungi piu' ombra"
})
}

View file

@ -1,84 +1,122 @@
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const express = require('express')
const sqlite3 = require('sqlite3').verbose()
const path = require('path')
const bcrypt = require('crypto');
const bcrypt = require('crypto')
const app = express();
const port = 3000;
const app = express()
const port = 3000
// Create a new SQLite database in memory
const db = new sqlite3.Database('ruscodb');
const db = new sqlite3.Database('ruscodb')
// Create a table to store data
db.serialize(() => {
db.table
db.get(`SELECT name FROM sqlite_master WHERE type='table' AND name='Customers'`, (err, row) => {
if(err){
db.run("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, description TEXT, image_url TEXT, latitude REAL, longitude REAL)");
}
})
});
db.get(
`SELECT name FROM sqlite_master WHERE type='table' AND name='ruschi';`,
(err, row) => {
if (!row) {
db.run(
'CREATE TABLE ruschi (id INTEGER PRIMARY KEY, type TEXT, name TEXT, description TEXT, image_url TEXT, latitude REAL, longitude REAL)'
)
}
}
)
})
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.json())
// app.get('/', (req, res) => {
// res.sendFile('index.html')
// });
// Insert a new item into the database
app.get('/addRuschi', (req, res) => {
const { name, description, image_url, latitude, longitude } = req.query;
db.run("INSERT INTO ruschi (type, nome, descrizione, foto, lat, lng) VALUES (?, ?, ?, ?, ?)", [type, name, description, image_url, latitude, longitude], function (err) {
if (err) {
return console.error(err.message);
}
console.log(`A row has been inserted with rowid ${this.lastID}`);
res.send(`Item added with ID: ${this.lastID}`);
});
});
app.post('/addRuschi', (req, res) => {
const { name, description, imageUrl, latitude, longitude } = req.body
const type = 'bho'
db.run(
'INSERT INTO ruschi (type, name, description, image_url, latitude, longitude) VALUES (?, ?, ?, ?, ?, ?)',
[type, name, description, imageUrl, latitude, longitude],
function (err) {
if (err) {
return console.error(err.message)
}
console.log(`A row has been inserted with rowid ${this.lastID}`)
res.send(`Item added with ID: ${this.lastID}`)
}
)
})
// Retrieve all items from the database
app.get('/getRuschi', (req, res) => {
db.all("SELECT * FROM ruschi", [], (err, rows) => {
if (err) {
return console.error(err.message);
}
res.json(rows);
});
});
db.all('SELECT * FROM ruschi', [], (err, rows) => {
if (err) {
return console.error(err.message)
}
let ruschi = []
rows.forEach(rusco => {
const coordinates = { latitude: rusco.latitude, longitude: rusco.longitude }
const marker = {
type: "Feature",
properties: {
name: rusco.name,
description: rusco.description,
image_url: rusco.image_url,
},
geometry: {
type: rusco.type,
coordinates: coordinates
}
}
ruschi.push(marker)
})
return res.json(ruschi)
})
})
app.post('/login', (req, res) => {
const username = req.body.username;
const password = req.body.password;
const hashedPassword = bcrypt.createHash('sha256').update(password).digest('base64'); //.digest('base64');
const username = req.body.username
const password = req.body.password
const hashedPassword = bcrypt
.createHash('sha256')
.update(password)
.digest('base64') //.digest('base64');
db.all("SELECT * FROM users WHERE username = ? AND password = ?", [username, hashedPassword], (err, rows) => {
if (err) {
return console.error(err.message)
}
if(rows.length == 1) {
return res.json('Logged in')
}
return res.json('Incorrect username or password')
})
db.all(
'SELECT * FROM users WHERE username = ? AND password = ?',
[username, hashedPassword],
(err, rows) => {
if (err) {
return console.error(err.message)
}
if (rows.length == 1) {
return res.json('Logged in')
}
return res.json('Incorrect username or password')
}
)
})
app.post('/register', (req, res) => {
console.log(req.body)
const username = req.body.username;
const password = req.body.password;
const hashedPassword = bcrypt.createHash('sha256').update(password).digest('base64'); //.digest('base64');
db.run('insert into users (username, password) VALUES (?, ?)', [username, hashedPassword], (err, rows) => {
if (err) {
return console.error(err.message);
}
res.json('OK');
});
});
console.log(req.body)
const username = req.body.username
const password = req.body.password
const hashedPassword = bcrypt
.createHash('sha256')
.update(password)
.digest('base64') //.digest('base64');
db.run(
'insert into users (username, password) VALUES (?, ?)',
[username, hashedPassword],
(err, rows) => {
if (err) {
return console.error(err.message)
}
res.json('OK')
}
)
})
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
console.log(`Server is running on http://localhost:${port}`)
})