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
use crate::logo::Logo;
use crate::pagesidebar::PageSidebar;

use yew::virtual_dom::VChild;
use yew::Properties;
use yew::{html, Children, Component, ComponentLink, Html};

#[derive(Clone, PartialEq, Properties)]
pub struct Props {
    pub children: Children,
    #[prop_or_default]
    pub sidebar: Option<VChild<PageSidebar>>,
    #[prop_or_default]
    pub tools: Children,
    #[prop_or_default]
    pub logo: Option<VChild<Logo>>,
}

pub struct Page {
    props: Props,
    link: ComponentLink<Self>,
}

pub enum Msg {
    ToggleSidebar,
}

impl Component for Page {
    type Message = Msg;
    type Properties = Props;

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

    fn update(&mut self, msg: Self::Message) -> bool {
        match msg {
            Msg::ToggleSidebar => self.toggle_sidebar(),
        }

        true
    }

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

    fn view(&self) -> Html {
        html! {
            <div class="pf-c-page">
                <header class="pf-c-page__header">
                    <div class="pf-c-page__header-brand">
                        { self.sidebar_button() }
                        <a href="#" class="pf-c-page__header-brand-link">{
                            self.props.logo.clone().map(Html::from).unwrap_or_default()
                        }</a>
                    </div>
                    <div class="pf-c-page__header-tools"> { for self.props.tools.iter() }</div>
                </header>

                { self.props.sidebar.clone().map(Html::from).unwrap_or_default() }

                <main class="pf-c-page__main" tabindex="-1">
                    { for self.props.children.iter() }
                </main>
            </div>
        }
    }
}

impl Page {
    fn sidebar_button(&self) -> Html {
        let click_callback = self.link.callback(|_| Msg::ToggleSidebar);

        match &self.props.sidebar {
            Some(_) => html! {<div class="pf-c-page__header-brand-toggle">
                <button
                    aria-expanded=self.props.sidebar.as_ref().map(|sidebar|sidebar.props.open).unwrap_or(false)
                    class="pf-c-button pf-m-plain"
                    type="button"
                    onclick=click_callback
                    >
                    <i class="fas fa-bars" aria-hidden="true"/>
                </button>
            </div>},
            None => html! {},
        }
    }

    fn toggle_sidebar(&mut self) {
        match &mut self.props.sidebar {
            Some(sidebar) => sidebar.props.open = !sidebar.props.open,
            _ => {}
        }
    }
}