1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/app/views/meals/_form.html.erb
2020-04-24 16:53:24 +02:00

104 lines
3.5 KiB
Plaintext

<%= error_messages_for @meal %>
<div class="box">
<div class="tabular">
<div class="splitcontent">
<div class="splitcontentleft">
<p>
<%= f.date_field :eaten_at_date, required: false %>
<%= f.time_field :eaten_at_time, value: format_time(@meal.eaten_at),
required: false, label: '' %>
</p>
</div>
<div class="splitcontentright">
<p><%= f.text_field :notes, required: false, style: "width: 95%;" %></p>
</div>
</div>
<% @meal.ingredients.each_with_index do |i, index| %>
<table style="width:95%;">
<%= f.fields_for 'ingredients_attributes', i, index: '' do |ff| %>
<tr class="ingredient">
<td style="width:90%;">
<p>
<%= ff.hidden_field :id %>
<%= ff.hidden_field :food_id, {class: "autocomplete-value"} %>
<%= label_tag :food_name do %>
<%= index > 0 ? '' : t(:field_ingredients) %><span class="required"> *</span>
<% end %>
<%= text_field_tag :food_name, (i.food.name if i.food),
{class: "autocomplete autocomplete-label", style: "width: 80%;"} %>
<%= ff.number_field :amount, {style: "width: 8%", step: :any, label: ''} %>
<%= i.food.ref_unit.shortname if i.food %>
<%= ff.hidden_field :_destroy %>
</p>
</td>
<td style="width:10%;">
<%= link_to t(".button_delete_ingredient"), '#',
class: 'icon icon-del',
style: (@meal.ingredients.length > 1 ? "" : "display:none"),
onclick: "deleteIngredient(); return false;" %>
</td>
</tr>
<% end %>
</table>
<% end %>
<p>
<%= link_to t(".button_new_ingredient"), '#', class: 'icon icon-add',
onclick: 'newIngredient(); return false;' %>
</p>
</div>
</div>
<%= javascript_tag do %>
function autocompleteFood($row) {
$row.find('.autocomplete-label').autocomplete({
source: '<%= j autocomplete_project_foods_path(@project) %>',
minLength: 2,
position: {collision: 'flipfit'},
search: function(event){
$(event.target).addClass('ajax-loading');
},
response: function(event){
$(event.target).removeClass('ajax-loading');
},
select: function(event, ui) {
$(event.target).val(ui.item.label);
$(event.target).siblings('.autocomplete-value').val(ui.item.value)
return false;
},
focus: function(event, ui) {
$(event.target).val(ui.item.label);
return false;
}
});
}
autocompleteFood($('tr.ingredient:visible'));
function newIngredient() {
var form = $(event.target).closest('form');
var row = form.find('tr.ingredient:visible:last');
var new_row = row.clone().insertAfter(row);
new_row.find('input:not([id$=__destroy])').val('');
new_row.find('label:first').hide();
form.find('tr.ingredient:visible a.icon-del').show();
autocompleteFood(new_row);
new_row.find('input:visible:first').focus();
}
function deleteIngredient() {
var form = $(event.target).closest('form');
var row = $(event.target).closest('tr.ingredient');
if (row.find('input[id$=__id]').val()) {
row.hide();
row.find('input[id$=__destroy]').val('1');
} else {
row.remove();
}
form.find('tr.ingredient:visible:first label:first').show();
if (form.find('tr.ingredient:visible').length <= 1) {
form.find('tr.ingredient:visible a.icon-del').hide();
}
}
<% end %>