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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ 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 std::rc::Rc;

use derivative::Derivative;
use itertools::Itertools;
use web_sys::{FocusEvent, HtmlInputElement, KeyboardEvent};
use yew::{classes, html, Callback, Component, Html, NodeRef, Properties, TargetCast};

use super::type_icon::TypeIconType;
use crate::components::type_icon::TypeIcon;
use crate::maybe;
use crate::session::Session;

#[derive(PartialEq, Properties, Derivative, Clone)]
#[derivative(Debug)]
pub struct EditableHeaderProps {
    pub icon_type: Option<TypeIconType>,
    pub on_change: Callback<(Option<String>, bool)>,
    pub editable: bool,
    pub initial_value: Option<String>,
    pub placeholder: Rc<String>,
    #[prop_or_default]
    pub reset_count: u8,
    #[derivative(Debug = "ignore")]
    pub session: Session,
}
impl EditableHeaderProps {
    fn split_placeholder(&self) -> String {
        let split = self
            .placeholder
            .split_once('\n')
            .map(|(a, _)| a)
            .unwrap_or(&*self.placeholder);
        match split.char_indices().nth(25) {
            None => split.to_string(),
            Some((idx, _)) => split[..idx].to_owned(),
        }
    }
}

#[derive(Default, Debug, PartialEq, Copy, Clone)]
pub enum ValueState {
    #[default]
    Unedited,
    Edited,
}

pub enum EditableHeaderMsg {
    SetNewValue(String),
    OnClick(()),
}

#[derive(Default, Debug)]
pub struct EditableHeader {
    noderef: NodeRef,
    edited: bool,
    valid: bool,
    value: Option<String>,
    placeholder: String,
}
impl Component for EditableHeader {
    type Message = EditableHeaderMsg;
    type Properties = EditableHeaderProps;

    fn create(ctx: &yew::prelude::Context<Self>) -> Self {
        Self {
            value: ctx.props().initial_value.clone(),
            placeholder: ctx.props().split_placeholder(),
            valid: true,
            ..Self::default()
        }
    }

    fn changed(&mut self, ctx: &yew::prelude::Context<Self>, old_props: &Self::Properties) -> bool {
        if ctx.props().reset_count != old_props.reset_count {
            self.value.clone_from(&ctx.props().initial_value);
        }
        if ctx.props().initial_value != old_props.initial_value {
            self.edited = false;
            self.value.clone_from(&ctx.props().initial_value);
        }
        if !ctx.props().editable {
            self.edited = false;
        }
        self.placeholder = ctx.props().split_placeholder();
        ctx.props() != old_props
    }

    fn update(&mut self, ctx: &yew::prelude::Context<Self>, msg: Self::Message) -> bool {
        match msg {
            EditableHeaderMsg::SetNewValue(new_value) => {
                let maybe_value = (!new_value.is_empty()).then_some(new_value.clone());
                self.edited = ctx.props().initial_value != maybe_value;

                self.valid = maybe!({
                    if maybe_value
                        .as_ref()
                        .map(|v| v == &self.placeholder)
                        .unwrap_or(true)
                    {
                        return Some(true);
                    }
                    if !self.edited {
                        return Some(true);
                    }
                    let metadata = ctx.props().session.metadata();
                    let expressions = metadata.get_expression_columns();
                    let found = metadata
                        .get_table_columns()?
                        .iter()
                        .chain(expressions)
                        .contains(&new_value);
                    Some(!found)
                })
                .unwrap_or(true);

                self.value.clone_from(&maybe_value);
                ctx.props().on_change.emit((maybe_value, self.valid));
                true
            },
            EditableHeaderMsg::OnClick(()) => {
                self.noderef
                    .cast::<HtmlInputElement>()
                    .unwrap()
                    .focus()
                    .unwrap();
                false
            },
        }
    }

    fn view(&self, ctx: &yew::prelude::Context<Self>) -> Html {
        let mut classes = classes!("sidebar_header_contents");
        if ctx.props().editable {
            classes.push("editable");
        }

        if !self.valid {
            classes.push("invalid");
        }

        if self.edited {
            classes.push("edited");
        }

        let onkeyup = ctx.link().callback(|e: KeyboardEvent| {
            let value = e.target_unchecked_into::<HtmlInputElement>().value();
            EditableHeaderMsg::SetNewValue(value)
        });

        let onblur = ctx.link().callback(|e: FocusEvent| {
            let value = e.target_unchecked_into::<HtmlInputElement>().value();
            EditableHeaderMsg::SetNewValue(value)
        });

        html! {
            <div class={classes} onclick={ctx.link().callback(|_| EditableHeaderMsg::OnClick(()))}>
                if let Some(icon) = ctx.props().icon_type { <TypeIcon ty={icon} /> }
                <input
                    ref={self.noderef.clone()}
                    type="search"
                    class="sidebar_header_title"
                    disabled={!ctx.props().editable}
                    {onblur}
                    {onkeyup}
                    value={self.value.clone()}
                    placeholder={self.placeholder.clone()}
                />
            </div>
        }
    }
}