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 crate::renderer::*;
19use crate::tasks::*;
20
21type CopyDropDownMenuItem = DropDownMenuItem<ExportFile>;
22
23#[derive(Properties, PartialEq)]
24pub struct CopyDropDownMenuProps {
25    pub callback: Callback<ExportFile>,
26    pub renderer: Renderer,
27}
28
29pub struct CopyDropDownMenu {}
30
31impl Component for CopyDropDownMenu {
32    type Message = ();
33    type Properties = CopyDropDownMenuProps;
34
35    fn create(_ctx: &Context<Self>) -> Self {
36        Self {}
37    }
38
39    fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> bool {
40        true
41    }
42
43    fn view(&self, ctx: &Context<Self>) -> yew::virtual_dom::VNode {
44        let plugin = ctx.props().renderer.get_active_plugin().unwrap();
45        let is_chart = plugin.name().as_str() != "Datagrid";
46        let has_selection = ctx.props().renderer.get_selection().is_some();
47        html! {
48            <>
49                <div id="test" />
50                <DropDownMenu<ExportFile>
51                    values={Rc::new(get_menu_items(is_chart, has_selection))}
52                    callback={&ctx.props().callback}
53                />
54            </>
55        }
56    }
57}
58
59fn get_menu_items(is_chart: bool, has_selection: bool) -> Vec<CopyDropDownMenuItem> {
60    let mut items = vec![
61        CopyDropDownMenuItem::OptGroup(
62            "Current View".into(),
63            if is_chart {
64                vec![
65                    ExportMethod::Csv.new_file("clipboard", is_chart),
66                    ExportMethod::Json.new_file("clipboard", is_chart),
67                    ExportMethod::Ndjson.new_file("clipboard", is_chart),
68                    ExportMethod::Plugin.new_file("clipboard", is_chart),
69                ]
70            } else {
71                vec![
72                    ExportMethod::Csv.new_file("clipboard", is_chart),
73                    ExportMethod::Json.new_file("clipboard", is_chart),
74                    ExportMethod::Ndjson.new_file("clipboard", is_chart),
75                ]
76            },
77        ),
78        CopyDropDownMenuItem::OptGroup("All".into(), vec![
79            ExportMethod::CsvAll.new_file("clipboard", is_chart),
80            ExportMethod::JsonAll.new_file("clipboard", is_chart),
81            ExportMethod::NdjsonAll.new_file("clipboard", is_chart),
82        ]),
83        CopyDropDownMenuItem::OptGroup("Config".into(), vec![
84            ExportMethod::JsonConfig.new_file("clipboard", is_chart),
85        ]),
86    ];
87
88    if has_selection {
89        items.insert(
90            0,
91            CopyDropDownMenuItem::OptGroup(
92                "Current Selection".into(),
93                if is_chart {
94                    vec![
95                        ExportMethod::CsvSelected.new_file("clipboard", is_chart),
96                        ExportMethod::JsonSelected.new_file("clipboard", is_chart),
97                        ExportMethod::NdjsonSelected.new_file("clipboard", is_chart),
98                    ]
99                } else {
100                    vec![
101                        ExportMethod::CsvSelected.new_file("clipboard", is_chart),
102                        ExportMethod::JsonSelected.new_file("clipboard", is_chart),
103                        ExportMethod::NdjsonSelected.new_file("clipboard", is_chart),
104                        ExportMethod::Plugin.new_file("clipboard", is_chart),
105                    ]
106                },
107            ),
108        )
109    }
110
111    items
112}