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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! Document utilities.
use std::{
    cell::RefCell,
    collections::HashMap,
    pin::{pin, Pin},
    task,
};

use futures::{channel::oneshot, Future};
use futures_signals::{
    signal::SignalExt,
    signal_vec::{self, SignalVec, SignalVecExt},
};
use paste::paste;
use pin_project::pin_project;
use silkenweb_base::document;
use silkenweb_signals_ext::value::SignalOrValue;
use wasm_bindgen::{JsCast, UnwrapThrowExt};

use crate::{
    dom::{self, Dom, Dry, Wet},
    event::{bubbling_events, GlobalEventCallback},
    hydration::HydrationStats,
    node::{
        element::{
            child_vec::{ChildVecHandle, ParentShared},
            Const, Element, GenericElement,
        },
        Node,
    },
    HEAD_ID_ATTRIBUTE,
};

mod dry;
mod hydro;
mod wet;

/// Manage an event handler.
///
/// This will remove the event handler when dropped.
#[must_use]
pub struct EventCallback(GlobalEventCallback<silkenweb_base::Document>);

impl EventCallback {
    fn new<Event: JsCast>(name: &'static str, f: impl FnMut(Event) + 'static) -> Self {
        Self(GlobalEventCallback::new(name, f))
    }

    /// Make this event permanent.
    pub fn perpetual(self) {
        self.0.perpetual()
    }
}

macro_rules! events{
    ($($name:ident: $typ:ty),* $(,)?) => { paste!{ $(
        #[doc = "Add a `" $name "` event handler at the document level." ]
        ///
        /// This only has an effect on WASM targets.
        pub fn [< on_ $name >] (f: impl FnMut($typ) + 'static) -> EventCallback {
            EventCallback::new(stringify!($name), f)
        }
    )*}}
}

/// Add a `DOMCContentLoaded` event handler at the document level." ]
///
/// This only has an effect on WASM targets.
pub fn on_dom_content_loaded(f: impl FnMut(web_sys::Event) + 'static) -> EventCallback {
    EventCallback::new("DOMContentLoaded", f)
}

events! {
    fullscreenchange: web_sys::Event,
    fullscreenerror: web_sys::Event,
    lostpointercapture: web_sys::PointerEvent,
    pointerlockchange: web_sys::Event,
    pointerlockerror: web_sys::Event,
    readystatechange: web_sys::Event,
    scroll: web_sys::Event,
    scrollend: web_sys::Event,
    selectionchange: web_sys::Event,
    visibilitychange: web_sys::Event,

    // These generate a `ClipboardEvent`, but that is currently unstable in `web_sys`.
    copy: web_sys::Event,
    cut: web_sys::Event,
    paste: web_sys::Event,
}

bubbling_events!();

pub trait Document: Dom + Sized {
    type MountOutput;
    type MountInHeadOutput;

    /// Mount an element on the document.
    ///
    /// `id` is the id of the mount point element. The element will replace
    /// the mount point.
    fn mount(id: &str, element: impl Into<GenericElement<Self, Const>>) -> Self::MountOutput;

    /// Mount some children in the document `<head>`
    ///
    /// `id` is used by hydration, which will set a `data-silkenweb-head-id`
    /// attribute on each top level element in `head` so it can identify which
    /// elements to hydrate against.
    ///
    /// # Panics
    ///
    /// Mounting something with the same `id` will cause a panic.
    fn mount_in_head(id: &str, head: DocumentHead<Self>) -> Self::MountInHeadOutput;

    /// Remove all mounted elements.
    ///
    /// All elements mounted with `mount` or `mount_in_head` will be removed.
    /// Mount points will not be restored. This is useful to ensure a clean
    /// environment for testing.
    fn unmount_all();

    /// Get the inner HTML of `<head>`.
    ///
    /// This only includes elements added with `mount_in_head`. It's useful for
    /// server side rendering, where it can be used to add any stylesheets
    /// required for the HTML.
    fn head_inner_html() -> String;
}

/// The document's `<head>` element.
///
/// This allows you to create a set of (possibly reactive) children that can be
/// added to the head. See
/// [`hydrate_in_head`][`crate::hydration::hydrate_in_head`] for more details.
pub struct DocumentHead<D: Dom> {
    child_vec: Pin<Box<dyn SignalVec<Item = GenericElement<D>>>>,
}

impl<D: Dom> Default for DocumentHead<D> {
    fn default() -> Self {
        Self::new()
    }
}

impl<D: Dom> DocumentHead<D> {
    pub fn new() -> Self {
        let child_vec = Box::pin(signal_vec::always(Vec::new()));
        Self { child_vec }
    }
}

impl<D: Dom> DocumentHead<D> {
    /// Add a child.
    ///
    /// This like [`ParentElement::child`][`crate::node::element::ParentElement::child`],
    /// but it only accepts element children, not text.
    pub fn child(
        self,
        child: impl SignalOrValue<Item = impl Into<GenericElement<D>> + 'static>,
    ) -> Self {
        self.optional_child(child.map(Some))
    }

    /// Add an optional child.
    ///
    /// This like [`ParentElement::optional_child`][`crate::node::element::ParentElement::optional_child`],
    /// but it only accepts element children, not text.
    pub fn optional_child(
        self,
        child: impl SignalOrValue<Item = Option<impl Into<GenericElement<D>> + 'static>>,
    ) -> Self {
        child.select(
            |parent, child| {
                if let Some(child) = child {
                    return parent.children_signal(signal_vec::always(vec![child]));
                }

                parent
            },
            |parent, child| {
                let child_vec = child
                    .map(|child| child.into_iter().collect::<Vec<_>>())
                    .to_signal_vec();
                parent.children_signal(child_vec)
            },
            self,
        )
    }

    /// Add some children
    ///
    /// This like [`ParentElement::children`][`crate::node::element::ParentElement::children`],
    /// but it only accepts element children, not text.
    pub fn children<N>(self, children: impl IntoIterator<Item = N>) -> Self
    where
        N: Into<GenericElement<D>>,
    {
        self.children_signal(signal_vec::always(
            children.into_iter().map(|child| child.into()).collect(),
        ))
    }

    /// Add some reactive children.
    ///
    /// This like [`ParentElement::children_signal`][`crate::node::element::ParentElement::children_signal`],
    /// but it only accepts element children, not text.
    pub fn children_signal<E>(mut self, children: impl SignalVec<Item = E> + 'static) -> Self
    where
        E: Into<GenericElement<D>>,
    {
        self.child_vec = self
            .child_vec
            .chain(children.map(|child| child.into()))
            .boxed_local();
        self
    }
}

// If we used `RemoteHandle`, we'd have to wait on the future for
// `HydrationStats` even if we want to discard it. Using `oneshot`, we can
// discard the whole future if we don't need `HydrationStats`.

/// The type of [`Hydro::MountOutput`][`crate::dom::Hydro::MountOutput`].
#[pin_project]
pub struct MountHydro(#[pin] oneshot::Receiver<HydrationStats>);

impl Future for MountHydro {
    type Output = HydrationStats;

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        poll_receiver(self.project().0, cx)
    }
}

/// The type of
/// [`Hydro::MountInHeadOutput`][`crate::dom::Hydro::MountInHeadOutput`].
#[pin_project]
pub struct MountHydroHead(#[pin] oneshot::Receiver<HydrationStats>);

impl Future for MountHydroHead {
    type Output = HydrationStats;

    fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> {
        poll_receiver(self.project().0, cx)
    }
}

fn poll_receiver<T>(
    receiver: Pin<&mut oneshot::Receiver<T>>,
    cx: &mut task::Context<'_>,
) -> task::Poll<T> {
    receiver.poll(cx).map(|r| {
        // We send from a future that is run using `spawn_local`, so we can't cancel it.
        r.unwrap_throw()
    })
}

fn document_head() -> <Wet as dom::private::Dom>::Element {
    <Wet as dom::private::Dom>::Element::from_element(document::head().unwrap_throw().into())
}

fn children_with_id<D: Dom>(head: DocumentHead<D>, id: &str) -> impl SignalVec<Item = Node<D>> {
    head.child_vec.map({
        let id = id.to_string();
        move |child| child.attribute(HEAD_ID_ATTRIBUTE, id.clone()).into()
    })
}

#[derive(Default)]
pub(crate) struct TaskLocal {
    mounted_in_dry_head: RefCell<HashMap<String, ChildVecHandle<Dry, ParentShared>>>,
}

fn wet_insert_mounted(id: &str, element: GenericElement<Wet, Const>) {
    let existing = WET_MOUNTED.with(|mounted| mounted.borrow_mut().insert(id.to_string(), element));

    assert!(
        existing.is_none(),
        "Attempt to insert duplicate id ({id}) into document"
    );
}

fn wet_unmount() {
    for element in WET_MOUNTED.take().into_values() {
        element.dom_element().remove()
    }
}

struct MountedInHead<D: Dom>(RefCell<HashMap<String, ChildVecHandle<D, ParentShared>>>);

impl<D: Dom> MountedInHead<D> {
    fn new() -> Self {
        Self(RefCell::new(HashMap::new()))
    }

    fn mount(&self, id: &str, child_vec: ChildVecHandle<D, ParentShared>) {
        let existing = self.0.borrow_mut().insert(id.to_string(), child_vec);

        assert!(
            existing.is_none(),
            "Attempt to insert duplicate id ({id}) into head"
        );
    }

    fn unmount_all(&self) {
        for element in self.0.take().into_values() {
            element.clear();
        }
    }

    fn inner_html(&self) -> String {
        let mut html = String::new();

        for elem in self.0.borrow().values() {
            html.push_str(&elem.inner_html());
        }

        html
    }
}

thread_local! {
    static WET_MOUNTED: RefCell<HashMap<String, GenericElement<Wet, Const>>> = RefCell::new(HashMap::new());
}