Skip to main content

perspective_viewer/components/column_selector/
pivot_column.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 perspective_client::config::ColumnType;
14use web_sys::*;
15use yew::prelude::*;
16
17use crate::components::containers::dragdrop_list::*;
18use crate::components::type_icon::TypeIcon;
19use crate::dragdrop::*;
20use crate::session::*;
21use crate::utils::*;
22use crate::*;
23
24#[derive(Properties, PerspectiveProperties!)]
25pub struct PivotColumnProps {
26    /// Column name.
27    pub column: String,
28
29    #[prop_or_default]
30    pub column_type: Option<ColumnType>,
31
32    /// The drag starte of this column, if applicable.
33    pub action: DragTarget,
34
35    // State
36    #[prop_or_default]
37    pub opt_session: Option<Session>,
38    pub dragdrop: DragDrop,
39}
40
41impl PartialEq for PivotColumnProps {
42    fn eq(&self, other: &Self) -> bool {
43        self.column == other.column && self.action == other.action
44    }
45}
46
47impl DragDropListItemProps for PivotColumnProps {
48    type Item = String;
49
50    fn get_item(&self) -> String {
51        self.column.clone()
52    }
53}
54
55pub struct PivotColumn;
56
57impl Component for PivotColumn {
58    type Message = ();
59    type Properties = PivotColumnProps;
60
61    fn create(_ctx: &Context<Self>) -> Self {
62        Self
63    }
64
65    fn view(&self, ctx: &Context<Self>) -> Html {
66        let dragstart = Callback::from({
67            let event_name = ctx.props().column.to_owned();
68            let dragdrop = ctx.props().dragdrop.clone();
69            let action = ctx.props().action;
70            move |event: DragEvent| {
71                dragdrop.set_drag_image(&event).unwrap();
72                dragdrop.notify_drag_start(event_name.to_string(), DragEffect::Move(action))
73            }
74        });
75
76        let dragend = Callback::from({
77            let dragdrop = ctx.props().dragdrop.clone();
78            move |_event| dragdrop.notify_drag_end()
79        });
80
81        let col_type = ctx.props().column_type.unwrap_or_else(|| {
82            ctx.props()
83                .opt_session
84                .as_ref()
85                .and_then(|x| x.metadata().get_column_table_type(&ctx.props().column))
86                .unwrap_or(ColumnType::Integer)
87        });
88
89        html! {
90            <div
91                class="pivot-column-draggable"
92                draggable="true"
93                ondragstart={dragstart}
94                ondragend={dragend}
95            >
96                <div class="pivot-column-border">
97                    <TypeIcon ty={col_type} />
98                    <span class="column_name">{ ctx.props().column.clone() }</span>
99                </div>
100            </div>
101        }
102    }
103}