1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2026-03-22 10:58:14 +00:00
Files
jekyll-theme-chirpy/_javascript/modules/components/search-display.js
Cotes Chung 6afe5a6d7b refactor: correct typos in comments and identifiers (#2681)
- Rename `loadTooptip` to `loadTooltip` in tooltip-loader.js, components.js, and basic.js
- Fix grammar: "This script make" → "This script makes" in search-display.js
- Fix "HomgPage" → "HomePage" in post-paginator.html
- Fix "CND URL" → "CDN URL" in media-url.html
- Fix "locale-dateime.js" → "locale-datetime.js" in datetime.html
- Fix "LaTex" → "LaTeX" in language-alias.html
- Fix "exist <a> tag" → "existing <a> tag" in refactor-content.html
- Fix "Archvies" → "Archives" in _archives.scss
- Fix "underlinke" → "underline" in _syntax.scss
2026-03-14 19:01:21 +08:00

111 lines
2.8 KiB
JavaScript

/**
* This script makes #search-result-wrapper switch to unload or shown automatically.
*/
const btnSbTrigger = document.getElementById('sidebar-trigger');
const btnSearchTrigger = document.getElementById('search-trigger');
const btnCancel = document.getElementById('search-cancel');
const content = document.querySelectorAll('#main-wrapper>.container>.row');
const topbarTitle = document.getElementById('topbar-title');
const search = document.getElementById('search');
const resultWrapper = document.getElementById('search-result-wrapper');
const results = document.getElementById('search-results');
const input = document.getElementById('search-input');
const hints = document.getElementById('search-hints');
// CSS class names
const LOADED = 'd-block';
const UNLOADED = 'd-none';
const FOCUS = 'input-focus';
const FLEX = 'd-flex';
/* Actions in mobile screens (Sidebar hidden) */
class MobileSearchBar {
static on() {
btnSbTrigger.classList.add(UNLOADED);
topbarTitle.classList.add(UNLOADED);
btnSearchTrigger.classList.add(UNLOADED);
search.classList.add(FLEX);
btnCancel.classList.add(LOADED);
}
static off() {
btnCancel.classList.remove(LOADED);
search.classList.remove(FLEX);
btnSbTrigger.classList.remove(UNLOADED);
topbarTitle.classList.remove(UNLOADED);
btnSearchTrigger.classList.remove(UNLOADED);
}
}
class ResultSwitch {
static resultVisible = false;
static on() {
if (!this.resultVisible) {
resultWrapper.classList.remove(UNLOADED);
content.forEach((el) => {
el.classList.add(UNLOADED);
});
this.resultVisible = true;
}
}
static off() {
if (this.resultVisible) {
results.innerHTML = '';
if (hints.classList.contains(UNLOADED)) {
hints.classList.remove(UNLOADED);
}
resultWrapper.classList.add(UNLOADED);
content.forEach((el) => {
el.classList.remove(UNLOADED);
});
input.textContent = '';
this.resultVisible = false;
}
}
}
function isMobileView() {
return btnCancel.classList.contains(LOADED);
}
export function displaySearch() {
btnSearchTrigger.addEventListener('click', () => {
MobileSearchBar.on();
ResultSwitch.on();
input.focus();
});
btnCancel.addEventListener('click', () => {
MobileSearchBar.off();
ResultSwitch.off();
});
input.addEventListener('focus', () => {
search.classList.add(FOCUS);
});
input.addEventListener('focusout', () => {
search.classList.remove(FOCUS);
});
input.addEventListener('input', () => {
if (input.value === '') {
if (isMobileView()) {
hints.classList.remove(UNLOADED);
} else {
ResultSwitch.off();
}
} else {
ResultSwitch.on();
if (isMobileView()) {
hints.classList.add(UNLOADED);
}
}
});
}