Update header template and add option for custom css

This commit is contained in:
r 2019-12-25 16:38:47 +00:00
parent bde2c03495
commit 656ff3931c
16 changed files with 90 additions and 47 deletions

View File

@ -16,6 +16,7 @@ type config struct {
StaticDirectory string
TemplatesGlobPattern string
DatabasePath string
CustomCSS string
Logfile string
}
@ -41,6 +42,7 @@ func getDefaultConfig() *config {
StaticDirectory: "static",
TemplatesGlobPattern: "templates/*",
DatabasePath: "database.db",
CustomCSS: "",
Logfile: "",
}
}
@ -83,6 +85,8 @@ func Parse(r io.Reader) (c *config, err error) {
c.TemplatesGlobPattern = val
case "database_path":
c.DatabasePath = val
case "custom_css":
c.CustomCSS = val
case "logfile":
c.Logfile = val
default:

View File

@ -4,4 +4,5 @@ client_scope=read write follow
client_website=http://localhost:8080
static_directory=static
templates_glob_pattern=templates/*
#custom_css=custom.css
database_path=database

View File

@ -6,6 +6,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"
"web/config"
@ -52,6 +53,12 @@ func main() {
sessionRepo := repository.NewSessionRepository(sessionDB)
appRepo := repository.NewAppRepository(appDB)
customCSS := config.CustomCSS
if !strings.HasPrefix(customCSS, "http://") &&
!strings.HasPrefix(customCSS, "https://") {
customCSS = "/static/" + customCSS
}
var logger *log.Logger
if len(config.Logfile) < 1 {
logger = log.New(os.Stdout, "", log.LstdFlags)
@ -64,7 +71,7 @@ func main() {
logger = log.New(lf, "", log.LstdFlags)
}
s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, renderer, sessionRepo, appRepo)
s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, customCSS, renderer, sessionRepo, appRepo)
s = service.NewAuthService(sessionRepo, appRepo, s)
s = service.NewLoggingService(logger, s)
handler := service.NewHandler(s, config.StaticDirectory)

View File

@ -5,12 +5,24 @@ import (
"web/model"
)
type HeaderData struct {
Title string
NotificationCount int
CustomCSS string
}
type NavbarData struct {
User *mastodon.Account
NotificationCount int
}
type CommonData struct {
HeaderData *HeaderData
NavbarData *NavbarData
}
type TimelineData struct {
*CommonData
Title string
Statuses []*mastodon.Status
HasNext bool
@ -18,36 +30,35 @@ type TimelineData struct {
HasPrev bool
PrevLink string
PostContext model.PostContext
NavbarData *NavbarData
}
type ThreadData struct {
*CommonData
Statuses []*mastodon.Status
PostContext model.PostContext
ReplyMap map[string][]mastodon.ReplyInfo
NavbarData *NavbarData
}
type NotificationData struct {
*CommonData
Notifications []*mastodon.Notification
HasNext bool
NextLink string
NavbarData *NavbarData
}
type UserData struct {
*CommonData
User *mastodon.Account
Statuses []*mastodon.Status
HasNext bool
NextLink string
NavbarData *NavbarData
}
type AboutData struct {
NavbarData *NavbarData
*CommonData
}
type EmojiData struct {
Emojis []*mastodon.Emoji
NavbarData *NavbarData
CommonData *CommonData
}

View File

@ -50,18 +50,20 @@ type service struct {
clientName string
clientScope string
clientWebsite string
customCSS string
renderer renderer.Renderer
sessionRepo model.SessionRepository
appRepo model.AppRepository
}
func NewService(clientName string, clientScope string, clientWebsite string,
renderer renderer.Renderer, sessionRepo model.SessionRepository,
customCSS string, renderer renderer.Renderer, sessionRepo model.SessionRepository,
appRepo model.AppRepository) Service {
return &service{
clientName: clientName,
clientScope: clientScope,
clientWebsite: clientWebsite,
customCSS: customCSS,
renderer: renderer,
sessionRepo: sessionRepo,
appRepo: appRepo,
@ -272,7 +274,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
DefaultVisibility: c.Session.Settings.DefaultVisibility,
}
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
@ -285,7 +287,7 @@ func (svc *service) ServeTimelinePage(ctx context.Context, client io.Writer,
HasPrev: hasPrev,
PrevLink: prevLink,
PostContext: postContext,
NavbarData: navbarData,
CommonData: commonData,
}
err = svc.renderer.RenderTimelinePage(ctx, client, data)
@ -349,7 +351,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
addToReplyMap(replyMap, statuses[i].InReplyToID, statuses[i].ID, i+1)
}
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
@ -358,7 +360,7 @@ func (svc *service) ServeThreadPage(ctx context.Context, client io.Writer, c *mo
Statuses: statuses,
PostContext: postContext,
ReplyMap: replyMap,
NavbarData: navbarData,
CommonData: commonData,
}
err = svc.renderer.RenderThreadPage(ctx, client, data)
@ -409,7 +411,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer,
nextLink = "/notifications?max_id=" + pg.MaxID
}
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
@ -418,7 +420,7 @@ func (svc *service) ServeNotificationPage(ctx context.Context, client io.Writer,
Notifications: notifications,
HasNext: hasNext,
NextLink: nextLink,
NavbarData: navbarData,
CommonData: commonData,
}
err = svc.renderer.RenderNotificationPage(ctx, client, data)
if err != nil {
@ -453,7 +455,7 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
nextLink = "/user/" + id + "?max_id=" + pg.MaxID
}
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
@ -463,7 +465,7 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
Statuses: statuses,
HasNext: hasNext,
NextLink: nextLink,
NavbarData: navbarData,
CommonData: commonData,
}
err = svc.renderer.RenderUserPage(ctx, client, data)
@ -475,13 +477,13 @@ func (svc *service) ServeUserPage(ctx context.Context, client io.Writer, c *mode
}
func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
data := &renderer.AboutData{
NavbarData: navbarData,
CommonData: commonData,
}
err = svc.renderer.RenderAboutPage(ctx, client, data)
if err != nil {
@ -492,7 +494,7 @@ func (svc *service) ServeAboutPage(ctx context.Context, client io.Writer, c *mod
}
func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *model.Client) (err error) {
navbarData, err := svc.getNavbarTemplateData(ctx, client, c)
commonData, err := svc.getCommonData(ctx, client, c)
if err != nil {
return
}
@ -504,7 +506,7 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
data := &renderer.EmojiData{
Emojis: emojis,
NavbarData: navbarData,
CommonData: commonData,
}
err = svc.renderer.RenderEmojiPage(ctx, client, data)
@ -515,10 +517,19 @@ func (svc *service) ServeEmojiPage(ctx context.Context, client io.Writer, c *mod
return
}
func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.NavbarData, err error) {
func (svc *service) getCommonData(ctx context.Context, client io.Writer, c *model.Client) (data *renderer.CommonData, err error) {
data = new(renderer.CommonData)
data.HeaderData = &renderer.HeaderData{
Title: "Web",
NotificationCount: 0,
CustomCSS: svc.customCSS,
}
if c != nil && c.Session.IsLoggedIn() {
notifications, err := c.GetNotifications(ctx, nil)
if err != nil {
return
return nil, err
}
var notificationCount int
@ -530,14 +541,17 @@ func (svc *service) getNavbarTemplateData(ctx context.Context, client io.Writer,
u, err := c.GetAccountCurrentUser(ctx)
if err != nil {
return
return nil, err
}
data = &renderer.NavbarData{
data.NavbarData = &renderer.NavbarData{
User: u,
NotificationCount: notificationCount,
}
data.HeaderData.NotificationCount = notificationCount
}
return
}

3
static/custom.css Normal file
View File

@ -0,0 +1,3 @@
html {
background: #000000;
}

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> About </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Emojis </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
<div class="page-title"> Error </div>
<div class="error-text"> {{.}} </div>
<div>

View File

@ -3,8 +3,11 @@
<head>
<meta charset='utf-8'>
<meta content='width=device-width, initial-scale=1' name='viewport'>
<title> Web </title>
<link rel="stylesheet" href="/static/main.css" />
<title>{{if gt .NotificationCount 0}}({{.NotificationCount}}) {{end}}{{.Title}}</title>
<link rel="stylesheet" href="/static/main.css">
{{if .CustomCSS}}
<link rel="stylesheet" href="{{.CustomCSS}}">
{{end}}
<link rel="stylesheet" href="/static/fonts/fork-awesome.css">
</head>
<body>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
<div class="page-title"> Home </div>
<a href="/signin"> Signin </a>
{{template "footer.tmpl"}}

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Notifications </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
<div class="page-title"> Signin </div>
<form class="signin-form" action="/signin" method="post">

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> Thread </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> {{.Title}} </div>

View File

@ -1,4 +1,4 @@
{{template "header.tmpl"}}
{{template "header.tmpl" .HeaderData}}
{{template "navigation.tmpl" .NavbarData}}
<div class="page-title"> User </div>