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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::{Button, Icon, Orientation, Popper, PopperContent, Variant};

use yew::prelude::*;

use crate::integration::popperjs;

// tooltip

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct PopoverProps {
    /// The target, rendered by the component, to which the popover will be aligned to.
    #[prop_or_default]
    pub target: Html,

    /// The header content of the popover.
    #[prop_or_default]
    pub header: Option<Html>,
    /// The content which will be show in the popover.
    pub children: Children,
    /// The footer content of the popover.
    #[prop_or_default]
    pub footer: Option<Html>,

    /// Binds the onclick handler of the target to toggle visibility.
    #[prop_or_default]
    pub toggle_by_onclick: bool,
}

pub struct Popover {
    node: NodeRef,
    active: bool,
}

#[derive(Clone, Debug)]
pub enum PopoverMsg {
    Open,
    Close,
}

impl Component for Popover {
    type Message = PopoverMsg;
    type Properties = PopoverProps;

    fn create(_ctx: &Context<Self>) -> Self {
        Self {
            node: NodeRef::default(),
            active: false,
        }
    }

    fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            PopoverMsg::Open => {
                if !self.active {
                    self.active = true;
                    true
                } else {
                    false
                }
            }
            PopoverMsg::Close => {
                if self.active {
                    self.active = false;
                    true
                } else {
                    false
                }
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let (onclick, onclose) = match ctx.props().toggle_by_onclick {
            true => (
                ctx.link().callback(|_| PopoverMsg::Open),
                ctx.link().callback(|_| PopoverMsg::Close),
            ),
            false => Default::default(),
        };

        let style = match self.active {
            true => "pointer-events: none;",
            false => "",
        };

        html! (
            <>
                <Popper<Popover>
                    active={self.active}
                    content={ctx.props().clone()}
                    onclose={onclose}
                    >
                    <span style={style} onclick={onclick} ref={self.node.clone()}>
                        { ctx.props().target.clone() }
                    </span>
                </Popper<Popover>>
            </>
        )
    }
}

impl PopperContent for Popover {
    fn view(
        props: &PopoverProps,
        onclose: Callback<()>,
        r#ref: NodeRef,
        state: Option<popperjs::State>,
    ) -> Html {
        let styles = match &state {
            Some(state) => &state.styles,
            None => "display: none;",
        }
        .to_string();

        let orientation = state
            .as_ref()
            .map(|s| s.orientation)
            .unwrap_or(Orientation::Bottom);

        html! (
            <PopoverPopup
                ref={r#ref}
                styles={styles}
                orientation={orientation}
                header={props.header.clone()}
                footer={props.footer.clone()}
                children={props.children.clone()}
                onclose={onclose}
            />
        )
    }
}

// popover popup

#[derive(Clone, PartialEq, Properties)]
pub struct PopoverPopupProps {
    #[prop_or_default]
    pub children: Children,
    #[prop_or_default]
    pub header: Option<Html>,
    #[prop_or_default]
    pub footer: Option<Html>,
    pub orientation: Orientation,
    #[prop_or_default]
    pub hidden: bool,
    #[prop_or_default]
    pub styles: String,

    /// called when the close button is clicked
    #[prop_or_default]
    pub onclose: Callback<()>,
}

#[derive(Clone)]
pub struct PopoverPopup {}

#[derive(Copy, Debug, Clone)]
pub enum PopoverPopupMsg {
    Close,
}

impl Component for PopoverPopup {
    type Message = PopoverPopupMsg;
    type Properties = PopoverPopupProps;

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

    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            PopoverPopupMsg::Close => {
                ctx.props().onclose.emit(());
                false
            }
        }
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        let mut classes = Classes::from("pf-c-popover");

        classes.extend(ctx.props().orientation.as_classes());

        let style = if ctx.props().hidden {
            "display: none;".to_string()
        } else {
            ctx.props().styles.to_string()
        };

        let onclose = ctx.link().callback(|_| PopoverPopupMsg::Close);

        return html! {
            <div style={style} class={classes} role="dialog" aria-model="true">
                <div class="pf-c-popover__arrow"></div>
                <div class="pf-c-popover__content">

                    <Button
                        variant={Variant::Plain}
                        icon={Icon::Times}
                        aria_label="Close"
                        onclick={onclose}
                    />

                    if let Some(header) = &ctx.props().header {
                        <h1 class="pf-c-title pf-m-md">
                            { header.clone() }
                        </h1>
                    }

                    <div class="pf-c-popover__body">
                        { for ctx.props().children.iter() }
                    </div>

                    if let Some(footer) = &ctx.props().footer {
                        <footer class="pf-c-popover__footer">
                            { footer.clone() }
                        </footer>
                    }

                </div>
            </div>
        };
    }
}