perspective_viewer/components/
copy_dropdown.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

use std::rc::Rc;

use yew::prelude::*;

use super::containers::dropdown_menu::*;
use super::modal::*;
use super::style::StyleProvider;
use crate::model::*;
use crate::renderer::*;
use crate::utils::*;

pub type CopyDropDownMenuMsg = DropDownMenuMsg;
pub type CopyDropDownMenuItem = DropDownMenuItem<ExportMethod>;

#[derive(Properties, PartialEq)]
pub struct CopyDropDownMenuProps {
    pub renderer: Renderer,
    pub callback: Callback<ExportMethod>,

    #[prop_or_default]
    weak_link: WeakScope<CopyDropDownMenu>,
}

pub struct CopyDropDownMenu {
    _sub: Subscription,
}

impl ModalLink<CopyDropDownMenu> for CopyDropDownMenuProps {
    fn weak_link(&self) -> &'_ WeakScope<CopyDropDownMenu> {
        &self.weak_link
    }
}

impl Component for CopyDropDownMenu {
    type Message = CopyDropDownMenuMsg;
    type Properties = CopyDropDownMenuProps;

    fn view(&self, ctx: &Context<Self>) -> yew::virtual_dom::VNode {
        let plugin = ctx.props().renderer.get_active_plugin().unwrap();
        let has_render = js_sys::Reflect::has(&plugin, js_intern::js_intern!("render")).unwrap();
        let has_selection = ctx.props().renderer.get_selection().is_some();
        html! {
            <StyleProvider>
                <DropDownMenu<ExportMethod>
                    values={Rc::new(get_menu_items(has_render, has_selection))}
                    callback={&ctx.props().callback}
                />
            </StyleProvider>
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> bool {
        true
    }

    fn create(ctx: &Context<Self>) -> Self {
        ctx.set_modal_link();
        let _sub = ctx
            .props()
            .renderer
            .plugin_changed
            .add_listener(ctx.link().callback(|_| ()));

        Self { _sub }
    }
}

fn get_menu_items(has_render: bool, has_selection: bool) -> Vec<CopyDropDownMenuItem> {
    let mut items = vec![
        CopyDropDownMenuItem::OptGroup(
            "Current View".into(),
            if has_render {
                vec![
                    ExportMethod::Csv,
                    ExportMethod::Json,
                    ExportMethod::Ndjson,
                    ExportMethod::Png,
                ]
            } else {
                vec![ExportMethod::Csv, ExportMethod::Json, ExportMethod::Ndjson]
            },
        ),
        CopyDropDownMenuItem::OptGroup("All".into(), vec![
            ExportMethod::CsvAll,
            ExportMethod::JsonAll,
            ExportMethod::NdjsonAll,
        ]),
        CopyDropDownMenuItem::OptGroup("Config".into(), vec![ExportMethod::JsonConfig]),
    ];

    if has_selection {
        items.insert(
            0,
            CopyDropDownMenuItem::OptGroup("Current Selection".into(), vec![
                ExportMethod::CsvSelected,
                ExportMethod::JsonSelected,
                ExportMethod::NdjsonSelected,
            ]),
        )
    }

    items
}