search a go

master
Bel LaPointe 2022-02-09 16:27:23 -07:00
parent 9e73818af3
commit 3cbe7e629f
2 changed files with 50 additions and 21 deletions

View File

@ -325,7 +325,6 @@ func (server *Server) apiV0FilesIDPutHandler(w http.ResponseWriter, r *http.Requ
}
func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request) error {
// TODO case insensitive
query := r.URL.Query().Get("q")
queries := strings.Split(query, " ")
if len(queries) == 0 {
@ -337,7 +336,7 @@ func (server *Server) apiV0SearchHandler(w http.ResponseWriter, r *http.Request)
for _, query := range queries {
if len(query) > 0 {
query = unsafepattern.ReplaceAllString(query, ".")
patterns = append(patterns, regexp.MustCompile(query))
patterns = append(patterns, regexp.MustCompile("(?i)"+query))
}
}
if len(patterns) == 0 {

View File

@ -37,9 +37,12 @@
drawTree()
setInterval(drawTree, 100000)
var queryF = getParameterByName("f")
console.log("init query f:", queryF)
var queryQ = getParameterByName("q")
console.log("init query f:", queryF, "q:", queryQ)
if (queryF && queryF.length > 0) {
drawFile(queryF)
} else if (queryQ && queryQ.length > 0) {
searchFilesFor(queryQ)
}
}
function getParameterByName(name, url = window.location.href) {
@ -70,6 +73,27 @@
}
})
}
function searchFiles() {
const q = document.getElementById("searchbox").value
searchFilesFor(q)
}
function searchFilesFor(q) {
http("GET", "/api/v0/search?q=" + q, (body, status) => {
var results = JSON.parse(body)
results.sort()
var innerHTML = "<ul>"
for (var result in results)
innerHTML += `<li><input type="button" onclick="drawFile('${results[result]}');" value="${results[result]}"</li>`
innerHTML += "</ul>"
if (!results || results.length == 0)
innerHTML = "no results"
disableMDE()
window.location.hash = "#?q="+q
document.getElementById("searchResults").innerHTML = innerHTML
})
}
var saveFeedbackInterval = null
function pushFile() {
const title = document.getElementById("title").innerHTML ? document.getElementById("title").innerHTML : ""
@ -108,10 +132,12 @@
setMDE(pid + "/" + crypto.randomUUID().substr(0, 5), "", "")
}
function enableMDE() {
document.getElementById("searchResults").style.display = "none";
document.getElementById("article").style.display = "";
}
function disableMDE() {
document.getElementById("article").style.display = "none";
document.getElementById("searchResults").style.display = "";
}
function setMDE(id, title, body) {
if (id[0] == "/")
@ -190,13 +216,16 @@
</header>
<body style="width: 90%; max-width: 1024px; margin: auto;" onload="init(); return false;">
<div style="width: 100%; display: flex; flex-direction: column;">
<div style="margin: 1em; display: flex; flex-direction: row;">
<input type="text" style="flex-grow: 1;" placeholder="todo"/>
<input type="submit" value="search"/>
</div>
<form action="return false;" style="margin: 1em; display: flex; flex-direction: row;">
<input type="text" id="searchbox" style="flex-grow: 1;" placeholder="todo"/>
<input type="submit" value="search" onclick="searchFiles(); return false;"/>
</form>
<div style="width: 100%; display: flex; flex-direction: row; flex-grow: 1;">
<div id="tree"></div>
<article id="article" style="display: none; flex-grow: 1; margin-left: 1em;">
<div style="flex-grow: 1; margin-left: 1em;">
<article id="searchResults" style="display: none">
</article>
<article id="article" style="display: none">
<div>
<h1 style="display: flex; flex-direction: row;">
<input type="submit" value="DELETE" onclick="deleteFile(); return false;" style="margin-left: 5%; margin-right: 5%"/>
@ -212,6 +241,7 @@
</article>
</div>
</div>
</div>
</body>
<footer>
<script>