1
0

WIP: Targets configurable with Quantities

This commit is contained in:
cryptogopher
2021-02-07 11:02:41 +01:00
parent 316005bf1f
commit 8b17b33603
23 changed files with 218 additions and 108 deletions

View File

@@ -2,9 +2,10 @@
<div class="box">
<div id='goal-form' class="tabular">
<% if @goal.persisted? %>
<p><%= render partial: 'goals/show_form' %></p>
<p><%= f.date_field :effective_from, disabled: !@goal.is_binding? %></p>
<% if goal_f.object.persisted? %>
<p><%= render partial: 'goals/show_form', locals: {goal_f: goal_f} %></p>
<p><%= goal_f.date_field :effective_from, value: @effective_from,
disabled: !goal_f.object.is_binding? %></p>
<% else %>
<%= render partial: 'goals/form' %>
<% end %>
@@ -13,34 +14,30 @@
<hr style="width: 95%;">
<div class="tabular">
<% @targets.each do |target| %>
<p class="target">
<%= f.fields_for 'targets', target, index: '' do |target_f| %>
<%= target_f.hidden_field :id %>
<%= target_f.hidden_field :_destroy %>
<em class="info"><%= t ".choose_quantity" %></em>
<% target.thresholds.each_with_index do |thr, index| %>
<%= target_f.fields_for 'thresholds_attributes', thr, index: '' do |threshold_f| %>
<%= threshold_f.hidden_field :id %>
<% if index == 0 %>
<%= threshold_f.select :quantity_id, quantity_options,
{include_blank: true, required: true, label: :field_target},
onchange: "showQuantityPath(event);" %>
<%= target_f.select :condition, condition_options, required: true,
label: '' %>
<% end %>
<%= threshold_f.number_field :value, {size: 8, step: :any, label: ''} %>
<% if index == 0 %>
<%= threshold_f.select :unit_id, unit_options, {label: ''} %>
<% end %>
<% end %>
<% end %>
<%= link_to t(".button_delete_target"), '#', class: 'icon icon-del',
style: (@targets.many? ? "" : "display:none"),
onclick: "deleteTarget(); return false;" %>
<p class="target">
<%= goal_f.fields_for :targets, @targets, child_index: '' do |target_f| %>
<%= target_f.hidden_field :_destroy %>
<em class="info"><%= t ".choose_quantity" %></em>
<%= target_f.select :quantity_id, quantity_options,
{include_blank: true, required: true, label: :field_target},
onchange: "showQuantityPath(event);" %>
<%= target_f.fields_for :thresholds do |threshold_f| %>
<%= render partial: 'thresholds/form', locals: {threshold_f: threshold_f} %>
<% end %>
</p>
<% last_quantity = target_f.object.thresholds.last.quantity %>
<% unless last_quantity.leaf? %>
<%= target_f.fields_for 'thresholds_attributes', Threshold.new do |threshold_f| %>
<%= render partial: 'thresholds/form',
locals: {threshold_f: threshold_f, parent: last_quantity} %>
<% end %>
<% end %>
<%= link_to t(".button_delete_target"), '#', class: 'icon icon-del',
style: (@targets.many? ? "" : "display:none"),
onclick: "deleteTarget(); return false;" %>
<% end %>
</p>
<p>
<%= link_to t(".button_new_target"), '#', class: 'icon icon-add',
onclick: 'newTarget(); return false;' %>
@@ -50,10 +47,11 @@
<%= javascript_tag do %>
function showQuantityPath(event) {
$(event.target).prevAll('em').text($('option:selected', event.target).attr('data-path'));
$(event.target).prevAll('em').text($('option:selected', event.target)
.attr('data-path'));
}
$(document).ajaxComplete(function() {
$('select[id$=__quantity_id]').trigger(jQuery.Event('change'));
$('p.target select:first-child[id$=__quantity_id]').trigger(jQuery.Event('change'));
})
function newTarget() {

View File

@@ -1,11 +1,11 @@
<h2><%= t ".heading_new_target" %></h2>
<%= labelled_form_for @targets,
<%= labelled_form_for @goal,
url: project_targets_path(@project, @view_params),
remote: true,
html: {id: 'new-target-form', name: 'new-target-form'} do |f| %>
html: {id: 'new-target-form', name: 'new-target-form'} do |goal_f| %>
<%= render partial: 'targets/form', locals: {f: f} %>
<%= render partial: 'targets/form', locals: {goal_f: goal_f} %>
<div class="tabular">
<p>

View File

@@ -0,0 +1,5 @@
<% if @threshold %>
$('#targets').html('<%= j render partial: 'targets/index' %>');
<% else %>
$(event.target).empty();
<% end %>