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