|
| 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 type * as pspViewer from "@finos/perspective-viewer"; |
| 42 | +import * as Workspace from "@finos/perspective-workspace"; |
| 43 | + |
| 44 | +const CLIENT = await perspective.worker(); |
| 45 | + |
| 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 | +await customElements.whenDefined("perspective-workspace"); |
| 53 | +const ready = document.createElement("div"); |
| 54 | +ready.innerText = "READY"; |
| 55 | +document.querySelector("nav.header")?.appendChild(ready); |
| 56 | + |
| 57 | +const ws = document.createElement("perspective-workspace"); |
| 58 | +document.body.appendChild(ws); |
| 59 | + |
| 60 | +const tablesMain: Record<string, Promise<psp.Table>> = {}; |
| 61 | +const tablesSwap: Record<string, Promise<psp.Table>> = {}; |
| 62 | + |
| 63 | +let layout: PerspectiveWorkspaceConfig<string> = { |
| 64 | + sizes: [1], |
| 65 | + viewers: {}, |
| 66 | + detail: undefined, |
| 67 | +}; |
| 68 | +let swap = false; |
| 69 | + |
| 70 | +function swapTables() { |
| 71 | + console.warn("DDD VR Swapping"); |
| 72 | + swap = !swap; |
| 73 | + ws.replaceTables(swap ? tablesMain : tablesSwap); |
| 74 | +} |
| 75 | + |
| 76 | +function addViewer() { |
| 77 | + console.warn("DDD VR Adding"); |
| 78 | + const name = window.crypto.randomUUID().slice(0, 7); |
| 79 | + tablesMain[name] = CLIENT.table("a,b,c\n1,2,3"); |
| 80 | + tablesSwap[name] = CLIENT.table("a,b,c\n4,5,6\n7,8,9"); |
| 81 | + ws.addTable(name, swap ? tablesMain[name] : tablesSwap[name]); |
| 82 | + layout = Workspace.addViewer(layout, { table: name, title: name }); |
| 83 | + ws.restore(layout); |
| 84 | +} |
| 85 | + |
| 86 | +document.querySelector("button.swap")?.addEventListener("click", () => { |
| 87 | + swapTables(); |
| 88 | +}); |
| 89 | +document.querySelector("button.add")?.addEventListener("click", () => { |
| 90 | + addViewer(); |
| 91 | +}); |
| 92 | + |
| 93 | +addViewer(); |
| 94 | +swapTables(); |
| 95 | +addViewer(); |
| 96 | +swapTables(); |
| 97 | + |
0 commit comments