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
116
117
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2018, the Perspective Authors.
//
// This file is part of the Perspective library, distributed under the terms
// of the Apache License 2.0.  The full license can be found in the LICENSE
// file.

use crate::config::*;
use crate::dragdrop::*;
use crate::model::*;
use crate::renderer::*;
use crate::session::*;
use crate::*;

use super::containers::dragdrop_list::*;

use web_sys::*;
use yew::prelude::*;

/// A `SortItem` includes the column name and `SortDir` arrow, a clickable
/// button which cycles through the available `SortDir` states.
pub struct SortItem {}

#[derive(Properties)]
pub struct SortItemProps {
    pub sort: Sort,
    pub idx: usize,
    pub session: Session,
    pub renderer: Renderer,
    pub dragdrop: DragDrop,
}

impl PartialEq for SortItemProps {
    fn eq(&self, other: &Self) -> bool {
        self.sort == other.sort && self.idx == other.idx
    }
}

derive_model!(Renderer, Session for SortItemProps);

impl DragDropListItemProps for SortItemProps {
    type Item = Sort;

    fn get_item(&self) -> Sort {
        self.sort.clone()
    }
}

pub enum SortItemMsg {
    SortDirClick(bool),
}

impl Component for SortItem {
    type Message = SortItemMsg;
    type Properties = SortItemProps;

    fn create(_ctx: &Context<Self>) -> Self {
        SortItem {}
    }

    fn update(&mut self, ctx: &Context<Self>, msg: SortItemMsg) -> bool {
        match msg {
            SortItemMsg::SortDirClick(shift_key) => {
                let is_split = ctx.props().session.get_view_config().split_by.is_empty();
                let mut sort = ctx.props().session.get_view_config().sort.clone();
                let sort_item = &mut sort.get_mut(ctx.props().idx).expect("Sort on no column");
                sort_item.1 = sort_item.1.cycle(!is_split, shift_key);
                let update = ViewConfigUpdate {
                    sort: Some(sort),
                    ..ViewConfigUpdate::default()
                };
                ctx.props().update_and_render(update);
                false
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let onclick = ctx
            .link()
            .callback(|event: MouseEvent| SortItemMsg::SortDirClick(event.shift_key()));

        let noderef = NodeRef::default();
        let dragstart = Callback::from({
            let event_name = ctx.props().sort.0.to_owned();
            let noderef = noderef.clone();
            let dragdrop = ctx.props().dragdrop.clone();
            move |event: DragEvent| {
                let elem = noderef.cast::<HtmlElement>().unwrap();
                event.data_transfer().unwrap().set_drag_image(&elem, 0, 0);
                dragdrop.drag_start(event_name.to_string(), DragEffect::Move(DragTarget::Sort))
            }
        });

        let dragend = Callback::from({
            let dragdrop = ctx.props().dragdrop.clone();
            move |_event| dragdrop.drag_end()
        });

        html_template! {
            <span
                draggable="true"
                ref={ noderef.clone() }
                ondragstart={ dragstart }
                ondragend={ dragend }>
                {
                    ctx.props().sort.0.to_owned()
                }
            </span>
            <span
                class={ format!("sort-icon {}", ctx.props().sort.1) }
                onmousedown={ onclick }>
            </span>
        }
    }
}