Display readout list in tree format
This commit is contained in:
parent
bb43370329
commit
96d1c40cb8
@ -27,5 +27,13 @@ class ReadoutsController < ApplicationController
|
||||
|
||||
@measurements, @filter_q = @routine.measurements.includes(:routine, :source)
|
||||
.filter(session[:m_filters], @quantities)
|
||||
|
||||
# Keep only non-nil readouts and their ancestors
|
||||
@measurements.each do |measurement, readouts|
|
||||
ancestors = {}
|
||||
readouts.keep_if do |q, readout|
|
||||
(readout || ancestors[q]) && (ancestors[q.parent] = true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,15 +1,17 @@
|
||||
module BodyTrackersHelper
|
||||
def format_value(value, precision=2, mfu_unit=nil)
|
||||
amount, unit = value
|
||||
def format_amount(amount, precision=2, mfu_unit=nil)
|
||||
value = amount.respond_to?(:value) ? amount.value : amount&.first
|
||||
unit = amount.respond_to?(:unit) ? amount.unit : amount&.last
|
||||
|
||||
case
|
||||
when amount.nil?
|
||||
'-'
|
||||
when amount.nan?
|
||||
when value.nil?
|
||||
''
|
||||
when value.nan?
|
||||
'?'
|
||||
else
|
||||
a = amount.round(precision)
|
||||
a_desc = a.nonzero? ? "%.#{precision}f" % a : '-'
|
||||
u_desc = unit && " [#{unit.shortname}]" || ' [-]' if unit != mfu_unit && a.nonzero?
|
||||
value = value.round(precision)
|
||||
a_desc = value.nonzero? ? "%.#{precision}f" % value : '-'
|
||||
u_desc = unit ? " [#{unit.shortname}]" : ' [-]' if unit != mfu_unit && value.nonzero?
|
||||
"#{a_desc}#{u_desc}"
|
||||
end
|
||||
end
|
||||
|
@ -1,4 +1,10 @@
|
||||
module MeasurementsHelper
|
||||
def readout_markup(quantity, readout)
|
||||
content = "#{' '*quantity.depth}#{quantity.name} #{format_amount(readout)}"
|
||||
classes = 'bolded' if @routine.quantities.include?(quantity)
|
||||
content_tag(:span, content, {class: classes}, false)
|
||||
end
|
||||
|
||||
def action_links(m)
|
||||
link_to(l(:button_retake), retake_measurement_path(m, @view_params),
|
||||
{remote: true, class: "icon icon-reload"}) +
|
||||
|
@ -42,8 +42,8 @@
|
||||
</td>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="primary right ellipsible">
|
||||
<%= format_value(nutrients[q], @food_summary[:precision][q],
|
||||
@food_summary[:mfu_unit][q]) %>
|
||||
<%= format_amount(nutrients[q], @food_summary[:precision][q],
|
||||
@food_summary[:mfu_unit][q]) %>
|
||||
</td>
|
||||
<% end %>
|
||||
<td class="right shrunk unwrappable"><%= action_links(food, :nutrients) %></td>
|
||||
@ -62,7 +62,7 @@
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="primary topleft ellipsible">
|
||||
<%= q.name %>
|
||||
<p class="right"><%= format_value(nutrients[q]) %></p>
|
||||
<p class="right"><%= format_amount(nutrients[q]) %></p>
|
||||
</td>
|
||||
<% end %>
|
||||
<td rowspan="<%= rows %>" class="right shrunk unwrappable">
|
||||
@ -76,7 +76,7 @@
|
||||
<% eqs.each do |q| %>
|
||||
<td class="extra topleft ellipsible">
|
||||
<%= q.name if nutrients[q] %>
|
||||
<p class="right"><%= format_value(nutrients[q]) %></p>
|
||||
<p class="right"><%= format_amount(nutrients[q]) %></p>
|
||||
</td>
|
||||
<% end %>
|
||||
<% if @quantities.length > eqs.length %>
|
||||
|
@ -37,7 +37,7 @@
|
||||
<td class="right ellipsible" style="border-left: none;"></td>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="right ellipsible">
|
||||
<%= format_value(@ingredient_summary[m][q], @ingredient_summary[:precision][q]) %>
|
||||
<%= format_amount(@ingredient_summary[m][q], @ingredient_summary[:precision][q]) %>
|
||||
</td>
|
||||
<% end %>
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
<td class="right ellipsible" style="border-left: none;"></td>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="right ellipsible">
|
||||
<%= format_value(@ingredient_summary[date][q], @ingredient_summary[:precision][q]) %>
|
||||
<%= format_amount(@ingredient_summary[date][q], @ingredient_summary[:precision][q]) %>
|
||||
</td>
|
||||
<% end %>
|
||||
<td></td>
|
||||
|
@ -6,8 +6,8 @@
|
||||
</td>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="right ellipsible">
|
||||
<%= format_value(@ingredients[i][q], @ingredient_summary[:precision][q],
|
||||
@ingredient_summary[:mfu_unit][q]) %>
|
||||
<%= format_amount(@ingredients[i][q], @ingredient_summary[:precision][q],
|
||||
@ingredient_summary[:mfu_unit][q]) %>
|
||||
</td>
|
||||
<% end %>
|
||||
<%# Moved buttons to helper to avoid spaces between buttons %>
|
||||
|
@ -16,58 +16,33 @@
|
||||
<tbody>
|
||||
<% extra_quantities = @measurements.values.first.keys - @quantities %>
|
||||
<% @measurements.each do |measurement, readouts| %>
|
||||
<% row_class = "measurement #{cycle('odd', 'even')}" %>
|
||||
<tr id="measurement-<%= measurement.id %>" class="primary <%= row_class %>">
|
||||
<td class="topleft unwrappable" style="cursor: pointer;"
|
||||
onclick="$(this).closest('tr').toggle(); $(this).closest('tr').nextUntil('tr.primary', '.measurement').toggle(); return false;">
|
||||
<span class="icon icon-bullet-closed">
|
||||
<%= format_datetime(measurement.taken_at) %>
|
||||
</span>
|
||||
<tr id="measurement-<%= measurement.id %>"
|
||||
class="measurement <%= cycle('odd', 'even') %>">
|
||||
<td class="topleft unwrappable">
|
||||
<%= link_to format_datetime(measurement.taken_at), '',
|
||||
{class: 'icon icon-arrow-right',
|
||||
onclick: "$(this).closest('tr').nextUntil('tr.measurement', '.details')
|
||||
.show(); return false;"} %>
|
||||
</td>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="primary right ellipsible"><%= format_value(readouts[q]) %></td>
|
||||
<td class="right ellipsible"><%= format_amount(readouts[q]) %></td>
|
||||
<% end %>
|
||||
<td class="right shrunk unwrappable"><%= action_links(measurement) %></td>
|
||||
</tr>
|
||||
|
||||
<tr class="<%= row_class %>" style="display:none">
|
||||
<% if @quantities.length > 0
|
||||
rows = (readouts.length - 1) / @quantities.length + 1
|
||||
else
|
||||
rows = 1
|
||||
end %>
|
||||
<td rowspan="<%= rows %>" class="topleft unwrappable" style="cursor: pointer;"
|
||||
onclick="$(this).closest('tr').prev('tr.primary').toggle(); $(this).closest('tr').prev('tr.primary').nextUntil('tr.primary', '.measurement').toggle(); return false;">
|
||||
<span class="icon icon-bullet-open">
|
||||
<%= format_datetime(measurement.taken_at) %>
|
||||
</span>
|
||||
<tr class="details <%= current_cycle %>" style="display:none">
|
||||
<td class="topleft">
|
||||
<%= link_to l(:button_close), "#", {class: 'icon icon-close',
|
||||
onclick: '$(this).closest("tr")
|
||||
.nextUntil("tr.measurement", ":not(.details)")
|
||||
.show().addBack().first().hide(); return false;'} %>
|
||||
</td>
|
||||
<% @quantities.each do |q| %>
|
||||
<td class="primary topleft ellipsible">
|
||||
<%= q.name %>
|
||||
<p class="right"><%= format_value(readouts[q]) %></p>
|
||||
</td>
|
||||
<% content = readouts.keys.inject('') do |output, q| %>
|
||||
<% raw "#{output}#{readout_markup(q, readouts[q])}\n" %>
|
||||
<% end %>
|
||||
<td rowspan="<%= rows %>" class="right shrunk unwrappable">
|
||||
<%= action_links(measurement) %>
|
||||
</td>
|
||||
<td class="topleft" colspan="<%= @quantities.length + 1 %>"
|
||||
style="white-space:pre-line;"><%= content %></td>
|
||||
</tr>
|
||||
|
||||
<% next if @quantities.empty? %>
|
||||
<% extra_quantities.each_slice(@quantities.length) do |eqs| %>
|
||||
<tr class="extra <%= row_class %>" style="display:none">
|
||||
<% eqs.each do |q| %>
|
||||
<td class="extra topleft ellipsible">
|
||||
<%= q.name if readouts[q] %>
|
||||
<p class="right"><%= format_value(readouts[q]) %></p>
|
||||
</td>
|
||||
<% end %>
|
||||
<% if @quantities.length > eqs.length %>
|
||||
<td class="space" colspan="<%= @quantities.length - eqs.length %>"></td>
|
||||
<% end %>
|
||||
</tr>
|
||||
<% end %>
|
||||
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
|
@ -109,7 +109,7 @@ module BodyTracking
|
||||
filter_values = completed_q.delete(filter_q)
|
||||
items.to_a.keep_if { |i| filter_values[i][0] } if filter_values
|
||||
subitems.merge!(completed_q)
|
||||
subitem_keys = subitems.keys.select { |k| k.class == Quantity }.sort_by { |q| q.lft }
|
||||
subitem_keys = subitems.keys.select { |k| k.instance_of?(Quantity) }.sort_by(&:lft)
|
||||
items.map { |i| [i, subitem_keys.map { |q| [q, subitems[q][i]] }.to_h] }.to_h
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user