Files
compiled.world/lib/CW/Layout/Default.pm
2026-08-22 23:59:41 -06:00

146 lines
4.5 KiB
Perl

package CW::Layout::Default;
use Moo;
extends 'CW::Layout';
my $HACKERSPACE_LINK = "https://en.wikipedia.org/wiki/Hackerspace";
sub render {
my ($self) = @_;
my $ctx = $self->ctx // {};
if ( !$ctx->{nav} ) {
$ctx->{nav} = [];
my @path = split '/', $ctx->{env}->{PATH_INFO};
if (@path) {
my $nav = [];
for ( 0 .. $#path ) {
my $text = $path[$_];
my @prev = @path[ 0 .. $_ ];
push @$nav,
{
text => $text,
path => '/' . join( '/', ( @prev, $text ) )
};
}
}
}
my @additional_links;
if ( !$ctx->{user} ) {
push @additional_links, $self->_make_nav_link("login");
}
else {
if ( $ctx->{user}->is_admin ) {
push @additional_links, $self->_make_nav_link("admin");
}
push @additional_links, $self->_make_nav_link("logout");
}
my $board = 'cw';
if ( $ctx->{board} ) {
$board = $ctx->{board}->{id};
}
return [
head => [
title => [ $self->ctx->{title} ],
meta => { charset => 'UTF-8' },
meta => {
name => 'viewport',
content => 'width=device-width, initial-scale=1.0'
},
meta => {
name => 'description',
content => (
$self->ctx->{description}
// 'compiled.world, a gated hackerspace '
. 'for humans to interact with humans'
)
},
meta => {
name => 'robots',
content => 'index, follow'
},
link => {
href => '/static/css/shared.css',
rel => 'stylesheet'
},
link => {
href => '/static/css/cw.css',
rel => 'stylesheet'
}
],
body => [
header => { style => "margin-top: 1rem" } => [
div => [
div => {
class => "header-wrapper"
} => [
img => {
src => "/static/images/cw.svg",
class => "logo"
},
div => [
a => {
href => "/",
style => "text-decoration: none"
} => [
h1 => { style => "margin: 0" } =>
["compiled.world"]
],
p => { style => "margin-top: 0" } => [
"A digital ",
a => {
href => $HACKERSPACE_LINK
} => ["hackerspace"],
" for human beings"
],
nav => { class => "navbar" } => [
(
map { $self->_make_nav_link($_) }
qw(recent popular badges about)
),
@additional_links,
]
]
],
]
],
hr => { style => 'margin: 1rem; display: block;' },
main => [
(
@{ $ctx->{nav} }
? (
nav => { class => "quicknav" } => [
map {
(
li => [
"/",
a => { href => $_->{path} } =>
[ $_->{text} ]
]
)
} @{ $ctx->{nav} }
]
)
: ()
),
div => { class => "main-content-wrapper" } =>
$self->template->render
],
footer => []
]
];
}
sub _make_nav_link {
my ( $self, $text ) = @_;
return a => { class => "navbar-link", href => "/$text" } => [$text];
}
1;