|
| 1 | +// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +// ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +// ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | +// # [Perspective bootstrapping](https://perspective.finos.org/guide/how_to/javascript/importing.html) |
| 14 | + |
| 15 | +// Here we're initializing the WASM interpreter that powers the perspective API |
| 16 | +// and viewer, as covered in the [user guide section on bundling](https://perspective.finos.org/guide/how_to/javascript/importing.html). |
| 17 | +// This example is written assuming that the bundler is configured |
| 18 | +// to treat these files as a "file" and returns a path as the default export. |
| 19 | +// Use ./build.js as an example. The type stubs are in ./globals.d.ts |
| 20 | + |
| 21 | +import perspective from "@finos/perspective"; |
| 22 | +import perspective_viewer from "@finos/perspective-viewer"; |
| 23 | +import "@finos/perspective-viewer-datagrid"; |
| 24 | +import "@finos/perspective-viewer-d3fc"; |
| 25 | + |
| 26 | +import SERVER_WASM from "@finos/perspective/dist/wasm/perspective-server.wasm"; |
| 27 | +import CLIENT_WASM from "@finos/perspective-viewer/dist/wasm/perspective-viewer.wasm"; |
| 28 | + |
| 29 | +await Promise.all([ |
| 30 | + perspective.init_server(fetch(SERVER_WASM)), |
| 31 | + perspective_viewer.init_client(fetch(CLIENT_WASM)), |
| 32 | +]); |
| 33 | + |
| 34 | +// # Data Source |
| 35 | + |
| 36 | +// Data source creates a static Web Worker instance of Perspective engine, and a |
| 37 | +// table creation function which both downloads data and loads it into the |
| 38 | +// engine. |
| 39 | + |
| 40 | +import type * as psp from "@finos/perspective"; |
| 41 | +import * as Workspace from "@finos/perspective-workspace"; |
| 42 | + |
| 43 | +import * as React from "react"; |
| 44 | +import { createRoot } from "react-dom/client"; |
| 45 | +import { PerspectiveWorkspace } from "@finos/perspective-react"; |
| 46 | +import { PerspectiveWorkspaceConfig } from "@finos/perspective-workspace"; |
| 47 | + |
| 48 | +import "@finos/perspective-viewer/dist/css/pro.css"; |
| 49 | +import "@finos/perspective-workspace/dist/css/pro.css"; |
| 50 | +import "./index.css"; |
| 51 | + |
| 52 | +const CLIENT = await perspective.worker(); |
| 53 | + |
| 54 | +interface WorkspaceState { |
| 55 | + mounted: boolean; |
| 56 | + config: PerspectiveWorkspaceConfig<string>; |
| 57 | + tables: Record<string, Promise<psp.Table>>; |
| 58 | + /// This object is kept for the 'swap tables' button. |
| 59 | + /// It is a backup set of tables that correspond in keys to `tables` |
| 60 | + /// but with different data. |
| 61 | + swapTables: Record<string, Promise<psp.Table>>; |
| 62 | + /// if false use `tables` and true use `swapTables` in the workspace |
| 63 | + swap: boolean; |
| 64 | +} |
| 65 | + |
| 66 | +const WorkspaceApp: React.FC = () => { |
| 67 | + const [state, setState] = React.useState<WorkspaceState>({ |
| 68 | + mounted: true, |
| 69 | + tables: {}, |
| 70 | + swapTables: {}, |
| 71 | + config: { |
| 72 | + sizes: [], |
| 73 | + viewers: {}, |
| 74 | + detail: undefined, |
| 75 | + }, |
| 76 | + swap: false, |
| 77 | + }); |
| 78 | + |
| 79 | + const onClickAddViewer = React.useCallback(async () => { |
| 80 | + const name = window.crypto.randomUUID().slice(0, 8); |
| 81 | + const data = `a,b,c\n${Math.random()},${Math.random()},${Math.random()}`; |
| 82 | + const swapData = `a,b,c\n${Math.random()},${Math.random()},${Math.random()}\n${Math.random()},${Math.random()},${Math.random()}`; |
| 83 | + // dont assign internal names to the tables they are not used by the workspace |
| 84 | + const t = CLIENT.table(data); |
| 85 | + const swap = CLIENT.table(swapData); |
| 86 | + const config = Workspace.addViewer(state.config, { |
| 87 | + table: name, |
| 88 | + title: name, |
| 89 | + }); |
| 90 | + const tables = { ...state.tables, [name]: t }; |
| 91 | + const swapTables = { ...state.swapTables, [name]: swap }; |
| 92 | + setState({ |
| 93 | + ...state, |
| 94 | + tables, |
| 95 | + config, |
| 96 | + swapTables, |
| 97 | + }); |
| 98 | + }, [state]); |
| 99 | + |
| 100 | + const onLayoutUpdate: (detail: { |
| 101 | + layout: PerspectiveWorkspaceConfig<string>; |
| 102 | + tables: Record<string, psp.Table | Promise<psp.Table>>; |
| 103 | + }) => void = React.useCallback( |
| 104 | + ({ layout, tables }) => { |
| 105 | + const newTables = Object.fromEntries( |
| 106 | + Object.entries(tables).map(([k, v]) => [k, Promise.resolve(v)]) |
| 107 | + ); |
| 108 | + setState({ |
| 109 | + ...state, |
| 110 | + config: layout, |
| 111 | + tables: state.swap ? state.tables : newTables, |
| 112 | + swapTables: state.swap ? newTables : state.swapTables, |
| 113 | + }); |
| 114 | + }, |
| 115 | + [state] |
| 116 | + ); |
| 117 | + |
| 118 | + const onClickToggleMount = () => |
| 119 | + setState((old) => ({ ...old, mounted: !state.mounted })); |
| 120 | + |
| 121 | + // swaps the tables out but uses the same name of them. |
| 122 | + // this keeps the layout the same, but the data within each viewer changes |
| 123 | + const swapTables = React.useCallback(() => { |
| 124 | + setState({ |
| 125 | + ...state, |
| 126 | + swap: !state.swap, |
| 127 | + }); |
| 128 | + }, [state]); |
| 129 | + |
| 130 | + return ( |
| 131 | + <div className="workspace-container"> |
| 132 | + <div className="workspace-toolbar"> |
| 133 | + <button className="toggle-mount" onClick={onClickToggleMount}> |
| 134 | + Toggle Mount |
| 135 | + </button> |
| 136 | + <button className="add-viewer" onClick={onClickAddViewer}> |
| 137 | + Add Viewer |
| 138 | + </button> |
| 139 | + <button className="swap" onClick={swapTables}> |
| 140 | + Swap underlying tables |
| 141 | + </button> |
| 142 | + </div> |
| 143 | + {state.mounted && ( |
| 144 | + <PerspectiveWorkspace |
| 145 | + tables={state.swap ? state.swapTables : state.tables} |
| 146 | + config={state.config} |
| 147 | + onLayoutUpdate={onLayoutUpdate} |
| 148 | + /> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + ); |
| 152 | +}; |
| 153 | + |
| 154 | +createRoot(document.getElementById("root")!).render(<WorkspaceApp />); |
0 commit comments