Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve nesting

Matthew Kennedy7b0209717a56
diff --git a/backend/app/assets/javascripts/spree/backend/navigation/menu.es6 b/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
index 4c3d09ae20..12d41b30bc 100644
--- a/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
+++ b/backend/app/assets/javascripts/spree/backend/navigation/menu.es6
@@ -8,6 +8,8 @@ document.addEventListener('DOMContentLoaded', function() {
put: true
},
handle: '.move-handle',
+ swapThreshold: 0.5,
+ emptyInsertThreshold: 8,
dragClass: 'menu-item-dragged',
draggable: '.dragable',
animation: 350,
diff --git a/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss b/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss
index cefc9065b1..04bb448a88 100644
--- a/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss
+++ b/backend/app/assets/stylesheets/spree/backend/shared/_sortable_js_tree.scss
@@ -7,23 +7,14 @@ $nestable-background-color: rgb(214, 214, 214);
margin: 2px;
}
- .standard-item-row {
- background-color: white;
- padding: $item-padding;
- border: 1px solid $border-color;
- border-bottom: 1px solid transparent;
- margin: 0;
- border-right: none;
- }
-
.container-item-row {
- border: 1px solid $nestable-background-color;
- border-right: none;
- padding: $item-padding;
- background-color:$nestable-background-color;
+ border: 1px solid $main-items-border-color;
+ margin: 2px;
+ background-color: white;
}
.menu-container-item {
+ border: solid 1px transparent;
padding: 0;
background-color: var(--light);
@@ -33,22 +24,8 @@ $nestable-background-color: rgb(214, 214, 214);
}
}
- #primaryMenuContainer {
- border: none;
- padding: 0;
- margin-left: 0;
- }
-
- #primaryMenuContainer > div.standard-item-row {
- border-left: 1px solid transparent;
- }
-
- #primaryMenuContainer > div.standard-item-row:first-child {
- border-top: 1px solid transparent;
- border-left: 1px solid transparent;
- }
-
- .standard-item-row.menu-item-dragged {
- border: 1px solid $border-color;
+ .sortable-chosen {
+ border: 1px dashed var(--gray);
+ border-radius: 5px;
}
}
diff --git a/backend/app/helpers/spree/admin/menu_helper.rb b/backend/app/helpers/spree/admin/menu_helper.rb
index 4371a1337d..9a21f272a8 100644
--- a/backend/app/helpers/spree/admin/menu_helper.rb
+++ b/backend/app/helpers/spree/admin/menu_helper.rb
@@ -2,18 +2,10 @@ module Spree
module Admin
module MenuHelper
def build_menu_item(menu, item)
- if item.container
- dragable = nil
- klazz = 'container-item-row'
- menu_item_that_accepts_nested_items(menu, item, klazz, dragable)
- else
- dragable = 'dragable'
- klazz = 'standard-item-row'
- standard_menu_item(menu, item, klazz, dragable)
- end
+ menu_item_that_accepts_nested_items(menu, item)
end
- def standard_menu_item(menu, item, klazz, dragable)
+ def menu_item_bar(menu, item)
edit_button = link_to_edit(item, no_text: true, url: spree.edit_admin_menu_menu_item_path(menu, item))
delete_button = link_to_delete(item, no_text: true, url: spree.admin_menu_menu_item_path(menu, item)) if can?(:destroy, item)
@@ -21,37 +13,24 @@ module Spree
description_area = content_tag(:div, item.name, class: 'd-flex align-items-center w-100')
buttons_area = content_tag(:div, edit_button + delete_button, class: 'd-flex align-items-center space-buttons px-2')
- content_tag(:div, move_handle + description_area + buttons_area, class: "#{klazz} #{dragable} d-flex flex-nowrap #{'menu-item' if !item.container}", data: {item_id: item.id, parent_id: item.parent_id})
+ content_tag(:div, move_handle + description_area + buttons_area,
+ class: 'container-item-row d-flex flex-nowrap',
+ data: { item_id: item.id, parent_id: item.parent_id })
end
- def menu_item_that_accepts_nested_items(menu, item, klazz, dragable)
- info_row = standard_menu_item(menu, item, klazz, dragable)
- sub_menu_container = content_tag(:div, nil, class: 'menu-container')
-
- content_tag(:div, info_row + sub_menu_container, class: 'menu-item menu-container-item dragable', data: {item_id: item.id})
- end
+ def menu_item_that_accepts_nested_items(menu, item)
+ decendents = build_menu_item(menu, item.descendants[0]) unless item.leaf?
+ info_row = menu_item_bar(menu, item)
+ sub_menu_container = content_tag(:div, decendents, class: 'menu-container')
+ content_tag(:div, info_row + sub_menu_container, class: 'menu-item menu-container-item dragable', data: { item_id: item.id })
+ end
- def child_check(objp)
- nested_set_options(objp) do |child_item|
- child_item.name
- end
- end
+ def test(a, b)
+ b.self_and_descendants.inspect
- # this is not used at the moment
- # Remove this if it never gets used.
- def humanize_class_name(object)
- if object.is_a? Array
- object.map do |obj|
- obj.split('::').last
- end
- elsif object.is_a? String
- object.split('::').last
- else
- 'Pass me a String or an Array'
- end
end
end
end
diff --git a/backend/app/views/spree/admin/menus/edit.html.erb b/backend/app/views/spree/admin/menus/edit.html.erb
index cd1d02be4d..72f96fa998 100644
--- a/backend/app/views/spree/admin/menus/edit.html.erb
+++ b/backend/app/views/spree/admin/menus/edit.html.erb
@@ -21,6 +21,8 @@
</div>
</div>
+
+
<div id="menuArea">
<div class="row mt-5">
<div class="col-12">
@@ -36,10 +38,13 @@
</div>
</div>
- <div class="card-body" style="padding: 0;">
+ <div class="card-body p-0 bg-light" >
<div id="primaryMenuContainer" class="menu-container">
- <% @menu.menu_items.each_with_level(@menu.menu_items.root.self_and_descendants) do |item, level| %>
- <%= build_menu_item(@menu, item) %>
+ <% nested_set_options(@menu.menu_items) do |item| %>
+ <% if item.root? %>
+ <%= build_menu_item(@menu, item) %>
+ <%= test(@menu, item) %>
+ <% end %>
<% end %>
</div>
</div>
@@ -47,3 +52,4 @@
</div>
</div>
</div>
+
diff --git a/core/app/models/spree/menu_item.rb b/core/app/models/spree/menu_item.rb
index 6f86ecd71a..a1c6d4196f 100644
--- a/core/app/models/spree/menu_item.rb
+++ b/core/app/models/spree/menu_item.rb
@@ -2,7 +2,7 @@ module Spree
class MenuItem < Spree::Base
belongs_to :menu
- acts_as_nested_set
+ acts_as_nested_set dependent: :destroy
after_save :touch_ancestors
after_touch :touch_ancestors
@@ -22,9 +22,21 @@ module Spree
validates :item_type, inclusion: { in: ITEM_TYPE }
validates :linked_resource_type, inclusion: { in: LINKED_RESOURCE_TYPE }
+ def cached_self_and_descendants_ids
+ Rails.cache.fetch("#{cache_key_with_version}/descendant-ids") do
+ self_and_descendants.ids
+ end
+ end
+
+ def cached_self_and_descendants
+ Rails.cache.fetch("#{cache_key_with_version}/descendant-ids") do
+ self_and_descendants
+ end
+ end
+
+ private
def touch_ancestors
- # Touches all ancestors at once to avoid recursive taxonomy touch, and reduce queries.
ancestors.update_all(updated_at: Time.current)
end
end