From a46963a6f9dff657a180ba0c6871c015871a80f5 Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 30 Dec 2015 15:08:03 +0100 Subject: instant. fucking. suche. diff --git a/assets/css/list.css b/assets/css/list.css index 7e6dde9..601f69b 100644 --- a/assets/css/list.css +++ b/assets/css/list.css @@ -24,4 +24,9 @@ background-size: cover; width: 64px; height: 64px; +} + +.search-results { + top: -50px; + z-index: 2; } \ No newline at end of file diff --git a/assets/js/search.js b/assets/js/search.js new file mode 100644 index 0000000..0f544ab --- /dev/null +++ b/assets/js/search.js @@ -0,0 +1,31 @@ +var strokeTimeout = null; + +function instantSearch() { + value = document.getElementById("search").value; + + if (strokeTimeout) { + clearTimeout(strokeTimeout); + } + strokeTimeout = setTimeout(doSearch, 150, value); +} + +function doSearch(value) { + if (value == "") { + strokeTimeout = null; + return; + } + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == XMLHttpRequest.DONE) { + if (xhr.status === 404 || xhr.status === 400 || typeof xhr.response === 'undefined') { + console.log(xhr.status); + document.getElementById("search-results").innerHTML = ''; + } + document.getElementById("search-results").innerHTML = xhr.response; + } + } + console.log(value); + xhr.open('GET', '/search/gril_instant/' + value, true); + xhr.send(null); + strokeTimeout = null; +} \ No newline at end of file diff --git a/main.go b/main.go index 850f0f9..10003cf 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "fagott.pw/grilist/grilist" "fagott.pw/grilist/modules/grils" "fagott.pw/grilist/modules/lists" + "fagott.pw/grilist/modules/search" "fagott.pw/grilist/modules/tags" "github.com/julienschmidt/httprouter" @@ -79,6 +80,7 @@ func main() { loadModule(grils.New()) loadModule(lists.New()) loadModule(tags.New()) + loadModule(search.New()) log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/modules/search/search.go b/modules/search/search.go new file mode 100644 index 0000000..da6d126 --- /dev/null +++ b/modules/search/search.go @@ -0,0 +1,81 @@ +package search + +import ( + "log" + "net/http" + + "fagott.pw/charakterin" + "fagott.pw/grilist/grilist" + "fagott.pw/grilist/modules/grils" + + "github.com/julienschmidt/httprouter" +) + +type Module struct { + g *grilist.Grilist + grils *grils.GrilsModule +} + +func New() *Module { + return &Module{} +} + +func (m *Module) Name() string { + return "Search" +} + +func (m *Module) Init(g *grilist.Grilist) { + m.g = g + gm, ok := g.Modules["Grils"] + if !ok { + log.Fatal("search: grils module not found") + } + m.grils = gm.(*grils.GrilsModule) + + m.g.Router.GET("/search/gril_instant/*name", m.instantSearchGril) +} + +func (m *Module) Interface() interface{} { + return m +} + +func (m *Module) ProvideDashboardData(user *charakterin.User) []grilist.DashboardCategory { + return make([]grilist.DashboardCategory, 0) +} + +func (m *Module) instantSearchGril(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + name := p.ByName("name")[1:] + if len(name) < 2 { + http.Error(w, "Bad Request", 400) + return + } + + rows, err := m.g.DB.Query(`SELECT id FROM grilist.search_grils($1)`, name) + if err != nil { + log.Println("error in instant gril search:", err) + return + } + + defer rows.Close() + + var ids []int + for rows.Next() { + var id int + if err := rows.Scan(&id); err != nil { + log.Println("error scanning in instant gril search", err) + continue + } + + ids = append(ids, id) + } + + grils, err := m.grils.FromIDs(ids) + if err != nil { + log.Println("error acquiring instant grils:", err) + return + } + + data := make(map[string]interface{}) + data["results"] = grils + m.g.Renderer.RenderPage("instant_search_results", w, data) +} diff --git a/views/instant_search_results.html b/views/instant_search_results.html new file mode 100644 index 0000000..e05c17a --- /dev/null +++ b/views/instant_search_results.html @@ -0,0 +1,9 @@ +{{ define "instant_search_results" }} +{{ range .results }} +
  • + + {{ .RomajiName }} +

    {{ .KanjiName }}

    +
  • +{{ end }} +{{ end }} \ No newline at end of file diff --git a/views/list.html b/views/list.html index 0393d19..5114ab9 100644 --- a/views/list.html +++ b/views/list.html @@ -7,55 +7,75 @@ grilist + {{ template "navbar" . }}

    {{ $list.Name }}von {{ $list.Owner.GetName }}


    - +
    + {{ end }} + -- cgit v0.10.1