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
//! State

#[cfg(feature = "yew")]
mod yew;

#[cfg(feature = "yew")]
pub use self::yew::*;

use crate::sys;
use js_sys::{Array, Object};
use std::collections::HashMap;
use std::fmt::Formatter;
use std::ops::{Deref, DerefMut};
use wasm_bindgen::prelude::*;
use web_sys::Element;

/// A map of styles.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct StylesMap(pub HashMap<String, String>);

impl Deref for StylesMap {
    type Target = HashMap<String, String>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for StylesMap {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl std::fmt::Display for StylesMap {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for (k, v) in &self.0 {
            write!(f, "{k}: {v};")?;
        }
        Ok(())
    }
}

impl StylesMap {
    /// Extend with a single key/value pair
    pub fn extend_with(&self, key: impl Into<String>, value: impl Into<String>) -> Self {
        let mut result = self.clone();
        result.insert(key.into(), value.into());
        result
    }

    /// Extend with multiple key/value pairs
    pub fn extend_from<I, K, V>(&self, i: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<String>,
        V: Into<String>,
    {
        let mut result = self.clone();
        result.extend(i.into_iter().map(|(k, v)| (k.into(), v.into())));
        result
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AttributesMap(pub HashMap<String, String>);

impl Deref for AttributesMap {
    type Target = HashMap<String, String>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for AttributesMap {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

/// A way to apply attributes to elements
pub trait ApplyAttributes {
    fn apply_attributes(&self, attributes: &AttributesMap);
}

impl ApplyAttributes for &Element {
    fn apply_attributes(&self, attributes: &AttributesMap) {
        for (k, v) in &attributes.0 {
            let _ = self.set_attribute(k, v);
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct State {
    pub styles: Styles,
    pub attributes: Attributes,
}

impl From<sys::State> for State {
    fn from(value: sys::State) -> Self {
        Self {
            styles: value.styles().into(),
            attributes: value.attributes().into(),
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Styles {
    pub popper: StylesMap,
    pub arrow: StylesMap,
}

impl From<sys::Styles> for Styles {
    fn from(value: sys::Styles) -> Self {
        Self {
            popper: StylesMap(to_map(value.popper())),
            arrow: StylesMap(to_map(value.arrow())),
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Attributes {
    pub popper: AttributesMap,
}

impl From<sys::Attributes> for Attributes {
    fn from(value: sys::Attributes) -> Self {
        Self {
            popper: AttributesMap(to_map(value.popper())),
        }
    }
}

fn to_map(value: JsValue) -> HashMap<String, String> {
    let value: Object = match value.dyn_into() {
        Err(_) => return Default::default(),
        Ok(value) => value,
    };

    let entries = match Object::entries(&value).dyn_into::<Array>() {
        Err(_) => return Default::default(),
        Ok(entries) => entries,
    };

    let mut result = HashMap::new();

    for entry in entries.into_iter() {
        let key = js_sys::Reflect::get_u32(&entry, 0);
        let value = js_sys::Reflect::get_u32(&entry, 1);
        if let (Ok(key), Ok(value)) = (key, value) {
            if let (Some(key), Some(value)) = (key.as_string(), value.as_string()) {
                result.insert(key, value);
            }
        }
    }

    result
}