the-glorious-startpage/js/web-menu.js

436 lines
12 KiB
JavaScript
Raw Normal View History

2020-06-09 04:02:03 +02:00
class WebMenu {
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
constructor() {
this._dashboard = document.querySelector('#rightDashboard');
this._weatherScreen = document.querySelector('#weatherScreen');
this._webSites = config.getWebSites();
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
this._webMenu = document.querySelector('#webMenu');
this._webMenuList = document.querySelector('#webMenuList');
this._webMenuListContainer = document.querySelector('#webMenuListContainer');
this._webMenuSearchBox = document.querySelector('#webMenuSearchBox');
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
this._webMenuVisibility = false;
2020-06-06 12:16:34 +02:00
2020-06-09 04:02:03 +02:00
this._webItemFocus;
this._webListIndex = 0;
2020-06-06 12:16:34 +02:00
2020-06-09 04:02:03 +02:00
this._fuzzySearch();
this._init();
2020-06-04 06:15:59 +02:00
}
2020-06-09 04:02:03 +02:00
// Return web menu status
getwebMenuVisibility = () => {
return this._webMenuVisibility;
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Disable textboxes
_disableWebMenuInputs = (status) => {
const elems = this._webMenu.getElementsByTagName('input');
const len = elems.length;
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
for (let i = 0; i < len; i++) {
elems[i].disabled = status;
}
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create callback property, to be used when enter was pressed while item is focused
_createWebItemCallback = (li, url) => {
// Create a callback property for the passed li
li.callback = () => {
window.location.href = encodeURI(url);
}
}
// Sort list alphabetically
_sortList = () => {
Array.from(this._webMenuList.getElementsByTagName('li'))
.sort((a, b) => a.textContent.localeCompare(b.textContent))
.forEach(li => this._webMenuList.appendChild(li));
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create/generate web items
_populateWebMenu = () => {
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Generate a list
for (let webData of this._webSites) {
2020-06-09 04:02:03 +02:00
const site = webData.site;
const icon = webData.icon;
const url = webData.url;
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
const li = document.createElement('li');
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create callback property
this._createWebItemCallback(li, url);
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create a href
const aWebLink = document.createElement('a');
aWebLink.className = 'webMenuLink';
aWebLink.href = url;
aWebLink.tabIndex = '-1';
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create an outer div, child of li
let webItemDiv = document.createElement('div')
webItemDiv.className = 'webItem';
webItemDiv.id = 'id' + site;
// Create a second div, webItemContainer
const webItemContainer = document.createElement('div');
webItemContainer.className = 'webItemContainer';
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create the innermost div, contains icon and label
const webItemBody = document.createElement('div');
webItemBody.className = 'webItemBody';
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create div for webItemIcon
const webItemIconContainer = document.createElement('div');
webItemIconContainer.className = 'webItemIconContainer';
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
const webItemIcon = document.createElement('div');
webItemIcon.className = 'webItemIcon';
webItemIcon.style.background = `url('assets/webcons/${icon}.svg')`;
webItemIcon.style.backgroundSize = 'cover';
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Create webItemName
const webItemName = document.createElement('div');
webItemName.className = 'webItemName';
webItemName.innerHTML = site;
2020-06-09 04:02:03 +02:00
// Append divs with heirarchy
webItemDiv.appendChild(webItemContainer);
webItemContainer.appendChild(webItemBody);
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
webItemIconContainer.appendChild(webItemIcon);
webItemBody.appendChild(webItemIconContainer);
webItemBody.appendChild(webItemName);
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
aWebLink.appendChild(webItemDiv);
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
li.appendChild(aWebLink);
this._webMenuList.appendChild(li);
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Call to sort list
this._sortList();
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Allow fuzzy searching in web menu
_fuzzySearch = () => {
String.prototype.fuzzy = function(term, ratio) {
const string = this.toLowerCase();
const compare = term.toLowerCase();
let matches = 0;
// Covers basic partial matches
if (string.indexOf(compare) > -1) return true;
for (let i = 0; i < compare.length; i++) {
string.indexOf(compare[i]) > -1 ? matches += 1 : matches -=1;
}
return ((matches / this.length) >= ratio || term == '');
};
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Focus on searched item
_filterWebList = () => {
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
let input, filter, ul, li, a, i, txtValue;
input = webMenuSearchBox;
filter = input.value.toUpperCase();
ul = this._webMenuList;
li = ul.getElementsByTagName('li');
// Loop through all list items, and focus if matches the search query
for (let i = 0; i < li.length; i++) {
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
a = li[i].getElementsByClassName('webItemName')[0];
txtValue = a.innerHTML || a.textContent || a.innerText;
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// If an item match, hightlight it and focus
// if (txtValue.toUpperCase().indexOf(filter) !== -1) {
if (txtValue.toUpperCase().fuzzy(filter, 1) === true) {
// Unselect/Unhightlight old active
const oldWebItemFocus = this._webItemFocus;
const oldWebItemFocusChild = oldWebItemFocus.querySelector('.webItem');
oldWebItemFocusChild.classList.remove('webItemFocus');
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Update webItemFocus
this._webItemFocus = li[i];
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Update weblistindex
this._webListIndex = i;
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Get child
const webItemFocusChild = this._webItemFocus.querySelector('.webItem');
// Add webItemFocus class to child
webItemFocusChild.classList.add('webItemFocus');
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Scroll focus into active
this._webItemFocus.scrollIntoView();
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
}
}
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Reset focus/go back to item #1
_focusReset = () => {
const oldWebItemFocus = this._webItemFocus;
const oldWebItemFocusChild = oldWebItemFocus.querySelector('.webItem');
oldWebItemFocusChild.classList.remove('webItemFocus');
this._webListIndex = 0;
}
2020-06-06 12:16:34 +02:00
2020-06-09 04:02:03 +02:00
// Get item #1
_getFirstItem = () => {
const ul = this._webMenuList;
const li = ul.getElementsByTagName('li');
2020-06-06 12:16:34 +02:00
2020-06-09 04:02:03 +02:00
// Focus on first item
this._webItemFocus = li[0];
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
// Get child
const webItemFocusChildren = this._webItemFocus.querySelector('.webItem');
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Add webItemFocus class
webItemFocusChildren.classList.add('webItemFocus');
}
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
// Show web menu screen
showWebMenu = () => {
this._webMenu.classList.add('showWebMenu');
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
// Enable inputs
this._disableWebMenuInputs(false);
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
this._webMenuVisibility = !this._webMenuVisibility;
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
// Focus to input field
this._webMenuSearchBox.focus();
}
2020-06-06 12:16:34 +02:00
2020-06-09 04:02:03 +02:00
// Hide web menu screen
hideWebMenu = () => {
// Clear input field
this._webMenuSearchBox.value = '';
2020-06-06 12:16:34 +02:00
2020-06-09 04:02:03 +02:00
// Unfocus input field
this._webMenuSearchBox.blur();
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Refilter web list
this._filterWebList();
// Scroll to top
this._webMenuListContainer.scrollTop = 0;
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Reset focus item
this._focusReset();
2020-06-09 04:02:03 +02:00
// Get first item
this._getFirstItem();
this._webMenu.classList.remove('showWebMenu');
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Disable inputs
this._disableWebMenuInputs(true);
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
this._webMenuVisibility = !this._webMenuVisibility;
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Toggle web menu screen
toggleWebMenu = () => {
console.log('toggle web menu');
// If profile anim is still running,
// Return to avoid spam
if (profileImage.getProfileAnimationStatus()) return;
// Rotate profile
profileImage.rotateProfile();
if (this._webMenuVisibility) {
// Hide web menu
this.hideWebMenu();
} else {
// Show Web menu
this.showWebMenu();
}
// Check if any of these are open, if yes, close it
if (searchBoxContainer.classList.contains('showSearchBox')) {
console.log('searchbox is open, closing...');
searchBoxShow.hideSearchBox();
} else if (this._dashboard.classList.contains('showRightDashboard')) {
console.log('dashboard is open, closing...');
dashboard.hideDashboard();
} else if (this._weatherScreen.classList.contains('showWeatherScreen')) {
console.log('weather screen is open, closing...');
weatherScreen.hideWeatherScreen();
return;
}
// Toggle center box
centeredBox.toggleCenteredBox();
}
2020-06-04 07:43:09 +02:00
2020-06-09 04:02:03 +02:00
// Remove focus class
_removeClass = (el, className) => {
// Remove webItemFocus class
const oldWebItemFocus = el.querySelector('.webItem');
oldWebItemFocus.classList.remove('webItemFocus');
}
2020-06-06 01:31:43 +02:00
2020-06-09 04:02:03 +02:00
// Add focus class
_addClass = (el, className) => {
const webItemFocusChild = el.querySelector('.webItem');
2020-06-09 04:02:03 +02:00
// Add webItemFocus class to child
webItemFocusChild.classList.add('webItemFocus');
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
// Scroll focus into active
webItemFocusChild.scrollIntoView();
}
2020-06-09 04:02:03 +02:00
// Arrow key navigation
_navigateWithArrows = (key, len) => {
// assign constiables to key codes
const [right, left, down, up] = [39, 37, 40, 38];
const getIndexByWindowWidth = () => {
if (window.innerWidth <= 580) { return 1; }
// width of elements in pixels
const menuItemWidth = 138;
const scrollBarWidth = 10;
// viewport width
const vw = (unit) => window.innerWidth * (unit / 100);
// Gets the number of columns by dividing the screen width minus the padding, scroll width and
// average of menu item width by the menu item width
const containerWindow = ((window.innerWidth - (menuItemWidth / 2) - scrollBarWidth - vw(24)) / menuItemWidth);
// Get rounded result
return Math.round(containerWindow);
}
// Determine the index position by key
const changeWebListIndex = () => {
switch (key) {
case right:
this._webListIndex++;
// Clear web menu searchbox
this._webMenuSearchBox.value = '';
break;
case left:
this._webListIndex--;
// Clear web menu searchbox
this._webMenuSearchBox.value = '';
break;
case up:
this._webListIndex = this._webListIndex - getIndexByWindowWidth();
// Clear web menu searchbox
this._webMenuSearchBox.value = '';
break;
case down:
this._webListIndex = this._webListIndex + getIndexByWindowWidth();
// Clear web menu searchbox
this._webMenuSearchBox.value = '';
break;
}
}
const changeItemFocus = (condition, overFlowIndex) => {
const next = this._webMenuList.getElementsByTagName('li')[this._webListIndex];
if(typeof next !== undefined && condition) {
this._webItemFocus = next;
} else {
this._webListIndex = overFlowIndex;
this._webItemFocus = this._webMenuList.getElementsByTagName('li')[overFlowIndex];
}
}
const changeItemFocusByKey = () => {
if (key === right) { return changeItemFocus((this._webListIndex <= len), 0) }
if (key === left) { return changeItemFocus((this._webListIndex >= 0), len) }
if (key === up) { return changeItemFocus((this._webListIndex >= 0), len) }
if (key === down) { return changeItemFocus((this._webListIndex <= len), 0) }
}
2020-06-09 04:02:03 +02:00
changeWebListIndex();
if (this._webItemFocus) {
this._removeClass(this._webItemFocus, 'webItemFocus');
changeItemFocusByKey();
this._addClass(this._webItemFocus, 'webItemFocus');
// console.log(webListIndex);
} else {
2020-06-09 04:02:03 +02:00
this._webListIndex = 0;
this._webItemFocus = this._webMenuList.getElementsByTagName('li')[0];
this._addClass(this._webItemFocus, 'webItemFocus');
}
}
2020-06-09 04:02:03 +02:00
_webMenuKeyDownEvent = e => {
const len = this._webMenuList.getElementsByTagName('li').length - 1;
this._navigateWithArrows(e.which, len);
}
2020-06-09 04:02:03 +02:00
_registerWebMenuKeyDownEvent = () => {
this._webMenu.addEventListener('keydown', this._webMenuKeyDownEvent, false);
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
_webMenuSearchBoxKeyDownEvent = e => {
// Don't hijack keyboard navigation buttons (up, down, left, right)
if ((e.key === 'ArrowRight') || (e.key === 'ArrowDown') ||
(e.key === 'ArrowLeft') || (e.key === 'ArrowUp')) return;
if (e.key === 'Tab') return;
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
if (e.key === 'Enter' && this._webItemFocus) {
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
// Run the focused li's callback
this._webItemFocus.callback();
2020-06-09 04:02:03 +02:00
// Hide web menu
this.toggleWebMenu();
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
} else if (e.key === 'Backspace' && webMenuSearchBox.value.length < 1) {
// Hide web menu if backspace is pressed and searchbox value is 0
this.toggleWebMenu();
return;
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
} else if ((e.key === 'Escape') || (e.key === 'Alt')) {
// Ignore escape and alt key
return;
}
// Filter
this._filterWebList();
2020-06-04 07:27:12 +02:00
}
2020-06-09 04:02:03 +02:00
_registerWebMenuSearchBoxKeyDownEvent = () => {
this._webMenuSearchBox.onkeydown = this._webMenuSearchBoxKeyDownEvent;
}
2020-06-04 07:27:12 +02:00
2020-06-09 04:02:03 +02:00
_init = () => {
this._populateWebMenu();
this._getFirstItem();
2020-06-06 12:16:34 +02:00
2020-06-09 04:02:03 +02:00
// Disable inputs
this._disableWebMenuInputs(true);
this._registerWebMenuSearchBoxKeyDownEvent();
this._registerWebMenuKeyDownEvent();
}
2020-06-04 06:15:59 +02:00
2020-06-09 04:02:03 +02:00
}