From 579fca3c197f8b3f2fcbb3dc05b2b7b666815e9c Mon Sep 17 00:00:00 2001 From: 0d0acre <0d0acre@esiliati.org> Date: Sat, 21 Sep 2024 01:11:50 +0200 Subject: [PATCH] Format and fix date --- public/index.html | 8 +++-- public/js/dayjs/dayjs.min.js | 1 + public/js/map.js | 39 +++++++++++----------- public/js/mapInit.js | 3 +- public/js/markerHandler.js | 65 ++++++++++++++++++++++-------------- server/dbHandler.js | 13 ++++---- 6 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 public/js/dayjs/dayjs.min.js diff --git a/public/index.html b/public/index.html index 733fa61..8f9b1f4 100644 --- a/public/index.html +++ b/public/index.html @@ -98,6 +98,7 @@ setTimeout(closePopup, 13000); window.addEventListener('load', () => { + /* // Add a click event listener to the button const gpsButton = document.getElementById("gps-button"); gpsButton.addEventListener("click", function () { @@ -107,9 +108,12 @@ lc.start(); } }); + */ }); - - + + + + diff --git a/public/js/dayjs/dayjs.min.js b/public/js/dayjs/dayjs.min.js new file mode 100644 index 0000000..61916d8 --- /dev/null +++ b/public/js/dayjs/dayjs.min.js @@ -0,0 +1 @@ +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).dayjs=e()}(this,(function(){"use strict";var t=1e3,e=6e4,n=36e5,r="millisecond",i="second",s="minute",u="hour",a="day",o="week",c="month",f="quarter",h="year",d="date",l="Invalid Date",$=/^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[Tt\s]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/,y=/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,M={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),ordinal:function(t){var e=["th","st","nd","rd"],n=t%100;return"["+t+(e[(n-20)%10]||e[n]||e[0])+"]"}},m=function(t,e,n){var r=String(t);return!r||r.length>=e?t:""+Array(e+1-r.length).join(n)+t},v={s:m,z:function(t){var e=-t.utcOffset(),n=Math.abs(e),r=Math.floor(n/60),i=n%60;return(e<=0?"+":"-")+m(r,2,"0")+":"+m(i,2,"0")},m:function t(e,n){if(e.date()1)return t(u[0])}else{var a=e.name;D[a]=e,i=a}return!r&&i&&(g=i),i||!r&&g},O=function(t,e){if(S(t))return t.clone();var n="object"==typeof e?e:{};return n.date=t,n.args=arguments,new _(n)},b=v;b.l=w,b.i=S,b.w=function(t,e){return O(t,{locale:e.$L,utc:e.$u,x:e.$x,$offset:e.$offset})};var _=function(){function M(t){this.$L=w(t.locale,null,!0),this.parse(t),this.$x=this.$x||t.x||{},this[p]=!0}var m=M.prototype;return m.parse=function(t){this.$d=function(t){var e=t.date,n=t.utc;if(null===e)return new Date(NaN);if(b.u(e))return new Date;if(e instanceof Date)return new Date(e);if("string"==typeof e&&!/Z$/i.test(e)){var r=e.match($);if(r){var i=r[2]-1||0,s=(r[7]||"0").substring(0,3);return n?new Date(Date.UTC(r[1],i,r[3]||1,r[4]||0,r[5]||0,r[6]||0,s)):new Date(r[1],i,r[3]||1,r[4]||0,r[5]||0,r[6]||0,s)}}return new Date(e)}(t),this.init()},m.init=function(){var t=this.$d;this.$y=t.getFullYear(),this.$M=t.getMonth(),this.$D=t.getDate(),this.$W=t.getDay(),this.$H=t.getHours(),this.$m=t.getMinutes(),this.$s=t.getSeconds(),this.$ms=t.getMilliseconds()},m.$utils=function(){return b},m.isValid=function(){return!(this.$d.toString()===l)},m.isSame=function(t,e){var n=O(t);return this.startOf(e)<=n&&n<=this.endOf(e)},m.isAfter=function(t,e){return O(t) { createMarker(marker, L, map); @@ -43,3 +34,13 @@ export async function fetchNewMarkers() { lastUpdateCheck = new Date().getTime(); } + +const main = async () => { + await fetchAllMarkers(); + + setInterval(fetchNewMarkers, 5000); +} + +main().then(() => { + console.log("Completed") +}).catch(console.error) \ No newline at end of file diff --git a/public/js/mapInit.js b/public/js/mapInit.js index 94c39cf..cbcfb75 100644 --- a/public/js/mapInit.js +++ b/public/js/mapInit.js @@ -4,8 +4,7 @@ export function mapInit() { const coordsPiazzaMaggiore = [44.49385, 11.34316]; - - var map = L.map("map").setView(coordsPiazzaMaggiore, 13); + const map = L.map("map").setView(coordsPiazzaMaggiore, 13); L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", { maxZoom: 19, diff --git a/public/js/markerHandler.js b/public/js/markerHandler.js index 26e9256..74a9491 100644 --- a/public/js/markerHandler.js +++ b/public/js/markerHandler.js @@ -1,17 +1,28 @@ +const formatDate = (date) => { + const day = date.getDate(); + const month = date.getMonth() + 1; + const year = date.getFullYear(); + + return `${day}/${month}/${year}`; +}; +const formatTime = (date) => { + return date.toTimeString().split(" ")[0]; +}; + export function initMarkers(markerList, L, map) { markerList.forEach((customMarker) => { - var marker = L.marker([customMarker.x, customMarker.y]).addTo(map); - let popUpContentTitle = "

" + customMarker.title + "

"; - let popUpContentdescription = "

" + customMarker.description + "

"; - let popUpContentImage = + const marker = L.marker([customMarker.x, customMarker.y]).addTo(map); + const popUpContentTitle = "

" + customMarker.title + "

"; + const popUpContentdescription = "

" + customMarker.description + "

"; + const popUpContentImage = ""; - let popUpContentDate = + const popUpContentDate = "

Inserito il: " + markerData.ts.substring(0, 10) + "
alle " + markerData.ts.substring(11, 16) + "

"; - let popUpContent = + const popUpContent = popUpContentTitle + popUpContentdescription + popUpContentImage + @@ -21,21 +32,22 @@ export function initMarkers(markerList, L, map) { } export function createMarker(markerData, L, map, returnMarker = false) { - var marker = L.marker([ + const marker = L.marker([ markerData.coordinate.y, markerData.coordinate.x, ]).addTo(map); - let popUpContentTitle = "

" + markerData.name + "

"; - let popUpContentdescription = "

" + markerData.description + "

"; - let popUpContentImage = ""; - let popUpContentDate = - "

Inserito il: " + - markerData.ts.substring(0, 10) + - "
alle: " + - markerData.ts.substring(11, 16) + - "

"; - //let popUpContentHour = "

" + "alle: " + markerData.ts.substring(11, 16) + "

"; - let popUpContent = + const date = new Date(markerData.ts); + + const popUpContentTitle = "

" + markerData.name + "

"; + const popUpContentdescription = "

" + markerData.description + "

"; + const popUpContentImage = + ""; + const popUpContentDate = `

Inserito il: ${formatDate( + date + )}
alle: ${formatTime(date)}

`; + + //const popUpContentHour = "

" + "alle: " + markerData.ts.substring(11, 16) + "

"; + const popUpContent = popUpContentTitle + popUpContentdescription + popUpContentImage + @@ -47,12 +59,12 @@ export function createMarker(markerData, L, map, returnMarker = false) { } export function createMarkerTemp(markerData, L, map, returnMarker = false) { - var marker = L.marker([ + const marker = L.marker([ markerData.coordinate.y, markerData.coordinate.x, ]).addTo(map); - let popUpContentdescription = "

" + markerData.description + "

"; - let popUpContent = popUpContentdescription; + const popUpContentdescription = "

" + markerData.description + "

"; + const popUpContent = popUpContentdescription; marker.bindPopup(popUpContent); if (returnMarker) { return marker; @@ -60,12 +72,15 @@ export function createMarkerTemp(markerData, L, map, returnMarker = false) { } export function updateMarkers(markerList, L, map) { - var layers = L.LayerGroup(), //layers contains all markers.. - contained = []; //makers in map boundingbox + const layers = L.LayerGroup(); //layers contains all markers.. + const contained = []; //makers in map boundingbox layers.eachLayer(function (l) { - if (l instanceof L.Marker && map.getBounds().contains(l.getLatLng())) - contained.push(l); + if ( + layer instanceof L.Marker && + map.getBounds().contains(layer.getLatLng()) + ) + contained.push(layer); }); console.log(contained); } diff --git a/server/dbHandler.js b/server/dbHandler.js index ecaa3bf..2a44f95 100644 --- a/server/dbHandler.js +++ b/server/dbHandler.js @@ -9,19 +9,18 @@ const pool = mysql.createPool({ }); function addMarker(marker) { - let insertQuery = "INSERT INTO markers VALUES (?,?,?,POINT(?,?),?)"; - const date = new Date().toISOString(); - const datePart = date.slice(0, 10); // '2024-09-06' - const timePart = date.slice(11, 19); // '21:33:44' - const formattedDate = `${datePart} ${timePart}`; - let query = mysql.format(insertQuery, [ + const insertQuery = "INSERT INTO markers VALUES (?,?,?,POINT(?,?),?)"; + const date = new Date(); + + const query = mysql.format(insertQuery, [ marker.name, marker.description, marker.filename, marker.long, marker.lat, - formattedDate, + date, ]); + pool.query(query, (err, response) => { if (err) { console.error(err);