Add pagination on files and lists page

This commit is contained in:
2019-02-07 23:09:54 +01:00
parent 63c1e09f8e
commit 95405e76d1
10 changed files with 128 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ package webcontroller
import (
"html/template"
"net/http"
"net/url"
"time"
"fornaxian.com/pixeldrain-web/pixelapi"
@@ -17,8 +18,9 @@ type TemplateData struct {
APIEndpoint template.URL
PixelAPI *pixelapi.PixelAPI
Other interface{}
Title string
Other interface{}
URLQuery url.Values
Title string
}
func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request) *TemplateData {
@@ -26,6 +28,7 @@ func (wc *WebController) newTemplateData(w http.ResponseWriter, r *http.Request)
Authenticated: false,
Username: "",
APIEndpoint: template.URL(wc.conf.APIURLExternal),
URLQuery: r.URL.Query(),
}
if key, err := wc.getAPIKey(r); err == nil {

View File

@@ -4,11 +4,14 @@ import (
"html/template"
"os"
"path/filepath"
"strconv"
"time"
"github.com/Fornaxian/log"
)
// TemplateManager parses templates and provides utility functions to the
// templates' scripting language
type TemplateManager struct {
templates *template.Template
@@ -18,6 +21,7 @@ type TemplateManager struct {
debugModeEnabled bool
}
// NewTemplateManager creates a new template manager
func NewTemplateManager(templateDir, externalAPIEndpoint string, debugMode bool) *TemplateManager {
return &TemplateManager{
templateDir: templateDir,
@@ -72,17 +76,31 @@ func (tm *TemplateManager) funcMap() template.FuncMap {
"bgPatternCount": tm.bgPatternCount,
"debugMode": tm.debugMode,
"apiUrl": tm.apiURL,
"pageNr": tm.pageNr,
"add": tm.add,
"sub": tm.sub,
}
}
func (tm *TemplateManager) bgPatternCount() uint8 {
return uint8(time.Now().UnixNano() % 17)
}
func (tm *TemplateManager) debugMode() bool {
return tm.debugModeEnabled
}
func (tm *TemplateManager) apiURL() string {
return tm.externalAPIEndpoint
}
func (tm *TemplateManager) pageNr(s string) (nr int) {
// Atoi returns 0 on error, which is fine for page numbers
if nr, _ = strconv.Atoi(s); nr < 0 {
return 0
}
return nr
}
func (tm *TemplateManager) add(a, b int) int {
return a + b
}
func (tm *TemplateManager) sub(a, b int) int {
return a - b
}

View File

@@ -59,6 +59,7 @@ func New(r *httprouter.Router, prefix string, conf *conf.PixelWebConfig) *WebCon
r.POST(p+"/logout" /* */, wc.serveLogout)
r.GET(p+"/user" /* */, wc.serveTemplate("user_home", true))
r.GET(p+"/user/files" /* */, wc.serveTemplate("user_files", true))
r.GET(p+"/user/lists" /* */, wc.serveTemplate("user_lists", true))
r.GET(p+"/user/filemanager" /**/, wc.serveTemplate("file_manager", true))
r.NotFound = http.HandlerFunc(wc.serveNotFound)