Skip to content

Commit

Permalink
Update add nested element buttons in current page when element added …
Browse files Browse the repository at this point in the history
…to clipboard
  • Loading branch information
dbwinger committed Apr 15, 2022
1 parent 0ef5aaf commit 9d40c82
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
11 changes: 11 additions & 0 deletions app/views/alchemy/admin/clipboard/insert.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@
Alchemy.growl('<%= j Alchemy.t("item copied to clipboard", name: @item.class.name == "Alchemy::Element" ? @item.display_name_with_preview_text : @item.name) %>')
<% end -%>
$('#clipboard_button .icon').removeClass('fa-clipboard').addClass('fa-paste');

if (window.location.pathname == "<%= edit_admin_page_path(@item.page.id) %>") {
<%# Update add nested element forms for any elements that accept ONLY this as a nested element %>
<% if @item.class == Alchemy::Element %>
<% @item.page.draft_version.elements.expanded.each do |element| %>
<% if element.definition["nestable_elements"] == [@item.name] %>
$(".add-nested-element[data-element-id='<%= element.id %>']").replaceWith('<%= j render "alchemy/admin/elements/add_nested_element_form", element: element %>')
<% end %>
<% end %>
<% end %>
}
27 changes: 27 additions & 0 deletions app/views/alchemy/admin/elements/_add_nested_element_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= content_tag :div, class: 'add-nested-element', data: { element_id: element.id } do %>
<% if element.expanded? || element.fixed? %>
<% if element.nestable_elements.length == 1 &&
(nestable_element = element.nestable_elements.first) &&
Alchemy::Element.all_from_clipboard_for_parent_element(get_clipboard("elements"), element).none?
%>
<%= form_for [:admin, Alchemy::Element.new(name: nestable_element)],
remote: true, html: { class: 'add-nested-element-form', id: nil } do |f| %>
<%= f.hidden_field :name %>
<%= f.hidden_field :page_version_id, value: element.page_version_id %>
<%= f.hidden_field :parent_element_id, value: element.id %>
<button class="button add-nestable-element-button" data-alchemy-button>
<%= Alchemy.t(:add_nested_element, name: Alchemy.t(nestable_element, scope: 'element_names')) %>
</button>
<% end %>
<% else %>
<%= link_to_dialog (nestable_element ? Alchemy.t(:add_nested_element, name: Alchemy.t(nestable_element, scope: 'element_names')) : Alchemy.t("New Element")),
alchemy.new_admin_element_path(
parent_element_id: element.id,
page_version_id: element.page_version_id
), {
size: "320x125",
title: Alchemy.t("New Element")
}, class: "button add-nestable-element-button" %>
<% end %>
<% end %>
<% end %>
26 changes: 1 addition & 25 deletions app/views/alchemy/admin/elements/_element.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,7 @@
} %>
<% end %>

<% if element.expanded? || element.fixed? %>
<% if element.nestable_elements.length == 1 &&
(nestable_element = element.nestable_elements.first) &&
Alchemy::Element.all_from_clipboard_for_parent_element(get_clipboard("elements"), element).none?
%>
<%= form_for [:admin, Alchemy::Element.new(name: nestable_element)],
remote: true, html: { class: 'add-nested-element-form', id: nil } do |f| %>
<%= f.hidden_field :name %>
<%= f.hidden_field :page_version_id, value: element.page_version_id %>
<%= f.hidden_field :parent_element_id, value: element.id %>
<button class="button add-nestable-element-button" data-alchemy-button>
<%= Alchemy.t(:add_nested_element, name: Alchemy.t(nestable_element, scope: 'element_names')) %>
</button>
<% end %>
<% else %>
<%= link_to_dialog (nestable_element ? Alchemy.t(:add_nested_element, name: Alchemy.t(nestable_element, scope: 'element_names')) : Alchemy.t("New Element")),
alchemy.new_admin_element_path(
parent_element_id: element.id,
page_version_id: element.page_version_id
), {
size: "320x125",
title: Alchemy.t("New Element")
}, class: "button add-nestable-element-button" %>
<% end %>
<% end %>
<%= render "alchemy/admin/elements/add_nested_element_form", element: element %>
</div>
<% end %>
<% end %>
20 changes: 18 additions & 2 deletions spec/features/admin/edit_elements_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,30 @@
end
end

context "when clipboard does not have a nestable element" do
scenario "the add element button immediately creates the nested element.", :js do
context "when clipboard does not have a nestable element", :js do
scenario "the add element button immediately creates the nested element." do
visit alchemy.admin_elements_path(page_version_id: element.page_version_id)
button = page.find(".add-nestable-element-button")
expect(button).to have_content "Add slide"
button.click
expect(page).to have_selector(".element-editor[data-element-name='slide']")
end

context "when a nested element is copied to clipboard" do
before do
visit alchemy.edit_admin_page_path(element.page)
page.find(".add-nestable-element-button").click
new_element = Alchemy::Element.last
page.find("#element_#{new_element.id} .element-header").hover
page.first("a[href^='/admin/clipboard/insert?remarkable_id=#{new_element.id}&remarkable_type=elements']").click
end

scenario "the add button now opens add element form with the clipboard tab" do
find(".add-nestable-element-button").click
expect(page).to have_select("Element")
expect(page).to have_link("Paste from clipboard")
end
end
end
end

Expand Down

0 comments on commit 9d40c82

Please sign in to comment.