1
0
mirror of https://github.com/avitex/avitex.github.io synced 2025-01-15 12:29:57 +00:00

mobile responsive, silence search error

This commit is contained in:
avitex 2020-04-14 18:18:17 +10:00
parent 2db949a45c
commit a6a778a9a1
Signed by: avitex
GPG Key ID: 38C76CBF3749D62C
8 changed files with 60 additions and 46 deletions

View File

@ -1,7 +1,6 @@
.home-page { .home-page {
width: 100%; width: 100%;
height: 100%; height: 100%;
min-width: 600px;
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
@ -11,4 +10,9 @@
margin: 0; margin: 0;
} }
} }
@media (max-width: $sm-breakpoint) {
text-align: center;
flex-direction: column;
}
} }

View File

@ -5,15 +5,16 @@
} }
html, body { html, body {
height: 100%;
width: 100%; width: 100%;
height: 100%;
} }
.container { .container {
width: 100%; width: 100%;
min-width: 600px;
max-width: 900px; max-width: 900px;
margin-left: auto; margin-left: auto;
margin-right: auto; margin-right: auto;
padding-left: 1rem;
padding-right: 1rem;
padding-bottom: 2rem; padding-bottom: 2rem;
} }

View File

@ -1,4 +1,4 @@
.search-container { #search {
input { input {
padding: 0.5rem 0.75rem; padding: 0.5rem 0.75rem;
border: none; border: none;
@ -7,7 +7,7 @@
border-radius: 4px; border-radius: 4px;
} }
.search-results { .results-container {
display: none; display: none;
} }
} }

View File

@ -12,18 +12,17 @@
li { li {
display: inline-block; display: inline-block;
padding: 0 0.2rem; padding: 0 0.2rem;
color: $grey;
& > a.active {
color: $white;
}
} }
a { a {
color: inherit; display: inline-block;
color: $grey;
border: none; border: none;
text-decoration: none; text-decoration: none;
&:hover, &.active {
color: $white;
}
} }
.main-menu { .main-menu {
@ -31,7 +30,6 @@
} }
.sub-menu { .sub-menu {
color: $grey;
font-size: 0.65rem; font-size: 0.65rem;
} }
} }

View File

@ -95,6 +95,14 @@ small {
font-size: 0.5em; font-size: 0.5em;
} }
ul, ol {
padding-left: 1rem;
li {
padding-bottom: 0.2rem;
}
}
hr { hr {
border: none; border: none;
height: 2px; height: 2px;

View File

@ -34,13 +34,13 @@ function makeTeaser(body, terms) {
}); });
var termFound = false; var termFound = false;
var index = 0; var index = 0;
var weighted = []; // contains elements of ["word", weight, index_in_document] var weighted = []; // contains elements of ['word', weight, index_in_document]
// split in sentences, then words // split in sentences, then words
var sentences = body.toLowerCase().split(". "); var sentences = body.toLowerCase().split('. ');
for (var i in sentences) { for (var i in sentences) {
var words = sentences[i].split(" "); var words = sentences[i].split(' ');
var value = FIRST_WORD_WEIGHT; var value = FIRST_WORD_WEIGHT;
for (var j in words) { for (var j in words) {
@ -108,73 +108,76 @@ function makeTeaser(body, terms) {
// add <em/> around search terms // add <em/> around search terms
if (word[1] === TERM_WEIGHT) { if (word[1] === TERM_WEIGHT) {
teaser.push("<b>"); teaser.push('<b>');
} }
startIndex = word[2] + word[0].length; startIndex = word[2] + word[0].length;
teaser.push(body.substring(word[2], startIndex)); teaser.push(body.substring(word[2], startIndex));
if (word[1] === TERM_WEIGHT) { if (word[1] === TERM_WEIGHT) {
teaser.push("</b>"); teaser.push('</b>');
} }
} }
teaser.push("…"); teaser.push('…');
return teaser.join(""); return teaser.join('');
} }
function formatSearchResultItem(item, terms) { function formatSearchResultItem(item, terms) {
return '<div class="search-results__item">' return '<div class="result">'
+ `<a href="${item.ref}">${item.doc.title}</a>` + `<a href="${item.ref}">${item.doc.title}</a>`
+ `<div>${makeTeaser(item.doc.body, terms)}</div>` + `<div class="summary">${makeTeaser(item.doc.body, terms)}</div>`
+ '</div>'; + '</div>';
} }
function initSearch() { function initSearch() {
var $searchInput = document.getElementById("search"); var $searchComponent = document.getElementById('search');
var $searchResults = document.querySelector(".search-results"); if ($searchComponent === null) {
var $searchResultsItems = document.querySelector(".search-results__items"); return;
}
var $searchInput = $searchComponent.querySelector('input');
var $searchResults = $searchComponent.querySelector('.results-container');
var $searchResultsItems = $searchComponent.querySelector('.results');
var MAX_ITEMS = 10; var MAX_ITEMS = 10;
var options = { var options = {
bool: "AND", bool: "AND",
fields: { fields: {
title: {boost: 2}, title: { boost: 2 },
body: {boost: 1}, body: { boost: 1 },
} }
}; };
var currentTerm = ""; var currentTerm = '';
var index = elasticlunr.Index.load(window.searchIndex); var index = elasticlunr.Index.load(window.searchIndex);
$searchInput.addEventListener("keyup", debounce(function() { $searchInput.addEventListener("keyup", debounce(function () {
var term = $searchInput.value.trim(); var term = $searchInput.value.trim();
if (term === currentTerm || !index) { if (term === currentTerm || !index) {
return; return;
} }
$searchResults.style.display = term === "" ? "none" : "block"; $searchResults.style.display = term === '' ? 'none' : 'block';
$searchResultsItems.innerHTML = ""; $searchResultsItems.innerHTML = '';
if (term === "") { if (term === '') {
return; return;
} }
var results = index.search(term, options); var results = index.search(term, options);
if (results.length === 0) { if (results.length === 0) {
$searchResults.style.display = "none"; $searchResults.style.display = 'none';
return; return;
} }
currentTerm = term; currentTerm = term;
for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) { for (var i = 0; i < Math.min(results.length, MAX_ITEMS); i++) {
var item = document.createElement("li"); var item = document.createElement('li');
item.innerHTML = formatSearchResultItem(results[i], term.split(" ")); item.innerHTML = formatSearchResultItem(results[i], term.split(' '));
$searchResultsItems.appendChild(item); $searchResultsItems.appendChild(item);
} }
}, 150)); }, 150));
} }
if (document.readyState === 'complete' ||
if (document.readyState === "complete" || (document.readyState !== 'loading' && !document.documentElement.doScroll)
(document.readyState !== "loading" && !document.documentElement.doScroll)
) { ) {
initSearch(); initSearch();
} else { } else {
document.addEventListener("DOMContentLoaded", initSearch); document.addEventListener('DOMContentLoaded', initSearch);
} }

View File

@ -1,6 +1,6 @@
<div class="search-container"> <div id="search">
<input id="search" type="search" placeholder="🔎 Search"> <input type="search" placeholder="🔎 Search">
<div class="search-results"> <div class="results-container">
<div class="search-results__items"></div> <div class="results"></div>
</div> </div>
</div> </div>

View File

@ -6,7 +6,7 @@
<meta name="theme-color" content="#111111"/> <meta name="theme-color" content="#111111"/>
<meta name="author" content="{{ config.extra.author }}"> <meta name="author" content="{{ config.extra.author }}">
<meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0">
<meta name="description" content="{% block meta_description %} <meta name="description" content="{% block meta_description %}
{%- if section -%}{{ macros::meta_desc(ctx=section) }}{%- endif -%} {%- if section -%}{{ macros::meta_desc(ctx=section) }}{%- endif -%}
{%- if page -%}{{ macros::meta_desc(ctx=page) }}{%- endif -%} {%- if page -%}{{ macros::meta_desc(ctx=page) }}{%- endif -%}