Compare commits

..

No commits in common. "search-filter" and "master" have entirely different histories.

8 changed files with 51 additions and 82 deletions

View File

@ -15,14 +15,11 @@ type Category struct {
Children []*Category
}
var CategoriesTree []*Category = nil
var CategoriesFlat map[int]*Category = nil
var CategoriesTree []*Category
var CategoriesFlat map[int]*Category
// Cavet: slight cheat. All parents must have Category_id < their children.
func LoadCategories(db *sql.DB) {
if CategoriesFlat != nil {
return //already loaded
}
CategoriesTree = []*Category{}
CategoriesFlat = make(map[int]*Category)
@ -102,14 +99,6 @@ func Delete(db *sql.DB, id int) error {
return err
}
func GetListOfChildrenIds(id int) []int {
list := []int{id}
for _,category := range CategoriesFlat[id].Children {
list = append(list, GetListOfChildrenIds(category.Id)...)
}
return list
}
func (category *Category) ChangeParent(db *sql.DB, parent *Category) error {
var err error
if parent == nil {

View File

@ -21,12 +21,8 @@ $(document).ready( function () {
$(".add-category-col").addClass("has-error");
}
});
$("form.search .filter-category").change(function (e) {
e.target.parentElement.submit();
});
$("form.change-parent .category-change-parent").change(function (e) {
$(".category-change-parent").change(function (e) {
e.target.parentElement.submit();
});

View File

@ -94,14 +94,10 @@ func Get(db *sql.DB, id int) (*News, error) {
}
// Load and return in NewsContainer format all the unexported news items
func LoadPage(db *sql.DB, offset, amount int, categoryFilter []int) ([]*News, int, error) {
func LoadPage(db *sql.DB, offset, amount int) ([]*News, int, error) {
categories.LoadCategories(db) // required by addContainer
where := ""
if categoryFilter != nil {
where = "WHERE id in $3"
}
rows, err := db.Query("SELECT " + SQL_NEWS_FIELDS + " FROM news " + where + " ORDER BY timestamp DESC LIMIT $1 OFFSET $2", amount, offset, categoryFilter)
rows, err := db.Query("SELECT " + SQL_NEWS_FIELDS + " FROM news ORDER BY timestamp DESC LIMIT $1 OFFSET $2", amount, offset)
if err != nil {
fmt.Println("DB errpr reading LoadPage news: ", err)
return nil, 0, err

View File

@ -405,7 +405,6 @@ func categoryDeleteHandler(w http.ResponseWriter, r *http.Request, user *user.Us
func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, session *sessions.Session) {
flashes := GetFlashes(session)
session.Save(r, w)
categories.LoadCategories(db)
var offset = 0
var amount = 100
@ -413,14 +412,8 @@ func newsFormHandler(w http.ResponseWriter, r *http.Request, user *user.User, se
if eOffset == nil {
offset = amount * argOffset
}
categoryFilterId, cidErr := strconv.Atoi(r.FormValue("category"))
categoryFilter := []int{}
if cidErr == nil {
categories.GetListOfChildrenIds(categoryFilterId)
}
news, count, err := news.LoadPage(db, offset, amount, categoryFilter)
news, count, err := news.LoadPage(db, offset, amount)
if err != nil {
session.AddFlash("Error loading news", flash_err)
}

View File

@ -80,32 +80,3 @@
{{end}}
</div>
{{end}}
<!-- reused widgets -->
<!--
Print a full <select> of categories with one selected
categories: a flat map of categories ordered by parent
id: the category id that should be selected (or -1 for none)
fieldName: the html form field name
-->
{{define "select-category"}}
<select class="form-control add-category category-change-parent filter-category" name="{{ .fieldName }}" >
<option value="none" {{if not .category.Parent.Valid}} selected="true"{{end}}>-- None --</option>
{{range $category := .categories}}
{{template "option-category" dict "category" $category "id" $.id}}
{{end}}
</select>
{{end}}
<!--
print all the <option> inside a select for a category and its children
category: the category to print as an option (and it's nested children)
id: the category id that should be selected
-->
{{define "option-category"}}
<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}}
{{end}}

View File

@ -6,12 +6,12 @@
{{end}}
<div class="row">
<form method="POST" action="/categories/add" class="add-category">
<form method="POST" action="/categories/add">
<div class="col-xs-3">
<input name="name" class="form-control" placeholder="Name" />
</div>
<div class="col-xs-3">
{{template "select-category" dict "categories" .categories "id" -1 "fieldName" "parent"}}
{{template "select-category" dict "categories" .categories "id" -1}}
</div>
<div class="col-xs-2">
{{ .csrfField }}
@ -39,13 +39,12 @@ new category select
</form>
</div>
<div class="col-xs-3">
<form action="/categories/{{.category.Id}}/change-parent" method="POST" class="change-parent">
<form action="/categories/{{.category.Id}}/change-parent" method="POST">
{{ .csrfField }}
<!-- select-category defined in layout -->
{{if $.category.Parent.Valid }}
{{template "select-category" dict "categories" .categories "id" $.category.Parent.Value "fieldName" "parent"}}
{{template "select-category" dict "categories" .categories "id" $.category.Parent.Value}}
{{else}}
{{template "select-category" dict "categories" .categories "id" -1 "fieldName" "parent"}}
{{template "select-category" dict "categories" .categories "id" -1}}
{{end}}
</form>
</div>
@ -56,3 +55,28 @@ new category select
{{end}}
{{end}}
<!--
Print a full <select> of categories with one selected
categories: a flat map of categories ordered by parent
id: the category id that should be selected (or -1 for none)
-->
{{define "select-category"}}
<select class="form-control category-change-parent" name="parent" >
<option value="none" {{if not .category.Parent.Valid}} selected="true"{{end}}>-- None --</option>
{{range $category := .categories}}
{{template "option-category" dict "category" $category "id" $.id}}
{{end}}
</select>
{{end}}
<!--
print all the <option> inside a select for a category and its children
category: the category to print as an option (and it's nested children)
id: the category id that should be selected
-->
{{define "option-category"}}
<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}}
{{end}}

View File

@ -10,19 +10,6 @@
Drag this bookmarklet to bookmark bar and click anywhere to add a link
</div>
<div class="col-xs-4"></div>
<div class="col-xs-12">&nbsp;</div>
<form method="GET" class="search" />
<input type="hidden" name="offset" value="{{.offset}}" />
<div class="col-xs-2">Filter by Category:</div>
<div class="col-xs-3">
{{template "select-category" dict "categories" .categories "id" -1 "fieldName" "category"}}
</div>
<div class="col-xs-1">&nbsp;</div>
<div class="col-xs-4"><input name="search" class="form-control" placeholder="Search title" /></div>
<div class="col-xs-2">&nbsp;</div>
</form>
{{template "pager" .}}

View File

@ -7,11 +7,24 @@
<div class="col-xs-2">Link:</div><div class="col-xs-10"><input type="text" class="form-control" name="link" placeholder="Link" value="{{.link}}"/></div>
<div class="col-xs-2">Title:</div><div class="col-xs-10"><input type="text" class="form-control" name="title" placeholder="Title" value="{{.title}}"/></div>
<div class="col-xs-2">Category:</div><div class="col-xs-10 add-category-col">
{{template "select-category" dict "categories" .categories "id" $.category_id "fieldName" "category"}}
<select class="form-control add-category" name="category" placeholder="Category">
<option value="-1">-- Pick a Category --</option>
{{range $category := .categories}}
{{template "option-category" dict "category" $category "category_id" $.category_id}}
{{end}}
</select>
</div>
<div class="col-xs-2">Notes:</div><div class="col-xs-10"><textarea class="form-control" name="notes" placeholder="Notes" rows="5" cols="80">{{.notes}}</textarea></div>
<div class="col-xs-2"></div><div class="col-xs-10">{{ .csrfField }}<input class="add-submit btn btn-lg btn-primary btn-block" type="submit" value="{{if eq .mode "add"}}Add Link{{else}}Commit Edit{{end}}" /></div>
</div>
</form>
{{end}}
{{define "option-category"}}
<option value="{{.category.Id}}" {{if eq .category.Id .category_id}}selected="true"{{end}} >{{.category.ToString}}</option>
{{range $child := .category.Children}}
{{template "option-category" dict "category" $child "category_id" $.category_id}}
{{end}}
{{end}}