categories list page: show parents

This commit is contained in:
Dan Ballard 2015-05-23 10:49:27 -07:00
parent aa1f256215
commit 2598dd481b
2 changed files with 54 additions and 25 deletions

View File

@ -6,16 +6,43 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"net/http" "net/http"
"errors"
) )
var ( var (
templates = map[string]*template.Template{} templates = map[string]*template.Template{}
) )
// template helper function
func dict (values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i+=2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
func stringTimes(times int, str string) string {
result := ""
for i := 0; i < times; i ++ {
result += str
}
return result
}
// Tempalte helper functions // Tempalte helper functions
var funcMap = template.FuncMap { var funcMap = template.FuncMap {
"add": func (x, y int) int { return x + y }, "add": func (x, y int) int { return x + y },
"minus": func (x, y int) int { return x - y }, "minus": func (x, y int) int { return x - y },
"dict": dict,
"stringTimes": stringTimes,
} }

View File

@ -1,48 +1,50 @@
{{define "body"}} {{define "body"}}
<h2 class="form-categories-heading">Categories</h2> <h2 class="form-categories-heading">Categories</h2>
{{template "flashes" .}} {{template "flashes" .}}
<div class="row">
{{range $category := .categories}} {{range $category := .categories}}
{{template "row-category" $category}} {{template "row-category" dict "category" $category "categories" $.categories}}
{{end}} {{end}}
</div>
{{end}} {{end}}
{{define "row-category"}} {{define "row-category"}}
{{if .Parent.Valid }} <div class="row">
{{with .Depth}}
<div class="col-xs-{{.}} child-depth"></div>
<div class="col-xs-{{minus 12 .}} category-row">
{{end}}
{{else}}
<div class="col-xs-12 category-row">
{{end}}
{{.Name}} <a href="/categories/delete?id={{.Id}}">delete</a>
<select class="form-control category-change-parent" name="category-change-parent-{{.Id}}" >
<option value="-1" {{if not .Parent.Valid}} selected="true"{{end}}>-- None --</option> <div class="col-xs-2">
{{stringTimes .category.Depth "- "}}
{{.category.Name}}
</div>
<div class="col-xs-1">
<a href="/categories/delete?id={{.category.Id}}">delete</a>
</div>
<div class="col-xs-3">
<select class="form-control category-change-parent" name="category-change-parent-{{.category.Id}}" >
<option value="-1" {{if not .category.Parent.Valid}} selected="true"{{end}}>-- None --</option>
{{range $category := .categories}} {{range $category := .categories}}
{{template "option-category" $category }} {{if $.category.Parent.Valid }}
{{template "option-category" dict "category" $category "id" $.category.Parent.Value}}
{{else}}
{{template "option-category" dict "category" $category "id" -1}}
{{end}}
{{end}} {{end}}
</select> </select>
</div>
</div> </div>
{{range $child := .category.Children}}
{{range $child := .Children}} {{template "row-category" dict "category" $child "categories" $.categories}}
{{template "row-category" $child}}
{{end}} {{end}}
{{end}} {{end}}
{{define "option-category"}} {{define "option-category"}}
<option value="{{.Id}}">{{.ToString}}</option> <option value="{{.category.Id}}" {{if eq .id .category.Id}}selected="true"{{end}}>{{.category.ToString}}</option>
{{range $child := .Children}} {{range $child := .category.Children}}
{{template "option-category" $child}} {{template "option-category" dict "category" $child "id" $.id}}
{{end}} {{end}}
=======
>>>>>>> fea21c9d10d89f90b71988dcb41722527b947f60
{{end}} {{end}}