perspective_viewer/components/
function_dropdown.rs

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

use perspective_client::config::CompletionItemSuggestion;
use web_sys::*;
use yew::prelude::*;

use super::modal::*;
use crate::utils::WeakScope;

static CSS: &str = include_str!(concat!(env!("OUT_DIR"), "/css/function-dropdown.css"));

pub enum FunctionDropDownMsg {
    SetValues(Vec<CompletionItemSuggestion>),
    SetCallback(Callback<CompletionItemSuggestion>),
    ItemDown,
    ItemUp,
    ItemSelect,
}

pub struct FunctionDropDown {
    values: Option<Vec<CompletionItemSuggestion>>,
    selected: usize,
    on_select: Option<Callback<CompletionItemSuggestion>>,
}

#[derive(Properties, PartialEq)]
pub struct FunctionDropDownProps {
    #[prop_or_default]
    pub weak_link: WeakScope<FunctionDropDown>,
}

impl ModalLink<FunctionDropDown> for FunctionDropDownProps {
    fn weak_link(&self) -> &'_ WeakScope<FunctionDropDown> {
        &self.weak_link
    }
}

impl Component for FunctionDropDown {
    type Message = FunctionDropDownMsg;
    type Properties = FunctionDropDownProps;

    fn create(ctx: &Context<Self>) -> Self {
        ctx.set_modal_link();
        Self {
            values: Some(vec![]),
            selected: 0,
            on_select: None,
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            FunctionDropDownMsg::SetCallback(callback) => {
                self.on_select = Some(callback);
                false
            },
            FunctionDropDownMsg::SetValues(values) => {
                self.values = Some(values);
                self.selected = 0;
                true
            },
            FunctionDropDownMsg::ItemSelect => {
                if let Some(ref values) = self.values {
                    match values.get(self.selected) {
                        None => {
                            console::error_1(&"Selected out-of-bounds".into());
                            false
                        },
                        Some(x) => {
                            self.on_select.as_ref().unwrap().emit(*x);
                            false
                        },
                    }
                } else {
                    console::error_1(&"No Values".into());
                    false
                }
            },
            FunctionDropDownMsg::ItemDown => {
                self.selected += 1;
                if let Some(ref values) = self.values {
                    if self.selected >= values.len() {
                        self.selected = 0;
                    };
                };

                true
            },
            FunctionDropDownMsg::ItemUp => {
                if let Some(ref values) = self.values {
                    if self.selected < 1 {
                        self.selected = values.len();
                    }
                }

                self.selected -= 1;
                true
            },
        }
    }

    fn changed(&mut self, _ctx: &Context<Self>, _old: &Self::Properties) -> bool {
        false
    }

    fn view(&self, _ctx: &Context<Self>) -> Html {
        let body = html! {
            if let Some(ref values) = self.values {
                if !values.is_empty() {
                    { for values
                            .iter()
                            .enumerate()
                            .map(|(idx, value)| {
                                let click = self.on_select.as_ref().unwrap().reform({
                                    let value = *value;
                                    move |_: MouseEvent| value
                                });

                                html! {
                                    if idx == self.selected {
                                        <div onmousedown={ click } class="selected">
                                            <span style="font-weight:500">{ value.label }</span>
                                            <br/>
                                            <span style="padding-left:12px">{ value.documentation }</span>
                                        </div>
                                    } else {
                                        <div onmousedown={ click }>
                                            <span style="font-weight:500">{ value.label }</span>
                                            <br/>
                                            <span style="padding-left:12px">{ value.documentation }</span>
                                        </div>
                                    }
                                }
                            }) }
                }
            }
        };

        html! { <><style>{ &CSS }</style>{ body }</> }
    }
}