summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/nodes_controller.rb23
-rw-r--r--app/helpers/nodes_helper.rb5
-rw-r--r--app/views/nodes/sitemap.html.erb32
3 files changed, 59 insertions, 1 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))
diff --git a/app/helpers/nodes_helper.rb b/app/helpers/nodes_helper.rb
index a89f879..1268b63 100644
--- a/app/helpers/nodes_helper.rb
+++ b/app/helpers/nodes_helper.rb
@@ -12,7 +12,6 @@ module NodesHelper
12 end 12 end
13 end 13 end
14 14
15
16 def truncated_title_for_node node 15 def truncated_title_for_node node
17 if (title = title_for_node node) && title.size > 20 16 if (title = title_for_node node) && title.size > 20
18 "<span title='#{title}'>#{truncate(title, 40)}</span>" 17 "<span title='#{title}'>#{truncate(title, 40)}</span>"
@@ -63,4 +62,8 @@ module NodesHelper
63 path = node.unique_path 62 path = node.unique_path
64 CccConventions::NODE_KINDS.select { |_, config| config[:parent_match]&.call(path) } 63 CccConventions::NODE_KINDS.select { |_, config| config[:parent_match]&.call(path) }
65 end 64 end
65
66 def sitemap_node_open?(node)
67 !CccConventions::SITEMAP_COLLAPSED_PATHS.include?(node.unique_name)
68 end
66end 69end
diff --git a/app/views/nodes/sitemap.html.erb b/app/views/nodes/sitemap.html.erb
new file mode 100644
index 0000000..a799c17
--- /dev/null
+++ b/app/views/nodes/sitemap.html.erb
@@ -0,0 +1,32 @@
1<h1>Sitemap</h1>
2
3<div id="sitemap">
4<%
5 open_details = [] # levels with a currently-open <details>
6%>
7<% @sitemap.each_with_index do |(node, level), index| %>
8 <% while open_details.any? && open_details.last >= level %>
9 </details>
10 <% open_details.pop %>
11 <% end %>
12
13 <div class="sitemap_node">
14 <h4><%= link_to title_for_node(node), node_path(node) %></h4>
15 <span class="field_hint"><%= link_to_path("#{node.unique_name} ↗", node.unique_name) %></span>
16 <p class="sitemap_node_actions">
17 <%= link_to 'Show', node_path(node) %>
18 <%= link_to 'Create Child', new_node_path(:parent_id => node.id) %>
19 </p>
20 </div>
21
22 <% next_level = @sitemap[index + 1]&.last %>
23 <% if next_level && next_level > level %>
24 <details<%= ' open' if sitemap_node_open?(node) %>>
25 <summary>
26 <%= pluralize(@sitemap_descendant_counts[node.id], 'descendant', 'descendants') %>
27 </summary>
28 <% open_details.push(level) %>
29 <% end %>
30<% end %>
31<% open_details.length.times { %></details><% } %>
32</div>