Skip to content

Commit

Permalink
Merge pull request #382 from jovandeginste/better-options
Browse files Browse the repository at this point in the history
Improve the page options for scripts and stylesheets
  • Loading branch information
jovandeginste authored Jan 6, 2025
2 parents a9d1135 + a57844a commit 1195f71
Show file tree
Hide file tree
Showing 44 changed files with 202 additions and 203 deletions.
2 changes: 1 addition & 1 deletion views/admin/edit_user.templ
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

templ EditUser(u *database.User) {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<h2>
@helpers.IconFor(`admin`)
{ i18n.T(ctx, "Manage user '%s'", u.Name) }
Expand Down
2 changes: 1 addition & 1 deletion views/admin/edit_user_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/admin/root.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

templ Root(users []*database.User) {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<h2>
@helpers.IconFor(`admin`)
{ i18n.T(ctx, "Manage") }
Expand Down
2 changes: 1 addition & 1 deletion views/admin/root_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/equipment/add.templ
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

templ Add() {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<h2>
@helpers.IconFor("equipment-add")
{ i18n.T(ctx, "Add equipment") }
Expand Down
2 changes: 1 addition & 1 deletion views/equipment/add_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/equipment/edit.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

templ Edit(e *database.Equipment) {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<div class="gap-4">
<h2>{ i18n.T(ctx, "Equipment") }: { e.Name }</h2>
</div>
Expand Down
2 changes: 1 addition & 1 deletion views/equipment/edit_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/equipment/list.templ
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

templ List(equipment []*database.Equipment) {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<div class="items-baseline flex flex-wrap">
<h2 class="grow justify-start">
@helpers.IconFor("equipment")
Expand Down
2 changes: 1 addition & 1 deletion views/equipment/list_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/equipment/show.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

templ Show(e *database.Equipment) {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
{{ currentUser := helpers.CurrentUser(ctx) }}
{{ pu := currentUser.PreferredUnits() }}
<div class="gap-4">
Expand Down
2 changes: 1 addition & 1 deletion views/equipment/show_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions views/partials/page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package partials

type PageOptions struct {
Scripts, Styles []string
}

func NewPageOptions() *PageOptions {
return &PageOptions{Scripts: nil, Styles: nil}
}

func (o *PageOptions) WithScripts(scripts ...string) *PageOptions {
return &PageOptions{
Scripts: append(o.Scripts, scripts...),
Styles: o.Styles,
}
}

func (o *PageOptions) WithStyles(styles ...string) *PageOptions {
return &PageOptions{
Scripts: o.Scripts,
Styles: append(o.Styles, styles...),
}
}

func (o *PageOptions) WithCharts() *PageOptions {
return o.
WithScripts("/dist/apexcharts.min.js").
WithStyles("/dist/apexcharts.css")
}

func (o *PageOptions) WithMaps() *PageOptions {
return o.
WithScripts("/dist/leaflet.js").
WithStyles("/dist/leaflet.css")
}

func (o *PageOptions) WithHeatMaps() *PageOptions {
return o.WithMaps().
WithScripts(
"/dist/simpleheat.js",
"/dist/leaflet-heat.js",
"/dist/leaflet.markercluster.js",
).
WithStyles(
"/dist/MarkerCluster.css",
"/dist/MarkerCluster.Default.css",
)
}

func (o *PageOptions) WithSharing() *PageOptions {
return o.
WithScripts("/dist/shareon.iife.js").
WithStyles("/dist/shareon.min.css")
}
8 changes: 1 addition & 7 deletions views/partials/page.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@ package partials

import "github.com/jovandeginste/workout-tracker/views/helpers"

type PageOptions struct {
Scripts []string
Styles []string
}

templ Page(options PageOptions) {
templ Page(options *PageOptions) {
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
for _, script := range options.Scripts {
<script src={ helpers.RouteFor(ctx, "assets") + script }></script>
}
Expand Down
13 changes: 4 additions & 9 deletions views/partials/page_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions views/partials/page_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package partials

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewPageOptions(t *testing.T) {
po := NewPageOptions()

assert.Empty(t, po.Scripts)
assert.Empty(t, po.Styles)
}

func TestPageOptions_WithScripts(t *testing.T) {
po := NewPageOptions().WithScripts("/a.js", "/b.js")

assert.Contains(t, po.Scripts, "/a.js")
assert.Contains(t, po.Scripts, "/b.js")
}

func TestPageOptions_WithStyles(t *testing.T) {
po := NewPageOptions().WithStyles("/a.css", "/b.css")

assert.Contains(t, po.Styles, "/a.css")
assert.Contains(t, po.Styles, "/b.css")
}

func TestPageOptions_WithScriptsAndStyles(t *testing.T) {
po := NewPageOptions().
WithScripts("/a.js", "/b.js").
WithStyles("/a.css", "/b.css")

assert.Contains(t, po.Scripts, "/a.js")
assert.Contains(t, po.Scripts, "/b.js")

assert.Contains(t, po.Styles, "/a.css")
assert.Contains(t, po.Styles, "/b.css")
}
2 changes: 1 addition & 1 deletion views/route_segments/add.templ
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

templ Add() {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<h2>
@helpers.IconFor("route-segment-add")
{ i18n.T(ctx, "Add route segment") }
Expand Down
2 changes: 1 addition & 1 deletion views/route_segments/add_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/route_segments/edit.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

templ Edit(s *database.RouteSegment) {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<div class="gap-4">
<h2>
@helpers.IconFor("route-segment")
Expand Down
2 changes: 1 addition & 1 deletion views/route_segments/edit_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion views/route_segments/list.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

templ List(segments []*database.RouteSegment) {
@partials.Page(partials.PageOptions{}) {
@partials.Page(partials.NewPageOptions()) {
<div class="items-baseline flex flex-wrap">
<h2 class="grow justify-start">
@helpers.IconFor("route-segment")
Expand Down
2 changes: 1 addition & 1 deletion views/route_segments/list_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions views/route_segments/show.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import (
)

templ Show(s *database.RouteSegment) {
@partials.Page(partials.PageOptions{
Scripts: []string{
"/dist/leaflet.js",
},
Styles: []string{
"/dist/leaflet.css",
},
}) {
@partials.Page(partials.NewPageOptions().WithMaps()) {
{{ preferredUnits := helpers.CurrentUser(ctx).PreferredUnits() }}
<div class="gap-4">
<span class="float-right actions">
Expand Down
Loading

0 comments on commit 1195f71

Please sign in to comment.