From 2216cea6d5307105a0ac3260c8de0532a17564ae Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 24 May 2020 18:57:10 +0200 Subject: [PATCH] Displaying heirarchical quantity headers --- app/helpers/body_trackers_helper.rb | 34 +++++++++++++++++++++++ app/views/meals/_index.html.erb | 40 ++++++++++++++++++---------- assets/stylesheets/body_tracking.css | 4 +-- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/app/helpers/body_trackers_helper.rb b/app/helpers/body_trackers_helper.rb index bcd85cd..36c2c0c 100644 --- a/app/helpers/body_trackers_helper.rb +++ b/app/helpers/body_trackers_helper.rb @@ -37,4 +37,38 @@ module BodyTrackersHelper [u.shortname, u.id] end end + + def table_header_spec(quantities) + # spec: table of rows (tr), where each row is a hash of cells (td) (hash keeps items + # ordered the way they were added). Hash values determine cell property: + # * int > 0 - quantity name-labelled cell with 'int' size colspan + # * int < 0 - quantity name-labelled cell with 'int' size rowspan + # * nil - non-labelled cell without col-/rowspan + spec = [] + default_row = Hash.new(0) + + # Determine colspans first... + quantities.each do |q| + ancestors = q.self_and_ancestors.each_with_index do |a, i| + spec[i] ||= default_row.dup + spec[i][a] += 1 + end + spec[ancestors.length...spec.length].each { |row| row[ancestors.last] = nil } + default_row[ancestors.last] = nil + end + + # ...then rowspans + single_columns = [] + spec.each_with_index do |row, i| + single_columns.each { |q| row.delete(q) } + row.each do |q, span| + if span == 1 + row[q] = -(spec.length - i) + single_columns << q + end + end + end + + spec + end end diff --git a/app/views/meals/_index.html.erb b/app/views/meals/_index.html.erb index 3d63f6c..deaaea3 100644 --- a/app/views/meals/_index.html.erb +++ b/app/views/meals/_index.html.erb @@ -8,21 +8,33 @@ <% total_width = 4 + @quantities.length %> + <% header = table_header_spec(@quantities) %> <% @meals_by_date.reverse_each do |date, meals| %> - - - - <% @quantities.each do |q| %> - - <% end %> - - + <% header.each_with_index do |row, i| %> + + <% if i == 0 %> + + + <% end %> + + <% row.each do |q, span| %> + + <% end %> + + <% if i == 0 %> + + <% end %> + + <% end %> diff --git a/assets/stylesheets/body_tracking.css b/assets/stylesheets/body_tracking.css index 3495a01..b02e4c4 100644 --- a/assets/stylesheets/body_tracking.css +++ b/assets/stylesheets/body_tracking.css @@ -8,11 +8,11 @@ table.list .quantity {text-align: left;} /* TODO: merge with .closable */ table.list .quantityhead { text-align: center; - vertical-align: bottom; position: relative; padding: 2px 0; - border: none; + border-bottom: none; } +table.list .quantityheadempty {border-top: none; border-bottom: none;} table.list .action, table.list .value {text-align: right; padding-right: 2px;}
- <%= l(:field_amount) %> - - <%= q.name %> -
<%= l(:field_amount) %> 0 %> + <%= "rowspan=#{-span}" if span && span < 0 %> + style="width: <%= (span && span > 0 ? span : 1) * 100/total_width %>%;" + title="<%= q.description %>"> + <%= q.name if span %> +
<%= "[#{@amount_mfu_unit.shortname}]" %>