-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
93 lines (74 loc) · 1.97 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package mtweb
import (
"time"
"github.com/mitoteam/dhtml"
)
// Count label
func RenderCount(count int64, title string) (out dhtml.HtmlPiece) {
out.Append(
Icon("abacus").Label(count).ElementClass("p-1 border border-dark-subtle fw-bold bg-info-subtle").Title(title),
)
return out
}
// Bootstrap styled table
func NewTable() *dhtml.TableElement {
return dhtml.NewTable().Class("table table-hover table-sm").
BodyClass("table-group-divider").
EmptyLabel("nothing here yet")
}
// Table count label
func RenderTableCount(table *dhtml.TableElement, title string) (out dhtml.HtmlPiece) {
cnt := int64(table.RowCount())
if cnt > 0 {
out.Append(
dhtml.Div().Class("text-end").Append(RenderCount(cnt, title)),
)
}
return out
}
// ================== timestamp element ====================
// Clock icon with datetime
type TimestampElement struct {
format string
icon string
ts time.Time
classes dhtml.Classes
}
// force interface implementation declaring fake variable
var _ dhtml.ElementI = (*TimestampElement)(nil)
const IconTimestamp = "clock"
func NewTimestamp(ts time.Time) *TimestampElement {
return &TimestampElement{
ts: ts,
icon: IconTimestamp,
format: time.DateTime,
}
}
// Element classes.
func (e *TimestampElement) Class(v ...any) *TimestampElement {
e.classes.Add(v...)
return e
}
func (e *TimestampElement) SmallMuted() *TimestampElement {
e.classes.Add("small", "text-muted")
return e
}
// Set date and time format (default is time.DateTime).
func (e *TimestampElement) Format(tsFormat string) *TimestampElement {
e.format = tsFormat
return e
}
// icon name. empty = no icon. default "clock".
func (e *TimestampElement) Icon(icon string) *TimestampElement {
e.icon = icon
return e
}
func (e *TimestampElement) GetTags() dhtml.TagList {
rootTag := dhtml.Div().Class(e.classes)
if e.icon != "" {
rootTag.Append(Icon(e.icon).Label(e.ts.Format(e.format)))
} else {
rootTag.Append(e.ts.Format(e.format))
}
return rootTag.GetTags()
}