Skip to content

Commit

Permalink
Merge pull request #17144 from opf/impl/59154-move-top-bottom
Browse files Browse the repository at this point in the history
Allow move hierarchy items to the top and to the bottom
  • Loading branch information
brunopagno authored Nov 7, 2024
2 parents 50332f2 + 3660047 commit f11601a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions app/components/admin/custom_fields/hierarchy/item_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ def menu_items(menu)
add_below_action_item(menu)
add_sub_item_action_item(menu)
menu.with_divider
move_up_action_item(menu) unless first_item?
move_down_action_item(menu) unless last_item?
if !first_item?
move_to_top_action_item(menu)
move_up_action_item(menu)
end
if !last_item?
move_down_action_item(menu)
move_to_bottom_action_item(menu)
end
menu.with_divider
deletion_action_item(menu)
end
Expand Down Expand Up @@ -113,6 +119,18 @@ def add_sub_item_action_item(menu)
) { _1.with_leading_visual_icon(icon: "op-arrow-in") }
end

def move_to_top_action_item(menu)
form_inputs = [{ name: "new_sort_order", value: 0 }]

menu.with_item(label: I18n.t(:label_sort_highest),
tag: :button,
href: move_custom_field_item_path(@root.custom_field_id, model),
content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
form_arguments: { method: :post, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "move-to-top")
end
end

def move_up_action_item(menu)
form_inputs = [{ name: "new_sort_order", value: model.sort_order - 1 }]

Expand All @@ -137,6 +155,18 @@ def move_down_action_item(menu)
end
end

def move_to_bottom_action_item(menu)
form_inputs = [{ name: "new_sort_order", value: model.parent.children.length + 1 }]

menu.with_item(label: I18n.t(:label_sort_lowest),
tag: :button,
href: move_custom_field_item_path(@root.custom_field_id, model),
content_arguments: { data: { turbo_frame: ItemsComponent.wrapper_key } },
form_arguments: { method: :post, inputs: form_inputs }) do |item|
item.with_leading_visual_icon(icon: "move-to-bottom")
end
end

def deletion_action_item(menu)
menu.with_item(label: I18n.t(:button_delete),
scheme: :danger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def move
item_service
.reorder_item(item: @active_item, new_sort_order: params.require(:new_sort_order))

redirect_to(custom_field_items_path(@custom_field), status: :see_other)
redirect_to(custom_field_item_path(@custom_field, @active_item.parent), status: :see_other)
end

def destroy
Expand Down

0 comments on commit f11601a

Please sign in to comment.