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