Properly implement the help popup

Move the help html to a template and populate the lists dynamically.
This also splits out the base HTML stuff (eg, the <html> and <body>
tags) to a base template file.
This commit is contained in:
Zorchenhimer 2019-03-16 16:15:45 -04:00
parent c4507cc496
commit d0cd90e7af
9 changed files with 130 additions and 70 deletions

View File

@ -24,8 +24,6 @@ type Command struct {
type CommandFunction func(client *Client, args []string) string
//type HelpFunction func(client *Client) string
var commands = &CommandControl{
user: map[string]Command{
common.CNMe.String(): Command{
@ -291,43 +289,33 @@ func (cc *CommandControl) RunCommand(command string, args []string, sender *Clie
func cmdHelp(cl *Client, args []string) string {
url := "/help"
if cl.IsMod {
url = "/modhelp"
url = "/help?mod=1"
}
if cl.IsAdmin {
url = "/adminhelp"
url = "/help?mod=1&admin=1"
}
return `Opening help in new window.<script>window.open("` + url + `", "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600")</script>`
cl.SendChatData(common.NewChatCommand(common.CmdHelp, []string{url}))
return `Opening help in new window.`
}
// Return a full HTML page for the help text. This should probably be rewritten with templates.
func helpPage(ismod, isadmin bool) string {
if commands == nil {
return "No commands loaded Jebaited"
func getHelp(lvl common.CommandLevel) map[string]string {
var cmdList map[string]Command
switch lvl {
case common.CmdUser:
cmdList = commands.user
case common.CmdMod:
cmdList = commands.mod
case common.CmdAdmin:
cmdList = commands.admin
}
text := []string{}
appendText := func(group map[string]Command) {
for key, cmd := range group {
for _, k := range strings.Split(key, common.CommandNameSeparator) {
text = append(text, fmt.Sprintf(`<dl class="helptext"><dt>%s</dt><dd>%s</dd></dl>`, k, cmd.HelpText))
}
}
helptext := map[string]string{}
for name, cmd := range cmdList {
helptext[name] = cmd.HelpText
}
appendText(commands.user)
if ismod {
appendText(commands.mod)
}
if isadmin {
appendText(commands.admin)
}
// This is ugly
return `<html><head><title>Help</title><link rel="stylesheet" type="text/css" href="/static/site.css"></head><body>` + strings.Join(text, "") + `</body></html>`
return helptext
}
// Commands below have more than one invoking command (aliases).

View File

@ -30,6 +30,15 @@ const (
CmdHelp
)
type CommandLevel int
// Command access levels
const (
CmdUser CommandLevel = iota
CmdMod
CmdAdmin
)
type EventType int
// Event Types

View File

@ -55,17 +55,6 @@ func wsStaticFiles(w http.ResponseWriter, r *http.Request) {
case "/justvideo":
http.ServeFile(w, r, "./static/justvideo.html")
return
// TODO: use a template for this, lol.
case "/help":
w.Write([]byte(helpPage(false, false)))
return
case "/modhelp":
w.Write([]byte(helpPage(true, false)))
return
case "/adminhelp":
w.Write([]byte(helpPage(true, true)))
return
}
goodPath := r.URL.Path[8:len(r.URL.Path)]
@ -150,8 +139,41 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
}()
}
func handleHelpTemplate(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./static/base.html", "./static/help.html")
if err != nil {
fmt.Printf("Error parsing template file, %v\n", err)
return
}
type Data struct {
Title string
Commands map[string]string
ModCommands map[string]string
AdminCommands map[string]string
}
data := Data{
Title: "Help",
Commands: getHelp(common.CmdUser),
}
if len(r.URL.Query().Get("mod")) > 0 {
data.ModCommands = getHelp(common.CmdMod)
}
if len(r.URL.Query().Get("admin")) > 0 {
data.AdminCommands = getHelp(common.CmdAdmin)
}
err = t.Execute(w, data)
if err != nil {
fmt.Printf("Error executing file, %v", err)
}
}
func handleIndexTemplate(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./static/index.html")
t, err := template.ParseFiles("./static/base.html", "./static/main.html")
if err != nil {
fmt.Printf("Error parsing template file, %v\n", err)
return

View File

@ -35,9 +35,7 @@ func main() {
http.HandleFunc("/favicon.ico", wsStaticFiles)
http.HandleFunc("/chat", handleIndexTemplate)
http.HandleFunc("/video", handleIndexTemplate)
http.HandleFunc("/help", wsStaticFiles)
http.HandleFunc("/modhelp", wsStaticFiles)
http.HandleFunc("/adminhelp", wsStaticFiles)
http.HandleFunc("/help", handleHelpTemplate)
http.HandleFunc("/", handleDefault)

20
static/base.html Normal file
View File

@ -0,0 +1,20 @@
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>{{ .Title }}</title>
<link rel="stylesheet" type="text/css" href="/static/css/hack/hack.css">
<link rel="stylesheet" type="text/css" href="/static/css/site.css">
<script type="application/javascript" src="/static/js/jquery.js"></script>
<script type="application/javascript" src="/static/js/flv.min.js"></script>
<script type="application/javascript" src="/static/js/wasm_exec.js"></script>
<script type="application/javascript" src="/static/js/client.js"></script>
{{template "header" .}}
</head>
<body class="scrollbar">
<div class="root">
{{template "body" .}}
</div>
</body>
</html>

View File

@ -178,3 +178,7 @@ span.svmsg {
#suggestions div.selectedName {
background: red;
}
#helpbody {
color: #B1B1B1;
}

29
static/help.html Normal file
View File

@ -0,0 +1,29 @@
{{define "header"}}
{{end}}
{{define "body"}}
<div id="helpbody">
<h2>Commands</h2>
<dl>
{{range $k, $v := .Commands}}
<dt>{{$k}}</dt><dd>{{$v}}</dd>
{{end}}
</dl>
{{if .ModCommands}}
<h2>Moderator</h2>
<dl>
{{range $k, $v := .ModCommands}}
<dt>{{$k}}</dt><dd>{{$v}}</dd>
{{end}}
</dl>
{{end}}
{{if .AdminCommands}}
<h2>Administrator</h2>
<dl>
{{range $k, $v := .AdminCommands}}
<dt>{{$k}}</dt><dd>{{$v}}</dd>
{{end}}
</dl>
{{end}}
</div>
{{end}}

View File

@ -1,16 +1,4 @@
<!doctype html>
<html>
<head>
<meta charset='utf-8'>
<title>{{ .Title }}</title>
<link rel="stylesheet" type="text/css" href="/static/css/hack/hack.css">
<link rel="stylesheet" type="text/css" href="/static/css/site.css">
<script type="application/javascript" src="/static/js/jquery.js"></script>
<script type="application/javascript" src="/static/js/flv.min.js"></script>
<script type="application/javascript" src="/static/js/wasm_exec.js"></script>
<script type="application/javascript" src="/static/js/client.js"></script>
{{define "header"}}
{{ if .Chat }}
<script>
maxMessageCount = {{ .MessageHistoryCount }}
@ -46,10 +34,9 @@
</style>
{{ end }}
{{ end }}
</head>
{{end}}
<body class="scrollbar">
<div class="root">
{{define "body"}}
{{ if .Video }}
<div id="video">
<video id="videoElement" controls autoplay x5-video-player-type="h5" x5-video-player-fullscreen="true"
@ -57,11 +44,7 @@
Your browser is too old and doesn't support HTML5 video.
<script>initPlayer();</script>
</video>
<div id="infoBox">
<div id="info">
<span id="playing"></span>
<a href="" target="_blank" id="playinglink"></a>
</div>
<div>
<button id="reload" class="button" onclick="initPlayer();">Reload Player</button>
</div>
</div>
@ -69,6 +52,12 @@
{{ if .Chat }}
<div>
<div id="infoBox">
<div id="info">
<span id="playing"></span>
<a href="" target="_blank" id="playinglink"></a>
</div>
</div>
<div id="error"></div>
<div id="chat" style="display: none;">
<div id="messages" class="scrollbar"></div>
@ -83,7 +72,4 @@
</div>
</div>
{{ end }}
</div>
</body>
</html>
{{end}}

View File

@ -209,8 +209,12 @@ func recieve(v []js.Value) {
case common.CmdPurgeChat:
fmt.Println("//TODO: chat purge command received.")
case common.CmdHelp:
url := "/help"
if d.Arguments != nil && len(d.Arguments) > 0 {
url = d.Arguments[0]
}
js.Call("appendMessages", data.HTML())
// TODO: open window
js.Call(`window.open("` + url + `", "_blank", "menubar=0,status=0,toolbar=0,width=300,height=600")`)
}
}
}