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
use crate::html::attributes::{class, classes, Attribute};
use crate::{dom::Effects, vdom::Node};
/// A component has a view and can update itself.
///
/// The update function returns an effect which can contain
/// follow ups and effects. Follow ups are executed on the next
/// update loop of this component, while the effects are executed
/// on the parent component that mounts it.
pub trait Component<MSG, XMSG>
where
MSG: 'static,
{
/// init the component
fn init(&mut self) -> Effects<MSG, XMSG> {
Effects::none()
}
/// Update the model of this component and return
/// follow up and/or effects that will be executed on the next update loop
fn update(&mut self, msg: MSG) -> Effects<MSG, XMSG>;
/// the view of the component
fn view(&self) -> Node<MSG>;
/// component can have static styles
fn stylesheet() -> Vec<String> {
vec![]
}
/// in addition, component can contain dynamic style
/// which can change when the model is updated
fn style(&self) -> Vec<String> {
vec![]
}
/// return the component name
/// defaults to the struct simplified name
fn component_name() -> String {
extract_simple_struct_name::<Self>()
}
/// prefix the class bane
fn prefix_class(class_name: &str) -> String {
let component_name = Self::component_name();
if class_name.is_empty() {
component_name
} else {
format!("{component_name}__{class_name}")
}
}
/// create a classname prepended with this component name
fn class_ns(class_name: &str) -> Attribute<MSG> {
class(Self::prefix_class(class_name))
}
/// create namespaced class names to pair that evaluates to true
fn classes_ns_flag(pair: impl IntoIterator<Item = (impl ToString, bool)>) -> Attribute<MSG> {
let class_list = pair.into_iter().filter_map(|(class, flag)| {
if flag {
Some(Self::prefix_class(&class.to_string()))
} else {
None
}
});
classes(class_list)
}
/// create a selector class prepended with this component name
fn selector_ns(class_name: &str) -> String {
let component_name = Self::component_name();
if class_name.is_empty() {
format!(".{component_name}")
} else {
format!(".{component_name}__{class_name}")
}
}
/// create namesspaced selector from multiple classnames
fn selectors_ns(class_names: impl IntoIterator<Item = impl ToString>) -> String {
let selectors: Vec<String> = class_names
.into_iter()
.map(|class_name| Self::selector_ns(&class_name.to_string()))
.collect();
selectors.join(" ")
}
}
/// A Container have children that is set from the parent component
///
/// It can update its Mode and returns follow ups and/or effects on the next
/// update loop.
///
/// The view in the container is set by the parent component. The container itself
/// can not listen to events on its view
pub trait Container<MSG, XMSG>
where
MSG: 'static,
{
/// init the container
fn init(&mut self) -> Effects<MSG, XMSG> {
Effects::none()
}
/// update the model of this component and return follow ups and/or effects
/// that will be executed on the next update loop.
fn update(&mut self, msg: MSG) -> Effects<MSG, XMSG>;
/// The container presents the children passed to it from the parent.
/// The container can decide how to display the children components here, but
/// the children nodes here can not trigger Msg that can update this component
fn view(&self, content: impl IntoIterator<Item = Node<XMSG>>) -> Node<MSG>;
/// optionally a Container can specify its own css style
fn stylesheet() -> Vec<String> {
vec![]
}
/// dynamic style
fn style(&self) -> Vec<String> {
vec![]
}
/// containers can append children
fn append_child(&mut self, child: Node<XMSG>);
/// return the component name
/// defaults to the struct simplified name
fn component_name() -> String {
extract_simple_struct_name::<Self>()
}
/// prefix the class bane
fn prefix_class(class_name: &str) -> String {
let component_name = Self::component_name();
if class_name.is_empty() {
component_name
} else {
format!("{component_name}__{class_name}")
}
}
/// create a classname prepended with this component name
fn class_ns(class_name: &str) -> Attribute<MSG> {
class(Self::prefix_class(class_name))
}
/// create namespaced class names to pair that evaluates to true
fn classes_ns_flag(pair: impl IntoIterator<Item = (impl ToString, bool)>) -> Attribute<MSG> {
let class_list = pair.into_iter().filter_map(|(class, flag)| {
if flag {
Some(Self::prefix_class(&class.to_string()))
} else {
None
}
});
classes(class_list)
}
/// create a selector class prepended with this component name
fn selector_ns(class_name: &str) -> String {
let component_name = Self::component_name();
if class_name.is_empty() {
format!(".{component_name}")
} else {
format!(".{component_name}__{class_name}")
}
}
/// create namesspaced selector from multiple classnames
fn selectors_ns(class_names: impl IntoIterator<Item = impl ToString>) -> String {
let selectors: Vec<String> = class_names
.into_iter()
.map(|class_name| Self::selector_ns(&class_name.to_string()))
.collect();
selectors.join(" ")
}
}
pub(crate) fn extract_simple_struct_name<T: ?Sized>() -> String {
let type_name = std::any::type_name::<T>();
let name = if let Some(first) = type_name.split(['<', '>']).next() {
first
} else {
type_name
};
name.rsplit("::")
.next()
.map(|s| s.to_string())
.expect("must have a name")
}
#[cfg(test)]
mod test {
use super::*;
use crate::html::*;
use std::marker::PhantomData;
#[test]
fn test_extract_component_name() {
enum Msg {}
struct AwesomeEditor {}
impl Component<Msg, ()> for AwesomeEditor {
fn update(&mut self, _msg: Msg) -> Effects<Msg, ()> {
Effects::none()
}
fn view(&self) -> Node<Msg> {
div([], [])
}
}
let name = extract_simple_struct_name::<AwesomeEditor>();
println!("name: {name}");
assert_eq!("AwesomeEditor", name);
}
#[test]
fn test_name_with_generics() {
struct ComplexEditor<XMSG> {
_phantom2: PhantomData<XMSG>,
}
enum Xmsg {}
let name = extract_simple_struct_name::<ComplexEditor<Xmsg>>();
println!("name: {name}");
assert_eq!("ComplexEditor", name);
}
#[test]
fn test_name_with_2_generics() {
struct ComplexEditor<MSG, XMSG> {
_phantom1: PhantomData<MSG>,
_phantom2: PhantomData<XMSG>,
}
enum Msg {}
enum Xmsg {}
let name = extract_simple_struct_name::<ComplexEditor<Msg, Xmsg>>();
println!("name: {name}");
assert_eq!("ComplexEditor", name);
}
}