forked from fixin.me/fixin.me
Add Quantity #new, #create, #destroy actions
This commit is contained in:
19
app/views/quantities/_form.html.erb
Normal file
19
app/views/quantities/_form.html.erb
Normal file
@@ -0,0 +1,19 @@
|
||||
<%= tabular_fields_for @quantity, form: form_tag do |form| %>
|
||||
<%- tag.tr id: row, class: "form", onkeydown: "processKey(event)",
|
||||
data: {link: link, form: form_tag, hidden_row: hidden_row} do %>
|
||||
|
||||
<td class="<%= class_names({subquantity: @quantity.parent}) %>">
|
||||
<%= form.text_field :name, required: true, autofocus: true, size: 20 %>
|
||||
</td>
|
||||
<td>
|
||||
<%= form.text_area :description, cols: 30, rows: 1, escape: false %>
|
||||
</td>
|
||||
|
||||
<td class="actions">
|
||||
<%= form.button %>
|
||||
<%= image_link_to t(:cancel), "close-outline", quantities_path, class: 'dangerous',
|
||||
name: :cancel, onclick: render_turbo_stream('form_close', {row: row}) %>
|
||||
</td>
|
||||
<td></td>
|
||||
<% end %>
|
||||
<% end %>
|
||||
2
app/views/quantities/_form_close.html.erb
Normal file
2
app/views/quantities/_form_close.html.erb
Normal file
@@ -0,0 +1,2 @@
|
||||
<%= turbo_stream.close_form row %>
|
||||
<%= turbo_stream.update :flashes %>
|
||||
28
app/views/quantities/_quantity.html.erb
Normal file
28
app/views/quantities/_quantity.html.erb
Normal file
@@ -0,0 +1,28 @@
|
||||
<%= tag.tr id: dom_id(quantity),
|
||||
ondragstart: 'dragStart(event)', ondragend: 'dragEnd(event)',
|
||||
ondragover: 'dragOver(event)', ondrop: 'drop(event)',
|
||||
ondragenter: 'dragEnter(event)', ondragleave: 'dragLeave(event)',
|
||||
data: {drag_path: rebase_quantity_path(quantity),
|
||||
drop_id: dom_id(quantity.parent || quantity)} do %>
|
||||
|
||||
<td class="link" style="--depth:<%= quantity.depth %>">
|
||||
<%= link_to quantity, edit_quantity_path(quantity), onclick: 'this.blur();',
|
||||
data: {turbo_stream: true} %>
|
||||
</td>
|
||||
<td><%= quantity.description %></td>
|
||||
|
||||
<% if current_user.at_least(:active) %>
|
||||
<td class="actions">
|
||||
<%= image_link_to t('.new_subquantity'), 'plus-outline', new_quantity_path(quantity),
|
||||
id: dom_id(quantity, :new, :link), onclick: 'this.blur();', data: {turbo_stream: true} %>
|
||||
|
||||
<%= image_button_to_if quantity.movable?, t('.destroy'), 'delete-outline',
|
||||
quantity_path(quantity), method: :delete %>
|
||||
</td>
|
||||
<% if quantity.movable? %>
|
||||
<td class="handle" draggable="true">⠿</td>
|
||||
<% else %>
|
||||
<td></td>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
7
app/views/quantities/create.turbo_stream.erb
Normal file
7
app/views/quantities/create.turbo_stream.erb
Normal file
@@ -0,0 +1,7 @@
|
||||
<%= turbo_stream.close_form dom_id(@quantity.parent || Quantity, :new) %>
|
||||
<%= turbo_stream.remove :no_items %>
|
||||
<% @ancestors.map do |ancestor| %>
|
||||
<%= turbo_stream.replace ancestor %>
|
||||
<% end %>
|
||||
<%= @before.nil? ? turbo_stream.append(:quantities, @quantity) :
|
||||
turbo_stream.before(@before, @quantity) %>
|
||||
5
app/views/quantities/destroy.turbo_stream.erb
Normal file
5
app/views/quantities/destroy.turbo_stream.erb
Normal file
@@ -0,0 +1,5 @@
|
||||
<% @ancestors.map do |ancestor| %>
|
||||
<%= turbo_stream.replace ancestor %>
|
||||
<% end %>
|
||||
<%= turbo_stream.remove @quantity %>
|
||||
<%= turbo_stream.append(:quantities, render_no_items) if current_user.quantities.empty? %>
|
||||
20
app/views/quantities/new.turbo_stream.erb
Normal file
20
app/views/quantities/new.turbo_stream.erb
Normal file
@@ -0,0 +1,20 @@
|
||||
<% dom_obj = @quantity.parent || @quantity %>
|
||||
<% ids = {row: dom_id(dom_obj, :new),
|
||||
hidden_row: nil,
|
||||
link: dom_id(dom_obj, :new, :link),
|
||||
form_tag: dom_id(dom_obj, :new, :form)} %>
|
||||
|
||||
<%= turbo_stream.disable ids[:link] -%>
|
||||
|
||||
<%= turbo_stream.append :quantity_form do %>
|
||||
<%- tabular_form_with model: @quantity, html: {id: ids[:form_tag]} do |form| %>
|
||||
<%= form.hidden_field :parent_id unless @quantity.parent.nil? %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
<% if @quantity.parent %>
|
||||
<%= turbo_stream.remove ids[:row] %>
|
||||
<%= turbo_stream.after @quantity.parent, partial: 'form', locals: ids %>
|
||||
<% else %>
|
||||
<%= turbo_stream.prepend :quantities, partial: 'form', locals: ids %>
|
||||
<% end %>
|
||||
Reference in New Issue
Block a user