Skip to content

Commit

Permalink
Home info is given directly as initial map data
Browse files Browse the repository at this point in the history
  • Loading branch information
krasch committed Dec 18, 2024
1 parent a1e74c3 commit f327220
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 26 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ <h1>Wo soll die Reise starten?</h1>
if (home && ["Berlin", "Hamburg", "Köln", "München"].includes(home)) {

try {
main(map, calendar);
main(home, map, calendar);
} catch (error) {
console.error(error);
}
Expand Down
17 changes: 7 additions & 10 deletions script/components/componentData.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function prepareDataForCalendar(journeys, database) {
return data;
}

function prepareInitialDataForMap(cityInfo, connections) {
function prepareInitialDataForMap(home, cityInfo, connections) {
const cities = { geo: {}, defaults: {} };
const edges = { geo: {}, defaults: {} };

Expand All @@ -129,6 +129,11 @@ function prepareInitialDataForMap(cityInfo, connections) {
cities.defaults[id].markerSize = "small";
cities.defaults[id].markerColor = "light";
}
if (cityInfo[id].name === home) {
cities.defaults[id].markerIcon = "home";
cities.defaults[id].markerSize = "large";
cities.defaults[id].markerColor = "dark";
}
}

for (let edge of c.edges) {
Expand All @@ -149,19 +154,11 @@ function prepareInitialDataForMap(cityInfo, connections) {
return [cities, edges];
}

function prepareDataForMap(home, journeys, database) {
function prepareDataForMap(journeys, database) {
const cities = {};
const edges = { state: {}, mapping: {} };
const journeyInfo = {};

if (home) {
cities[CITY_NAME_TO_ID[home]] = {
markerIcon: "home",
markerSize: "large",
markerColor: "dark",
};
}

const activeJourney = journeys.activeJourney; // might be null
for (let journey of journeys.journeys) {
const active = activeJourney !== null && journey.id === activeJourney.id;
Expand Down
8 changes: 3 additions & 5 deletions script/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function initUpdateViews(map, calendar, database) {
function updateViews(state) {
map.updateView(prepareDataForMap(state.home, state.journeys, database));
map.updateView(prepareDataForMap(state.journeys, database));
calendar.updateView(prepareDataForCalendar(state.journeys, database));

if (state.journeys.activeJourney) {
Expand All @@ -24,11 +24,9 @@ function addIsDestinationInfo(CITIES, ROUTES) {
}
}

async function main(map, calendar) {
async function main(home, map, calendar) {
// init state
const params = new URLSearchParams(window.location.search);
const state = {
home: params.get("start"),
journeys: new JourneyCollection(),
};

Expand All @@ -42,7 +40,7 @@ async function main(map, calendar) {
const database = new Database(connections);

// initial drawing of all necessary geo information
const initialMapData = prepareInitialDataForMap(CITIES, connections);
const initialMapData = prepareInitialDataForMap(home, CITIES, connections);
const mapLoadedPromise = map.load(initialMapData);

// init update views
Expand Down
19 changes: 9 additions & 10 deletions tests/componentData.map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const { Database } = require("../script/database.js");
const { createConnection, testCities } = require("../tests/data.js");

test("prepareInitialDataForMap", function () {
const home = "City1";

const c1 = createConnection([
["2024-10-15", "06:00", "city1MainStationId"],
["2024-10-15", "07:00", "city1ExtraStationId"],
Expand All @@ -29,7 +31,7 @@ test("prepareInitialDataForMap", function () {
["2024-10-15", "09:00", "city1MainStationId"],
]);

const got = prepareInitialDataForMap(testCities, [c1, c2, c3]);
const got = prepareInitialDataForMap(home, testCities, [c1, c2, c3]);
const expCities = {
geo: {
cityId1: {
Expand All @@ -49,6 +51,9 @@ test("prepareInitialDataForMap", function () {
cityId1: {
rank: 1,
menuDestination: false,
markerIcon: "home",
markerSize: "large",
markerColor: "dark",
},
cityId2: {
rank: 2,
Expand Down Expand Up @@ -92,7 +97,7 @@ test("prepareDataForMapEmpty", function () {

const journeys = new JourneyCollection();

const got = prepareDataForMap(null, journeys, database);
const got = prepareDataForMap(journeys, database);
expect(got).toStrictEqual([{}, { mapping: {}, state: {} }, {}]);
});

Expand All @@ -112,9 +117,6 @@ test("prepareDataForMapNoActiveJourney", function () {
const expCities = {
cityId1: {
circleVisible: true,
markerIcon: "home",
markerSize: "large",
markerColor: "dark",
},
cityId2: { circleVisible: true },
cityId3: {
Expand Down Expand Up @@ -153,7 +155,7 @@ test("prepareDataForMapNoActiveJourney", function () {

const expJourneys = { "City1->City3": getJourneySummary([c1]) };

const got = prepareDataForMap("City1", journeys, database);
const got = prepareDataForMap(journeys, database);
expect(got).toStrictEqual([expCities, expEdges, expJourneys]);
});

Expand Down Expand Up @@ -185,9 +187,6 @@ test("prepareDataForMap", function () {
cityId1: {
circleVisible: true,
circleColor: `rgb(${getColor(0)})`,
markerIcon: "home",
markerSize: "large",
markerColor: "dark",
},
cityId2: {
circleVisible: true,
Expand Down Expand Up @@ -265,6 +264,6 @@ test("prepareDataForMap", function () {
"City1->City5": getJourneySummary([c3]),
};

const got = prepareDataForMap("City1", journeys, database);
const got = prepareDataForMap(journeys, database);
expect(got).toStrictEqual([expCities, expEdges, expJourneys]);
});

0 comments on commit f327220

Please sign in to comment.