From 4475202f055be559bca77db51f59a7962f88bf91 Mon Sep 17 00:00:00 2001 From: pakut2 Date: Wed, 24 Feb 2021 13:19:14 +0100 Subject: [PATCH] bug fixes and optimalization --- css/style.css | 6 ++-- js/app.js | 63 +++++++++++++++++++++--------------------- js/directSearchApp.js | 26 +++++++++++++---- js/localSearchApp.js | 36 ++++++++++++++---------- views/localSearch.html | 9 ++++++ 5 files changed, 87 insertions(+), 53 deletions(-) diff --git a/css/style.css b/css/style.css index 035ea27..03f045e 100644 --- a/css/style.css +++ b/css/style.css @@ -312,11 +312,13 @@ form button:hover { opacity: 0; } -.offline { +.offline, +.location { padding-right: 0.5rem; padding-left: 0.5rem; } -.offline span { +.offline span, +.location span { display: flex; justify-content: center; align-content: center; diff --git a/js/app.js b/js/app.js index bc276cf..f4b43ad 100644 --- a/js/app.js +++ b/js/app.js @@ -71,17 +71,19 @@ const app = () => { return; } - for (let i = 0; i < stops.length; i++) { + let dataArr; + + stops.filter((stop) => { if ( - latinize(stops[i].stopName) === latinize(input) && - stops[i].stopCode === "01" && - stops[i].zoneId === 1 + latinize(stop.stopName) === latinize(input) && + stop.stopCode === "01" && + stop.zoneId === 1 ) { - let ID = stops[i].stopId; - let dataArr = [ID, stops[i].stopName]; - return dataArr; + let ID = stop.stopId; + dataArr = [ID, stop.stopName]; } - } + }); + return dataArr; }; const getTimeData = async () => { @@ -300,15 +302,15 @@ const app = () => { } if (directionId === "01") { - for (let i = 0; i < stops.length; i++) { + stops.filter((stopArg) => { if ( - stops[i].stopName === input && - stops[i].stopCode === "02" && - stops[i].zoneId === 1 + stopArg.stopName === input && + stopArg.stopCode === "02" && + stopArg.zoneId === 1 ) { - ID = stops[i].stopId; + ID = stopArg.stopId; stop1[3].children[1].innerText = ID; - dataArr = [ID, stops[i].stopName]; + dataArr = [ID, stopArg.stopName]; stop1[3].children[0].innerText = "02"; stop1[3].children[0].classList.add("hidden"); directionId = stop1[3].children[0].innerText; @@ -316,21 +318,21 @@ const app = () => { localStorage.setItem("directions", JSON.stringify(directions)); } - } + }); } else if (directionId === "02") { if (input === "Wały Piastowskie") { input = "Wały Piastowskie "; } - for (let i = 0; i < stops.length; i++) { + + stops.filter((stopArg) => { if ( - stops[i].stopName === input && - stops[i].stopCode === "01" && - stops[i].zoneId === 1 + stopArg.stopName === input && + stopArg.stopCode === "01" && + stopArg.zoneId === 1 ) { - ID = stops[i].stopId; - + ID = stopArg.stopId; stop1[3].children[1].innerText = ID; - dataArr = [ID, stops[i].stopName]; + dataArr = [ID, stopArg.stopName]; stop1[3].children[0].innerText = "01"; stop1[3].children[0].classList.add("hidden"); directionId = stop1[3].children[0].innerText; @@ -338,7 +340,7 @@ const app = () => { localStorage.setItem("directions", JSON.stringify(directions)); } - } + }); } API_URL = `https://ckan2.multimediagdansk.pl/delays?stopId=${ID}`; @@ -452,17 +454,16 @@ const app = () => { let ID; let dataArr = []; - for (let i = 0; i < stops.length; i++) { + stops.filter((stop) => { if ( - stops[i].stopName === input && - stops[i].stopCode === directions[terminus.indexOf(input)] && - stops[i].zoneId === 1 + stop.stopName === input && + stop.stopCode === directions[terminus.indexOf(input)] && + stop.zoneId === 1 ) { - ID = stops[i].stopId; - - dataArr = [ID, stops[i].stopName]; + ID = stop.stopId; + dataArr = [ID, stop.stopName]; } - } + }); const ID_API_URL = `https://ckan2.multimediagdansk.pl/delays?stopId=${ID}`; diff --git a/js/directSearchApp.js b/js/directSearchApp.js index 3d160dd..973652c 100644 --- a/js/directSearchApp.js +++ b/js/directSearchApp.js @@ -92,6 +92,20 @@ const app = () => { return dataArr; } } + + let dataArr = []; + + stops.filter((stop) => { + if ( + latinize(stop.stopName) === latinize(input) && + stop.stopCode === code && + stop.zoneId === 1 + ) { + let ID = stop.stopId; + dataArr = [ID, `${stop.stopName} ${stop.stopCode}`]; + } + }); + return dataArr; }; const getTimeData = async () => { @@ -302,15 +316,15 @@ const app = () => { let ID; - for (let i = 0; i < stops.length; i++) { + stops.filter((stop) => { if ( - stops[i].stopName === input && - stops[i].stopCode === code && - stops[i].zoneId === 1 + stop.stopName === input && + stop.stopCode === code && + stop.zoneId === 1 ) { - ID = stops[i].stopId; + ID = stop.stopId; } - } + }); ID_API_URL = `https://ckan2.multimediagdansk.pl/delays?stopId=${ID}`; diff --git a/js/localSearchApp.js b/js/localSearchApp.js index dbebbab..59456de 100644 --- a/js/localSearchApp.js +++ b/js/localSearchApp.js @@ -1,4 +1,5 @@ const prompt = document.querySelector(".offline"); +const userLocation = document.querySelector(".location"); window.addEventListener("load", () => { const handleConnection = () => { @@ -62,21 +63,21 @@ const successCallback = (position) => { let array = []; let dataArr = []; - for (let i = 0; i < stops.length; i++) { - const distance = calDist(lat1, lon1, stops[i].stopLat, stops[i].stopLon); + stops.filter((stop) => { + const distance = calDist(lat1, lon1, stop.stopLat, stop.stopLon); if (distance <= 500) { - let ID = stops[i].stopId; - // dataArr = [ID, stops[i].stopName]; + let ID = stop.stopId; + dataArr.push(Math.round(distance)); dataArr.push(ID); - dataArr.push(stops[i].stopName); - dataArr.push(stops[i].stopLon); - dataArr.push(stops[i].stopLat); - dataArr.push(stops[i].stopCode); + dataArr.push(stop.stopName); + dataArr.push(stop.stopLon); + dataArr.push(stop.stopLat); + dataArr.push(stop.stopCode); array.push(dataArr); dataArr = []; } - } + }); return array; }; @@ -90,6 +91,12 @@ const successCallback = (position) => { const getTimeData = async () => { const array = await getIdData(); + + if (array.length === 0) { + userLocation.classList.remove("indicator"); + return; + } + //console.log(array.sort(sortFunction)); array.sort(sortFunction); @@ -212,10 +219,10 @@ const successCallback = (position) => { } }; - getTimeData().catch((error) => { - console.error(); - }); - // getTimeData(); + // getTimeData().catch((error) => { + // console.error(); + // }); + getTimeData(); //get vehicle ids const getVehicleCodes = async (ID) => { @@ -385,7 +392,7 @@ const successCallback = (position) => { marker.remove(); }); - geojson.features.forEach(function (marker) { + geojson.features.forEach((marker) => { const element = document.createElement("div"); element.innerHTML = ''; element.className = "marker"; @@ -453,6 +460,7 @@ const successCallback = (position) => { const errorCallback = (error) => { console.log(error.message); + userLocation.classList.remove("indicator"); }; const app = () => { diff --git a/views/localSearch.html b/views/localSearch.html index 7bd3fbe..9d91b96 100644 --- a/views/localSearch.html +++ b/views/localSearch.html @@ -51,6 +51,15 @@

Closer

from the internet until you reconnect. +
+ It looks like you forbid user localization. + In order to experience all features, please refresh and allow + user localization. +