diff --git a/app/views/alchemy/admin/clipboard/insert.js.erb b/app/views/alchemy/admin/clipboard/insert.js.erb index 900d1b5427..bbea9b0d1d 100644 --- a/app/views/alchemy/admin/clipboard/insert.js.erb +++ b/app/views/alchemy/admin/clipboard/insert.js.erb @@ -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 %> +} \ No newline at end of file diff --git a/app/views/alchemy/admin/elements/_add_nested_element_form.html.erb b/app/views/alchemy/admin/elements/_add_nested_element_form.html.erb new file mode 100644 index 0000000000..6f49ce4b79 --- /dev/null +++ b/app/views/alchemy/admin/elements/_add_nested_element_form.html.erb @@ -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 %> + + <% 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 %> \ No newline at end of file diff --git a/app/views/alchemy/admin/elements/_element.html.erb b/app/views/alchemy/admin/elements/_element.html.erb index 9871a9bd9a..e865e74b72 100644 --- a/app/views/alchemy/admin/elements/_element.html.erb +++ b/app/views/alchemy/admin/elements/_element.html.erb @@ -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 %> - - <% 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 %> <% end %> <% end %> diff --git a/spec/features/admin/edit_elements_feature_spec.rb b/spec/features/admin/edit_elements_feature_spec.rb index 1a7a5da0f1..f3fd0e0d17 100644 --- a/spec/features/admin/edit_elements_feature_spec.rb +++ b/spec/features/admin/edit_elements_feature_spec.rb @@ -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