Skip to main content

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