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
226
227
use std::fmt::Debug;
use std::rc::Rc;
use yew::prelude::*;
use yew_router::agent::RouteRequest::GetCurrentRoute;
use yew_router::agent::*;
use yew_router::Switch;

pub struct SwitchTransformer<GSWITCH, LSWITCH>
where
    GSWITCH: 'static + Switch + Clone + PartialEq + Debug,
    LSWITCH: 'static + Clone + PartialEq + Debug,
{
    from: Box<dyn Fn(&GSWITCH) -> Option<&LSWITCH>>,
    to: Box<dyn Fn(LSWITCH) -> GSWITCH>,
}

impl<GSWITCH, LSWITCH> SwitchTransformer<GSWITCH, LSWITCH>
where
    GSWITCH: 'static + Switch + Clone + PartialEq + Debug,
    LSWITCH: 'static + Clone + PartialEq + Debug,
{
    pub fn new<FROM, TO>(from: FROM, to: TO) -> Rc<Self>
    where
        FROM: Fn(&GSWITCH) -> Option<&LSWITCH> + 'static,
        TO: Fn(LSWITCH) -> GSWITCH + 'static,
    {
        Rc::new(Self {
            from: Box::new(from),
            to: Box::new(to),
        })
    }
}

// tab router

#[derive(Properties, Clone)]
pub struct Props<GSWITCH, LSWITCH>
where
    GSWITCH: 'static + Switch + Clone + PartialEq + Debug,
    LSWITCH: 'static + Clone + PartialEq + Debug,
{
    pub transformer: Rc<SwitchTransformer<GSWITCH, LSWITCH>>,

    #[prop_or_default]
    pub r#box: bool,
    #[prop_or_default]
    pub vertical: bool,
    #[prop_or_default]
    pub filled: bool,
    #[prop_or_default]
    pub children: ChildrenWithProps<TabRouterItem<LSWITCH>>,
}

#[derive(Clone, Debug)]
pub enum TabsRouterMsg<GSWITCH, LSWITCH>
where
    GSWITCH: 'static + Switch + Clone + PartialEq + Debug,
    LSWITCH: 'static + Clone + PartialEq + Debug,
{
    RouteChange(Option<GSWITCH>),
    Select(LSWITCH),
}

pub struct TabsRouter<GSWITCH, LSWITCH>
where
    GSWITCH: 'static + Switch + Clone + PartialEq + Debug,
    LSWITCH: 'static + Clone + PartialEq + Debug,
{
    props: Props<GSWITCH, LSWITCH>,
    link: ComponentLink<Self>,
    _router: RouteAgentBridge,

    active: Option<GSWITCH>,
}

impl<GSWITCH, LSWITCH> Component for TabsRouter<GSWITCH, LSWITCH>
where
    GSWITCH: 'static + Switch + Clone + PartialEq + Debug,
    LSWITCH: 'static + Clone + PartialEq + Debug,
{
    type Message = TabsRouterMsg<GSWITCH, LSWITCH>;
    type Properties = Props<GSWITCH, LSWITCH>;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        let callback = link.callback(|route: yew_router::route::Route| {
            TabsRouterMsg::RouteChange(Switch::switch(route))
        });
        let mut bridge = RouteAgentBridge::new(callback);
        bridge.send(GetCurrentRoute);
        Self {
            props,
            link,
            active: None,
            _router: bridge,
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            TabsRouterMsg::RouteChange(route) => {
                self.active = route;
                true
            }
            TabsRouterMsg::Select(route) => {
                let route = (self.props.transformer.to)(route);
                RouteAgentDispatcher::<()>::new().send(RouteRequest::ChangeRoute(route.into()));
                false
            }
        }
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        self.props = props;
        true
    }

    fn view(&self) -> Html {
        let mut classes = Classes::from("pf-c-tabs");

        if self.props.r#box {
            classes = classes.extend("pf-m-box");
        }

        if self.props.vertical {
            classes = classes.extend("pf-m-vertical");
        }

        if self.props.filled {
            classes = classes.extend("pf-m-fill");
        }

        let local = self
            .active
            .as_ref()
            .and_then(|active| (self.props.transformer.from)(active));

        return html! {
            <div class=classes>
                <ul class="pf-c-tabs__list">
                    { for self.props.children.iter().map(|mut c|{
                        let to = c.props.to.clone();
                        c.props.current = local.map(|local| *local == c.props.to).unwrap_or_default();
                        c.props.onselect = self.link.callback(move |_|TabsRouterMsg::Select(to.clone()));
                        c
                    }) }
                </ul>
            </div>
        };
    }
}

// tab router item

#[derive(Properties, Clone, Debug, PartialEq)]
pub struct TabRouterItemProps<SWITCH>
where
    SWITCH: 'static + Clone + Debug + PartialEq,
{
    /// The tab label
    pub label: String,
    /// The switch this item references to
    pub to: SWITCH,
    /// If the item is currently selected
    #[prop_or_default]
    pub(crate) current: bool,
    #[prop_or_default]
    pub(crate) onselect: Callback<()>,
}

#[derive(Clone, Copy, Debug)]
pub enum TabRouterItemMsg {
    Clicked,
}

pub struct TabRouterItem<SWITCH>
where
    SWITCH: 'static + Clone + Debug + PartialEq,
{
    props: TabRouterItemProps<SWITCH>,
    link: ComponentLink<Self>,
}

impl<SWITCH> Component for TabRouterItem<SWITCH>
where
    SWITCH: 'static + Clone + Debug + PartialEq,
{
    type Message = TabRouterItemMsg;
    type Properties = TabRouterItemProps<SWITCH>;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        Self { props, link }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            TabRouterItemMsg::Clicked => {
                self.props.onselect.emit(());
            }
        }
        false
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        if self.props != props {
            self.props = props;
            true
        } else {
            false
        }
    }

    fn view(&self) -> Html {
        let mut classes = Classes::from("pf-c-tabs__item");

        if self.props.current {
            classes = classes.extend("pf-m-current");
        }

        return html! {
            <li class=classes>
                <button class="pf-c-tabs__link" onclick=self.link.callback(|_|TabRouterItemMsg::Clicked)>
                    <span class="pf-c-tabs__item-text"> { &self.props.label } </span>
                </button>
            </li>
        };
    }
}