perspective_viewer/
lib.rs

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//! The API for the [`@finos/perspective-viewer`]() JavaScript library.
14
15// Required by yew's `html` macro.
16#![recursion_limit = "1024"]
17#![feature(const_type_name)]
18#![feature(let_chains)]
19#![feature(macro_metavar_expr)]
20#![feature(iter_intersperse)]
21#![feature(stmt_expr_attributes)]
22#![warn(
23    clippy::all,
24    clippy::panic_in_result_fn,
25    clippy::await_holding_refcell_ref
26)]
27
28pub mod components;
29pub mod config;
30pub mod custom_elements;
31mod custom_events;
32mod dragdrop;
33pub mod exprtk;
34mod js;
35
36#[doc(hidden)]
37pub mod model;
38mod presentation;
39mod renderer;
40mod session;
41pub mod utils;
42
43use perspective_js::utils::*;
44use wasm_bindgen::prelude::*;
45
46use crate::custom_elements::copy_dropdown::CopyDropDownMenuElement;
47use crate::custom_elements::debug_plugin::PerspectiveDebugPluginElement;
48use crate::custom_elements::export_dropdown::ExportDropDownMenuElement;
49use crate::custom_elements::viewer::PerspectiveViewerElement;
50use crate::utils::define_web_component;
51
52#[wasm_bindgen(typescript_custom_section)]
53const TS_APPEND_CONTENT: &'static str = r#"
54import type {
55    TableInitOptions, 
56    ViewWindow, 
57    OnUpdateOptions,
58    UpdateOptions, 
59    ViewConfigUpdate,
60} from "@finos/perspective";
61"#;
62
63/// Register a plugin globally.
64#[wasm_bindgen]
65#[allow(non_snake_case)]
66pub fn registerPlugin(name: &str) {
67    use crate::renderer::*;
68    PLUGIN_REGISTRY.register_plugin(name);
69}
70
71/// Register this crate's Custom Elements in the browser's current session.
72///
73/// This must occur before calling any public API methods on these Custom
74/// Elements from JavaScript, as the methods themselves won't be defined yet.
75/// By default, this crate does not register `PerspectiveViewerElement` (as to
76/// preserve backwards-compatible synchronous API).
77#[cfg(not(feature = "external-bootstrap"))]
78#[wasm_bindgen(js_name = "init")]
79pub fn js_init() {
80    perspective_js::utils::set_global_logging();
81    define_web_components!("export * as psp from '../../perspective-viewer.js'");
82    tracing::info!("Perspective initialized.");
83}
84
85/// Register Web Components with the global registry, given a Perspective
86/// module.
87///
88/// This function shouldn't be called directly;  instead, use the
89/// `define_web_components!` macro to both call this method and hook the
90/// wasm_bindgen module object.
91pub fn bootstrap_web_components(psp: &JsValue) {
92    define_web_component::<PerspectiveViewerElement>(psp);
93    define_web_component::<PerspectiveDebugPluginElement>(psp);
94
95    define_web_component::<ExportDropDownMenuElement>(psp);
96    define_web_component::<CopyDropDownMenuElement>(psp);
97}
98
99#[macro_export]
100macro_rules! define_web_components {
101    ($x:expr) => {{
102        #[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
103        extern "C" {
104            #[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
105            #[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
106            pub static PSP: wasm_bindgen::prelude::JsValue;
107        }
108
109        PSP.with(|x| $crate::bootstrap_web_components(x));
110    }};
111}