Skip to content

Commit

Permalink
web: show only one result after click on a word link
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Oct 11, 2024
1 parent 9a41521 commit 5ba5e6a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/application/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func onQuery(
case 3:
mode = dictmgr.QueryModeGlob
}
results := dictmgr.LookupHTML(query, conf, mode, resultFlags)
results := dictmgr.LookupHTML(query, conf, mode, resultFlags, 0)
slog.Debug("LookupHTML running time", "dt", time.Since(t), "query", query)
queryArgs.ResultList.SetResults(results)
if len(results) == 0 {
Expand Down
9 changes: 6 additions & 3 deletions pkg/dictmgr/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func LookupHTML(
conf *config.Config,
mode QueryMode,
resultFlags uint32,
limit int,
) []common.SearchResultIface {
results := []common.SearchResultIface{}
for _, dic := range dicts.DictList {
Expand Down Expand Up @@ -104,9 +105,11 @@ func LookupHTML(
// }
return res1.EntryIndex() < res2.EntryIndex()
})
cutoff := conf.MaxResultsTotal
if cutoff > 0 && len(results) > cutoff {
results = results[:cutoff]
if limit == 0 {
limit = conf.MaxResultsTotal
}
if limit > 0 && len(results) > limit {
results = results[:limit]
}
return results
}
18 changes: 17 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log/slog"
"net/http"
"os"
"strconv"
"text/template"
"time"

Expand Down Expand Up @@ -106,7 +107,22 @@ func query(w http.ResponseWriter, r *http.Request) {
return
}

raw_results := dictmgr.LookupHTML(query, conf, mode, flags)
limit := 0
limitStr := r.FormValue("limit")
if limitStr != "" {
limitI64, err := strconv.ParseUint(limitStr, 10, 0)
if err != nil {
err := jsonEncoder.Encode(ErrorResponse{Error: "invalid limit"})
if err != nil {
slog.Error("error in jsonEncoder.Encode", "err", err)
}
w.WriteHeader(http.StatusBadRequest)
return
}
limit = int(limitI64)
}

raw_results := dictmgr.LookupHTML(query, conf, mode, flags, limit)
// pass resultFlags to LookupHTML
results := make([]Result, len(raw_results))
for i, entry := range raw_results {
Expand Down
20 changes: 10 additions & 10 deletions web/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,6 @@
resultListElem <= ul


def ajax_query(query, mode):
ajax.post(
"/query?query=" + query + "&mode=" + mode,
cache=False,
oncomplete=on_query_result,
)


def clear_results():
resultListElem.clear()
headerLabel.clear()
Expand All @@ -151,7 +143,11 @@
if not input.value:
clear_results()
return
ajax_query(input.value, modeInput.value)
ajax.post(
"/query?query=" + input.value + "&mode=" + modeInput.value,
cache=False,
oncomplete=on_query_result,
)


def on_word_link_click(event):
Expand All @@ -163,7 +159,11 @@
if target.startswith("bword://"):
target = target[8:]
input.value = target
ajax_query(target, modeInput.value)
ajax.post(
"/query?query=" + target + "&mode=" + modeInput.value + "&limit=1",
cache=False,
oncomplete=on_query_result,
)


def on_random_result(res):
Expand Down

0 comments on commit 5ba5e6a

Please sign in to comment.