diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-07-12 02:15:44 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-07-12 02:15:44 +0200 |
| commit | 45bf65d04d046c0ea4a1150096b2a9b846d6c0b8 (patch) | |
| tree | 24dad426779cb3b0ac59c405245aac1d28109982 /app/controllers | |
| parent | 13c8cb415813e90c883a44b0c0888382161de92a (diff) | |
Give the sitemap its own view, with collapse and descendant counts
Extracted from admin#index's inline table into NodesController#sitemap.
Nested <details>/<summary> per branch, one linear pass over the
existing flat [node, level] list (no added queries) -- each node's own
descendant count computed the same way, via a small stack rather than
re-walking the tree per node. Branches under updates/, club/erfas,
club/chaostreffs, and disclosure start collapsed by default
(CccConventions::SITEMAP_COLLAPSED_PATHS); any branch currently
collapsed, whether by that default or because someone just closed it,
is highlighted via a plain :not([open]) selector -- no state tracked
outside the DOM itself.
Dropped the update?-post exclusion this view used to rely on -- no
longer needed now that updates/ collapses instead of being filtered
out, so its real children (previously silently absent) now show up
correctly. admin#index's own, separate @sitemap query is unchanged;
that view has no collapse mechanism to compensate and wasn't part of
this.
Diffstat (limited to 'app/controllers')
| -rw-r--r-- | app/controllers/nodes_controller.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/app/controllers/nodes_controller.rb b/app/controllers/nodes_controller.rb index ede91ad..772bf4b 100644 --- a/app/controllers/nodes_controller.rb +++ b/app/controllers/nodes_controller.rb | |||
| @@ -216,6 +216,11 @@ class NodesController < ApplicationController | |||
| 216 | @nodes = nodes_matching_tags(tags) | 216 | @nodes = nodes_matching_tags(tags) |
| 217 | end | 217 | end |
| 218 | 218 | ||
| 219 | def sitemap | ||
| 220 | @sitemap = Node.root.self_and_descendants_ordered_with_level | ||
| 221 | @sitemap_descendant_counts = descendant_counts_for(@sitemap) | ||
| 222 | end | ||
| 223 | |||
| 219 | private | 224 | private |
| 220 | 225 | ||
| 221 | def slug_for(title) | 226 | def slug_for(title) |
| @@ -248,6 +253,24 @@ class NodesController < ApplicationController | |||
| 248 | end | 253 | end |
| 249 | end | 254 | end |
| 250 | 255 | ||
| 256 | def descendant_counts_for(ordered_with_level) | ||
| 257 | counts = Hash.new(0) | ||
| 258 | stack = [] # [node, level, index] | ||
| 259 | ordered_with_level.each_with_index do |(node, level), index| | ||
| 260 | while stack.any? && stack.last[1] >= level | ||
| 261 | ancestor_node, _ancestor_level, ancestor_index = stack.pop | ||
| 262 | counts[ancestor_node.id] = index - ancestor_index - 1 | ||
| 263 | end | ||
| 264 | stack << [node, level, index] | ||
| 265 | end | ||
| 266 | total = ordered_with_level.length | ||
| 267 | while stack.any? | ||
| 268 | ancestor_node, _ancestor_level, ancestor_index = stack.pop | ||
| 269 | counts[ancestor_node.id] = total - ancestor_index - 1 | ||
| 270 | end | ||
| 271 | counts | ||
| 272 | end | ||
| 273 | |||
| 251 | def nodes_matching_tags(tags) | 274 | def nodes_matching_tags(tags) |
| 252 | matching_pages = Page.tagged_with(tags, any: true).reselect(:id) | 275 | matching_pages = Page.tagged_with(tags, any: true).reselect(:id) |
| 253 | base = Node.where(head_id: matching_pages).or(Node.where(draft_id: matching_pages)) | 276 | base = Node.where(head_id: matching_pages).or(Node.where(draft_id: matching_pages)) |
