From a0540d21d2fb8bc9f6bd10c64e3517605f7a6747 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Wed, 18 Nov 2026 17:10:52 -0700 Subject: [PATCH] Keep a place page's map on its place after the list is applied fitToVisible compared the whole list of a place page (every trail there, drawn or not) with the drawn trails the page opened on, found them different, and fitted the map to the extent of the visible tracks: the American Discovery Trail's. Only trails with geometry now count on a place page (data-place). Co-Authored-By: Claude Fable 5.2 --- web/assets/app.js | 9 +++++++++ web/olt/home.py | 3 ++- web/tests/test_place_view.py | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/web/assets/app.js b/web/assets/app.js index 22bbde8..72b22b3 100644 --- a/web/assets/app.js +++ b/web/assets/app.js @@ -916,6 +916,8 @@ var frame = (fig.dataset.frame || '').split(',').map(Number); if (frame.length !== 4 || frame.some(isNaN)) frame = null; entry.frame = frame; + /* A place page (data-place) is about its place: what its map settles on is the place's box. */ + entry.place = fig.hasAttribute('data-place'); entry.initialIds = onlyIds.length ? onlyIds : null; /* The page's own drawing of the tracks (class map__sketch) stays until the live map has @@ -1368,6 +1370,13 @@ /* The trails the page opened on are the ground its drawing shows: that view needs no move. */ var first = entry.initialIds || Object.keys(entry.boundsById); var same = ids.length === first.length && ids.every(function (id) { return first.indexOf(id) !== -1; }); + /* The list of a place page holds trails the map cannot draw (no track, no known start or end), and + the trails the map drew are the page's own data-ids. The same trails drawn is the same view. */ + if (!same && entry.place && entry.initialIds) { + var drawn = function (list) { return list.filter(function (id) { return entry.boundsById[id]; }); }; + var now = drawn(ids), then = drawn(first); + same = now.length === then.length && now.every(function (id) { return then.indexOf(id) !== -1; }); + } if (entry.frame && same) { target = entry.frame; padding = 0; } map.fitBounds([[target[0], target[1]], [target[2], target[3]]], { padding: padding, maxZoom: 11, animate: !reduceMotion }); diff --git a/web/olt/home.py b/web/olt/home.py index 2c05441..39360ed 100644 --- a/web/olt/home.py +++ b/web/olt/home.py @@ -694,7 +694,8 @@ def overview_map(recs, label, *, wide=False, scope="", saved=False, place=None): # The frame takes the shape of the ground it shows, within calm bounds, so the tracks fill it # (style.css .browse__map); the drawing and the live map fit the same ground either way. frame_attr += f' style="--frame-n: {frame_ratio(fr):.3f}"' - return Markup(f"""
+ place_attr = " data-place" if place and not saved else "" + return Markup(f"""
diff --git a/web/tests/test_place_view.py b/web/tests/test_place_view.py index 0cb5c92..14d8ee0 100644 --- a/web/tests/test_place_view.py +++ b/web/tests/test_place_view.py @@ -103,6 +103,26 @@ class PlaceViewTest(unittest.TestCase): whole = figure(home.overview_map(self.records, "Map", scope="all")) self.assertEqual(attr(whole, "bbox"), rounded(home.overview_box(self.records))) + def test_a_place_page_declares_its_place_and_no_other_page_does(self): + self.assertIn(" data-place", self.region_figure("us-ut")) + self.assertIn(" data-place", figure(regions.group_page("mountain-west", self.records, self.site)[2])) + self.assertNotIn("data-place", figure(home.overview_map(self.records[:12], "Map"))) + self.assertNotIn("data-place", figure(home.overview_map(self.records, "Map", scope="all"))) + with mock.patch.object(bounds, "_boxes", {}): + self.assertNotIn("data-place", self.region_figure("us-ut")) + + def test_the_script_keeps_a_place_page_on_its_place_when_the_list_holds_trails_it_cannot_draw(self): + # The list of a place page holds every trail there, drawn or not; the filters hand fitToVisible + # that list. Comparing it with the drawn trails (data-ids) alone sent Utah and Mountain West to + # the extent of the American Discovery Trail. Only trails with geometry count. + with open(os.path.join(WEB, "assets", "app.js"), encoding="utf-8") as f: + js = f.read() + fit = re.search(r"function fitToVisible\(.*?\n \}\n", js, re.S).group(0) + self.assertIn("entry.place", fit) + self.assertIn("entry.boundsById[id]", fit) + self.assertIn("if (entry.frame && same) { target = entry.frame; padding = 0; }", fit) + self.assertIn("entry.place = fig.hasAttribute('data-place');", js) + if __name__ == "__main__": unittest.main()