From 061cc4543c9e01a8e04a5dda31fb39426e5f0a04 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 4 Mar 2022 18:20:23 +0100 Subject: [PATCH 1/5] Use alchemyPageSelect for page parent select This is useful for large sites with many pages --- .../admin/pages/_new_page_form.html.erb | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/app/views/alchemy/admin/pages/_new_page_form.html.erb b/app/views/alchemy/admin/pages/_new_page_form.html.erb index fe27aa2075..e805280370 100644 --- a/app/views/alchemy/admin/pages/_new_page_form.html.erb +++ b/app/views/alchemy/admin/pages/_new_page_form.html.erb @@ -3,11 +3,7 @@ <%= f.hidden_field(:parent_id) %> <% else %> <% @page.parent = @current_language.root_page %> - <%= f.input :parent_id, - collection: @current_language.pages.contentpages, - label_method: :name, - value_method: :id, - input_html: { class: "alchemy_selectbox" } %> + <%= f.input :parent_id, as: :string, input_html: { class: 'alchemy_selectbox' } %> <% end %> <%= f.hidden_field(:language_id) %> <%= f.hidden_field(:layoutpage) %> @@ -21,3 +17,17 @@ <%= f.input :name %> <%= f.submit Alchemy.t(:create) %> <% end %> + + From 2a36a6ae44ce44f95d871d6ffaac520fd7341975 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 4 Mar 2022 19:50:17 +0100 Subject: [PATCH 2/5] Allow to set allowClear in alchemyPageSelect Sometimes we do not want to allow to clear the selection. --- app/assets/javascripts/alchemy/page_select.js | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/alchemy/page_select.js b/app/assets/javascripts/alchemy/page_select.js index e51d9e6f9d..a3d470067c 100644 --- a/app/assets/javascripts/alchemy/page_select.js +++ b/app/assets/javascripts/alchemy/page_select.js @@ -1,9 +1,11 @@ -$.fn.alchemyPageSelect = function(options) { +$.fn.alchemyPageSelect = function (options) { var pageTemplate = HandlebarsTemplates.page return this.select2({ placeholder: options.placeholder, - allowClear: true, + allowClear: options.hasOwnProperty("allowClear") + ? options.allowClear + : true, minimumInputLength: 3, initSelection: function (_$el, callback) { if (options.initialSelection) { @@ -12,13 +14,16 @@ $.fn.alchemyPageSelect = function(options) { }, ajax: { url: options.url, - datatype: 'json', + datatype: "json", quietMillis: 300, data: function (term, page) { return { - q: $.extend({ - name_cont: term - }, options.query_params), + q: $.extend( + { + name_cont: term + }, + options.query_params + ), page: page } }, @@ -34,8 +39,8 @@ $.fn.alchemyPageSelect = function(options) { formatSelection: function (page) { return page.text || page.name }, - formatResult: function(page) { - return pageTemplate({page: page}) + formatResult: function (page) { + return pageTemplate({ page: page }) } }) } From a813d6fa73313621d319dc98fea51d33aae94909 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 4 Mar 2022 19:52:27 +0100 Subject: [PATCH 3/5] Allow to change page parent Unless the page is the current language root or a layoutpage, we allow to change the parent. This is much more efficient than using the dragndrop interface, especially in large trees. --- app/views/alchemy/admin/pages/_form.html.erb | 19 +++++++++++++++ .../admin/page_creation_feature_spec.rb | 5 ++-- .../admin/page_editing_feature_spec.rb | 23 ++++++++++++++++--- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/views/alchemy/admin/pages/_form.html.erb b/app/views/alchemy/admin/pages/_form.html.erb index 99356f045d..f7601db285 100644 --- a/app/views/alchemy/admin/pages/_form.html.erb +++ b/app/views/alchemy/admin/pages/_form.html.erb @@ -1,4 +1,8 @@ <%= alchemy_form_for [:admin, @page], class: 'edit_page' do |f| %> + <% unless @page.language_root? || @page.layoutpage %> + <%= f.input :parent_id, required: true, input_html: { class: 'alchemy_selectbox' } %> + <% end %> +
@@ -37,3 +41,18 @@ <%= f.submit Alchemy.t(:save) %> <% end %> + + diff --git a/spec/features/admin/page_creation_feature_spec.rb b/spec/features/admin/page_creation_feature_spec.rb index fbd75bc8a5..cf21a3f4af 100644 --- a/spec/features/admin/page_creation_feature_spec.rb +++ b/spec/features/admin/page_creation_feature_spec.rb @@ -5,13 +5,13 @@ RSpec.describe "Page creation", type: :system do before { authorize_user(:as_admin) } - describe "parent selection" do + describe "parent selection", :js do let!(:homepage) { create(:alchemy_page, :language_root) } context "without having a parent id in the params" do it "contains a parent select" do visit new_admin_page_path - expect(page).to have_select("Parent", selected: homepage.name) + expect(page).to have_css("#s2id_page_parent_id") end end @@ -19,6 +19,7 @@ it "contains a hidden parent_id field" do visit new_admin_page_path(parent_id: homepage) expect(page).to have_field("page_parent_id", type: "hidden") + expect(page).to_not have_css("#s2id_page_parent_id") end end end diff --git a/spec/features/admin/page_editing_feature_spec.rb b/spec/features/admin/page_editing_feature_spec.rb index 3343601f12..d4d18cab6e 100644 --- a/spec/features/admin/page_editing_feature_spec.rb +++ b/spec/features/admin/page_editing_feature_spec.rb @@ -189,11 +189,14 @@ class FooPreviewSource < Alchemy::Admin::PreviewUrl; end before { authorize_user(:as_admin) } let!(:a_page) { create(:alchemy_page) } + before do + visit alchemy.admin_pages_path + find(".sitemap_page[name='#{a_page.name}'] .icon.fa-cog").click + expect(page).to have_selector(".alchemy-dialog-overlay.open") + end + context "when updating the name" do it "saves the name" do - visit alchemy.admin_pages_path - find(".sitemap_page[name='#{a_page.name}'] .icon.fa-cog").click - expect(page).to have_selector(".alchemy-dialog-overlay.open") within(".alchemy-dialog.modal") do find("input#page_name").set("name with some %!x^)'([@!{}]|/?\:# characters") find(".submit button").click @@ -202,6 +205,20 @@ class FooPreviewSource < Alchemy::Admin::PreviewUrl; end expect(page).to have_selector("#sitemap a.sitemap_pagename_link", text: "name with some %!x^)'([@!{}]|/?\:# characters") end end + + describe "changing parent" do + let!(:new_parent) { create(:alchemy_page) } + + it "can change page parent" do + within(".alchemy-dialog.modal") do + expect(page).to have_css("#s2id_page_parent_id") + select2_search(new_parent.name, from: "Parent") + find(".submit button").click + end + expect(page).to_not have_selector(".alchemy-dialog-overlay.open") + expect(page).to have_selector("#sitemap .sitemap_url", text: "/#{new_parent.urlname}/#{a_page.urlname}") + end + end end describe "fixed attributes" do From 6cc8ee629a6ff0dada146b412524053fed7bc711 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 4 Mar 2022 19:53:53 +0100 Subject: [PATCH 4/5] Do not allow to clear parent in new page form A page needs a parent, unless it is the root page, but this does not get created in this dialog. --- app/views/alchemy/admin/pages/_new_page_form.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/alchemy/admin/pages/_new_page_form.html.erb b/app/views/alchemy/admin/pages/_new_page_form.html.erb index e805280370..b2d070fd54 100644 --- a/app/views/alchemy/admin/pages/_new_page_form.html.erb +++ b/app/views/alchemy/admin/pages/_new_page_form.html.erb @@ -22,6 +22,7 @@ $('input[type="text"]#page_parent_id').alchemyPageSelect({ placeholder: "<%= Alchemy.t(:search_page) %>", url: "<%= alchemy.api_pages_path %>", + allowClear: false, <% if @page.parent %> initialSelection: { id: <%= @page.parent.id %>, From 5ecdf46c59c1cf3cccb2240265efe77246d3d68d Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Sat, 5 Mar 2022 12:54:55 +0100 Subject: [PATCH 5/5] Reload page tree after parents changes This is more reliable than just replacing the old and the new parents --- app/controllers/alchemy/admin/pages_controller.rb | 1 + app/views/alchemy/admin/pages/update.js.erb | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/app/controllers/alchemy/admin/pages_controller.rb b/app/controllers/alchemy/admin/pages_controller.rb index 2a78ad0f47..30eb3e69f9 100644 --- a/app/controllers/alchemy/admin/pages_controller.rb +++ b/app/controllers/alchemy/admin/pages_controller.rb @@ -123,6 +123,7 @@ def configure # * fetches page via before filter # def update + @old_parent_id = @page.parent_id if @page.update(page_params) @notice = Alchemy.t("Page saved", name: @page.name) @while_page_edit = request.referer.include?("edit") diff --git a/app/views/alchemy/admin/pages/update.js.erb b/app/views/alchemy/admin/pages/update.js.erb index 144eec2f46..de76d0bd46 100644 --- a/app/views/alchemy/admin/pages/update.js.erb +++ b/app/views/alchemy/admin/pages/update.js.erb @@ -8,6 +8,13 @@ Alchemy.growl("<%= j @notice %>"); Alchemy.closeCurrentDialog(); +<% elsif @page.parent_id != @old_parent_id -%> + + Alchemy.closeCurrentDialog(function() { + Alchemy.growl("<%= j @notice %>"); + Alchemy.currentSitemap.load(<%= @page.get_language_root.id %>); + }); + <% else -%> if (page) {