Added the AddScript and AddSheet methods to *HeaderVars and replaced the manual appends with them.

This commit is contained in:
Azareal 2018-04-04 05:49:13 +01:00
parent b32e8d6c21
commit ca935d61a8
4 changed files with 50 additions and 42 deletions

View File

@ -7,16 +7,12 @@ import (
"time" "time"
) )
// TODO: Implement this and use it
// TODO: Allow resources in spots other than /static/ and possibly even external domains (e.g. CDNs) // TODO: Allow resources in spots other than /static/ and possibly even external domains (e.g. CDNs)
type HeaderResource struct { // TODO: Preload Trumboyg on Cosora on the forum list
Path string
Preload bool
}
type HeaderVars struct { type HeaderVars struct {
NoticeList []string NoticeList []string
Scripts []string Scripts []string
//PreloadScripts []string
Stylesheets []string Stylesheets []string
Widgets PageWidgets Widgets PageWidgets
Site *site Site *site
@ -30,6 +26,18 @@ type HeaderVars struct {
ExtData ExtData ExtData ExtData
} }
func (header *HeaderVars) AddScript(name string) {
header.Scripts = append(header.Scripts, name)
}
/*func (header *HeaderVars) PreloadScript(name string) {
header.PreloadScripts = append(header.PreloadScripts, name)
}*/
func (header *HeaderVars) AddSheet(name string) {
header.Stylesheets = append(header.Stylesheets, name)
}
// TODO: Add this to routes which don't use templates. E.g. Json APIs. // TODO: Add this to routes which don't use templates. E.g. Json APIs.
type HeaderLite struct { type HeaderLite struct {
Site *site Site *site

View File

@ -122,7 +122,7 @@ func panelUserCheck(w http.ResponseWriter, r *http.Request, user *User) (headerV
} }
// TODO: We should probably initialise headerVars.ExtData // TODO: We should probably initialise headerVars.ExtData
headerVars.Stylesheets = append(headerVars.Stylesheets, theme.Name+"/panel.css") headerVars.AddSheet(theme.Name + "/panel.css")
if len(theme.Resources) > 0 { if len(theme.Resources) > 0 {
rlist := theme.Resources rlist := theme.Resources
for _, resource := range rlist { for _, resource := range rlist {
@ -130,9 +130,9 @@ func panelUserCheck(w http.ResponseWriter, r *http.Request, user *User) (headerV
extarr := strings.Split(resource.Name, ".") extarr := strings.Split(resource.Name, ".")
ext := extarr[len(extarr)-1] ext := extarr[len(extarr)-1]
if ext == "css" { if ext == "css" {
headerVars.Stylesheets = append(headerVars.Stylesheets, resource.Name) headerVars.AddSheet(resource.Name)
} else if ext == "js" { } else if ext == "js" {
headerVars.Scripts = append(headerVars.Scripts, resource.Name) headerVars.AddScript(resource.Name)
} }
} }
} }
@ -231,9 +231,9 @@ func userCheck(w http.ResponseWriter, r *http.Request, user *User) (headerVars *
extarr := strings.Split(resource.Name, ".") extarr := strings.Split(resource.Name, ".")
ext := extarr[len(extarr)-1] ext := extarr[len(extarr)-1]
if ext == "css" { if ext == "css" {
headerVars.Stylesheets = append(headerVars.Stylesheets, resource.Name) headerVars.AddSheet(resource.Name)
} else if ext == "js" { } else if ext == "js" {
headerVars.Scripts = append(headerVars.Scripts, resource.Name) headerVars.AddScript(resource.Name)
} }
} }
} }

View File

@ -670,9 +670,9 @@ func routePanelAnalyticsViews(w http.ResponseWriter, r *http.Request, user commo
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -710,9 +710,9 @@ func routePanelAnalyticsRouteViews(w http.ResponseWriter, r *http.Request, user
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -751,9 +751,9 @@ func routePanelAnalyticsAgentViews(w http.ResponseWriter, r *http.Request, user
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -798,9 +798,9 @@ func routePanelAnalyticsForumViews(w http.ResponseWriter, r *http.Request, user
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -847,9 +847,9 @@ func routePanelAnalyticsSystemViews(w http.ResponseWriter, r *http.Request, user
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -892,9 +892,9 @@ func routePanelAnalyticsLanguageViews(w http.ResponseWriter, r *http.Request, us
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -937,9 +937,9 @@ func routePanelAnalyticsReferrerViews(w http.ResponseWriter, r *http.Request, us
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -976,9 +976,9 @@ func routePanelAnalyticsTopics(w http.ResponseWriter, r *http.Request, user comm
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {
@ -1016,9 +1016,9 @@ func routePanelAnalyticsPosts(w http.ResponseWriter, r *http.Request, user commo
if ferr != nil { if ferr != nil {
return ferr return ferr
} }
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
headerVars.Scripts = append(headerVars.Scripts, "analytics.js") headerVars.AddScript("analytics.js")
timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange")) timeRange, err := panelAnalyticsTimeRange(r.FormValue("timeRange"))
if err != nil { if err != nil {

View File

@ -74,8 +74,8 @@ func ViewTopic(w http.ResponseWriter, r *http.Request, user common.User, urlBit
} }
headerVars.Zone = "view_topic" headerVars.Zone = "view_topic"
// TODO: Only include these on pages with polls // TODO: Only include these on pages with polls
headerVars.Stylesheets = append(headerVars.Stylesheets, "chartist/chartist.min.css") headerVars.AddSheet("chartist/chartist.min.css")
headerVars.Scripts = append(headerVars.Scripts, "chartist/chartist.min.js") headerVars.AddScript("chartist/chartist.min.js")
topic.ContentHTML = common.ParseMessage(topic.Content, topic.ParentID, "forums") topic.ContentHTML = common.ParseMessage(topic.Content, topic.ParentID, "forums")
topic.ContentLines = strings.Count(topic.Content, "\n") topic.ContentLines = strings.Count(topic.Content, "\n")