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.
2019-11-27 21:24:49 +01:00

77 lines
3.0 KiB
Plaintext

<div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
<h2><%= t ".heading_new_ingredient" %></h2>
<%= labelled_form_for @ingredient,
url: project_ingredients_path(@project),
html: {id: 'ingredient-form'} do |f| %>
<%= error_messages_for @ingredient %>
<div class="box tabular">
<p><%= f.text_field :name, size: 40, required: true %></p>
<p>
<%= f.number_field :ref_amount, size: 8, required: true, min: 0,
label: :field_reference %>
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
</p>
<p><%= f.select :group, group_options, required: true %></p>
<div class="splitcontent">
<div class="splitcontentleft">
<p>
<%= f.select :source_id, source_options, required: false, include_blank: true %>
</p>
</div>
<div class="splitcontentright">
<p><%= f.text_field :source_ident, size: 25, required: false %></p>
</div>
</div>
<% @ingredient.nutrients.each_with_index do |n, index| %>
<%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>
<p class="nutrient">
<%= ff.select :quantity_id, quantity_options,
{include_blank: true, label: (index > 0 ? '' : :field_nutrients)} %>
<%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %>
<%= ff.select :unit_id, unit_options, {label: ''} %>
<%= ff.check_box :_destroy, {style: "display:none", label: ''} %>
<%= link_to t(".button_delete_nutrient"), '#',
class: 'icon icon-del',
style: (@ingredient.nutrients.length > 1 ? "" : "display:none"),
onclick: "deleteNutrient(); return false;" %>
</p>
<% end %>
<% end %>
<p>
<%= link_to t(".button_add_nutrient"), '#', class: 'icon icon-add',
onclick: 'addNutrient(); return false;' %>
</p>
</div>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#",
onclick: '$("#add-ingredient").hide(); return false;' %>
<% end %>
<hr>
</div>
<%= javascript_tag do %>
function addNutrient() {
var row = $('p.nutrient:visible:last');
var new_row = row.clone().insertAfter(row);
new_row.find('input[id$=_amount], select[id$=_quantity_id]').val('');
new_row.find('select[id$=_unit_id]').val(row.find('select[id$=_unit_id]').val());
new_row.find('label:first').text('');
if ($('p.nutrient:visible').length > 1) {
$('p.nutrient a.icon-del').show();
}
}
function deleteNutrient() {
var row = $(event.target).closest('p.nutrient');
row.find('[id$=_destroy]').val(1);
// FIXME: should only hide() row if record already saved (to send _destroy to backend)
row.remove();
$('p.nutrient:visible:first label:first').text('<%= t "field_nutrients" %>');
if ($('p.nutrient:visible').length <= 1) {
$('p.nutrient a.icon-del').hide();
}
}
<% end %>