Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create menubar web component #2611

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alchemy_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Gem::Specification.new do |gem|

gem.add_development_dependency "capybara", ["~> 3.0"]
gem.add_development_dependency "capybara-screenshot", ["~> 1.0"]
gem.add_development_dependency "capybara-shadowdom", ["~> 0.3"]
gem.add_development_dependency "factory_bot_rails", ["~> 6.0"]
gem.add_development_dependency "puma", ["~> 6.0"]
gem.add_development_dependency "rails-controller-testing", ["~> 1.0"]
Expand Down
1 change: 0 additions & 1 deletion app/assets/config/alchemy_manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//= link alchemy/admin/all.js
//= link alchemy/preview.js
//= link alchemy/menubar.css
//= link alchemy/menubar.js
//= link alchemy/print.css
//= link alchemy/welcome.css
//= link tinymce/plugins/alchemy_link/plugin.min.js
Expand Down
8 changes: 0 additions & 8 deletions app/assets/javascripts/alchemy/menubar.js.coffee

This file was deleted.

13 changes: 7 additions & 6 deletions app/assets/stylesheets/alchemy/menubar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
}

&:after {
content: '';
content: "";
width: 24px;
height: 24px;
position: absolute;
right: 10px;
top: 50%;
background: image-url('alchemy/icon-white.svg') 1px 1px no-repeat;
background: image-url("alchemy/icon-white.svg") 1px 1px no-repeat;
background-size: 24px 24px;
transform: translateY(-50%);
}
Expand All @@ -57,7 +57,8 @@
list-style-type: none;
text-align: center;

a, button {
a,
button {
@include button-defaults(
$background-color: $main-menu-bg-color,
$hover-color: $blue,
Expand All @@ -70,10 +71,10 @@
);
width: 100%;
text-decoration: none !important;
text-transform: none;
font-family: $default-font-family;

&:after { display: none }
&:after {
display: none;
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions app/javascript/menubar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Menubar extends HTMLElement {
constructor() {
super()
const template = this.querySelector("template")
const attachedShadowRoot = this.attachShadow({ mode: "open" })
attachedShadowRoot.appendChild(template.content.cloneNode(true))
}
}

customElements.define("alchemy-menubar", Menubar)
30 changes: 17 additions & 13 deletions app/views/alchemy/_menubar.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<% if !@preview_mode && @page && can?(:edit_content, @page) %>
<div id="alchemy_menubar" style="display: none" data-turbo="false">
<ul>
<li><%= link_to Alchemy.t(:to_alchemy), alchemy.admin_dashboard_url %></li>
<li><%= link_to Alchemy.t(:edit_page), alchemy.edit_admin_page_url(@page) %></li>
<li>
<%= form_tag Alchemy.logout_path, method: Alchemy.logout_method do %>
<%= button_tag Alchemy.t(:logout) %>
<% end %>
</li>
</ul>
</div>
<alchemy-menubar>
<template>
<%= stylesheet_link_tag("alchemy/menubar") %>
<div id="alchemy_menubar" data-turbo="false">
<ul>
<li><%= link_to Alchemy.t(:to_alchemy), alchemy.admin_dashboard_url %></li>
<li><%= link_to Alchemy.t(:edit_page), alchemy.edit_admin_page_url(@page) %></li>
<li>
<%= form_tag Alchemy.logout_path, method: Alchemy.logout_method do %>
<%= button_tag Alchemy.t(:logout) %>
<% end %>
</li>
</ul>
</div>
</template>
</alchemy-menubar>

<%= stylesheet_link_tag("alchemy/menubar") %>
<%= javascript_include_tag('alchemy/menubar') %>
<%= javascript_include_tag('menubar', type: "module") %>
<% end %>
24 changes: 13 additions & 11 deletions spec/features/page_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,63 +68,65 @@
context "rendering for guest users" do
it "is prohibited" do
visit "/#{public_page.urlname}"
within("body") { expect(page).not_to have_selector("#alchemy_menubar") }
within("body") { expect(page).not_to have_selector("alchemy-menubar") }
end
end

context "rendering for members" do
it "is prohibited" do
authorize_user(build(:alchemy_dummy_user))
visit "/#{public_page.urlname}"
within("body") { expect(page).not_to have_selector("#alchemy_menubar") }
within("body") { expect(page).not_to have_selector("alchemy-menubar") }
end
end

context "rendering for authors" do
it "is allowed" do
authorize_user(:as_author)
visit "/#{public_page.urlname}"
within("body") { expect(page).to have_selector("#alchemy_menubar") }
within("body") { expect(page).to have_selector("alchemy-menubar") }
end
end

context "rendering for editors" do
it "is allowed" do
authorize_user(:as_editor)
visit "/#{public_page.urlname}"
within("body") { expect(page).to have_selector("#alchemy_menubar") }
within("body") { expect(page).to have_selector("alchemy-menubar") }
end
end

context "rendering for admins" do
it "is allowed" do
authorize_user(:as_admin)
visit "/#{public_page.urlname}"
within("body") { expect(page).to have_selector("#alchemy_menubar") }
within("body") { expect(page).to have_selector("alchemy-menubar") }
end
end

context "contains" do
context "contains", js: true do
let(:host) { "#{page.server.host}:#{page.server.port}" }

before do
authorize_user(:as_admin)
visit "/#{public_page.urlname}"
end

it "a link to the admin area" do
within("#alchemy_menubar") do
expect(page).to have_selector("li a[href='#{alchemy.admin_dashboard_url(host: Capybara.current_host)}']")
within find("alchemy-menubar").shadow_root do
expect(page).to have_selector("li a[href='#{alchemy.admin_dashboard_url(host: host)}']")
end
end

it "a link to edit the current page" do
within("#alchemy_menubar") do
within find("alchemy-menubar").shadow_root do
expect(page).to \
have_selector("li a[href='#{alchemy.edit_admin_page_url(public_page, host: Capybara.current_host)}']")
have_selector("li a[href='#{alchemy.edit_admin_page_url(public_page, host: host)}']")
end
end

it "a form and button to logout of alchemy" do
within("#alchemy_menubar") do
within find("alchemy-menubar").shadow_root do
expect(page).to \
have_selector("li form[action='#{Alchemy.logout_path}'][method='post']")
expect(page).to \
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

require "capybara/rails"
require "capybara-screenshot/rspec"
require "capybara/shadowdom"
require "rails-controller-testing"
require "rspec-activemodel-mocks"
require "rspec/rails"
Expand Down
Loading