From daf0bd421e4840874f6ee70c787308995d65f728 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 27 Oct 2019 19:29:12 +0100 Subject: [PATCH] Running Quantity#toggle/up/down/left/right through AJAX --- app/controllers/quantities_controller.rb | 14 ++-- app/views/quantities/_list.html.erb | 86 ++++++++++++++++++++++++ app/views/quantities/index.html.erb | 71 +------------------ app/views/quantities/toggle.js.erb | 1 + 4 files changed, 99 insertions(+), 73 deletions(-) create mode 100644 app/views/quantities/_list.html.erb create mode 100644 app/views/quantities/toggle.js.erb diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index 4bba6fb..953a43e 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -28,27 +28,31 @@ class QuantitiesController < ApplicationController def toggle @quantity.update(primary: !@quantity.primary) - redirect_to project_quantities_url(@project) + @quantities = @project.quantities end def up @quantity.move_left if @quantity.left_sibling.present? - redirect_to project_quantities_url(@project) + @quantities = @project.quantities + render :toggle end def down @quantity.move_right if @quantity.right_sibling.present? - redirect_to project_quantities_url(@project) + @quantities = @project.quantities + render :toggle end def left @quantity.move_to_right_of(@quantity.parent) if @quantity.parent.present? - redirect_to project_quantities_url(@project) + @quantities = @project.quantities + render :toggle end def right @quantity.move_to_child_of(@quantity.left_sibling) if @quantity.left_sibling.present? - redirect_to project_quantities_url(@project) + @quantities = @project.quantities + render :toggle end private diff --git a/app/views/quantities/_list.html.erb b/app/views/quantities/_list.html.erb new file mode 100644 index 0000000..7cb0ea8 --- /dev/null +++ b/app/views/quantities/_list.html.erb @@ -0,0 +1,86 @@ +<% if @quantities.many? %> + + + + + + + + + + + + <% Quantity.each_with_level(@quantities) do |q, level| %> + <% + next if q.new_record? + quantity_class = "quantity" + quantity_class += " project idnt idnt-#{level+1}" + quantity_class += " primary" if q.primary + quantity_class += " anchor" if @anchor == q.id + %> + + + + + + + + <% end %> + +
<%= l(:field_name) %><%= l(:field_order) %><%= l(:field_domain) %><%= l(:field_description) %><%= l(:field_action) %>
+ <%= link_to '', toggle_quantity_path(q), { + remote: true, + method: :post, + class: "icon #{q.primary ? "icon-fav" : "icon-fav-off"}" + } + %> + <%= q.name %> + + <%= + if q.left_sibling.present? + link_to '', up_quantity_path(q), { + remote: true, + method: :post, + class: "icon icon-move-up" + } + else + link_to '', '', {class: "icon"} + end + %> + <%= + if q.right_sibling.present? + link_to '', down_quantity_path(q), { + remote: true, + method: :post, + class: "icon icon-move-down" + } + else + link_to '', '', {class: "icon"} + end + %> + <%= + if q.parent.present? + link_to '', left_quantity_path(q), { + remote: true, + method: :post, + class: "icon icon-move-left" + } + else + link_to '', '', {class: "icon"} + end + %> + <%= + if q.left_sibling.present? + link_to '', right_quantity_path(q), { + remote: true, + method: :post, + class: "icon icon-move-right" + } + else + link_to '', '', {class: "icon"} + end + %> + <%= q.domain %><%= q.description %><%= delete_link quantity_path(q), data: {} %>
+<% else %> +

<%= l(:label_no_data) %>

+<% end %> diff --git a/app/views/quantities/index.html.erb b/app/views/quantities/index.html.erb index a627436..91f0868 100644 --- a/app/views/quantities/index.html.erb +++ b/app/views/quantities/index.html.erb @@ -23,71 +23,6 @@

<%= t ".heading" %>

-<% if @quantities.many? %> - - - - - - - - - - - - <% Quantity.each_with_level(@quantities) do |q, level| %> - <% - next if q.new_record? - quantity_class = "quantity" - quantity_class += " project idnt idnt-#{level+1}" - quantity_class += " primary" if q.primary - %> - - - - - - - - <% end %> - -
<%= l(:field_name) %><%= l(:field_order) %><%= l(:field_domain) %><%= l(:field_description) %><%= l(:field_action) %>
- <%= link_to '', toggle_quantity_path(q), - {method: :post, class: "icon #{q.primary ? "icon-fav" : "icon-fav-off"}"} %> - <%= q.name %> - - <%= - if q.left_sibling.present? - link_to '', up_quantity_path(q), {method: :post, class: "icon icon-move-up"} - else - link_to '', '', {class: "icon"} - end - %> - <%= - if q.right_sibling.present? - link_to '', down_quantity_path(q), - {method: :post, class: "icon icon-move-down"} - else - link_to '', '', {class: "icon"} - end - %> - <%= - if q.parent.present? - link_to '', left_quantity_path(q), - {method: :post, class: "icon icon-move-left"} - else - link_to '', '', {class: "icon"} - end - %> - <%= - if q.left_sibling.present? - link_to '', right_quantity_path(q), - {method: :post, class: "icon icon-move-right"} - else - link_to '', '', {class: "icon"} - end - %> - <%= q.domain %><%= q.description %><%= delete_link quantity_path(q), data: {} %>
-<% else %> -

<%= l(:label_no_data) %>

-<% end %> +
+ <%= render :partial => 'quantities/list' %> +
diff --git a/app/views/quantities/toggle.js.erb b/app/views/quantities/toggle.js.erb new file mode 100644 index 0000000..6501902 --- /dev/null +++ b/app/views/quantities/toggle.js.erb @@ -0,0 +1 @@ +$('#quantities').html('<%= escape_javascript(render :partial => 'quantities/list') %>');