Skip to content

Commit

Permalink
Add folding menus; improve configuration (#24)
Browse files Browse the repository at this point in the history
* Adds folding menus properly
* Improves configuration settings
  • Loading branch information
jfi authored Oct 10, 2024
1 parent eaf590b commit ba86716
Show file tree
Hide file tree
Showing 24 changed files with 258 additions and 191 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"titleBar.activeBackground": "#8d5eb7",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#8d5eb799",
"titleBar.inactiveForeground": "#e7e7e799",
"statusBar.background": "#8d5eb7"
"titleBar.inactiveForeground": "#e7e7e799"
}
}
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ PATH
awesome_nested_set (~> 3.7)
aws-sdk-s3 (~> 1)
danger (~> 9.5)
dry-configurable (~> 1.2)
faraday (~> 2)
faraday-multipart (~> 1.0)
faraday-retry (~> 2)
Expand All @@ -25,7 +26,10 @@ PATH
pg (~> 1.5)
rails (~> 7.2.1)
redcarpet (~> 3.6)
sentry-rails (~> 5)
sentry-ruby (~> 5)
silencer (~> 2.0)
stackprof (~> 0.2)
stimulus-rails (~> 1.3)
tailwindcss-rails (~> 2.7)
view_component (~> 3.14)
Expand Down Expand Up @@ -548,6 +552,12 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sentry-rails (5.21.0)
railties (>= 5.0)
sentry-ruby (~> 5.21.0)
sentry-ruby (5.21.0)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
sexp_processor (4.17.2)
shoulda-matchers (6.4.0)
activesupport (>= 5.2.0)
Expand All @@ -569,6 +579,7 @@ GEM
hashie
version_gem (~> 1.1, >= 1.1.1)
sorbet-runtime (0.5.11589)
stackprof (0.2.26)
standard (1.40.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
Expand Down
10 changes: 6 additions & 4 deletions app/components/panda_cms/page_menu_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<nav class="<%= @styles[:container] %>">
<nav class="<%= styles[:container] %>">
<ul role="list" class="p-0 m-0">
<li>
<a href="<%= menu_item.page.path %>" class="<%= menu_item.page == PandaCms::Current.page ? @styles[:current_page_active] : @styles[:current_page_inactive] %>">
<a href="<%= menu_item.page.path %>" class="<%= menu_item.page == PandaCms::Current.page ? styles[:current_page_active] : styles[:current_page_inactive] %>">
<%= menu_item.text %>
</a>
</li>
<ul>
<% PandaCms::MenuItem.each_with_level(menu_item.descendants) do |submenu_item, level| %>
<li data-level="<%= level %>" data-page-id="<%= submenu_item.page.id %>" class="<% if level > 1 && !PandaCms::Current.page.id.in?(submenu_item.page.self_and_ancestors.ids + submenu_item.page.siblings.ids) %>hidden <% end %> <%= submenu_item.page == PandaCms::Current.page ? @styles[:active] : @styles[:inactive] %>">
<% PandaCms::MenuItem.includes(:page).each_with_level(menu_item.descendants) do |submenu_item, level| %>
<% next if PandaCms::Current.page == menu_item.page && level > 1 # If we're on the "top" menu item, only show its direct ancestors %>
<% next if submenu_item.page.depth > PandaCms::Current.page.depth && !PandaCms::Current.page.in?(submenu_item.page.ancestors) %>
<li data-level="<%= level %>" data-page-id="<%= submenu_item.page.id %>" class="<%= submenu_item.page == PandaCms::Current.page ? @styles[:active] : @styles[:inactive] %>">
<a href="<%= submenu_item.page.path %>" class="<%= helpers.menu_indent(submenu_item, indent_with: @styles[:indent_with]) %>"><%= submenu_item.page.title %></a>
</li>
<% end %>
Expand Down
33 changes: 21 additions & 12 deletions app/components/panda_cms/page_menu_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@

module PandaCms
class PageMenuComponent < ViewComponent::Base
attr_accessor :page
attr_accessor :menu_item
attr_accessor :children
attr_accessor :styles

def initialize(page:, start_depth:, styles: {})
start_page = if page.depth == start_depth
page
else
page.ancestors.find { |anc| anc.depth == start_depth }
end
@page = page

unless @page.nil?
start_page = if @page.depth == start_depth
@page
else
@page.ancestors.find { |anc| anc.depth == start_depth }
end

menu = start_page&.page_menu
return if menu.nil?

menu = PandaCms::Menu.find_by(kind: "auto", start_page: start_page)
@menu_item = menu.menu_items.order(:lft)&.first unless menu.nil?
@menu_item = menu.menu_items.order(:lft)&.first

@children = menu_item&.descendants unless menu_item.nil?
# Set some default styles for sanity
@styles = styles
@styles[:indent_with] ||= "pl-2"
end
end

# Set some default styles for sanity
@styles = styles
@styles[:indent_with] ||= "pl-2"
def render?
@menu_item.present?
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/panda_cms/admin/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def show
private

def set_initial_breadcrumb
add_breadcrumb "Dashboard", PandaCms.admin_path
add_breadcrumb "Dashboard", PandaCms.route_namespace
end

def domain_expiry
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/panda_cms/admin/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ class SessionsController < ApplicationController
layout "panda_cms/public"

def new
@providers = PandaCms.authentication.select { |_, v| v[:enabled] && !v[:hidden] }.keys
@providers = PandaCms.config.authentication.select { |_, v| v[:enabled] && !v[:hidden] }.keys
end

def create
user_info = request.env.dig("omniauth.auth", "info")
provider = params[:provider].to_sym

unless PandaCms.authentication.dig(provider, :enabled)
unless PandaCms.config.authentication.dig(provider, :enabled)
Rails.logger.error "Authentication provider '#{provider}' is not enabled"
redirect_to admin_login_path, flash: {error: t("panda_cms.admin.sessions.create.error")}
return
end

user = PandaCms::User.find_by(email: user_info["email"])

if !user && PandaCms.authentication.dig(provider, :create_account_on_first_login)
create_as_admin = PandaCms.authentication.dig(provider, :create_as_admin)
if !user && PandaCms.config.authentication.dig(provider, :create_account_on_first_login)
create_as_admin = PandaCms.config.authentication.dig(provider, :create_as_admin)

# Always create the first user as admin, regardless of what our settings look like
# else we can't ever really login. :)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/panda_cms/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def set_current_request_details
PandaCms::Current.page = nil
PandaCms::Current.user ||= User.find_by(id: session[:user_id]) if session[:user_id]

PandaCms.url ||= PandaCms::Current.root
PandaCms.config.url ||= PandaCms::Current.root
end

def authenticate_user!
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/panda_cms/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def root
end

def show
if PandaCms.require_login_to_view && !user_signed_in?
if PandaCms.config.require_login_to_view && !user_signed_in?
redirect_to panda_cms_maintenance_path and return
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/panda_cms/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module PandaCms
class PostsController < ApplicationController
def show
@posts_index_page = PandaCms::Page.find_by(path: "/#{PandaCms.posts[:prefix]}")
@posts_index_page = PandaCms::Page.find_by(path: "/#{PandaCms.config.posts[:prefix]}")
@post = PandaCms::Post.find_by(slug: "/#{params[:slug]}")
@title = @post.title

Expand Down
4 changes: 2 additions & 2 deletions app/helpers/panda_cms/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def component(name, *, **, &)

def title_tag
if @breadcrumbs.present?
"#{@breadcrumbs.last&.name} &middot; #{PandaCms.title}".html_safe
"#{@breadcrumbs.last&.name} &middot; #{PandaCms.config.title}".html_safe
else
PandaCms.title
PandaCms.config.title
end
end

Expand Down
6 changes: 6 additions & 0 deletions app/javascript/panda_cms/controllers/slug_controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<<<<<<<< HEAD:app/javascript/panda_cms/controllers/slug_controller.js
import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
========
import { Controller as PandaCmsController } from "@hotwired/stimulus";

export class TextFieldUpdateController extends PandaCmsController {
>>>>>>>> 051dfc5 (Improve rich text editor support):app/javascript/panda_cms/controllers/text_field_update_controller.js
static targets = [
"existing_root",
"input_select",
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/panda_cms/panda_cms_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const panda_cms = PandaCmsApplication.start();
panda_cms.debug = false;
window.pandaStimulus = panda_cms;

// import { SlugController } from "./controllers/slug_controller";
// panda_cms.register("slug", SlugController);
import { SlugController } from "controllers/slug_controller";
panda_cms.register("slug", SlugController);

import {
Alert,
Expand Down
2 changes: 1 addition & 1 deletion app/models/panda_cms/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Page < ApplicationRecord
has_many :menu_items, foreign_key: :panda_cms_page_id, class_name: "PandaCms::MenuItem", inverse_of: :page
has_many :menus, through: :menu_items
has_many :menus_of_parent, through: :parent, source: :menus
has_one :page_menu, foreign_key: :panda_cms_menu_id, class_name: "PandaCms::Menu", inverse_of: :start_page
has_one :page_menu, foreign_key: :start_page_id, class_name: "PandaCms::Menu"

validates :title, presence: true
validates :path,
Expand Down
2 changes: 1 addition & 1 deletion app/models/panda_cms/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def excerpt(length = 100, squish: true)
end

def path
"/" + PandaCms.posts[:prefix] + slug.to_s
"/" + PandaCms.config.posts[:prefix] + slug.to_s
end

def formatted_slug
Expand Down
3 changes: 1 addition & 2 deletions app/views/panda_cms/admin/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
<img src="/panda-cms-assets/panda-nav.png" class="py-2 mx-auto w-auto h-32">
<h2 class="mt-10 mb-6 text-2xl font-bold text-center text-white"><%= t("panda_cms.admin.sessions.new.title") %></h2>
</div>

<% @providers.each do |provider| %>
<div class="mt-4 text-center sm:mx-auto sm:w-full sm:max-w-sm">
<%= form_tag "#{PandaCms.admin_path}/auth/#{provider}", method: "post", data: {turbo: false} do %>
<%= form_tag "#{PandaCms.route_namespace}/auth/#{provider}", method: "post", data: {turbo: false} do %>
<input type="hidden" name="redirect_uri" value="<%= admin_login_callback_url(provider: provider) %>">
<button type="submit" id="button-sign-in-<%= provider %>" class="inline-flex gap-x-2 items-center py-2.5 px-3.5 mx-auto mb-4 bg-white rounded-md border min-w-56 border-neutral-400">
<i class="fa-brands fa-<%= provider %> text-xl mr-1"></i>
Expand Down
22 changes: 9 additions & 13 deletions app/views/panda_cms/admin/shared/_sidebar.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<nav class="flex flex-col flex-1">
<ul role="list" class="flex flex-col flex-1">
<a class="block p-0 mt-4 mb-4 ml-2 text-xl font-medium text-white"><%= PandaCms.title %></a>

<a class="block p-0 mt-4 mb-4 ml-2 text-xl font-medium text-white"><%= PandaCms.config.title %></a>
<% [
{ path: admin_dashboard_path, text: "Dashboard", icon: "fa-house", matcher: :exact },
{ path: admin_pages_path, text: "Pages", icon: "fa-page", matcher: :starts_with },
Expand All @@ -10,32 +9,29 @@
{ path: admin_menus_path, text: "Menus", icon: "fa-list-dropdown", matcher: :starts_with },
{ path: admin_settings_path, text: "Settings", icon: "fa-cog", matcher: :starts_with },
].each do |link| %>
<li>
<%= link_to link[:path], class: "#{active_link?(link[:path], match: link[:matcher]) ? selected_nav_highlight_colour_classes(request) : nav_highlight_colour_classes(request)}" do %>
<span class="text-center"><i class="fa-regular <%= link[:icon] %> text-xl fa-fw"></i></span>
<span class="mt-1"><%= link[:text] %></span>
<% end %>
</li>
<li>
<%= link_to link[:path], class: "#{active_link?(link[:path], match: link[:matcher]) ? selected_nav_highlight_colour_classes(request) : nav_highlight_colour_classes(request)}" do %>
<span class="text-center"><i class="fa-regular <%= link[:icon] %> text-xl fa-fw"></i></span>
<span class="mt-1"><%= link[:text] %></span>
<% end %>
</li>
<% end %>
<li>
<%= button_to admin_logout_path, method: :delete, data: { turbo: false }, class: nav_highlight_colour_classes(request) + " w-full" do %>
<span class="text-center"><i class="text-xl fa-regular fa-door-open fa-fw"></i></span>
<span class="mt-1">Sign Out</span>
<% end %>
</li>

<li class="mt-auto">
<%= button_to '#', data: { turbo: false }, class: nav_highlight_colour_classes(request) + " w-full" do %>
<% if !current_user.image_url.empty? %>
<span class="text-center"><img src="<%= current_user.image_url %>" class="w-auto h-7 rounded-full"></span>
<span class="text-center"><img src="<%= current_user.image_url %>" class="w-auto h-7 rounded-full"></span>
<% else %>
<span class="text-center"><i class="text-xl fa-regular fa-circle-user fa-fw"></i></span>
<span class="text-center"><i class="text-xl fa-regular fa-circle-user fa-fw"></i></span>
<% end %>

<span class="mt-0.5"><%= current_user.firstname %> <%= current_user.lastname %></span>
<% end %>
</li>

<li class="">
<img src="/panda-cms-assets/panda-nav.png" class="inline py-2 mr-1 ml-0.5 w-auto h-14">
<span class="text-xs text-white">v<%= PandaCms::VERSION %> | <span class="font-semibold">License:</span> Perpetual</span>
Expand Down
Loading

0 comments on commit ba86716

Please sign in to comment.