Skip to content

Commit

Permalink
Move out duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
krasch committed Dec 19, 2024
1 parent a21ebd8 commit f1b9a2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@
<div class="title"></div>
<div class="row menu-entry">
<!-- id, name and for will be made unique when instantiating the template -->
<input type="checkbox" id="routes" name="show" value="routes">
<label for="routes">Routen hierher</label>
<input type="checkbox" id="city-menu-routes" name="city-menu-routes" value="routes">
<label for="city-menu-routes">Routen hierher</label>
</div>
</div>
</template>
Expand All @@ -125,8 +125,8 @@
<div class="row">Reisezeit: &nbsp; <strong class="travel-time"></strong></div>
<!--div class="row menu-entry"-->
<!-- id, name and for will be made unique when instantiating the template -->
<!--input type="checkbox" id="showJourneyCalendar" name="show" value="journey">
<label for="showJourneyCalendar">Reisekalender anzeigen</label>
<!--input type="checkbox" id="edge-menu-calendar" name="edge-menu-calendar" value="journey">
<label for="edge-menu-calendar">Reisekalender anzeigen</label>
</div-->
</div>
</template>
Expand Down
31 changes: 8 additions & 23 deletions script/components/map/mapObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,17 @@ class CityMenu {
}

#initMenuEntry(element, cityId, cityName) {
const input = element.querySelector("input");
const label = element.querySelector("label");

// make unique across document by adding city to strings
input.name = `city-menu-${cityId}`;
input.id = `city-menu-${cityId}-${input.id}`;
label.setAttribute(
"for",
`city-menu-${cityId}-${label.getAttribute("for")}`,
);
const result = initInputAndLabel(element, cityId);

// needed for reacting when entry is chosen
input.data = {
result.input.data = {
type: "city",
cityName: cityName,
cityId: cityId,
entry: input.value,
entry: result.input.value,
};

return input.value; // what is the "name" of this entry?
return result.input.value; // what is the "name" of this entry?
}
}

Expand Down Expand Up @@ -155,22 +146,16 @@ class JourneyMenu {
update(diff) {}

#initMenuEntry(element, id) {
const input = element.querySelector("input");
const label = element.querySelector("label");

// make unique across document by adding id to strings
input.name = `edge-menu-${id}`;
input.id = `edge-menu-${id}-${input.id}`;
label.setAttribute("for", `edge-menu-${id}-${label.getAttribute("for")}`);
const result = initInputAndLabel(element, id);

// needed for reacting when entry is chosen
input.data = {
result.input.data = {
type: "edge",
id: id,
entry: input.value,
entry: result.input.value,
};

return input.value; // what is the "name" of this entry?
return result.input.value; // what is the "name" of this entry?
}

remove() {
Expand Down
11 changes: 11 additions & 0 deletions script/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ function updateElement(element, data) {
}
}

function initInputAndLabel(container, idSuffix) {
const input = container.querySelector("input");
const label = container.querySelector("label");

input.name = `${input.name}-${idSuffix}`;
input.id = `${input.id}-${idSuffix}`;
label.setAttribute("for", input.id);

return { input: input, label: label };
}

function* calculateDiffs(oldObjectOfObjects, newObjectOfObjects) {
for (let id in oldObjectOfObjects) {
// this id is not present in the new object
Expand Down

0 comments on commit f1b9a2c

Please sign in to comment.