From: Marwin Hochfelsner Date: Tue, 4 Feb 2025 13:39:00 +0000 (+0100) Subject: Reevaluate iteration methods X-Git-Tag: live~156^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/550e5d336f87d5b3c2eb3c4d9458b5162c1b7ed0 Reevaluate iteration methods --- diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index ad34479d3..8ef067740 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -97,9 +97,7 @@ $(document).ready(function () { var position = $("html").attr("dir") === "rtl" ? "topleft" : "topright"; function addControlGroup(controls) { - controls.forEach(function (control) { - control.addTo(map); - }); + for (const control of controls) control.addTo(map); var firstContainer = controls[0].getContainer(); $(firstContainer).find(".control-button").first() diff --git a/app/assets/javascripts/index/history.js b/app/assets/javascripts/index/history.js index ae8f027ed..3c7101c74 100644 --- a/app/assets/javascripts/index/history.js +++ b/app/assets/javascripts/index/history.js @@ -96,7 +96,7 @@ OSM.History = function (map) { function updateBounds() { group.clearLayers(); - changesets.forEach(function (changeset) { + for (const changeset of changesets) { var bottomLeft = map.project(L.latLng(changeset.bbox.minlat, changeset.bbox.minlon)), topRight = map.project(L.latLng(changeset.bbox.maxlat, changeset.bbox.maxlon)), width = topRight.x - bottomLeft.x, @@ -115,16 +115,15 @@ OSM.History = function (map) { changeset.bounds = L.latLngBounds(map.unproject(bottomLeft), map.unproject(topRight)); - }); + } changesets.sort(function (a, b) { return b.bounds.getSize() - a.bounds.getSize(); }); - for (var i = 0; i < changesets.length; ++i) { - var changeset = changesets[i], - rect = L.rectangle(changeset.bounds, - { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 }); + for (const changeset of changesets) { + const rect = L.rectangle(changeset.bounds, + { weight: 2, color: "#FF9500", opacity: 1, fillColor: "#FFFFAF", fillOpacity: 0 }); rect.id = changeset.id; rect.addTo(group); } diff --git a/app/assets/javascripts/index/layers/notes.js b/app/assets/javascripts/index/layers/notes.js index 24bf969b3..1bc3714f0 100644 --- a/app/assets/javascripts/index/layers/notes.js +++ b/app/assets/javascripts/index/layers/notes.js @@ -84,9 +84,7 @@ OSM.initializeNotesLayer = function (map) { function success(json) { var oldNotes = notes; notes = {}; - json.features.forEach(updateMarkers); - - function updateMarkers(feature) { + for (const feature of json.features) { var marker = oldNotes[feature.properties.id]; delete oldNotes[feature.properties.id]; notes[feature.properties.id] = updateMarker(marker, feature); diff --git a/app/assets/javascripts/index/query.js b/app/assets/javascripts/index/query.js index 3874dbdbb..5c8ee8876 100644 --- a/app/assets/javascripts/index/query.js +++ b/app/assets/javascripts/index/query.js @@ -109,9 +109,9 @@ OSM.Query = function (map) { var tags = feature.tags, locales = OSM.preferred_languages; - for (var i = 0; i < locales.length; i++) { - if (tags["name:" + locales[i]]) { - return tags["name:" + locales[i]]; + for (const locale of locales) { + if (tags["name:" + locale]) { + return tags["name:" + locale]; } } @@ -193,22 +193,20 @@ OSM.Query = function (map) { elements = elements.sort(compare); } - for (var i = 0; i < elements.length; i++) { - var element = elements[i]; - - if (interestingFeature(element)) { - var $li = $("
  • ") - .addClass("list-group-item list-group-item-action") - .text(featurePrefix(element) + " ") - .appendTo($ul); - - $("") - .addClass("stretched-link") - .attr("href", "/" + element.type + "/" + element.id) - .data("geometry", featureGeometry(element)) - .text(featureName(element)) - .appendTo($li); - } + for (const element of elements) { + if (!interestingFeature(element)) continue; + + var $li = $("
  • ") + .addClass("list-group-item list-group-item-action") + .text(featurePrefix(element) + " ") + .appendTo($ul); + + $("") + .addClass("stretched-link") + .attr("href", "/" + element.type + "/" + element.id) + .data("geometry", featureGeometry(element)) + .text(featureName(element)) + .appendTo($li); } if (results.remark) { diff --git a/app/assets/javascripts/leaflet.layers.js b/app/assets/javascripts/leaflet.layers.js index 3f577b532..a7a2335f3 100644 --- a/app/assets/javascripts/leaflet.layers.js +++ b/app/assets/javascripts/leaflet.layers.js @@ -60,11 +60,11 @@ L.OSM.layers = function (options) { }); input.on("click", function () { - layers.forEach(function (other) { + for (const other of layers) { if (other !== layer) { map.removeLayer(other); } - }); + } map.addLayer(layer); }); diff --git a/app/assets/javascripts/leaflet.share.js b/app/assets/javascripts/leaflet.share.js index 1eed2151a..7d8462f46 100644 --- a/app/assets/javascripts/leaflet.share.js +++ b/app/assets/javascripts/leaflet.share.js @@ -201,41 +201,31 @@ L.OSM.share = function (options) { .attr("class", "form-check-input") .bind("change", toggleFilter)))); - ["minlon", "minlat", "maxlon", "maxlat", "lat", "lon"].forEach(function (name) { + const mapnikNames = ["minlon", "minlat", "maxlon", "maxlat", "lat", "lon"]; + + for (const name of mapnikNames) { $("") .attr("id", "mapnik_" + name) .attr("name", name) .attr("type", "hidden") .appendTo($form); - }); - - $("") - .attr("id", "map_format") - .attr("name", "format") - .attr("value", "mapnik") - .attr("type", "hidden") - .appendTo($form); - - $("") - .attr("id", "map_zoom") - .attr("name", "zoom") - .attr("value", map.getZoom()) - .attr("type", "hidden") - .appendTo($form); + } - $("") - .attr("id", "map_width") - .attr("name", "width") - .attr("value", 0) - .attr("type", "hidden") - .appendTo($form); + const hiddenExportDefaults = { + format: "mapnik", + zoom: map.getZoom(), + width: 0, + height: 0 + }; - $("") - .attr("id", "map_height") - .attr("name", "height") - .attr("value", 0) - .attr("type", "hidden") - .appendTo($form); + for (const name in hiddenExportDefaults) { + $("") + .attr("id", "map_" + name) + .attr("name", name) + .attr("value", hiddenExportDefaults[name]) + .attr("type", "hidden") + .appendTo($form); + } var csrf_param = $("meta[name=csrf-param]").attr("content"), csrf_token = $("meta[name=csrf-token]").attr("content"); diff --git a/app/assets/javascripts/router.js b/app/assets/javascripts/router.js index a8659d308..30a71e0f1 100644 --- a/app/assets/javascripts/router.js +++ b/app/assets/javascripts/router.js @@ -84,14 +84,12 @@ OSM.Router = function (map, rts) { return route; } - var routes = []; - for (var r in rts) { - routes.push(new Route(r, rts[r])); - } + const routes = Object.entries(rts) + .map(([r, t]) => new Route(r, t)); routes.recognize = function (path) { - for (var i = 0; i < this.length; i++) { - if (this[i].match(path)) return this[i]; + for (const route of this) { + if (route.match(path)) return route; } };