/* dashboard.js - ESP3D WebUI navigation page file Copyright (c) 2020 Luc Lebosse. All rights reserved. This code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with This code; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ import { Fragment, FunctionalComponent, JSX } from "preact" import { useEffect, useState, useRef } from "preact/hooks" import { useUiContext, useUiContextFn, useModalsContext } from "../../contexts" import { T } from "../../components/Translations" import { List, CheckCircle, Circle, HelpCircle } from "preact-feather" import { iconsFeather } from "../../components/Images" import { defaultPanelsList, iconsTarget, QuickButtonsBar } from "../../targets" import { ExtraPanelElement } from "../../components/Panels/ExtraPanel" import { showModal } from "../../components/Modal" interface KeyTracker { keybListenerCounter: number keyState: number lastcall: Date lastkey: string } interface FixedPanel { index: number id: string } const fixedPanels: FixedPanel[] = [] const keyTracker: KeyTracker = { keybListenerCounter: 0, keyState: 0, lastcall: new Date(), lastkey: "", } //Need to put outside of Dashboard object to be sure add/remove alsways use same address const keyboardEventHandlerUp = (e: KeyboardEvent): void => { keyTracker.keyState = 0 } const keyboardEventHandlerDown = (e: KeyboardEvent): void => { // Bail if User is actively typing text. We don't want to disrupt them entering gcode. if ( document.activeElement && document.activeElement.tagName == "INPUT" && ((document.activeElement as HTMLInputElement).type == "text" || (document.activeElement as HTMLInputElement).type == "number") ) { return } let keyval = "" if (e.ctrlKey) keyval += "Control+" if (e.altKey) keyval += "Alt+" if (e.shiftKey) keyval += "Shift+" if (e.metaKey) keyval += "Meta+" if ( !( e.key == "Control" || e.key == "Alt" || e.key == "Shift" || e.key == "Meta" ) ) keyval += e.key.toUpperCase() let cmdMatch: string | null = null const keysRefs: string[] = ["keymap", "macros"] keysRefs.forEach((list: string) => { const keyMapObj = useUiContextFn.getValue(list) if (keyMapObj) { keyMapObj.forEach((element: any) => { element.value.forEach((sub: any) => { if ( sub.name == "key" && sub.value && sub.value.length > 0 && sub.value?.toUpperCase() == keyval?.toUpperCase() && document.getElementById(element.id) ) { cmdMatch = element.id } }) }) } }) //console.log("KeyMap override match, key = " + e.key + ", cmd= " + cmdMatch) if (cmdMatch) { e.preventDefault() const autorepeat = useUiContextFn.getValue("enableautorepeat") if (keyTracker.keyState == 1 && !autorepeat) { return } keyTracker.keyState = 1 const delay = useUiContextFn.getValue("autorepeatdelay") if (delay && autorepeat && keyTracker.lastkey == keyval) { let t = new Date() if (t.getTime() - keyTracker.lastcall.getTime() < delay) { return } keyTracker.lastcall = new Date() } keyTracker.lastkey = keyval const element = document.getElementById(cmdMatch) if (element) { element.click() } } } let intialisationDone = false const Dashboard: FunctionalComponent = (): JSX.Element => { console.log("Dashboard") const iconsList: Record = { ...iconsTarget, ...iconsFeather } const { panels, uisettings, shortcuts } = useUiContext() const { modals } = useModalsContext() const menuPanelsList = useRef(null) const isfixed = uisettings.getValue("fixedpanels") const [isKeyboardEnabled, setIsKeyboardEnabled] = useState( shortcuts.enabled ) //Show keyboard mapped keys const showKeyboarHelp = (): void => { useUiContextFn.haptic() const keysRefs: string[] = ["keymap", "macros"] const helpKeyboardJog: JSX.Element[] = [] keysRefs.forEach((list: string) => { const keyMapObj = useUiContextFn.getValue(list) if (keyMapObj) { keyMapObj.forEach((element: any) => { const help: any = {} element.value.forEach((sub: any) => { if (sub.name == "key") { help.key = sub.value } if (sub.name == "name") { help.name = sub.value } }) if (document.getElementById(element.id)) helpKeyboardJog.push( {T(help.name)} [{T(help.key)}] ) }) } }) showModal({ modals, id: "keyboardhelp", title: T("S216"), button1: { text: T("S24"), }, icon: , content: {helpKeyboardJog}
, }) } //Add keyboard listener const AddKeyboardListener = (): void => { if (keyTracker.keybListenerCounter == 0) { window.addEventListener("keydown", keyboardEventHandlerDown as EventListener, true) window.addEventListener("keyup", keyboardEventHandlerUp as EventListener, true) keyTracker.keybListenerCounter++ } } //Remove keyboard listener const RemoveKeyboardListener = (): void => { if (keyTracker.keybListenerCounter != 0) { window.removeEventListener( "keydown", keyboardEventHandlerDown as EventListener, true ) window.removeEventListener("keyup", keyboardEventHandlerUp as EventListener, true) keyTracker.keybListenerCounter-- } } useEffect(() => { if (!intialisationDone) { //console.log("Init") intialisationDone = true //console.log(uisettings.getValue("enableshortcuts")) setIsKeyboardEnabled(uisettings.getValue("enableshortcuts")) shortcuts.enable(uisettings.getValue("enableshortcuts")) } else { //console.log("Init Done") } return () => { //console.log("Unmount dashboard") } }, []) useEffect(() => { if (shortcuts.enabled) { AddKeyboardListener() } return () => { if (shortcuts.enabled) { RemoveKeyboardListener() } } }, [shortcuts.enabled]) useEffect(() => { if (!panels.initDone && panels.list.length != 0) { if (isfixed && fixedPanels.length == 0) { const panelOrder = uisettings.getValue("panelsorder") panelOrder.forEach((panel: any) => { fixedPanels.push({ index: panel.index, id: panel.value[0].value, }) }) panels.setPanelsOrder(fixedPanels) const newList = fixedPanels.reduce((acc: any[], panel) => { const paneldesc = panels.list.filter( (p: any) => p.settingid == panel.id ) if (paneldesc.length > 0) acc.push(...paneldesc) return acc }, []) panels.set([...newList]) panels.setVisibles( newList.reduce((acc: any[], curr: any) => { if ( uisettings.getValue(curr.onstart) && uisettings.getValue(curr.show) ) acc.push(curr) return acc }, []) ) } else { panels.setVisibles( panels.list.reduce((acc: any[], curr: any) => { if ( uisettings.getValue(curr.onstart) && uisettings.getValue(curr.show) ) acc.push(curr) return acc }, []) ) } panels.setInitDone(true) } else { //now remove if any visible that is not in list panels.visibles.forEach((element) => { if (!panels.list.find((panel) => panel.id == element.id)) panels.hide(element.id) }) } }) useEffect(() => { if (uisettings.getValue("showextracontents")) { const extraContents = uisettings.getValue("extracontents") const extraPanelsList = extraContents.reduce((acc: any[], curr: any) => { const item = curr.value.reduce((accumulator: any, current: any) => { accumulator[current.name] = current.initial return accumulator }, {}) if (item.target == "panel") { acc.push(ExtraPanelElement(item, curr.id)) } return acc }, []) panels.set([...defaultPanelsList, ...extraPanelsList]) } else { panels.set([...defaultPanelsList]) } /* */ //now remove if any visible that is not in list panels.visibles.forEach((element: any) => { if (!uisettings.getValue(element.show)) panels.hide(element.id) }) }, []) return (
{panels.list.length > 0 && ( )}
{panels.visibles.map((panel: any) => { return {panel.content as any} })}
) } export default Dashboard