Skip to main content

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