use perspective_client::config::ColumnType;
use web_sys::*;
use yew::prelude::*;
use super::expr_edit_button::*;
use crate::components::dragdrop_list::*;
use crate::components::type_icon::TypeIcon;
use crate::presentation::{ColumnSettingsTarget, Presentation};
use crate::session::*;
use crate::utils::*;
#[derive(Properties)]
pub struct PivotColumnProps {
pub column: String,
#[prop_or_default]
pub column_type: Option<ColumnType>,
pub action: DragTarget,
#[prop_or_default]
pub metadata: Option<SessionMetadataRc>,
#[prop_or_default]
pub on_open_expr_panel: Option<Callback<ColumnSettingsTarget>>,
#[prop_or_default]
pub is_editing: bool,
#[prop_or_default]
pub opt_session: Option<Session>,
pub presentation: Presentation,
}
impl PartialEq for PivotColumnProps {
fn eq(&self, other: &Self) -> bool {
self.column == other.column
&& self.action == other.action
&& self.metadata == other.metadata
&& self.is_editing == other.is_editing
&& self.on_open_expr_panel.is_some() == other.on_open_expr_panel.is_some()
}
}
impl DragDropListItemProps for PivotColumnProps {
type Item = String;
fn get_item(&self) -> String {
self.column.clone()
}
}
pub struct PivotColumn;
impl Component for PivotColumn {
type Message = ();
type Properties = PivotColumnProps;
fn create(_ctx: &Context<Self>) -> Self {
Self
}
fn view(&self, ctx: &Context<Self>) -> Html {
let dragstart = Callback::from({
let event_name = ctx.props().column.to_owned();
let presentation = ctx.props().presentation.clone();
let action = ctx.props().action;
move |event: DragEvent| {
if presentation.set_drag_image(&event) {
presentation.notify_drag_start(event_name.to_string(), DragEffect::Move(action))
}
}
});
let dragend = Callback::from({
let presentation = ctx.props().presentation.clone();
move |_event| presentation.notify_drag_end()
});
let col_type = ctx.props().column_type.unwrap_or_else(|| {
ctx.props()
.metadata
.as_ref()
.and_then(|x| x.get_column_table_type(&ctx.props().column))
.unwrap_or(ColumnType::Integer)
});
html! {
<div
class="pivot-column-draggable"
draggable="true"
ondragstart={dragstart}
ondragend={dragend}
>
<div class="pivot-column-border">
<span class="drag-handle icon" />
<TypeIcon ty={col_type} />
<span class="column_name">{ ctx.props().column.clone() }</span>
if let Some(on_open_expr_panel) = &ctx.props().on_open_expr_panel {
<ExprEditButton
name={ctx.props().column.clone()}
{on_open_expr_panel}
is_expression=false
is_disabled=false
is_editing={ctx.props().is_editing}
/>
}
</div>
</div>
}
}
}