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<ExportMethod>;
26
27#[derive(Properties, PartialEq)]
28pub struct CopyDropDownMenuProps {
29    pub renderer: Renderer,
30    pub callback: Callback<ExportMethod>,
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, js_intern::js_intern!("render")).unwrap();
53        let has_selection = ctx.props().renderer.get_selection().is_some();
54        html! {
55            <StyleProvider>
56                <DropDownMenu<ExportMethod>
57                    values={Rc::new(get_menu_items(has_render, has_selection))}
58                    callback={&ctx.props().callback}
59                />
60            </StyleProvider>
61        }
62    }
63
64    fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> bool {
65        true
66    }
67
68    fn create(ctx: &Context<Self>) -> Self {
69        ctx.set_modal_link();
70        let _sub = ctx
71            .props()
72            .renderer
73            .plugin_changed
74            .add_listener(ctx.link().callback(|_| ()));
75
76        Self { _sub }
77    }
78}
79
80fn get_menu_items(has_render: bool, has_selection: bool) -> Vec<CopyDropDownMenuItem> {
81    let mut items = vec![
82        CopyDropDownMenuItem::OptGroup(
83            "Current View".into(),
84            if has_render {
85                vec![
86                    ExportMethod::Csv,
87                    ExportMethod::Json,
88                    ExportMethod::Ndjson,
89                    ExportMethod::Png,
90                ]
91            } else {
92                vec![ExportMethod::Csv, ExportMethod::Json, ExportMethod::Ndjson]
93            },
94        ),
95        CopyDropDownMenuItem::OptGroup("All".into(), vec![
96            ExportMethod::CsvAll,
97            ExportMethod::JsonAll,
98            ExportMethod::NdjsonAll,
99        ]),
100        CopyDropDownMenuItem::OptGroup("Config".into(), vec![ExportMethod::JsonConfig]),
101    ];
102
103    if has_selection {
104        items.insert(
105            0,
106            CopyDropDownMenuItem::OptGroup("Current Selection".into(), vec![
107                ExportMethod::CsvSelected,
108                ExportMethod::JsonSelected,
109                ExportMethod::NdjsonSelected,
110            ]),
111        )
112    }
113
114    items
115}