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
use crate::sealed::Sealed; use cope::singleton::{react, Atom}; use wasm_bindgen::{prelude::*, JsCast}; use web_sys::{window, Element, Event, EventTarget, HtmlButtonElement}; #[must_use] pub struct ElementBuilder<E> { element: E, } impl<E> ElementBuilder<E> { pub fn new(element: E) -> Self { ElementBuilder { element } } } impl<E> ElementBuilder<E> { pub fn build(self) -> E { self.element } } impl<E> AsRef<E> for ElementBuilder<E> { fn as_ref(&self) -> &E { &self.element } } impl<E: AsRef<Element>> ElementBuilder<E> { pub fn child(self, value: impl ElementChild) -> Self { value.append(&self); self } pub fn class_name(self, value: &str) -> Self { self.element.as_ref().set_class_name(value); self } pub fn id(self, value: &str) -> Self { self.element.as_ref().set_id(value); self } pub fn on_click(self, f: impl FnMut() + 'static) -> Self { self.element .as_ref() .add_event_listener_with_callback( "click", Closure::wrap(Box::new(f) as Box<dyn FnMut()>) .into_js_value() .unchecked_ref(), ) .unwrap_throw(); self } } pub trait ElementChild: Sealed { fn append<P: AsRef<Element>>(self, parent: &ElementBuilder<P>); } impl<T: ElementChild> Sealed for T {} impl<E: AsRef<Element>> ElementChild for ElementBuilder<E> { fn append<P: AsRef<Element>>(self, parent: &ElementBuilder<P>) { let parent = parent.element.as_ref(); let node = self.build(); parent.append_with_node_1(node.as_ref()).unwrap_throw(); } } impl ElementChild for &str { fn append<P: AsRef<Element>>(self, parent: &ElementBuilder<P>) { parent .element .as_ref() .append_with_str_1(self) .unwrap_throw(); } } impl ElementChild for String { fn append<P: AsRef<Element>>(self, parent: &ElementBuilder<P>) { ElementChild::append(self.as_str(), parent); } } impl ElementChild for &Atom<String> { fn append<P: AsRef<Element>>(self, parent: &ElementBuilder<P>) { let document = window().unwrap_throw().document().unwrap_throw(); let node = document.create_text_node(""); let parent = parent.as_ref().as_ref(); parent.append_with_node_1(&node).unwrap_throw(); let value = self.clone(); react(move || { node.set_node_value(Some(&value.get())); }); } } impl<E: AsRef<EventTarget>> ElementBuilder<E> { pub fn add_event_listener_with_callback( self, type_: &str, listener: impl Fn(Event) + 'static, ) -> Self { self.as_ref() .as_ref() .add_event_listener_with_callback( type_, Closure::wrap(Box::new(listener) as Box<dyn Fn(Event)>) .into_js_value() .unchecked_ref(), ) .unwrap_throw(); self } } impl<E: AsRef<HtmlButtonElement>> ElementBuilder<E> { pub fn type_(self, value: &str) -> Self { self.element.as_ref().set_type(value); self } } macro_rules! define_builder { ($name:ident => $type:ident) => { pub fn $name() -> ElementBuilder<::web_sys::$type> { let document = window().unwrap_throw().document().unwrap_throw(); let element = document.create_element(stringify!($name)).unwrap_throw(); ElementBuilder::new(element.unchecked_into()) } }; } define_builder!(a => Element); define_builder!(button => HtmlButtonElement); define_builder!(div => Element); define_builder!(h1 => Element); define_builder!(span => Element); define_builder!(table => Element); define_builder!(tbody => Element); define_builder!(td => Element); define_builder!(tr => Element); #[cfg(test)] mod tests { use crate::elements::{div, span}; use wasm_bindgen_test::wasm_bindgen_test; #[wasm_bindgen_test] fn build() { let div = div().build(); assert_eq!(div.node_name(), "DIV"); } #[wasm_bindgen_test] fn child_element() { let div = div().child(span()).build(); assert_eq!(div.outer_html(), "<div><span></span></div>"); } #[wasm_bindgen_test] fn child_str() { let div = div().child("str").build(); assert_eq!(div.outer_html(), "<div>str</div>"); } #[wasm_bindgen_test] fn child_string() { let div = div().child("string".to_string()).build(); assert_eq!(div.outer_html(), "<div>string</div>"); } }