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    DeleteOptions,
61    ViewConfigUpdate,
62} from "@finos/perspective";
63"#;
64
65/// Register a plugin globally.
66#[wasm_bindgen]
67#[allow(non_snake_case)]
68pub fn registerPlugin(name: &str) {
69    use crate::renderer::*;
70    PLUGIN_REGISTRY.register_plugin(name);
71}
72
73/// Register this crate's Custom Elements in the browser's current session.
74///
75/// This must occur before calling any public API methods on these Custom
76/// Elements from JavaScript, as the methods themselves won't be defined yet.
77/// By default, this crate does not register `PerspectiveViewerElement` (as to
78/// preserve backwards-compatible synchronous API).
79#[cfg(not(feature = "external-bootstrap"))]
80#[wasm_bindgen(js_name = "init")]
81pub fn js_init() {
82    perspective_js::utils::set_global_logging();
83    define_web_components!("export * as psp from '../../perspective-viewer.js'");
84    tracing::info!("Perspective initialized.");
85}
86
87/// Register Web Components with the global registry, given a Perspective
88/// module.
89///
90/// This function shouldn't be called directly;  instead, use the
91/// `define_web_components!` macro to both call this method and hook the
92/// wasm_bindgen module object.
93pub fn bootstrap_web_components(psp: &JsValue) {
94    define_web_component::<PerspectiveViewerElement>(psp);
95    define_web_component::<PerspectiveDebugPluginElement>(psp);
96
97    define_web_component::<ExportDropDownMenuElement>(psp);
98    define_web_component::<CopyDropDownMenuElement>(psp);
99}
100
101#[macro_export]
102macro_rules! define_web_components {
103    ($x:expr) => {{
104        #[wasm_bindgen::prelude::wasm_bindgen(inline_js = $x)]
105        extern "C" {
106            #[wasm_bindgen::prelude::wasm_bindgen(js_name = "psp")]
107            #[wasm_bindgen::prelude::wasm_bindgen(thread_local_v2)]
108            pub static PSP: wasm_bindgen::prelude::JsValue;
109        }
110
111        PSP.with(|x| $crate::bootstrap_web_components(x));
112    }};
113}