diff --git a/.gitignore b/.gitignore index 804bde0c..8bd1753a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ nyaa.exe nyaa-master.exe *.zip *.swp +.vscode diff --git a/router/faqHandler.go b/router/faqHandler.go index e4ab1768..92540af1 100644 --- a/router/faqHandler.go +++ b/router/faqHandler.go @@ -4,20 +4,16 @@ import ( "html/template" "net/http" + "github.com/ewhal/nyaa/templates" "github.com/gorilla/mux" ) var faqTemplate = template.Must(template.New("FAQ").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/FAQ.html")) -func init() { - // common - template.Must(faqTemplate.ParseGlob("templates/_*.html")) -} - func FaqHandler(w http.ResponseWriter, r *http.Request) { - searchForm := NewSearchForm() + searchForm := templates.NewSearchForm() searchForm.HideAdvancedSearch = true - err := faqTemplate.ExecuteTemplate(w, "index.html", FaqTemplateVariables{Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)}) + err := faqTemplate.ExecuteTemplate(w, "index.html", FaqTemplateVariables{templates.Navigation{}, searchForm, r.URL, mux.CurrentRoute(r)}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/router/homeHandler.go b/router/homeHandler.go index 70a44af0..e180d668 100644 --- a/router/homeHandler.go +++ b/router/homeHandler.go @@ -8,15 +8,12 @@ import ( "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/torrent" + "github.com/ewhal/nyaa/templates" "github.com/gorilla/mux" ) var homeTemplate = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html")) -func init() { - template.Must(homeTemplate.ParseGlob("templates/_*.html")) // common -} - func HomeHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) page := vars["page"] @@ -41,8 +38,8 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) { b = append(b, res) } - navigationTorrents := Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} - htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)} + navigationTorrents := templates.Navigation{nbTorrents, maxPerPage, pagenum, "search_page"} + htv := HomeTemplateVariables{b, torrentService.GetAllCategories(false), templates.NewSearchForm(), navigationTorrents, r.URL, mux.CurrentRoute(r)} err := homeTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { diff --git a/router/searchHandler.go b/router/searchHandler.go index 75f9f329..7a64dbf5 100644 --- a/router/searchHandler.go +++ b/router/searchHandler.go @@ -8,16 +8,13 @@ import ( "github.com/ewhal/nyaa/model" "github.com/ewhal/nyaa/service/torrent" + "github.com/ewhal/nyaa/templates" "github.com/ewhal/nyaa/util/search" "github.com/gorilla/mux" ) var searchTemplate = template.Must(template.New("home").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/home.html")) -func init() { - template.Must(searchTemplate.ParseGlob("templates/_*.html")) // common -} - func SearchHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) page := vars["page"] @@ -37,8 +34,8 @@ func SearchHandler(w http.ResponseWriter, r *http.Request) { b = append(b, res) } - navigationTorrents := Navigation{nbTorrents, search_param.Max, pagenum, "search_page"} - searchForm := SearchForm{ + navigationTorrents := templates.Navigation{nbTorrents, search_param.Max, pagenum, "search_page"} + searchForm := templates.SearchForm{ search_param.Query, search_param.Status, search_param.Category, diff --git a/router/templateFunctions.go b/router/templateFunctions.go index a2fad90f..a01e73e6 100644 --- a/router/templateFunctions.go +++ b/router/templateFunctions.go @@ -6,6 +6,8 @@ import ( "math" "net/url" "strconv" + + "github.com/ewhal/nyaa/templates" ) var FuncMap = template.FuncMap{ @@ -20,11 +22,11 @@ var FuncMap = template.FuncMap{ "genRouteWithQuery": func(name string, currentUrl *url.URL, params ...string) template.HTML { url, err := Router.Get(name).URL(params...) if err == nil { - return template.HTML(url.String()+ "?" + currentUrl.RawQuery) + return template.HTML(url.String() + "?" + currentUrl.RawQuery) } return "error" }, - "genNav": func(nav Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML { + "genNav": func(nav templates.Navigation, currentUrl *url.URL, pagesSelectable int) template.HTML { maxPages := math.Ceil(float64(nav.TotalItem) / float64(nav.MaxItemPerPage)) var ret = "" @@ -57,4 +59,13 @@ var FuncMap = template.FuncMap{ } return template.HTML(ret) }, + "SearchCommon": func(s templates.SearchForm) template.HTML { + return template.HTML(templates.SearchCommon(s)) + }, + "SearchButton": func(s templates.SearchForm) template.HTML { + return template.HTML(templates.SearchButton(s)) + }, + "SearchAdvanced": func(nav templates.Navigation, s templates.SearchForm) template.HTML { + return template.HTML(templates.SearchAdvanced(nav, s)) + }, } diff --git a/router/templateVariables.go b/router/templateVariables.go index 657b9a02..cf33cf45 100644 --- a/router/templateVariables.go +++ b/router/templateVariables.go @@ -1,9 +1,11 @@ package router import ( - "github.com/ewhal/nyaa/model" - "github.com/gorilla/mux" "net/url" + + "github.com/ewhal/nyaa/model" + "github.com/ewhal/nyaa/templates" + "github.com/gorilla/mux" ) /* Each Page should have an object to pass to their own template @@ -13,16 +15,16 @@ import ( */ type FaqTemplateVariables struct { - Navigation Navigation - Search SearchForm + Navigation templates.Navigation + Search templates.SearchForm URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } type ViewTemplateVariables struct { Torrent model.TorrentsJson - Search SearchForm - Navigation Navigation + Search templates.SearchForm + Navigation templates.Navigation URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } @@ -30,62 +32,16 @@ type ViewTemplateVariables struct { type HomeTemplateVariables struct { ListTorrents []model.TorrentsJson ListCategories []model.Categories - Search SearchForm - Navigation Navigation + Search templates.SearchForm + Navigation templates.Navigation URL *url.URL // For parsing Url in templates Route *mux.Route // For getting current route in templates } type UploadTemplateVariables struct { Upload UploadForm - Search SearchForm - Navigation Navigation + Search templates.SearchForm + Navigation templates.Navigation URL *url.URL Route *mux.Route } - -/* - * Variables used by the upper ones - */ -type Navigation struct { - TotalItem int - MaxItemPerPage int - CurrentPage int - Route string -} - -type SearchForm struct { - Query string - Status string - Category string - Sort string - Order string - HideAdvancedSearch bool -} - -// Some Default Values to ease things out -func NewSearchForm(params ...string) (searchForm SearchForm) { - if len(params) > 1 { - searchForm.Category = params[0] - } else { - searchForm.Category = "_" - } - if len(params) > 2 { - searchForm.Sort = params[1] - } else { - searchForm.Sort = "torrent_id" - } - if len(params) > 3 { - order := params[2] - if order == "DESC" { - searchForm.Order = order - } else if order == "ASC" { - searchForm.Order = order - } else { - // TODO: handle invalid value (?) - } - } else { - searchForm.Order = "DESC" - } - return -} diff --git a/router/uploadHandler.go b/router/uploadHandler.go index 76b9d91c..f40ae283 100644 --- a/router/uploadHandler.go +++ b/router/uploadHandler.go @@ -4,15 +4,12 @@ import ( "html/template" "net/http" + "github.com/ewhal/nyaa/templates" "github.com/gorilla/mux" ) var uploadTemplate = template.Must(template.New("upload").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/upload.html")) -func init() { - template.Must(uploadTemplate.ParseGlob("templates/_*.html")) // common -} - func UploadHandler(w http.ResponseWriter, r *http.Request) { var err error var uploadForm UploadForm @@ -24,7 +21,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) { //add to db and redirect depending on result } } else if r.Method == "GET" { - htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := UploadTemplateVariables{uploadForm, templates.NewSearchForm(), templates.Navigation{}, r.URL, mux.CurrentRoute(r)} err = uploadTemplate.ExecuteTemplate(w, "index.html", htv) } else { w.WriteHeader(http.StatusMethodNotAllowed) diff --git a/router/viewTorrentHandler.go b/router/viewTorrentHandler.go index ed1654c7..5d33f51a 100644 --- a/router/viewTorrentHandler.go +++ b/router/viewTorrentHandler.go @@ -5,15 +5,12 @@ import ( "net/http" "github.com/ewhal/nyaa/service/torrent" + "github.com/ewhal/nyaa/templates" "github.com/gorilla/mux" ) var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html")) -func init() { - template.Must(viewTemplate.ParseGlob("templates/_*.html")) // common -} - func ViewHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) id := vars["id"] @@ -21,7 +18,7 @@ func ViewHandler(w http.ResponseWriter, r *http.Request) { torrent, err := torrentService.GetTorrentById(id) b := torrent.ToJson() - htv := ViewTemplateVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)} + htv := ViewTemplateVariables{b, templates.NewSearchForm(), templates.Navigation{}, r.URL, mux.CurrentRoute(r)} err = viewTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { diff --git a/templates/_search.html b/templates/_search.html deleted file mode 100644 index 69ca25af..00000000 --- a/templates/_search.html +++ /dev/null @@ -1,72 +0,0 @@ -{{define "search_common"}} - - -{{end}} -{{define "search_advanced"}} - - - -{{end}} - -{{define "search_button"}} -
- - - - -
-{{end}} diff --git a/templates/index.html b/templates/index.html index 89166005..81bbe941 100755 --- a/templates/index.html +++ b/templates/index.html @@ -41,7 +41,7 @@