mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-10 07:55:24 +02:00
comments added
This commit is contained in:
parent
8475e22635
commit
8cd1b12f5b
1 changed files with 33 additions and 2 deletions
|
@ -82,15 +82,20 @@ export default class Sidebar {
|
||||||
this.nodes.search = moduleEl.querySelector('.' + Sidebar.CSS.sidebarSearch);
|
this.nodes.search = moduleEl.querySelector('.' + Sidebar.CSS.sidebarSearch);
|
||||||
let className = Sidebar.CSS.sidebarSearchWrapperOther;
|
let className = Sidebar.CSS.sidebarSearchWrapperOther;
|
||||||
|
|
||||||
|
// Search initialize with platform specific shortcut.
|
||||||
if (window.navigator.userAgent.indexOf('Mac') != -1) {
|
if (window.navigator.userAgent.indexOf('Mac') != -1) {
|
||||||
className = Sidebar.CSS.sidebarSearchWrapperMac;
|
className = Sidebar.CSS.sidebarSearchWrapperMac;
|
||||||
}
|
}
|
||||||
this.nodes.search.parentElement.classList.add(className);
|
this.nodes.search.parentElement.classList.add(className);
|
||||||
|
// Add event listener for search input.
|
||||||
this.nodes.search.addEventListener('input', e => {
|
this.nodes.search.addEventListener('input', e => {
|
||||||
e.stopImmediatePropagation();
|
e.stopImmediatePropagation();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
this.search(e.target.value);
|
this.search(e.target.value);
|
||||||
});
|
});
|
||||||
|
// Add event listener for keyboard events.
|
||||||
|
document.addEventListener('keydown', e => this.keyboardListener(e));
|
||||||
|
|
||||||
this.ready();
|
this.ready();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +204,6 @@ export default class Sidebar {
|
||||||
*/
|
*/
|
||||||
ready() {
|
ready() {
|
||||||
this.nodes.sidebarContent.classList.remove(Sidebar.CSS.sidebarContentInvisible);
|
this.nodes.sidebarContent.classList.remove(Sidebar.CSS.sidebarContentInvisible);
|
||||||
document.addEventListener('keydown', e => this.keyboardListener(e));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -208,35 +212,44 @@ export default class Sidebar {
|
||||||
* @param {KeyboardEvent} e - keyboard event.
|
* @param {KeyboardEvent} e - keyboard event.
|
||||||
*/
|
*/
|
||||||
keyboardListener(e) {
|
keyboardListener(e) {
|
||||||
|
// If Ctrl + P is pressed, focus search input.
|
||||||
if (e.ctrlKey && e.code === 'KeyP') {
|
if (e.ctrlKey && e.code === 'KeyP') {
|
||||||
this.nodes.search.focus();
|
this.nodes.search.focus();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
// Get search results if search input is empty.
|
||||||
this.search('');
|
this.search('');
|
||||||
}
|
}
|
||||||
|
// If search is focused and arrow keys are pressed, move focus to next/previous item.
|
||||||
if (this.nodes.search === document.activeElement) {
|
if (this.nodes.search === document.activeElement) {
|
||||||
if (e.code === 'ArrowUp' || e.code === 'ArrowDown') {
|
if (e.code === 'ArrowUp' || e.code === 'ArrowDown') {
|
||||||
e.stopImmediatePropagation();
|
e.stopImmediatePropagation();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
// check for search results.
|
||||||
if (this.nodes.searchResults.length === 0) {
|
if (this.nodes.searchResults.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// get current focused item.
|
||||||
const prevSelectedSearchResultIndex = this.nodes.selectedSearchResultIndex;
|
const prevSelectedSearchResultIndex = this.nodes.selectedSearchResultIndex;
|
||||||
|
|
||||||
|
// get next item index.
|
||||||
if (this.nodes.selectedSearchResultIndex === null) {
|
if (this.nodes.selectedSearchResultIndex === null) {
|
||||||
|
// if no item is focused and up press, focus last item.
|
||||||
if (e.code === 'ArrowUp') {
|
if (e.code === 'ArrowUp') {
|
||||||
this.nodes.selectedSearchResultIndex = this.nodes.searchResults.length - 1;
|
this.nodes.selectedSearchResultIndex = this.nodes.searchResults.length - 1;
|
||||||
}
|
}
|
||||||
|
// if no item is focused and down press, focus first item.
|
||||||
if (e.code === 'ArrowDown') {
|
if (e.code === 'ArrowDown') {
|
||||||
this.nodes.selectedSearchResultIndex = 0;
|
this.nodes.selectedSearchResultIndex = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// if item is focused and up press, focus previous item.
|
||||||
if (e.code === 'ArrowUp') {
|
if (e.code === 'ArrowUp') {
|
||||||
this.nodes.selectedSearchResultIndex--;
|
this.nodes.selectedSearchResultIndex--;
|
||||||
if ( this.nodes.selectedSearchResultIndex < 0 ) {
|
if ( this.nodes.selectedSearchResultIndex < 0 ) {
|
||||||
this.nodes.selectedSearchResultIndex = this.nodes.searchResults.length - 1;
|
this.nodes.selectedSearchResultIndex = this.nodes.searchResults.length - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// if item is focused and down press, focus next item.
|
||||||
if (e.code === 'ArrowDown') {
|
if (e.code === 'ArrowDown') {
|
||||||
this.nodes.selectedSearchResultIndex++;
|
this.nodes.selectedSearchResultIndex++;
|
||||||
if ( this.nodes.selectedSearchResultIndex >= this.nodes.searchResults.length ) {
|
if ( this.nodes.selectedSearchResultIndex >= this.nodes.searchResults.length ) {
|
||||||
|
@ -245,10 +258,12 @@ export default class Sidebar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove focus from previous item.
|
||||||
if (prevSelectedSearchResultIndex !== null) {
|
if (prevSelectedSearchResultIndex !== null) {
|
||||||
const { element:preElement, type:preType } = this.nodes.searchResults[prevSelectedSearchResultIndex];
|
const { element:preElement, type:preType } = this.nodes.searchResults[prevSelectedSearchResultIndex];
|
||||||
|
|
||||||
if (preElement) {
|
if (preElement) {
|
||||||
|
// remove focus from previous item or title.
|
||||||
if (preType === 'title') {
|
if (preType === 'title') {
|
||||||
preElement.classList.remove(Sidebar.CSS.sectionTitleSelected);
|
preElement.classList.remove(Sidebar.CSS.sectionTitleSelected);
|
||||||
}
|
}
|
||||||
|
@ -261,6 +276,7 @@ export default class Sidebar {
|
||||||
const { element, type } = this.nodes.searchResults[this.nodes.selectedSearchResultIndex];
|
const { element, type } = this.nodes.searchResults[this.nodes.selectedSearchResultIndex];
|
||||||
|
|
||||||
if (element) {
|
if (element) {
|
||||||
|
// add focus to next item or title.
|
||||||
if (type === 'title') {
|
if (type === 'title') {
|
||||||
element.classList.add(Sidebar.CSS.sectionTitleSelected);
|
element.classList.add(Sidebar.CSS.sectionTitleSelected);
|
||||||
}
|
}
|
||||||
|
@ -269,6 +285,7 @@ export default class Sidebar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// if enter is pressed, click on focused item.
|
||||||
if (e.code === 'Enter') {
|
if (e.code === 'Enter') {
|
||||||
if (this.nodes.selectedSearchResultIndex!==null) {
|
if (this.nodes.selectedSearchResultIndex!==null) {
|
||||||
this.nodes.searchResults[this.nodes.selectedSearchResultIndex].element.click();
|
this.nodes.searchResults[this.nodes.selectedSearchResultIndex].element.click();
|
||||||
|
@ -283,21 +300,28 @@ export default class Sidebar {
|
||||||
* @param {InputEvent} searchValue - search value.
|
* @param {InputEvent} searchValue - search value.
|
||||||
*/
|
*/
|
||||||
search(searchValue) {
|
search(searchValue) {
|
||||||
|
// remove selection from previous search results.
|
||||||
if (this.nodes.selectedSearchResultIndex) {
|
if (this.nodes.selectedSearchResultIndex) {
|
||||||
const { element, type } = this.nodes.searchResults[this.nodes.selectedSearchResultIndex];
|
const { element, type } = this.nodes.searchResults[this.nodes.selectedSearchResultIndex];
|
||||||
|
|
||||||
if (element) {
|
if (element) {
|
||||||
|
// remove focus from previous item or title.
|
||||||
if (type === 'title') {
|
if (type === 'title') {
|
||||||
element.classList.remove(Sidebar.CSS.sectionTitleSelected);
|
element.classList.remove(Sidebar.CSS.sectionTitleSelected);
|
||||||
}
|
}
|
||||||
if ( type === 'item') {
|
if ( type === 'item') {
|
||||||
element.classList.remove(Sidebar.CSS.sectionListItemSlelected);
|
element.classList.remove(Sidebar.CSS.sectionListItemSlelected);
|
||||||
}
|
}
|
||||||
|
// empty selected index.
|
||||||
this.nodes.selectedSearchResultIndex = null;
|
this.nodes.selectedSearchResultIndex = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// empty search results.
|
||||||
this.nodes.searchResults = [];
|
this.nodes.searchResults = [];
|
||||||
|
|
||||||
|
// match search value with sidebar sections.
|
||||||
this.nodes.sections.forEach(section => {
|
this.nodes.sections.forEach(section => {
|
||||||
|
// match with section title.
|
||||||
const sectionTitle = section.querySelector('.' + Sidebar.CSS.sectionTitle);
|
const sectionTitle = section.querySelector('.' + Sidebar.CSS.sectionTitle);
|
||||||
let isTitleMatch = true;
|
let isTitleMatch = true;
|
||||||
const matchResults = [];
|
const matchResults = [];
|
||||||
|
@ -307,6 +331,7 @@ export default class Sidebar {
|
||||||
isTitleMatch = false;
|
isTitleMatch = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// match with section items.
|
||||||
const sectionList = section.querySelector('.' + Sidebar.CSS.sectionList);
|
const sectionList = section.querySelector('.' + Sidebar.CSS.sectionList);
|
||||||
let isItemMatch = false;
|
let isItemMatch = false;
|
||||||
|
|
||||||
|
@ -316,21 +341,27 @@ export default class Sidebar {
|
||||||
sectionListItems.forEach(item => {
|
sectionListItems.forEach(item => {
|
||||||
if (item.innerText.trim().toLowerCase()
|
if (item.innerText.trim().toLowerCase()
|
||||||
.indexOf(searchValue.toLowerCase()) !== -1) {
|
.indexOf(searchValue.toLowerCase()) !== -1) {
|
||||||
|
// remove hiden class from item.
|
||||||
item.parentElement.classList.remove(Sidebar.CSS.sectionListItemWrapperHidden);
|
item.parentElement.classList.remove(Sidebar.CSS.sectionListItemWrapperHidden);
|
||||||
|
// add item to search results.
|
||||||
matchResults.push({
|
matchResults.push({
|
||||||
element:item,
|
element:item,
|
||||||
type:'item',
|
type:'item',
|
||||||
});
|
});
|
||||||
isItemMatch = true;
|
isItemMatch = true;
|
||||||
} else {
|
} else {
|
||||||
|
// hide item if it is not a match.
|
||||||
item.parentElement.classList.add(Sidebar.CSS.sectionListItemWrapperHidden);
|
item.parentElement.classList.add(Sidebar.CSS.sectionListItemWrapperHidden);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!isTitleMatch && !isItemMatch) {
|
if (!isTitleMatch && !isItemMatch) {
|
||||||
|
// hide section and it's items are not a match.
|
||||||
section.classList.add(Sidebar.CSS.sectionHidden);
|
section.classList.add(Sidebar.CSS.sectionHidden);
|
||||||
} else {
|
} else {
|
||||||
|
// show section and it's items are a match.
|
||||||
section.classList.remove(Sidebar.CSS.sectionHidden);
|
section.classList.remove(Sidebar.CSS.sectionHidden);
|
||||||
|
// add section title to search results.
|
||||||
this.nodes.searchResults.push({
|
this.nodes.searchResults.push({
|
||||||
element:sectionTitle,
|
element:sectionTitle,
|
||||||
type:'title',
|
type:'title',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue