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