Files
@ 7888cfff657b
Branch filter:
Location: light9/web/TiledHome.ts
7888cfff657b
3.6 KiB
video/MP2T
+ py-spy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | import debug from "debug";
import * as FlexLayout from "flexlayout-react";
import { LitElement, TemplateResult, html } from "lit";
import { customElement } from "lit/decorators.js";
import * as React from "react";
import { createRoot } from "react-dom/client";
import { getTopGraph } from "./RdfdbSyncedGraph";
import { SyncedGraph } from "./SyncedGraph";
import { panelDisplayName, panelElementNames, panelUrl } from "./panels";
export { RdfdbSyncedGraph } from "./RdfdbSyncedGraph";
const log = debug("home");
// see https://github.com/lit/lit/tree/main/packages/labs/react
// Store flexlayout panels in per-browser localstorage.
class PersistentLayout {
key = "light9.home.layout";
defaultLayout: FlexLayout.IJsonRowNode = {
type: "row",
children: [
{
type: "tabset",
weight: 50,
children: [{ type: "tab", name: "fade", component: "light9-fade-ui" }],
},
{
type: "tabset",
weight: 50,
children: [{ type: "tab", name: "devsettings", component: "light9-device-settings" }],
},
],
};
getOrDefault(): FlexLayout.IJsonRowNode {
let savedLayout = localStorage.getItem(this.key);
if (savedLayout === null) {
return this.defaultLayout;
}
return JSON.parse(savedLayout);
}
save(layout: FlexLayout.IJsonRowNode) {
localStorage.setItem(this.key, JSON.stringify(layout));
}
}
// This lets lit call a method on a react element.
let addTab: (component: string) => void;
class Main extends React.Component {
state: { model: FlexLayout.Model; persistence: PersistentLayout };
constructor(props: any) {
super(props);
const persistence = new PersistentLayout();
const config = {
global: {
tabEnableRename: false,
},
borders: [],
layout: persistence.getOrDefault(),
};
this.state = { model: FlexLayout.Model.fromJson(config), persistence: persistence };
}
factory = (node: any) => {
var component = node.getComponent();
return React.createElement(component, null, "");
};
render() {
addTab = (component) => {
const name = panelDisplayName(component);
if (name === undefined) throw new Error("no such panel: " + component);
const newTab = { type: "tab", name: name, component: component };
const firstTabSet = this.state.model.getRoot().getChildren()[0];
const action = FlexLayout.Actions.addNode(newTab, firstTabSet.getId(), FlexLayout.DockLocation.LEFT, 0);
this.state.model.doAction(action);
};
return React.createElement(FlexLayout.Layout, {
model: this.state.model,
realtimeResize: true,
onModelChange: this.onModelChange.bind(this),
factory: this.factory,
});
}
onModelChange() {
this.state.persistence.save(this.state.model.toJson().layout);
}
}
@customElement("light9-home-status")
export class Light9HomeStatus extends LitElement {
graph!: SyncedGraph;
render() {
return html`
<rdfdb-synced-graph></rdfdb-synced-graph>
<a href="metrics/">metrics</a>
Open tab or new window: ${panelElementNames().map((elem) => this.linkToPanelPage(elem))}
`;
}
linkToPanelPage(elem: string): TemplateResult {
return html`<a @click=${this.onClickPanelLink} data-panel-elem="${elem}" href=${panelUrl(elem)}> ${panelDisplayName(elem)} </a> `;
}
constructor() {
super();
getTopGraph().then((g) => {
this.graph = g;
});
}
onClickPanelLink(ev: MouseEvent) {
ev.preventDefault();
const a = ev.target as HTMLAnchorElement;
const elem = a.dataset.panelElem!;
addTab(elem);
}
}
const root = createRoot(document.getElementById("container")!);
root.render(React.createElement(Main));
|