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
use std::collections::HashSet;
use wasm_bindgen::JsValue;
use yew::agent::*;
use yew::prelude::*;
use yew::utils::window;

#[derive(Clone, Debug)]
pub struct Backdrop {
    pub content: Html,
}

#[doc(hidden)]
#[derive(Debug)]
pub enum BackdropRequest {
    Open(Backdrop),
    Close,
}

#[doc(hidden)]
pub enum BackdropAction {
    Open(Backdrop),
    Close,
}

/// An agent for displaying toasts.
pub struct Backdropper {
    link: AgentLink<Self>,
    /// The backdrop viewer.
    ///
    /// While we can handle more than one, we will only send backdrops to one viewer. Registering
    /// more than one viewer will produce unexpected results.
    viewer: HashSet<HandlerId>,
}

impl Agent for Backdropper {
    type Reach = Context<Self>;
    type Message = ();
    type Input = BackdropRequest;
    type Output = BackdropAction;

    fn create(link: AgentLink<Self>) -> Self {
        Self {
            link,
            viewer: HashSet::new(),
        }
    }

    fn update(&mut self, _: Self::Message) {}

    fn connected(&mut self, id: HandlerId) {
        if id.is_respondable() {
            self.viewer.insert(id);
        }
    }

    fn handle_input(&mut self, msg: Self::Input, _: HandlerId) {
        match msg {
            BackdropRequest::Open(backdrop) => {
                self.notify_backdrop(BackdropAction::Open(backdrop));
            }
            BackdropRequest::Close => {
                self.notify_backdrop(BackdropAction::Close);
            }
        }
    }

    fn disconnected(&mut self, id: HandlerId) {
        if id.is_respondable() {
            self.viewer.remove(&id);
        }
    }
}

impl Backdropper {
    fn notify_backdrop(&self, msg: BackdropAction) {
        let viewer = self.viewer.iter().next();
        if let Some(viewer) = viewer {
            self.link.respond(*viewer, msg);
        } else {
            window()
                .alert_with_message(&format!(
                    "Dropped backdrop. No backdrop component registered."
                ))
                .ok();
        }
    }
}

/// Client to the backdrop agent which can be used to request backdrops.
pub struct BackdropDispatcher(Dispatcher<Backdropper>);

impl BackdropDispatcher {
    pub fn new() -> Self {
        Self(Backdropper::dispatcher())
    }

    /// Request a backdrop from the backdrop agent.
    pub fn open(&mut self, backdrop: Backdrop) {
        self.0.send(BackdropRequest::Open(backdrop))
    }

    /// Close the current backdrop.
    pub fn close(&mut self) {
        self.0.send(BackdropRequest::Close)
    }
}

impl Default for BackdropDispatcher {
    fn default() -> Self {
        Self::new()
    }
}

pub struct BackdropBridge(Box<dyn Bridge<Backdropper>>);

impl BackdropBridge {
    pub fn new(callback: Callback<<Backdropper as Agent>::Output>) -> Self {
        BackdropBridge(Backdropper::bridge(callback))
    }
}

// component

#[derive(Clone, PartialEq, Properties)]
pub struct Props {}

pub struct BackdropViewer {
    props: Props,
    _bridge: BackdropBridge,

    content: Html,
    open: bool,
}

pub enum Msg {
    Open(Backdrop),
    Close,
}

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

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        let bridge = BackdropBridge::new(link.callback(|action| match action {
            BackdropAction::Open(backdrop) => Msg::Open(backdrop),
            BackdropAction::Close => Msg::Close,
        }));
        Self {
            props,
            _bridge: bridge,
            content: Default::default(),
            open: false,
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Open(content) => {
                self.content = content.content;
                self.open();
            }
            Msg::Close => {
                self.content = Default::default();
                self.close();
            }
        }
        true
    }

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

    fn view(&self) -> Html {
        if self.open {
            return html! {
                <div class="pf-c-backdrop">
                    { self.content.clone() }
                </div>
            };
        } else {
            return html! {};
        }
    }
}

impl BackdropViewer {
    fn open(&mut self) {
        if let Some(body) = yew::utils::document().body() {
            let classes = js_sys::Array::of1(&JsValue::from_str("pf-c-backdrop__open"));
            body.class_list().add(&classes).ok();
        }
        self.open = true;
    }

    fn close(&mut self) {
        if let Some(body) = yew::utils::document().body() {
            let classes = js_sys::Array::of1(&JsValue::from_str("pf-c-backdrop__open"));
            body.class_list().remove(&classes).ok();
        }
        self.open = false;
    }
}