Skip to main content

perspective_viewer/components/
export_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::session::Session;
20use crate::tasks::*;
21
22pub type ExportDropDownMenuItem = DropDownMenuItem<ExportFile>;
23
24#[derive(Properties, PartialEq)]
25pub struct ExportDropDownMenuProps {
26    pub renderer: Renderer,
27    pub session: Session,
28    pub callback: Callback<ExportFile>,
29}
30
31pub enum ExportDropDownMenuMsg {
32    TitleChange,
33}
34
35#[derive(Default)]
36pub struct ExportDropDownMenu {
37    title: String,
38    input_ref: NodeRef,
39    invalid: bool,
40}
41
42impl Component for ExportDropDownMenu {
43    type Message = ExportDropDownMenuMsg;
44    type Properties = ExportDropDownMenuProps;
45
46    fn view(&self, ctx: &Context<Self>) -> yew::virtual_dom::VNode {
47        let callback = ctx.link().callback(|_| ExportDropDownMenuMsg::TitleChange);
48        let plugin = ctx.props().renderer.get_active_plugin().unwrap();
49        let is_chart = plugin.name().as_str() != "Datagrid";
50        html! {
51            <>
52                <span class="dropdown-group-label">{ "Save as" }</span>
53                <input
54                    class={if self.invalid { "invalid" } else { "" }}
55                    oninput={callback}
56                    ref={&self.input_ref}
57                    value={self.title.to_owned()}
58                />
59                <DropDownMenu<ExportFile>
60                    values={Rc::new(get_menu_items(&self.title, is_chart))}
61                    callback={&ctx.props().callback}
62                />
63            </>
64        }
65    }
66
67    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
68        match msg {
69            ExportDropDownMenuMsg::TitleChange => {
70                self.title = self
71                    .input_ref
72                    .cast::<web_sys::HtmlInputElement>()
73                    .unwrap()
74                    .value();
75
76                self.invalid = self.title.is_empty();
77                true
78            },
79        }
80    }
81
82    fn create(ctx: &Context<Self>) -> Self {
83        Self {
84            title: ctx
85                .props()
86                .session
87                .get_title()
88                .unwrap_or_else(|| "untitled".to_owned()),
89            ..Default::default()
90        }
91    }
92}
93
94fn get_menu_items(name: &str, is_chart: bool) -> Vec<ExportDropDownMenuItem> {
95    vec![
96        ExportDropDownMenuItem::OptGroup(
97            "Current View".into(),
98            if is_chart {
99                vec![
100                    ExportMethod::Csv.new_file(name, is_chart),
101                    ExportMethod::Json.new_file(name, is_chart),
102                    ExportMethod::Ndjson.new_file(name, is_chart),
103                    ExportMethod::Arrow.new_file(name, is_chart),
104                    ExportMethod::Html.new_file(name, is_chart),
105                    ExportMethod::Plugin.new_file(name, is_chart),
106                ]
107            } else {
108                vec![
109                    ExportMethod::Csv.new_file(name, is_chart),
110                    ExportMethod::Json.new_file(name, is_chart),
111                    ExportMethod::Ndjson.new_file(name, is_chart),
112                    ExportMethod::Arrow.new_file(name, is_chart),
113                    ExportMethod::Html.new_file(name, is_chart),
114                ]
115            },
116        ),
117        ExportDropDownMenuItem::OptGroup("All".into(), vec![
118            ExportMethod::CsvAll.new_file(name, is_chart),
119            ExportMethod::JsonAll.new_file(name, is_chart),
120            ExportMethod::NdjsonAll.new_file(name, is_chart),
121            ExportMethod::ArrowAll.new_file(name, is_chart),
122        ]),
123        ExportDropDownMenuItem::OptGroup("Config".into(), vec![
124            ExportMethod::JsonConfig.new_file(name, is_chart),
125        ]),
126    ]
127}