refined searchbar; added options to use a global searchbar or a search link in the menu

This commit is contained in:
Gabriele Musco 2021-04-13 16:04:16 +02:00
parent f51db60b35
commit 746a750cf1
No known key found for this signature in database
GPG Key ID: 8539FD3454380B83
5 changed files with 55 additions and 29 deletions

View File

@ -45,6 +45,8 @@ summaryLength = 70 # number of words for article summaries
highlightBgColor = "#34363b"; # card and circle navigation background color for discrete card mode highlightBgColor = "#34363b"; # card and circle navigation background color for discrete card mode
enableSearch = true # enable search page enableSearch = true # enable search page
searchbarEverywhere = true # if the searchbar should be shown in every page; requires enableSearch
searchMenuLink = false # add a search link to the navigation menu; requires enableSearch
# enable comments support with commento using the script from your server # enable comments support with commento using the script from your server
commento = "https://example.com/js/commento.js" commento = "https://example.com/js/commento.js"

View File

@ -341,6 +341,7 @@ ul.list {
flex-direction: row; flex-direction: row;
flex-wrap: nowrap; flex-wrap: nowrap;
align-items: center; align-items: center;
margin: 12px 0;
.nerdlink { .nerdlink {
display: inline-block; display: inline-block;
cursor: pointer; cursor: pointer;
@ -359,3 +360,13 @@ ul.list {
outline: none; outline: none;
} }
} }
.titleAndSearchContainer {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
.search {
&, #searchbar {font-size: 1em;}
}
}

View File

@ -2,10 +2,7 @@
<noscript> <noscript>
You need to enable JavaScript to be able to search. You need to enable JavaScript to be able to search.
</noscript> </noscript>
<div class="search"> {{- partial "searchbar.html" . -}}
<input id="searchbar" type="text" placeholder="{{ .Title }}" />
<a class="nerdlink" onclick="newSearch();">&#xf002;</a>
</div>
<div class="postlist {{ if .Site.Params.gridView }}gridView{{ end }}" id="postlist"> <div class="postlist {{ if .Site.Params.gridView }}gridView{{ end }}" id="postlist">
</div> </div>
{{- partial "js_paginator.html" . -}} {{- partial "js_paginator.html" . -}}
@ -43,9 +40,5 @@
performSearch(searchTerm); performSearch(searchTerm);
} }
} }
function newSearch() {
location.href = baseUrl+`?q=${searchbar.value}`;
}
searchbar.onkeyup = (ev) => {if (ev.keyCode == 13) newSearch()};
</script> </script>
{{ end }} {{ end }}

View File

@ -1,5 +1,6 @@
<header> <header>
{{- partial "inject/header-before.html" . -}} {{- partial "inject/header-before.html" . -}}
<div class="titleAndSearchContainer">
<div id="titleContainer"> <div id="titleContainer">
<img src='{{ .Site.Params.Logo | default "/img/icon.svg" }}' /> <img src='{{ .Site.Params.Logo | default "/img/icon.svg" }}' />
<div> <div>
@ -16,13 +17,20 @@
{{ if (.Site.Params.showTags | default true) }} {{ if (.Site.Params.showTags | default true) }}
<li><a href="/tags">Tags</a></li> <li><a href="/tags">Tags</a></li>
{{ end }} {{ end }}
{{ if (.Site.Params.enableSearch | default true) }} {{ if and (.Site.Params.searchMenuLink | default false) (.Site.Params.enableSearch | default true) }}
<li><a href="/search">Search</a></li> <li><a href="/search">Search</a></li>
{{ end }} {{ end }}
</ul> </ul>
</nav> </nav>
</div> </div>
</div> </div>
{{ if and (.Site.Params.searchbarEverywhere | default true) (and (.Site.Params.enableSearch | default true) (not (eq .Title "Search"))) }}
{{- partial "searchbar.html" . -}}
<script>
document.getElementById('searchbar').
</script>
{{ end }}
</div>
<div id="links"> <div id="links">
{{ if .Site.Params.showRss | default true }} {{ if .Site.Params.showRss | default true }}
{{- partial "nerdlink" (slice "RSS" "/index.xml") -}} {{- partial "nerdlink" (slice "RSS" "/index.xml") -}}

View File

@ -0,0 +1,12 @@
<div class="search">
<input id="searchbar" type="text" placeholder="Search" />
<a class="nerdlink" onclick="newSearch();">&#xf002;</a>
</div>
<script>
function newSearch() {
let term = searchbar.value.trim();
if (!term) return;
location.href = `/search?q=${term}`;
}
searchbar.onkeyup = (ev) => {if (ev.keyCode == 13) newSearch()};
</script>