2016-06-10 21:18:44 +00:00
|
|
|
package filemanager
|
|
|
|
|
|
|
|
import (
|
2016-06-11 09:37:36 +00:00
|
|
|
"encoding/json"
|
2016-06-11 21:15:42 +00:00
|
|
|
"html/template"
|
2016-06-10 21:38:52 +00:00
|
|
|
"log"
|
2016-06-11 09:37:36 +00:00
|
|
|
"net/http"
|
2016-06-11 09:08:33 +00:00
|
|
|
"strings"
|
2016-06-10 21:18:44 +00:00
|
|
|
)
|
|
|
|
|
2016-06-11 21:15:42 +00:00
|
|
|
// Page contains the informations and functions needed to show the page
|
|
|
|
type Page struct {
|
|
|
|
Info *PageInfo
|
|
|
|
}
|
|
|
|
|
2016-06-11 09:37:36 +00:00
|
|
|
// PageInfo contains the information of a page
|
|
|
|
type PageInfo struct {
|
2016-06-11 21:15:42 +00:00
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
Config *Config
|
|
|
|
Data interface{}
|
2016-06-11 09:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BreadcrumbMap returns p.Path where every element is a map
|
|
|
|
// of URLs and path segment names.
|
|
|
|
func (p PageInfo) BreadcrumbMap() map[string]string {
|
|
|
|
result := map[string]string{}
|
|
|
|
|
|
|
|
if len(p.Path) == 0 {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip trailing slash
|
|
|
|
lpath := p.Path
|
|
|
|
if lpath[len(lpath)-1] == '/' {
|
|
|
|
lpath = lpath[:len(lpath)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(lpath, "/")
|
|
|
|
for i, part := range parts {
|
|
|
|
if i == 0 && part == "" {
|
|
|
|
// Leading slash (root)
|
|
|
|
result["/"] = "/"
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result[strings.Join(parts[:i+1], "/")] = part
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2016-06-10 21:18:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 09:37:36 +00:00
|
|
|
// PrintAsHTML formats the page in HTML and executes the template
|
|
|
|
func (p Page) PrintAsHTML(w http.ResponseWriter, templates ...string) (int, error) {
|
2016-06-10 21:38:52 +00:00
|
|
|
templates = append(templates, "base")
|
|
|
|
var tpl *template.Template
|
|
|
|
|
|
|
|
// For each template, add it to the the tpl variable
|
|
|
|
for i, t := range templates {
|
|
|
|
// Get the template from the assets
|
|
|
|
page, err := Asset("templates/" + t + ".tmpl")
|
|
|
|
|
|
|
|
// Check if there is some error. If so, the template doesn't exist
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2016-06-11 09:37:36 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2016-06-10 21:18:44 +00:00
|
|
|
}
|
2016-06-10 21:38:52 +00:00
|
|
|
|
|
|
|
// If it's the first iteration, creates a new template and add the
|
|
|
|
// functions map
|
|
|
|
if i == 0 {
|
|
|
|
tpl, err = template.New(t).Parse(string(page))
|
|
|
|
} else {
|
|
|
|
tpl, err = tpl.Parse(string(page))
|
2016-06-10 21:18:44 +00:00
|
|
|
}
|
2016-06-10 21:38:52 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
2016-06-11 09:37:36 +00:00
|
|
|
return http.StatusInternalServerError, err
|
2016-06-10 21:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-11 09:37:36 +00:00
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
err := tpl.Execute(w, p.Info)
|
2016-06-11 09:08:33 +00:00
|
|
|
|
2016-06-11 09:37:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
2016-06-11 09:08:33 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 09:37:36 +00:00
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
2016-06-11 09:08:33 +00:00
|
|
|
|
2016-06-11 09:37:36 +00:00
|
|
|
// PrintAsJSON prints the current page infromation in JSON
|
|
|
|
func (p Page) PrintAsJSON(w http.ResponseWriter) (int, error) {
|
|
|
|
marsh, err := json.Marshal(p.Info.Data)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
2016-06-11 09:08:33 +00:00
|
|
|
}
|
|
|
|
|
2016-06-11 09:37:36 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
return w.Write(marsh)
|
2016-06-11 09:08:33 +00:00
|
|
|
}
|