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"
"regexp"
"net/http"
"errors"
)
var (
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
var funcMap = template.FuncMap {
"add": 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"}}
<h2 class="form-categories-heading">Categories</h2>
{{template "flashes" .}}
<div class="row">
{{range $category := .categories}}
{{template "row-category" $category}}
{{template "row-category" dict "category" $category "categories" $.categories}}
{{end}}
</div>
{{end}}
{{define "row-category"}}
{{if .Parent.Valid }}
{{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>
<div class="row">
<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}}
{{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}}
</select>
</div>
</div>
{{range $child := .Children}}
{{template "row-category" $child}}
{{range $child := .category.Children}}
{{template "row-category" dict "category" $child "categories" $.categories}}
{{end}}
{{end}}
{{define "option-category"}}
<option value="{{.Id}}">{{.ToString}}</option>
{{range $child := .Children}}
{{template "option-category" $child}}
<option value="{{.category.Id}}" {{if eq .id .category.Id}}selected="true"{{end}}>{{.category.ToString}}</option>
{{range $child := .category.Children}}
{{template "option-category" dict "category" $child "id" $.id}}
{{end}}
=======
>>>>>>> fea21c9d10d89f90b71988dcb41722527b947f60
{{end}}