Skip to main content

perspective_viewer/custom_elements/
debug_plugin.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
13use wasm_bindgen::prelude::*;
14
15use crate::utils::*;
16use crate::*;
17
18/// The `<perspective-viewer-plugin>` element.
19///
20/// The default perspective plugin which is registered and activated
21/// automcatically when a `<perspective-viewer>` is loaded without plugins.
22/// While you will not typically instantiate this class directly, it is simple
23/// enough to function as a good "default" plugin implementation which can be
24/// extended to create custom plugins.
25///
26/// # Example
27/// ```javascript
28/// class MyPlugin extends customElements.get("perspective-viewer-plugin") {
29///    // Custom plugin overrides
30/// }
31/// ```
32#[wasm_bindgen]
33pub struct PerspectiveDebugPluginElement {
34    elem: web_sys::HtmlElement,
35}
36
37impl CustomElementMetadata for PerspectiveDebugPluginElement {
38    const CUSTOM_ELEMENT_NAME: &'static str = "perspective-viewer-plugin";
39}
40
41#[wasm_bindgen]
42impl PerspectiveDebugPluginElement {
43    #[wasm_bindgen(constructor)]
44    pub fn new(elem: web_sys::HtmlElement) -> Self {
45        Self { elem }
46    }
47
48    #[wasm_bindgen(getter)]
49    pub fn name(&self) -> String {
50        "Debug".to_owned()
51    }
52
53    #[wasm_bindgen(getter)]
54    pub fn select_mode(&self) -> String {
55        "select".to_owned()
56    }
57
58    #[wasm_bindgen(getter)]
59    pub fn min_config_columns(&self) -> JsValue {
60        JsValue::UNDEFINED
61    }
62
63    #[wasm_bindgen(getter)]
64    pub fn config_column_names(&self) -> JsValue {
65        JsValue::UNDEFINED
66    }
67
68    /// Delegates to `draw()` VIRTUALLY — through the JS element's `draw`
69    /// property, never `self.draw(view)` (Rust static dispatch). This
70    /// element is the documented base class for custom plugins
71    /// (`class MyPlugin extends
72    /// customElements.get("perspective-viewer-plugin")`), whose contract is
73    /// "`update()` defaults to dispatch to `draw()`" — a
74    /// subclass overriding only `draw` must receive `update`-path repaints
75    /// (`BindDisposition::Unchanged`/`Adopted` runs,
76    /// `PLUGIN_DRAW_INVARIANT_PLAN.md`); the static call bypassed the
77    /// override and repainted the Debug CSV instead (the
78    /// `view_lifecycle.spec` regression).
79    pub fn update(&self, view: &perspective_js::View) -> ApiFuture<()> {
80        clone!(self.elem, view);
81        ApiFuture::new(async move {
82            let draw = js_sys::Reflect::get(&elem, &JsValue::from_str("draw"))?
83                .dyn_into::<js_sys::Function>()?;
84
85            let task = draw.call1(&elem, &JsValue::from(view))?;
86            wasm_bindgen_futures::JsFuture::from(js_sys::Promise::resolve(&task)).await?;
87            Ok(())
88        })
89    }
90
91    /// # Notes
92    ///
93    /// When you pass a `wasm_bindgen` wrapped type _into_ Rust, it acts like a
94    /// move. Ergo, if you replace the `&` in the `view` argument, the JS copy
95    /// of the `View` will be invalid
96    pub fn draw(&self, view: &perspective_js::View) -> ApiFuture<()> {
97        let css = "margin:0;overflow:scroll;position:absolute;width:100%;height:100%";
98        clone!(self.elem, view);
99        ApiFuture::new(async move {
100            let csv = view.to_csv(None).await?;
101            elem.style().set_property("background-color", "#fff")?;
102            elem.set_inner_html(&format!("<pre style='{css}'>{csv}</pre>"));
103            Ok(())
104        })
105    }
106
107    pub fn clear(&self) -> ApiFuture<()> {
108        ApiFuture::default()
109    }
110
111    pub fn resize(&self) -> ApiFuture<()> {
112        ApiFuture::default()
113    }
114
115    pub fn restyle(&self) {}
116
117    pub fn save(&self) -> ApiResult<JsValue> {
118        Ok(JsValue::null())
119    }
120
121    pub fn restore(&self, _config: Option<JsValue>) -> ApiResult<()> {
122        Ok(())
123    }
124
125    pub fn delete(&self) -> ApiFuture<()> {
126        ApiFuture::default()
127    }
128
129    #[wasm_bindgen(js_name = "connectedCallback")]
130    pub fn connected_callback(&self) {}
131}