-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist.go
99 lines (92 loc) · 3.76 KB
/
list.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
94
95
96
97
98
99
package view
import (
"fmt"
"strings"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/file"
"projectforge.dev/projectforge/app/project/export/files/helper"
"projectforge.dev/projectforge/app/project/export/golang"
"projectforge.dev/projectforge/app/project/export/model"
)
func list(m *model.Model, args *model.Args, addHeader bool, linebreak string) (*file.File, error) {
g := golang.NewGoTemplate([]string{"views", m.PackageWithGroup("v")}, "List.html")
g.AddImport(helper.ImpApp, helper.ImpComponents, helper.ImpCutil, helper.ImpFilter, helper.ImpLayout)
if m.HasSearches() {
g.AddImport(helper.ImpComponentsEdit)
}
if m.HasTag("count") {
g.AddImport(helper.ImpAppUtil)
}
g.AddImport(helper.AppImport(m.PackageWithGroup("")))
g.AddBlocks(exportViewListClass(m, args.Models, g), exportViewListBody(m, args.Models))
return g.Render(addHeader, linebreak)
}
func exportViewListClass(m *model.Model, models model.Models, g *golang.Template) *golang.Block {
ret := golang.NewBlock("List", "struct")
ret.W("{%% code type List struct {")
ret.W(" layout.Basic")
ret.W(" Models %s.%s", m.Package, m.ProperPlural())
lo.ForEach(m.Relations, func(rel *model.Relation, _ int) {
relModel := models.Get(rel.Table)
relCols := rel.SrcColumns(m)
relNames := strings.Join(relCols.ProperNames(), "")
g.AddImport(helper.AppImport(relModel.PackageWithGroup("")))
ret.W(commonLine, relModel.ProperPlural(), relNames, relModel.Package, relModel.ProperPlural())
})
ret.W(" Params filter.ParamSet")
if m.HasSearches() {
ret.W(" SearchQuery string")
}
if m.HasTag("count") {
ret.W(" Count int")
}
ret.W("} %%}")
return ret
}
func exportViewListBody(m *model.Model, models model.Models) *golang.Block {
ret := golang.NewBlock("ListBody", "func")
suffix := ""
lo.ForEach(m.Relations, func(rel *model.Relation, _ int) {
if relModel := models.Get(rel.Table); relModel.CanTraverseRelation() {
relCols := rel.SrcColumns(m)
relNames := strings.Join(relCols.ProperNames(), "")
suffix += fmt.Sprintf(", p.%sBy%s", relModel.ProperPlural(), relNames)
}
})
ret.W("{%% func (p *List) Body(as *app.State, ps *cutil.PageState) %%}")
ret.W(" <div class=\"card\">")
const twoInd = " "
if m.HasSearches() {
ret.W(` <div class="right">{%%%%= edit.SearchForm("", "q", "Search %s", p.SearchQuery, ps) %%%%}</div>`, m.TitlePlural())
ret.W(` <div class="right mrs large-buttons">`)
ret.W(` {%%%%- if len(p.Models) > 0 -%%%%}<a href="/%s/_random"><button>Random</button></a>{%%%%- endif -%%%%}`, m.Route())
ret.W(` <a href="/%s/_new"><button>New</button>`+helper.TextEndAnchor, m.Route())
ret.W(` </div>`)
ret.W(" %s{%%%%= components.SVGRefIcon(`%s`, ps) %%%%}{%%%%s ps.Title %%%%}%s", helper.TextH3Start, m.Icon, helper.TextH3End)
} else {
ret.W(" <div class=\"right\"><a href=\"/%s/_new\"><button>New</button></a></div>", m.Route())
ret.W(" %s%s{%%%%s ps.Title %%%%}%s", helper.TextH3Start, svgRef(m.Icon), helper.TextH3End)
}
if m.HasTag("count") {
ret.W(" {%%- if p.Count > 0 -%%}")
ret.W(" <em>{%%s util.StringPlural(p.Count, \"items\") %%}</em>")
ret.W(" {%%- endif -%%}")
}
if m.HasSearches() {
ret.W(" <div class=\"clear\"></div>")
ret.W(" {%%- if p.SearchQuery != \"\" -%%}")
ret.W(" <hr />")
ret.W(" <em>Search results for [{%%s p.SearchQuery %%}]</em> (<a href=\"?\">clear</a>)")
ret.W(twoInd + helper.TextEndIfDash)
}
ret.W(" {%%- if len(p.Models) == 0 -%%}")
ret.W(" <div class=\"mt\"><em>No %s available</em></div>", m.TitlePluralLower())
ret.W(" {%%- else -%%}")
ret.W(" <div class=\"mt\">")
ret.W(" {%%= Table(p.Models" + suffix + ", p.Params, as, ps) %%}")
ret.W(" </div>")
ret.W(twoInd + helper.TextEndIfDash)
ret.W(" </div>")
ret.W(helper.TextEndFunc)
return ret
}