1
0

Added Ingredient editing

Improved Ingredient to match Measurement functionality
This commit is contained in:
cryptogopher
2019-12-30 23:45:59 +01:00
parent 20deb944c3
commit a4ec6a0c12
21 changed files with 191 additions and 116 deletions

View File

@@ -1,6 +1,7 @@
<% if User.current.allowed_to?(:manage_common, @project) %>
<%= link_to t(".link_import_ingredients"), '#', class: 'icon icon-multiple',
onclick: '$("#import-ingredients").show(); $("#filename").focus(); return false;' %>
<%= link_to t(".link_new_ingredient"), '#', class: 'icon icon-add',
onclick: '$("#add-ingredient").show(); $("#ingredient_name").focus(); return false;' %>
<%= link_to t(".link_new_ingredient"),
new_project_ingredient_path(@project, view_mode: current_view),
{remote: true, class: 'icon icon-add'} %>
<% end %>

View File

@@ -0,0 +1,15 @@
<%= labelled_form_for @ingredient,
url: ingredient_path(@ingredient, view_mode: current_view),
method: :patch, remote: true,
html: {id: 'ingredient-edit-form', name: 'ingredient-edit-form'} do |f| %>
<%= render partial: 'ingredients/form', locals: {f: f} %>
<div class="tabular">
<p>
<%= submit_tag l(:button_save) %>
<%= link_to l(:button_cancel), "#",
onclick: '$(this).closest("tr").remove(); return false;' %>
</p>
</div>
<% end %>

View File

@@ -1,79 +1,70 @@
<div id="add-ingredient" <%= 'style=display:none;' if @ingredient.errors.empty? %>>
<h2><%= t ".heading_new_ingredient" %></h2>
<%= error_messages_for @ingredient %>
<%= labelled_form_for @ingredient,
url: project_ingredients_path(@project),
html: {id: 'ingredient-add-form', name: 'ingredient-add-form'} do |f| %>
<%= error_messages_for @ingredient %>
<div class="box tabular">
<p><%= f.text_field :name, size: 40, required: true %></p>
<p><%= f.text_area :comment, cols: 40, rows: 3, required: false,
style: "width: 100%;" %></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 class="box tabular">
<p><%= f.text_field :name, size: 40, required: true %></p>
<p><%= f.text_area :comment, cols: 40, rows: 3, required: false,
style: "width: 100%;" %></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: t('.null_source')} %></p>
</div>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#",
onclick: '$("#add-ingredient").hide(); return false;' %>
<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.hidden_field :id %>
<%= ff.select :quantity_id, quantity_options,
{include_blank: true, required: 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.hidden_field :_destroy %>
<%= link_to t(".button_delete_nutrient"), '#',
class: 'icon icon-del',
style: (@ingredient.nutrients.length > 1 ? "" : "display:none"),
onclick: "deleteNutrient(); return false;" %>
</p>
<% end %>
<% end %>
<hr>
<p>
<%= link_to t(".button_new_nutrient"), '#', class: 'icon icon-add',
onclick: 'newNutrient(); return false;' %>
</p>
</div>
<%= javascript_tag do %>
function addNutrient() {
var row = $('p.nutrient:visible:last');
function newNutrient() {
var form = $(event.target).closest('form');
var row = form.find('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();
}
new_row.find('input[id$=__id], 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('input[id$=__destroy]').val('');
new_row.find('label:first').hide();
form.find('p.nutrient:visible a.icon-del').show();
}
function deleteNutrient() {
var form = $(event.target).closest('form');
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();
if (row.find('input[id$=__id]').val()) {
row.hide();
row.find('input[id$=__destroy]').val('1');
} else {
row.remove();
}
form.find('p.nutrient:visible:first label:first').show();
if (form.find('p.nutrient:visible').length <= 1) {
form.find('p.nutrient:visible a.icon-del').hide();
}
}
<% end %>

View File

@@ -16,7 +16,8 @@
<tbody>
<% @ingredients.each do |i| %>
<% next if i.new_record? %>
<tr id="ingredient-<%= i.id %>" class="ingredient <%= 'hidden' if i.hidden %>">
<tr id="ingredient-<%= i.id %>"
class="ingredient primary<%= ' hidden' if i.hidden %>">
<td class="name ellipsible">
<%= link_to '', toggle_ingredient_path(i), {
remote: true,

View File

@@ -0,0 +1,18 @@
<h2><%= t ".heading_new_ingredient" %></h2>
<%= labelled_form_for @ingredient,
url: project_ingredients_path(@project, view_mode: current_view),
remote: true,
html: {id: 'new-ingredient-form', name: 'new-ingredient-form'} do |f| %>
<%= render partial: 'ingredients/form', locals: {f: f} %>
<div class="tabular">
<p>
<%= submit_tag l(:button_create) %>
<%= link_to l(:button_cancel), "#",
onclick: '$("#new-ingredient").empty(); return false;' %>
</p>
</div>
<% end %>
<hr>

View File

@@ -1,5 +1,5 @@
<%= render partial: 'ingredients/filters',
locals: {url: filter_nutrients_project_ingredients_path(@project)} %>
locals: {url: filter_project_ingredients_path(@project, view_mode: current_view)} %>
<% if @ingredients.any? %>
<%= render partial: 'ingredients/options' %>
@@ -13,7 +13,8 @@
<th style="width:<%= 100/total_width %>%" class="closable ellipsible">
<div style="float:right;position:relative;">
<%= link_to '',
toggle_column_project_ingredients_path(@project, quantity_id: q.id),
toggle_column_project_ingredients_path(@project, quantity_id: q.id,
view_mode: current_view),
{class: "icon icon-close", method: :post, remote: true} %>
</div>
<%= q.name %>

View File

@@ -1,7 +1,7 @@
<fieldset id="options" class="collapsible">
<legend onclick="toggleFieldset(this);"><%= l(:label_options) %></legend>
<div>
<%= form_tag toggle_column_project_ingredients_path(@project),
<%= form_tag toggle_column_project_ingredients_path(@project, view_mode: current_view),
id: 'toggle-column-form', name: 'toggle-column-form',
method: :post, remote: true do %>

View File

@@ -0,0 +1,7 @@
<%= render partial: 'body_trackers/flash' %>
$('#new-ingredient').empty();
<% if current_view == :nutrients %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% else %>
$('#ingredients').html('<%= j render partial: 'ingredients/index' %>');
<% end %>

View File

@@ -0,0 +1,4 @@
<%= render partial: 'body_trackers/flash' %>
<% if @ingredient.destroyed? %>
$('tr[id=ingredient-<%= @ingredient.id %>]').nextUntil('tr.primary').addBack().remove();
<% end %>

View File

@@ -0,0 +1,9 @@
<%= render partial: 'body_trackers/flash' %>
$('tr[id=ingredient-<%= @ingredient.id %>]').nextUntil('tr.primary', ':not(.ingredient)')
.remove();
var columns = $('table > thead > tr > th').length;
$('tr[id=ingredient-<%= @ingredient.id %>]').nextAll('tr.primary').first().before(
'<tr><td class="form" colspan="'+columns+'">' +
'<div id="edit-ingredient"><%= j render partial: "ingredients/edit_form" %></div>' +
'</td></tr>'
);

View File

@@ -6,7 +6,8 @@
<%= render partial: 'ingredients/import' %>
<%= render partial: 'ingredients/form' %>
<div id="new-ingredient">
</div>
<h2><%= t ".heading" %></h2>
<div id='ingredients'>

View File

@@ -0,0 +1,6 @@
<%= render partial: 'body_trackers/flash' %>
<% if current_view == :nutrients %>
$('#nutrients').html('<%= j render partial: 'ingredients/nutrients' %>');
<% else %>
$('#ingredients').html('<%= j render partial: 'ingredients/index' %>');
<% end %>

View File

@@ -0,0 +1,2 @@
<%= render partial: 'body_trackers/flash' %>
$('#new-ingredient').html('<%= j render partial: 'ingredients/new_form' %>');

View File

@@ -6,7 +6,8 @@
<%= render partial: 'ingredients/import' %>
<%= render partial: 'ingredients/form' %>
<div id="new-ingredient">
</div>
<h2><%= t ".heading" %></h2>
<div id='nutrients'>

View File

@@ -1,3 +1,2 @@
$('div[id^=flash_]').remove();
$('#content').prepend('<%= escape_javascript(render_flash_messages) %>');
$('#ingredients').html('<%= escape_javascript(render partial: 'ingredients/index') %>');
<%= render partial: 'body_trackers/flash' %>
$('#ingredients').html('<%= j render partial: 'ingredients/index' %>');

View File

@@ -1,3 +0,0 @@
$('div[id^=flash_]').remove();
$('#content').prepend('<%= escape_javascript(render_flash_messages) %>');
$('#nutrients').html('<%= escape_javascript(render partial: 'ingredients/nutrients') %>');