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 pub fn update(&self, view: &perspective_js::View) -> ApiFuture<()> {
69 self.draw(view)
70 }
71
72 /// # Notes
73 ///
74 /// When you pass a `wasm_bindgen` wrapped type _into_ Rust, it acts like a
75 /// move. Ergo, if you replace the `&` in the `view` argument, the JS copy
76 /// of the `View` will be invalid
77 pub fn draw(&self, view: &perspective_js::View) -> ApiFuture<()> {
78 let css = "margin:0;overflow:scroll;position:absolute;width:100%;height:100%";
79 clone!(self.elem, view);
80 ApiFuture::new(async move {
81 let csv = view.to_csv(None).await?;
82 elem.style().set_property("background-color", "#fff")?;
83 elem.set_inner_html(&format!("<pre style='{}'>{}</pre>", css, csv));
84 Ok(())
85 })
86 }
87
88 pub fn clear(&self) -> ApiFuture<()> {
89 ApiFuture::default()
90 }
91
92 pub fn resize(&self) -> ApiFuture<()> {
93 ApiFuture::default()
94 }
95
96 pub fn restyle(&self) -> ApiFuture<()> {
97 ApiFuture::default()
98 }
99
100 pub fn save(&self) -> ApiFuture<()> {
101 ApiFuture::default()
102 }
103
104 pub fn restore(&self) -> ApiFuture<()> {
105 ApiFuture::default()
106 }
107
108 pub fn delete(&self) -> ApiFuture<()> {
109 ApiFuture::default()
110 }
111
112 #[wasm_bindgen(js_name = "connectedCallback")]
113 pub fn connected_callback(&self) {}
114}