Skip to content

Commit 2d85bd2

Browse files
committed
fix
1 parent f3364ec commit 2d85bd2

File tree

5 files changed

+59
-65
lines changed

5 files changed

+59
-65
lines changed

routers/common/pagetmpl.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models/db"
1212
issues_model "code.gitea.io/gitea/models/issues"
1313
"code.gitea.io/gitea/modules/log"
14+
"code.gitea.io/gitea/modules/util"
1415
"code.gitea.io/gitea/services/context"
1516
)
1617

@@ -22,8 +23,7 @@ type StopwatchTmplInfo struct {
2223
Seconds int64
2324
}
2425

25-
func getActiveStopwatch(goCtx goctx.Context) *StopwatchTmplInfo {
26-
ctx := context.GetWebContext(goCtx)
26+
func getActiveStopwatch(ctx *context.Context) *StopwatchTmplInfo {
2727
if ctx.Doer == nil {
2828
return nil
2929
}
@@ -48,8 +48,7 @@ func getActiveStopwatch(goCtx goctx.Context) *StopwatchTmplInfo {
4848
}
4949
}
5050

51-
func notificationUnreadCount(goCtx goctx.Context) int64 {
52-
ctx := context.GetWebContext(goCtx)
51+
func notificationUnreadCount(ctx *context.Context) int64 {
5352
if ctx.Doer == nil {
5453
return 0
5554
}
@@ -66,10 +65,32 @@ func notificationUnreadCount(goCtx goctx.Context) int64 {
6665
return count
6766
}
6867

69-
func PageTmplFunctions(ctx *context.Context) {
70-
if ctx.IsSigned {
71-
// defer the function call to the last moment when the tmpl renders
72-
ctx.Data["NotificationUnreadCount"] = notificationUnreadCount
73-
ctx.Data["GetActiveStopwatch"] = getActiveStopwatch
68+
type pageHeadNavbarDataType struct {
69+
IsSigned bool
70+
IsSiteAdmin bool
71+
72+
GetNotificationUnreadCount func() int64
73+
cachedNotificationUnreadCount *int64
74+
75+
GetActiveStopwatch func() *StopwatchTmplInfo
76+
cachedActiveStopwatch **StopwatchTmplInfo
77+
}
78+
79+
func PageHeadNavbarData(ctx *context.Context) {
80+
var data pageHeadNavbarDataType
81+
data.IsSigned = ctx.Doer != nil
82+
data.IsSiteAdmin = ctx.Doer != nil && ctx.Doer.IsAdmin
83+
data.GetNotificationUnreadCount = func() int64 {
84+
if data.cachedNotificationUnreadCount == nil {
85+
data.cachedNotificationUnreadCount = util.ToPointer(notificationUnreadCount(ctx))
86+
}
87+
return *data.cachedNotificationUnreadCount
88+
}
89+
data.GetActiveStopwatch = func() *StopwatchTmplInfo {
90+
if data.cachedActiveStopwatch == nil {
91+
data.cachedActiveStopwatch = util.ToPointer(getActiveStopwatch(ctx))
92+
}
93+
return *data.cachedActiveStopwatch
7494
}
95+
ctx.Data["HeadNavbarData"] = data
7596
}

routers/web/web.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func Routes() *web.Router {
281281
}
282282

283283
mid = append(mid, goGet)
284-
mid = append(mid, common.PageTmplFunctions)
284+
mid = append(mid, common.PageHeadNavbarData)
285285

286286
webRoutes := web.NewRouter()
287287
webRoutes.Use(mid...)

templates/base/head_navbar.tmpl

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
{{$notificationUnreadCount := 0}}
2-
{{if and .IsSigned .NotificationUnreadCount}}
3-
{{$notificationUnreadCount = call .NotificationUnreadCount ctx}}
4-
{{end}}
5-
{{$activeStopwatch := NIL}}
6-
{{if and .IsSigned EnableTimetracking .GetActiveStopwatch}}
7-
{{$activeStopwatch = call .GetActiveStopwatch ctx}}
8-
{{end}}
91
<nav id="navbar" aria-label="{{ctx.Locale.Tr "aria.navbar"}}">
102
<div class="navbar-left">
113
<!-- the logo -->
@@ -15,22 +7,7 @@
157

168
<!-- mobile right menu, it must be here because in mobile view, each item is a flex column, the first item is a full row column -->
179
<div class="ui secondary menu navbar-mobile-right only-mobile">
18-
{{if $activeStopwatch}}
19-
<a id="mobile-stopwatch-icon" class="active-stopwatch item" href="{{$activeStopwatch.IssueLink}}" title="{{ctx.Locale.Tr "active_stopwatch"}}" data-seconds="{{$activeStopwatch.Seconds}}">
20-
<div class="tw-relative">
21-
{{svg "octicon-stopwatch"}}
22-
<span class="header-stopwatch-dot"></span>
23-
</div>
24-
</a>
25-
{{end}}
26-
{{if .IsSigned}}
27-
<a id="mobile-notifications-icon" class="item" href="{{AppSubUrl}}/notifications" data-tooltip-content="{{ctx.Locale.Tr "notifications"}}" aria-label="{{ctx.Locale.Tr "notifications"}}">
28-
<div class="tw-relative">
29-
{{svg "octicon-bell"}}
30-
<span class="notification_count{{if not $notificationUnreadCount}} tw-hidden{{end}}">{{$notificationUnreadCount}}</span>
31-
</div>
32-
</a>
33-
{{end}}
10+
{{template "base/head_navbar_icons" dict "HeadNavbarData" .HeadNavbarData}}
3411
<button class="item ui icon mini button tw-m-0" id="navbar-expand-toggle" aria-label="{{ctx.Locale.Tr "home.nav_menu"}}">{{svg "octicon-three-bars"}}</button>
3512
</div>
3613

@@ -85,22 +62,7 @@
8562
</div><!-- end content avatar menu -->
8663
</div><!-- end dropdown avatar menu -->
8764
{{else if .IsSigned}}
88-
{{if $activeStopwatch}}
89-
<a class="item not-mobile active-stopwatch" href="{{$activeStopwatch.IssueLink}}" title="{{ctx.Locale.Tr "active_stopwatch"}}" data-seconds="{{$activeStopwatch.Seconds}}">
90-
<div class="tw-relative">
91-
{{svg "octicon-stopwatch"}}
92-
<span class="header-stopwatch-dot"></span>
93-
</div>
94-
</a>
95-
{{end}}
96-
97-
<a class="item not-mobile" href="{{AppSubUrl}}/notifications" data-tooltip-content="{{ctx.Locale.Tr "notifications"}}" aria-label="{{ctx.Locale.Tr "notifications"}}">
98-
<div class="tw-relative">
99-
{{svg "octicon-bell"}}
100-
<span class="notification_count{{if not $notificationUnreadCount}} tw-hidden{{end}}">{{$notificationUnreadCount}}</span>
101-
</div>
102-
</a>
103-
65+
{{template "base/head_navbar_icons" dict "ItemExtraClass" "not-mobile" "HeadNavbarData" .HeadNavbarData}}
10466
<div class="ui dropdown jump item" data-tooltip-content="{{ctx.Locale.Tr "create_new"}}">
10567
<span class="text">
10668
{{svg "octicon-plus"}}
@@ -130,8 +92,6 @@
13092
<span class="only-mobile">{{.SignedUser.Name}}</span>
13193
<span class="not-mobile">{{svg "octicon-triangle-down"}}</span>
13294
</span>
133-
{{/* do not localize it, here it needs the fixed length (width) to make UI comfortable */}}
134-
{{if .IsAdmin}}<span class="navbar-profile-admin">admin</span>{{end}}
13595
<div class="menu user-menu">
13696
<div class="header">
13797
{{ctx.Locale.Tr "signed_in_as"}} <strong>{{.SignedUser.Name}}</strong>
@@ -189,6 +149,7 @@
189149
{{end}}
190150
</div><!-- end full right menu -->
191151

152+
{{$activeStopwatch := call .HeadNavbarData.GetActiveStopwatch}}
192153
{{if $activeStopwatch}}
193154
<div class="active-stopwatch-popup tippy-target">
194155
<div class="tw-flex tw-items-center tw-gap-2 tw-p-3">

templates/base/head_navbar_icons.tmpl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{{$itemExtraClass := .ItemExtraClass}}
2+
{{$data := .HeadNavbarData}}
3+
{{if $data.IsSigned}}
4+
{{$activeStopwatch := call $data.GetActiveStopwatch}}
5+
{{$notificationUnreadCount := call $data.GetNotificationUnreadCount}}
6+
{{if $activeStopwatch}}
7+
<a class="item active-stopwatch {{$itemExtraClass}}" href="{{$activeStopwatch.IssueLink}}" title="{{ctx.Locale.Tr "active_stopwatch"}}" data-seconds="{{$activeStopwatch.Seconds}}">
8+
<div class="tw-relative">
9+
{{svg "octicon-stopwatch"}}
10+
<span class="header-stopwatch-dot"></span>
11+
</div>
12+
</a>
13+
{{end}}
14+
<a class="item {{$itemExtraClass}}" href="{{AppSubUrl}}/notifications" data-tooltip-content="{{ctx.Locale.Tr "notifications"}}">
15+
<div class="tw-relative">
16+
{{svg "octicon-bell"}}
17+
<span class="notification_count{{if not $notificationUnreadCount}} tw-hidden{{end}}">{{$notificationUnreadCount}}</span>
18+
</div>
19+
</a>
20+
{{if $data.IsSiteAdmin}}
21+
<a class="item {{$itemExtraClass}}" href="{{AppSubUrl}}/-/admin" data-tooltip-content="{{ctx.Locale.Tr "admin_panel"}}">
22+
{{svg "octicon-server"}}
23+
</a>
24+
{{end}}
25+
{{end}}

web_src/css/modules/navbar.css

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,6 @@
101101
}
102102
}
103103

104-
#navbar .ui.dropdown .navbar-profile-admin {
105-
display: block;
106-
position: absolute;
107-
font-size: 9px;
108-
font-weight: var(--font-weight-bold);
109-
color: var(--color-nav-bg);
110-
background: var(--color-primary);
111-
padding: 2px 3px;
112-
border-radius: 10px;
113-
top: -1px;
114-
left: 18px;
115-
}
116-
117104
#navbar a.item:hover .notification_count,
118105
#navbar a.item:hover .header-stopwatch-dot {
119106
border-color: var(--color-nav-hover-bg);

0 commit comments

Comments
 (0)