Addresses #3: Add search functionality to topic list
This commit is contained in:
parent
37f5e91a49
commit
5b00a3df3c
32
slidify.js
32
slidify.js
@ -2,7 +2,10 @@ window.addEventListener('load', init);
|
|||||||
|
|
||||||
let slides;
|
let slides;
|
||||||
let currentSlide;
|
let currentSlide;
|
||||||
|
|
||||||
let topicList;
|
let topicList;
|
||||||
|
let topicLinks;
|
||||||
|
let topicListSearch;
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
slides = Array.from(document.querySelectorAll('article'));
|
slides = Array.from(document.querySelectorAll('article'));
|
||||||
@ -102,7 +105,7 @@ function showPreviousFragment() {
|
|||||||
|
|
||||||
function getTopicListContent() {
|
function getTopicListContent() {
|
||||||
let results = [...document.querySelectorAll('h1, h2, h3, h4, h5, h6')];
|
let results = [...document.querySelectorAll('h1, h2, h3, h4, h5, h6')];
|
||||||
let resultHtml = '<ul>';
|
let resultHtml = '<input type="text" id="topicListSearch"/><ul>';
|
||||||
let currentLevel = 1;
|
let currentLevel = 1;
|
||||||
|
|
||||||
results.forEach(element => {
|
results.forEach(element => {
|
||||||
@ -147,12 +150,32 @@ function createTopicList() {
|
|||||||
topicList.style.visibility = 'hidden';
|
topicList.style.visibility = 'hidden';
|
||||||
topicList.style.overflowY = 'scroll';
|
topicList.style.overflowY = 'scroll';
|
||||||
|
|
||||||
topicList.querySelectorAll('a').forEach(link => {
|
topicLinks = [...topicList.querySelectorAll('a')]
|
||||||
|
topicLinks.forEach(link => {
|
||||||
link.addEventListener('click', e => topicList.style.visibility = 'hidden');
|
link.addEventListener('click', e => topicList.style.visibility = 'hidden');
|
||||||
})
|
});
|
||||||
|
|
||||||
|
topicListSearch = topicList.querySelector('#topicListSearch');
|
||||||
|
topicListSearch.style.display = 'block';
|
||||||
|
topicListSearch.style.width = '100%';
|
||||||
|
topicListSearch.addEventListener('input', onTopicListSearchInput);
|
||||||
|
|
||||||
return topicList;
|
return topicList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function onTopicListSearchInput(e) {
|
||||||
|
let searchTerms = topicListSearch.value.toLowerCase();
|
||||||
|
topicLinks.forEach(link => {
|
||||||
|
if (link.textContent.toLowerCase().includes(searchTerms) || searchTerms.length == 0) {
|
||||||
|
link.style.opacity = 1;
|
||||||
|
link.removeAttribute('tabindex');
|
||||||
|
} else {
|
||||||
|
link.tabIndex = -1;
|
||||||
|
link.style.opacity = 0.2;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let lastCtrlPress = null;
|
let lastCtrlPress = null;
|
||||||
|
|
||||||
function toggleTopicList() {
|
function toggleTopicList() {
|
||||||
@ -165,7 +188,10 @@ function toggleTopicList() {
|
|||||||
if (topicList.style.visibility === 'visible') {
|
if (topicList.style.visibility === 'visible') {
|
||||||
topicList.style.visibility = 'hidden';
|
topicList.style.visibility = 'hidden';
|
||||||
} else {
|
} else {
|
||||||
|
topicListSearch.value = '';
|
||||||
|
onTopicListSearchInput();
|
||||||
topicList.style.visibility = 'visible';
|
topicList.style.visibility = 'visible';
|
||||||
|
topicListSearch.focus();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lastCtrlPress = Date.now();
|
lastCtrlPress = Date.now();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user