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::config::*;
19use crate::renderer::*;
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 is_chart = ctx.props().renderer.is_chart();
45        let has_selection = ctx.props().renderer.get_selection().is_some();
46        html! {
47            <>
48                <div id="test" />
49                <DropDownMenu<ExportFile>
50                    values={Rc::new(get_menu_items(is_chart, has_selection))}
51                    callback={&ctx.props().callback}
52                />
53            </>
54        }
55    }
56}
57
58fn get_menu_items(is_chart: bool, has_selection: bool) -> Vec<CopyDropDownMenuItem> {
59    let mut items = vec![
60        CopyDropDownMenuItem::OptGroup(
61            "Current View".into(),
62            if is_chart {
63                vec![
64                    ExportMethod::Csv.new_file("clipboard", is_chart),
65                    ExportMethod::Json.new_file("clipboard", is_chart),
66                    ExportMethod::Ndjson.new_file("clipboard", is_chart),
67                    ExportMethod::Plugin.new_file("clipboard", is_chart),
68                ]
69            } else {
70                vec![
71                    ExportMethod::Csv.new_file("clipboard", is_chart),
72                    ExportMethod::Json.new_file("clipboard", is_chart),
73                    ExportMethod::Ndjson.new_file("clipboard", is_chart),
74                ]
75            },
76        ),
77        CopyDropDownMenuItem::OptGroup("All".into(), vec![
78            ExportMethod::CsvAll.new_file("clipboard", is_chart),
79            ExportMethod::JsonAll.new_file("clipboard", is_chart),
80            ExportMethod::NdjsonAll.new_file("clipboard", is_chart),
81        ]),
82        CopyDropDownMenuItem::OptGroup("Config".into(), vec![
83            ExportMethod::JsonConfig.new_file("clipboard", is_chart),
84        ]),
85    ];
86
87    if has_selection {
88        items.insert(
89            0,
90            CopyDropDownMenuItem::OptGroup(
91                "Current Selection".into(),
92                if is_chart {
93                    vec![
94                        ExportMethod::CsvSelected.new_file("clipboard", is_chart),
95                        ExportMethod::JsonSelected.new_file("clipboard", is_chart),
96                        ExportMethod::NdjsonSelected.new_file("clipboard", is_chart),
97                    ]
98                } else {
99                    vec![
100                        ExportMethod::CsvSelected.new_file("clipboard", is_chart),
101                        ExportMethod::JsonSelected.new_file("clipboard", is_chart),
102                        ExportMethod::NdjsonSelected.new_file("clipboard", is_chart),
103                        ExportMethod::Plugin.new_file("clipboard", is_chart),
104                    ]
105                },
106            ),
107        )
108    }
109
110    items
111}