perspective_viewer/components/
copy_dropdown.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 std::rc::Rc;
14
15use yew::prelude::*;
16
17use super::containers::dropdown_menu::*;
18use super::modal::*;
19use super::style::StyleProvider;
20use crate::model::*;
21use crate::renderer::*;
22use crate::utils::*;
23
24pub type CopyDropDownMenuMsg = DropDownMenuMsg;
25pub type CopyDropDownMenuItem = DropDownMenuItem<ExportFile>;
26
27#[derive(Properties, PartialEq)]
28pub struct CopyDropDownMenuProps {
29    pub renderer: Renderer,
30    pub callback: Callback<ExportFile>,
31
32    #[prop_or_default]
33    weak_link: WeakScope<CopyDropDownMenu>,
34}
35
36pub struct CopyDropDownMenu {
37    _sub: Subscription,
38}
39
40impl ModalLink<CopyDropDownMenu> for CopyDropDownMenuProps {
41    fn weak_link(&self) -> &'_ WeakScope<CopyDropDownMenu> {
42        &self.weak_link
43    }
44}
45
46impl Component for CopyDropDownMenu {
47    type Message = CopyDropDownMenuMsg;
48    type Properties = CopyDropDownMenuProps;
49
50    fn view(&self, ctx: &Context<Self>) -> yew::virtual_dom::VNode {
51        let plugin = ctx.props().renderer.get_active_plugin().unwrap();
52        // let has_render = js_sys::Reflect::has(&plugin,
53        // js_intern::js_intern!("render")).unwrap();
54        let is_chart = plugin.name().as_str() != "Datagrid";
55        let has_selection = ctx.props().renderer.get_selection().is_some();
56        html! {
57            <StyleProvider>
58                <DropDownMenu<ExportFile>
59                    values={Rc::new(get_menu_items(is_chart, has_selection))}
60                    callback={&ctx.props().callback}
61                />
62            </StyleProvider>
63        }
64    }
65
66    fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> bool {
67        true
68    }
69
70    fn create(ctx: &Context<Self>) -> Self {
71        ctx.set_modal_link();
72        let _sub = ctx
73            .props()
74            .renderer
75            .plugin_changed
76            .add_listener(ctx.link().callback(|_| ()));
77
78        Self { _sub }
79    }
80}
81
82fn get_menu_items(is_chart: bool, has_selection: bool) -> Vec<CopyDropDownMenuItem> {
83    let mut items = vec![
84        CopyDropDownMenuItem::OptGroup(
85            "Current View".into(),
86            if is_chart {
87                vec![
88                    ExportMethod::Csv.new_file("clipboard", is_chart),
89                    ExportMethod::Json.new_file("clipboard", is_chart),
90                    ExportMethod::Ndjson.new_file("clipboard", is_chart),
91                    ExportMethod::Plugin.new_file("clipboard", is_chart),
92                ]
93            } else {
94                vec![
95                    ExportMethod::Csv.new_file("clipboard", is_chart),
96                    ExportMethod::Json.new_file("clipboard", is_chart),
97                    ExportMethod::Ndjson.new_file("clipboard", is_chart),
98                ]
99            },
100        ),
101        CopyDropDownMenuItem::OptGroup("All".into(), vec![
102            ExportMethod::CsvAll.new_file("clipboard", is_chart),
103            ExportMethod::JsonAll.new_file("clipboard", is_chart),
104            ExportMethod::NdjsonAll.new_file("clipboard", is_chart),
105        ]),
106        CopyDropDownMenuItem::OptGroup("Config".into(), vec![
107            ExportMethod::JsonConfig.new_file("clipboard", is_chart),
108        ]),
109    ];
110
111    if has_selection {
112        items.insert(
113            0,
114            CopyDropDownMenuItem::OptGroup(
115                "Current Selection".into(),
116                if is_chart {
117                    vec![
118                        ExportMethod::CsvSelected.new_file("clipboard", is_chart),
119                        ExportMethod::JsonSelected.new_file("clipboard", is_chart),
120                        ExportMethod::NdjsonSelected.new_file("clipboard", is_chart),
121                    ]
122                } else {
123                    vec![
124                        ExportMethod::CsvSelected.new_file("clipboard", is_chart),
125                        ExportMethod::JsonSelected.new_file("clipboard", is_chart),
126                        ExportMethod::NdjsonSelected.new_file("clipboard", is_chart),
127                        ExportMethod::Plugin.new_file("clipboard", is_chart),
128                    ]
129                },
130            ),
131        )
132    }
133
134    items
135}