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.

Update views and javascript.

Matthew Kennedydae0f30e0b2c
diff --git a/backend/app/assets/javascripts/spree/backend/cms/_index.es6 b/backend/app/assets/javascripts/spree/backend/cms/_index.es6
index e62a28d019..7c42f74ada 100644
--- a/backend/app/assets/javascripts/spree/backend/cms/_index.es6
+++ b/backend/app/assets/javascripts/spree/backend/cms/_index.es6
@@ -1,2 +1,3 @@
//= require spree/backend/cms/page
//= require spree/backend/cms/section
+//= require spree/backend/cms/linkable
diff --git a/backend/app/assets/javascripts/spree/backend/cms/linkable.es6 b/backend/app/assets/javascripts/spree/backend/cms/linkable.es6
new file mode 100644
index 0000000000..69b1109182
--- /dev/null
+++ b/backend/app/assets/javascripts/spree/backend/cms/linkable.es6
@@ -0,0 +1,24 @@
+document.addEventListener('DOMContentLoaded', function() {
+ const linkSwitcher = $('.link_switcher').select2()
+
+ linkSwitcher.on('change', function() {
+ const selectedLinkTo = $(linkSwitcher).val()
+ const message = document.getElementById('linkClickUpdate')
+ const activePanel = document.querySelector('[data-panel-type]')
+ const panelType = activePanel.dataset.panelType
+
+ if (selectedLinkTo === panelType) {
+ activePanel.classList = ''
+ activePanel.classList.add('d-block')
+
+ message.classList = ''
+ message.classList.add('d-none')
+ } else {
+ activePanel.classList = ''
+ activePanel.classList.add('d-none')
+
+ message.classList = ''
+ message.classList.add('d-block')
+ }
+ })
+})
diff --git a/backend/app/assets/javascripts/spree/backend/cms/section.es6 b/backend/app/assets/javascripts/spree/backend/cms/section.es6
index 7d5e06be7d..82b3759b62 100644
--- a/backend/app/assets/javascripts/spree/backend/cms/section.es6
+++ b/backend/app/assets/javascripts/spree/backend/cms/section.es6
@@ -1,27 +1,24 @@
document.addEventListener('DOMContentLoaded', function() {
- $('#cms_section_linked_resource_type').select2()
- $('#cms_section_item_type').select2()
+ const sectionKindSelector = $('#cms_section_kind').select2()
- $('#cms_section_linked_resource_type').on('change', function() {
- const selectedLinkTo = $('#cms_section_linked_resource_type').val()
+ sectionKindSelector.on('change', function() {
+ const selectedValue = $(sectionKindSelector).val()
const message = document.getElementById('alertToClickUpdate')
- const activePanel = document.getElementById('linkResourcePanel')
- const panelType = activePanel.dataset.panelType
+ const activeSectionKind = document.getElementById('CmsSectionKind')
+ const panelType = activeSectionKind.dataset.panelKind
- if (selectedLinkTo === panelType) {
- activePanel.classList = ''
- activePanel.classList.add('d-block')
+ if (selectedValue === panelType) {
+ activeSectionKind.classList = ''
+ activeSectionKind.classList.add('d-block')
message.classList = ''
message.classList.add('d-none')
} else {
- activePanel.classList = ''
- activePanel.classList.add('d-none')
+ activeSectionKind.classList = ''
+ activeSectionKind.classList.add('d-none')
message.classList = ''
message.classList.add('d-block')
}
});
-
-
})
diff --git a/backend/app/assets/javascripts/spree/backend/menus/menu_item.es6 b/backend/app/assets/javascripts/spree/backend/menus/menu_item.es6
index b0c1058d34..5221118548 100644
--- a/backend/app/assets/javascripts/spree/backend/menus/menu_item.es6
+++ b/backend/app/assets/javascripts/spree/backend/menus/menu_item.es6
@@ -1,28 +1,4 @@
document.addEventListener('DOMContentLoaded', function() {
- $('#menu_item_linked_resource_type').select2()
- $('#menu_item_item_type').select2()
-
- $('#menu_item_linked_resource_type').on('change', function() {
- const selectedLinkTo = $('#menu_item_linked_resource_type').val()
- const message = document.getElementById('alertToClickUpdate')
- const activePanel = document.getElementById('linkResourcePanel')
- const panelType = activePanel.dataset.panelType
-
- if (selectedLinkTo === panelType) {
- activePanel.classList = ''
- activePanel.classList.add('d-block')
-
- message.classList = ''
- message.classList.add('d-none')
- } else {
- activePanel.classList = ''
- activePanel.classList.add('d-none')
-
- message.classList = ''
- message.classList.add('d-block')
- }
- });
-
updateContainerMessage()
$('#menu_item_item_type').on('change', function() {
diff --git a/backend/app/helpers/spree/admin/cms_page_helper.rb b/backend/app/helpers/spree/admin/cms_page_helper.rb
index 0939f9141d..6e5f8a2408 100644
--- a/backend/app/helpers/spree/admin/cms_page_helper.rb
+++ b/backend/app/helpers/spree/admin/cms_page_helper.rb
@@ -13,6 +13,10 @@ module Spree
render "spree/admin/cms_pages/sections/#{preview_type}", section: section, width: css_width
end
+
+ def linkable_as_path(item)
+ item.parameterize(separator: '_')
+ end
end
end
end
diff --git a/backend/app/views/spree/admin/cms_sections/_form.html.erb b/backend/app/views/spree/admin/cms_sections/_form.html.erb
index a7b27f2646..9bb1a7a3dc 100644
--- a/backend/app/views/spree/admin/cms_sections/_form.html.erb
+++ b/backend/app/views/spree/admin/cms_sections/_form.html.erb
@@ -15,45 +15,54 @@
<% end %>
</div>
<div class="col-6">
- <%= f.field_container :kind, class: ['form-group'] do %>
- <%= f.label :kind, Spree.t('admin.navigation.section_type') %>
- <%= f.select(:kind, @cms_section_types, {include_blank: false}, class: 'select2') %>
- <%= f.error_message_on :kind %>
- <% end %>
- </div>
-
- <div class="col-6">
- <%= f.field_container :width, class: ['form-group'] do %>
- <%= f.label :width, Spree.t('admin.navigation.width') %>
- <%= f.select(:width, @cms_section_widths, {include_blank: false}, class: 'select2') %>
- <%= f.error_message_on :width %>
- <% end %>
+ <%= f.field_container :kind, class: ['form-group'] do %>
+ <%= f.label :kind, Spree.t('admin.navigation.section_type') %>
+ <%= f.select(:kind, @cms_section_types, {include_blank: false}) %>
+ <%= f.error_message_on :kind %>
+ <% end %>
</div>
-
</div>
</div>
</div>
</div>
</div>
-</div>
-<div data-hook="admin_cms_section_form_fields">
- <div class="row">
- <div class="col-12 col-md-6">
- <div class="card mb-3">
- <div class="card-header">
- <h5 class="mb-0"><%= Spree.t('admin.cms.settings_for', section_kind: @cms_section.kind) %></h5>
+ <div id="alertToClickUpdate" class="d-none">
+ <div class="row">
+ <div class="col-12">
+ <div class="alert alert-warning mt-3">
+ <% if %w[new create].include? controller.action_name %>
+ <%= raw Spree.t('admin.cms.click_create_to_load_new_link') %>
+ <% else %>
+ <%= raw Spree.t('admin.cms.click_update_to_load_new_link') %>
+ <% end %>
</div>
- <div class="card-body">
- <% if @cms_section.kind.present? %>
- <div class="row">
- <div class="col-12">
- <%= render "spree/admin/cms_sections/types/#{@cms_section.kind.parameterize(separator: '_')}", f: f %>
+ </div>
+ </div>
+ </div>
+
+ <% if @cms_section.kind.present? %>
+ <div id="CmsSectionKind"
+ data-hook="admin_cms_section_form_fields"
+ data-panel-kind="<%= @cms_section.kind %>">
+ <div class="row">
+ <div class="col-12">
+ <div class="card mb-3">
+ <div class="card-header">
+ <h5 class="mb-0"><%= Spree.t('admin.cms.settings_for', section_kind: @cms_section.kind) %></h5>
+ </div>
+ <div class="card-body">
+ <% if @cms_section.kind.present? %>
+ <div class="row">
+ <div class="col-12">
+ <%= render "spree/admin/cms_sections/types/#{@cms_section.kind.parameterize(separator: '_')}", f: f %>
+ </div>
+ </div>
+ <% end %>
</div>
</div>
- <% end %>
</div>
</div>
</div>
- </div>
+ <% end %>
</div>
diff --git a/backend/app/views/spree/admin/cms_sections/types/_hero.html.erb b/backend/app/views/spree/admin/cms_sections/types/_hero.html.erb
index 2711382f15..8e8ebeffee 100644
--- a/backend/app/views/spree/admin/cms_sections/types/_hero.html.erb
+++ b/backend/app/views/spree/admin/cms_sections/types/_hero.html.erb
@@ -1,53 +1,57 @@
-<%= f.field_container :title, class: ['form-group'] do %>
- <%= f.label :title, Spree.t(:title) %>
- <%= f.text_field :title, class: 'form-control' %>
- <%= f.error_message_on :title %>
-<% end %>
+<div class="row">
+ <div class="col-6">
+ <%= f.field_container :title, class: ['form-group'] do %>
+ <%= f.label :title, Spree.t(:title) %>
+ <%= f.text_field :title, class: 'form-control' %>
+ <%= f.error_message_on :title %>
+ <% end %>
-<%= f.field_container :subtitle, class: ['form-group'] do %>
- <%= f.label :subtitle, Spree.t(:button_text) %>
- <%= f.text_field :subtitle, class: 'form-control' %>
- <%= f.error_message_on :subtitle %>
-<% end %>
+ <%= f.field_container :width, class: ['form-group'] do %>
+ <%= f.label :width, Spree.t('admin.navigation.width') %>
+ <%= f.select(:width, @cms_section_widths, {include_blank: false}, class: 'select2') %>
+ <%= f.error_message_on :width %>
+ <% end %>
-<% @cms_section.build_icon unless @cms_section.icon %>
- <%= f.fields_for :icon do |icon_field| %>
- <%= f.field_container :icon_attachment do %>
- <% unless @cms_section.icon.new_record? %>
- <div id="menuItemImgContainer" class="my-3 ">
- <%= image_tag(main_app.url_for(@cms_section.icon.attachment)) %>
- <%#= link_to Spree.t(:remove_image), remove_icon_admin_menu_menu_item_path(@cms_page, @cms_section), method: :delete %>
+ <%= f.field_container :subtitle, class: ['form-group'] do %>
+ <%= f.label :subtitle, Spree.t(:button_text) %>
+ <%= f.text_field :subtitle, class: 'form-control' %>
+ <%= f.error_message_on :subtitle %>
+ <% end %>
+ </div>
+ <div class="col-6">
+ <% @cms_section.build_icon unless @cms_section.icon %>
+ <%= f.fields_for :icon do |icon_field| %>
+ <%= f.field_container :icon_attachment do %>
+ <% unless @cms_section.icon.new_record? %>
+ <div id="menuItemImgContainer" class="my-3 ">
+ <%= image_tag(main_app.url_for(@cms_section.icon.attachment)) %>
+ <%#= link_to Spree.t(:remove_image), remove_icon_admin_menu_menu_item_path(@cms_page, @cms_section), method: :delete %>
+ </div>
+ <% end %>
+ <div id="<% unless @cms_section.icon.new_record? %>MenuItemImageButton<% end %>">
+ <%= icon_field.file_field :attachment %>
</div>
<% end %>
- <div id="<% unless @cms_section.icon.new_record? %>MenuItemImageButton<% end %>">
- <%= icon_field.file_field :attachment %>
- </div>
- <% end %>
- <%= f.field_container :icon_alt do %>
- <% unless @cms_section.icon.new_record? %>
- <div class="form-group my-3">
- <%= icon_field.label :alt, Spree.t('admin.navigation.image_alt_text') %>
- <%= icon_field.text_field :alt, class: 'form-control' %>
- </div>
+ <%= f.field_container :icon_alt do %>
+ <% unless @cms_section.icon.new_record? %>
+ <div class="form-group my-3">
+ <%= icon_field.label :alt, Spree.t('admin.navigation.image_alt_text') %>
+ <%= icon_field.text_field :alt, class: 'form-control' %>
+ </div>
+ <% end %>
+ <% end %>
<% end %>
- <% end %>
-<% end %>
-
-<%= f.field_container :linked_resource_type, class: ['form-group'] do %>
- <%= f.label :linked_resource_type, Spree.t('admin.navigation.link_to') %>
- <%= f.select(:linked_resource_type, @cms_section_links_to, {include_blank: false}) %>
- <%= f.error_message_on :linked_resource_type %>
-<% end %>
-
-<%= render 'link_inputx', linked_resource_type: @cms_section.linked_resource_type, f: f %>
-
-<div id="alertToClickUpdate" class="d-none">
- <div class="alert alert-warning mt-3">
- <% if %w[new create].include? controller.action_name %>
- <%= raw Spree.t('admin.navigation.click_create_to_load_new_link') %>
- <% else %>
- <%= raw Spree.t('admin.navigation.click_update_to_load_new_link') %>
+ </div>
+ <div class="col-12"><hr></div>
+ <div class="col-6">
+ <%= f.field_container :linked_resource_type, class: ['form-group'] do %>
+ <%= f.label :linked_resource_type, Spree.t('admin.navigation.link_to') %>
+ <%= f.select(:linked_resource_type, @cms_section_links_to, {include_blank: false}, class: 'link_switcher') %>
+ <%= f.error_message_on :linked_resource_type %>
<% end %>
</div>
+ <div class="col-6">
+ <%= render "spree/admin/shared/link_switcher", resource: @cms_section, f: f %>
+ </div>
</div>
diff --git a/backend/app/views/spree/admin/shared/_link_switcher.html.erb b/backend/app/views/spree/admin/shared/_link_switcher.html.erb
new file mode 100644
index 0000000000..1c7138b819
--- /dev/null
+++ b/backend/app/views/spree/admin/shared/_link_switcher.html.erb
@@ -0,0 +1,14 @@
+<% if resource.linked_resource_type.present? %>
+ <div data-panel-type="<%= resource.linked_resource_type %>">
+ <%= render "spree/admin/shared/linkables/#{linkable_as_path(resource.linked_resource_type)}", resource: resource, f: f %>
+ </div>
+<% end %>
+<div id="linkClickUpdate" class="d-none">
+ <div class="alert alert-warning mt-3">
+ <% if %w[new create].include? controller.action_name %>
+ <%= raw Spree.t('admin.navigation.click_create_to_load_new_link') %>
+ <% else %>
+ <%= raw Spree.t('admin.navigation.click_update_to_load_new_link') %>
+ <% end %>
+ </div>
+</div>
diff --git a/backend/app/views/spree/admin/shared/linkables/_home.html.erb b/backend/app/views/spree/admin/shared/linkables/_home.html.erb
new file mode 100644
index 0000000000..9437dd56e8
--- /dev/null
+++ b/backend/app/views/spree/admin/shared/linkables/_home.html.erb
@@ -0,0 +1,3 @@
+<div class="alert alert-info mt-3">
+ <%= Spree.t('admin.navigation.this_link_takes_you_to_your_stores_home_page') %>
+</div>
diff --git a/backend/app/views/spree/admin/shared/linkables/_spree_product.html.erb b/backend/app/views/spree/admin/shared/linkables/_spree_product.html.erb
new file mode 100644
index 0000000000..dc97ae44d7
--- /dev/null
+++ b/backend/app/views/spree/admin/shared/linkables/_spree_product.html.erb
@@ -0,0 +1,23 @@
+<% if resource.linked_resource %>
+ <% resorce_holder = [resource.linked_resource] %>
+ <% id_holder = :id %>
+ <% name_holder = :name %>
+ <% active_id_holder = resource.linked_resource.id %>
+ <% end %>
+ <%= f.field_container :linked_resource_id, class: ['form-group'] do %>
+ <%= f.label :linked_resource_id, Spree.t('product') %>
+ <%= f.select :linked_resource_id, options_from_collection_for_select(resorce_holder || [] , id_holder, name_holder, active_id_holder || nil), { include_blank: true }, class: 'autocomplete' %>
+ <%= f.error_message_on :linked_resource_id %>
+ <small class="form-text text-muted"><%= resource.link %></small>
+ <% end %>
+
+ <script>
+ document.addEventListener('DOMContentLoaded', function() {
+ $('.autocomplete').select2Autocomplete({
+ data_attrbute_name: 'products',
+ placeholder: "<%= Spree.t('admin.navigation.seach_for_a_product') %>",
+ allow_clear: true,
+ apiUrl: Spree.routes.products_api_v2,
+ })
+ })
+ </script>
diff --git a/backend/app/views/spree/admin/shared/linkables/_spree_taxon.html.erb b/backend/app/views/spree/admin/shared/linkables/_spree_taxon.html.erb
new file mode 100644
index 0000000000..0dffe9f7a1
--- /dev/null
+++ b/backend/app/views/spree/admin/shared/linkables/_spree_taxon.html.erb
@@ -0,0 +1,24 @@
+<% if resource.linked_resource %>
+ <% resorce_holder = [resource.linked_resource] %>
+ <% id_holder = :id %>
+ <% name_holder = :pretty_name %>
+ <% active_id_holder = resource.linked_resource.id %>
+ <% end %>
+ <%= f.field_container :linked_resource_id, class: ['form-group'] do %>
+ <%= f.label :linked_resource_id, Spree.t('taxon') %>
+ <%= f.select :linked_resource_id, options_from_collection_for_select(resorce_holder || [] , id_holder, name_holder, active_id_holder || nil), { include_blank: true }, class: 'autocomplete' %>
+ <%= f.error_message_on :linked_resource_id %>
+ <small class="form-text text-muted"><%= resource.link %></small>
+ <% end %>
+
+ <script>
+ document.addEventListener('DOMContentLoaded', function() {
+ $('.autocomplete').select2Autocomplete({
+ data_attrbute_name: 'taxons',
+ placeholder: "<%= Spree.t('admin.navigation.seach_for_a_taxon') %>",
+ allow_clear: true,
+ apiUrl: Spree.routes.taxons_api_v2,
+ return_attribute: 'pretty_name',
+ })
+ })
+ </script>
diff --git a/backend/app/views/spree/admin/shared/linkables/_url.html.erb b/backend/app/views/spree/admin/shared/linkables/_url.html.erb
new file mode 100644
index 0000000000..5ff45fe4e0
--- /dev/null
+++ b/backend/app/views/spree/admin/shared/linkables/_url.html.erb
@@ -0,0 +1,16 @@
+<%= f.field_container :destination, class: ['form-group'] do %>
+ <%= f.label :destination, Spree.t(:url) %>
+ <%= f.text_field :destination, class: 'form-control' %>
+ <%= f.error_message_on :destination %>
+ <small class="form-text text-muted">
+ <%= raw Spree.t('admin.navigation.url_info') %>
+ </small>
+<% end %>
+
+<%= f.field_container :new_window do %>
+ <div class="custom-control custom-checkbox">
+ <%= f.check_box :new_window, class: 'custom-control-input' %>
+ <%= f.label :new_window, Spree.t('admin.navigation.open_link_in_new_window'), class: 'custom-control-label' %>
+ <%= f.error_message_on :new_window %>
+ </div>
+<% end %>
diff --git a/core/app/models/spree/cms_page.rb b/core/app/models/spree/cms_page.rb
index ff96518de1..af5c59e208 100644
--- a/core/app/models/spree/cms_page.rb
+++ b/core/app/models/spree/cms_page.rb
@@ -9,9 +9,6 @@ module Spree
has_many :cms_sections
has_many :menu_items, as: :linked_resource
- after_save :sync_menu_item_paths
- after_commit :sync_menu_item_paths
-
before_save :create_slug
validates :title, presence: true
@@ -26,12 +23,6 @@ module Spree
private
- def sync_menu_item_paths
- return unless saved_change_to_slug?
-
- Spree::MenuItem.refresh_paths(self)
- end
-
def create_slug
self.slug = if slug.blank?
title.to_url
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 2a55e3a020..922b8bcf66 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -534,6 +534,8 @@ en:
adjustments: Adjustments
admin:
cms:
+ click_create_to_load_new_link: 'Click the <b>Create</b> button below to save your new page section, and load the section settings.'
+ click_update_to_load_new_link: 'Click the <b>Update</b> button below to load the new section settings.'
settings_for: 'Available Options For %{section_kind}'
page_sections: '%{page_name} Sections'
page_sections_info: 'The area below represents your %{page_type} displayed in %{store}, add sections and drag them into position'
diff --git a/core/db/migrate/20210514204251_create_spree_cms_sections.rb b/core/db/migrate/20210514204251_create_spree_cms_sections.rb
index 538b2e7f22..a8af9ec035 100644
--- a/core/db/migrate/20210514204251_create_spree_cms_sections.rb
+++ b/core/db/migrate/20210514204251_create_spree_cms_sections.rb
@@ -5,17 +5,15 @@ class CreateSpreeCmsSections < ActiveRecord::Migration[6.0]
t.column :title, :string
t.column :subtitle, :string
t.column :content, :text
- t.column :width, :string, default: 'Full'
- t.column :kind, :string, default: 'Text Block'
+ t.column :width, :string
+ t.column :kind, :string
t.column :position, :integer
- t.column :linked_resource_type, :string, default: 'none'
- t.column :linked_resource_id, :integer
+
+ t.references :linked_resource, polymorphic: true, index: { name: 'index_spree_cms_sections_on_linked_resource' }
t.belongs_to :cms_page
t.timestamps
end
-
- add_index :spree_cms_sections, [:linked_resource_type, :linked_resource_id], name: 'index_spree_cms_sections_on_linked_resource'
end
end
diff --git a/frontend/app/views/spree/cms_pages/sections/_hero.html.erb b/frontend/app/views/spree/cms_pages/sections/_hero.html.erb
index e11592c566..6c3632f774 100644
--- a/frontend/app/views/spree/cms_pages/sections/_hero.html.erb
+++ b/frontend/app/views/spree/cms_pages/sections/_hero.html.erb
@@ -2,7 +2,7 @@
<div class="full-width">
<div class="homepage-hero-image mb-3 position-relative p-0">
<% if section.icon.present? %>
- <%= image_tag main_app.url_for(section.icon.attachment) %>
+ <%= image_tag main_app.url_for(section.icon.attachment), alt: section.icon.alt %>
<% end %>
<div class="hero-content">
@@ -10,7 +10,7 @@
<%= section.title %>
</div>
<div>
- <%= link_to section.subtitle, spree.products_path, class: 'btn btn-primary spree-btn px-5' %>
+ <%= link_to section.subtitle, section.link, class: 'btn btn-primary spree-btn px-5' %>
</div>
</div>
</div>