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