This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
/
menugen.go
214 lines (176 loc) · 5.81 KB
/
menugen.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package genericsite
import (
"github.com/xyproto/onthefly"
"github.com/xyproto/pinterface"
"github.com/xyproto/webhandle"
"net/http"
"strconv"
)
type (
MenuEntry struct {
id string
text string
url string
}
MenuEntries []*MenuEntry
)
var menuIdCounter = 0
// Generate a new menu ID
func (me *MenuEntry) autoId() string {
newId := ""
// Start with the menu item number and increase the counter
newId += strconv.Itoa(menuIdCounter)
menuIdCounter++
// Then add the first letter of the menu text
if len(me.text) > 0 {
newId += string(me.text[0])
}
return newId
}
// Takes something like "Admin:/admin" and returns a *MenuEntry
func NewMenuEntry(text_and_url string) *MenuEntry {
var me MenuEntry
me.text, me.url = webhandle.HostPortSplit(text_and_url)
me.id = me.autoId()
return &me
}
func Links2menuEntries(links []string) MenuEntries {
menuEntries := make(MenuEntries, len(links))
for i, text_and_url := range links {
menuEntries[i] = NewMenuEntry(text_and_url)
}
return menuEntries
}
// Generate tags for the menu based on a list of "MenuDescription:/menu/url"
func MenuSnippet(menuEntries MenuEntries) *onthefly.Page {
var a, li, sep *onthefly.Tag
page, ul := onthefly.StandaloneTag("ul")
ul.AddAttrib("class", "menuList")
//ul.AddStyle("list-style-type", "none")
//ul.AddStyle("float", "left")
//ul.AddStyle("margin", "0")
for i, menuEntry := range menuEntries {
li = ul.AddNewTag("li")
li.AddAttrib("class", "menuEntry")
// TODO: Make sure not duplicate ids are added for two menu entries named "Hi there" and "Hi you". Add i to string?
menuId := "menu" + menuEntry.id
li.AddAttrib("id", menuId)
// All menu entries are now hidden by default!
//li.AddStyle("display", "none")
//li.AddStyle("display", "inline")
li.SansSerif()
//li.CustomSansSerif("Armata")
// For every element, except the first one
if i > 0 {
// Insert a '|' character in a div
sep = li.AddNewTag("div")
sep.AddContent("|")
sep.AddAttrib("class", "separator")
}
a = li.AddNewTag("a")
a.AddAttrib("class", "menulink")
a.AddAttrib("href", menuEntry.url)
a.AddContent(menuEntry.text)
}
return page
}
// Checks if a *MenuEntry exists in a []*MenuEntry (MenuEntries)
func HasEntry(checkEntry *MenuEntry, menuEntries MenuEntries) bool {
for _, menuEntry := range menuEntries {
if menuEntry.url == checkEntry.url {
return true
}
}
return false
}
func AddIfNotAdded(url string, filteredMenuEntries *MenuEntries, menuEntry *MenuEntry) {
//if currentMenuURL != url {
if menuEntry.url == url {
if !HasEntry(menuEntry, *filteredMenuEntries) {
*filteredMenuEntries = append(*filteredMenuEntries, menuEntry)
}
}
//}
}
/*
* Functions that generate functions that generate content that can be used in templates.
* type TemplateValues map[string]string
* type TemplateValueGenerator func(*web.Context) TemplateValues
* type TemplateValueGeneratorFactory func(*UserState) TemplateValueGenerator
*/
// TODO: Take the same parameters as the old menu generating code
// TODO: Put one if these in each engine then combine them somehow
// TODO: Check for the menyEntry.url first, then check the rights, not the other way around
// TODO: Fix and refactor this one
// TODO: Check the user status _once_, and the admin status _once_, then generate the menu
// TODO: Some way of marking menu entries as user, admin or other rights. Add a group system?
func DynamicMenuFactoryGenerator(menuEntries MenuEntries) TemplateValueGeneratorFactory {
return func(state pinterface.IUserState) webhandle.TemplateValueGenerator {
return func(w http.ResponseWriter, req *http.Request) onthefly.TemplateValues {
userRights := state.UserRights(req)
adminRights := state.AdminRights(req)
var filteredMenuEntries MenuEntries
var logoutEntry *MenuEntry = nil
// Build up filteredMenuEntries based on what should be shown or not
for _, menuEntry := range menuEntries {
// Don't add duplicates
if HasEntry(menuEntry, filteredMenuEntries) {
continue
}
// Add this one last
if menuEntry.url == "/logout" {
if userRights {
logoutEntry = menuEntry
}
continue
}
// Always show the Overview menu
AddIfNotAdded("/", &filteredMenuEntries, menuEntry)
//if menuEntry.url == "/" {
// if !HasEntry(menuEntry, filteredMenuEntries) {
// filteredMenuEntries = append(filteredMenuEntries, menuEntry)
// }
//}
// If logged in, show Logout and the content
if userRights {
// Add every link except the current page we're on
//if menuEntry.url != currentMenuURL {
if !HasEntry(menuEntry, filteredMenuEntries) {
if (menuEntry.url != "/login") && (menuEntry.url != "/register") && (menuEntry.url != "/admin") {
filteredMenuEntries = append(filteredMenuEntries, menuEntry)
}
}
//}
// Show admin content
if adminRights {
AddIfNotAdded("/admin", &filteredMenuEntries, menuEntry)
}
} else {
// Only show Login and Register
AddIfNotAdded("/login", &filteredMenuEntries, menuEntry)
AddIfNotAdded("/register", &filteredMenuEntries, menuEntry)
}
}
if logoutEntry != nil {
AddIfNotAdded("/logout", &filteredMenuEntries, logoutEntry)
}
page := MenuSnippet(filteredMenuEntries)
retval := page.String()
// TODO: Return the CSS as well somehow
//css := page.CSS()
return onthefly.TemplateValues{"menu": retval}
}
}
}
// Combines two TemplateValueGenerators into one TemplateValueGenerator by adding the strings per key
func TemplateValueGeneratorCombinator(tvg1, tvg2 webhandle.TemplateValueGenerator) webhandle.TemplateValueGenerator {
return func(w http.ResponseWriter, req *http.Request) onthefly.TemplateValues {
tv1 := tvg1(w, req)
tv2 := tvg2(w, req)
for key, value := range tv2 {
// TODO: Check if key exists in tv1 first
tv1[key] += value
}
return tv1
}
}