@@ -11,6 +11,39 @@ use wasm_bindgen::{closure::Closure, JsCast};
1111use crate :: vdom:: Mailbox ;
1212
1313
14+
15+
16+
17+ // pub tag: Tag,
18+ // pub attrs: Attrs,
19+ // pub style: Style,
20+ // pub listeners: Vec<Listener<Ms>>,
21+ // pub text: Option<String>,
22+ // pub children: Vec<El<Ms>>,
23+ //
24+ // // Things that get filled in later, to assist with rendering.
25+ // pub id: Option<u32>, // todo maybe not optional...
26+ // pub nest_level: Option<u32>,
27+ // pub el_ws: Option<web_sys::Element>,
28+ //
29+ // // todo temp?
30+ //// pub key: Option<u32>,
31+ //
32+ // pub raw_html: bool,
33+ // pub namespace: Option<Namespace>,
34+ //
35+ // // static: bool
36+ // // static_to_parent: bool
37+ // // ancestors: Vec<u32> // ids of parent, grandparent etc.
38+ //
39+ // // Lifecycle hooks
40+ // pub did_mount: Option<Box<Fn(&web_sys::Element)>>,
41+ // pub did_update: Option<Box<Fn(&web_sys::Element)>>,
42+ // pub will_unmount: Option<Box<Fn(&web_sys::Element)>>,
43+ //
44+ //
45+
46+
1447/// Common Namespaces
1548#[ derive( Debug , Clone , PartialEq ) ]
1649pub enum Namespace {
@@ -115,7 +148,6 @@ impl<Ms: Clone + 'static> Listener<Ms> {
115148 }
116149 }
117150
118- // todo deal with duplicates between the main methods and the trait
119151 /// This method is where the processing logic for events happens.
120152 pub fn attach < T > ( & mut self , el_ws : & T , mailbox : Mailbox < Ms > )
121153 where T : AsRef < web_sys:: EventTarget > {
@@ -149,41 +181,6 @@ impl<Ms: Clone + 'static> Listener<Ms> {
149181 }
150182}
151183
152- // todo don't have both method and trait fns for these
153- impl < Ms : Clone + ' static > crate :: vdom:: Listener < Ms > for Listener < Ms > {
154- /// This method is where the processing logic for events happens.
155- fn attach < T > ( & mut self , el_ws : & T , mailbox : Mailbox < Ms > )
156- where T : AsRef < web_sys:: EventTarget > {
157- // This and detach taken from Draco.
158- let mut handler = self . handler . take ( ) . expect ( "Can't find old handler" ) ;
159-
160- let closure = Closure :: wrap (
161- Box :: new ( move |event : web_sys:: Event | {
162- mailbox. send ( handler ( event) )
163- } )
164- as Box < FnMut ( web_sys:: Event ) + ' static > ,
165- ) ;
166-
167- ( el_ws. as_ref ( ) as & web_sys:: EventTarget )
168- . add_event_listener_with_callback ( & self . trigger , closure. as_ref ( ) . unchecked_ref ( ) )
169- . expect ( "problem adding listener to element" ) ;
170-
171- // Store the closure so we can detach it later. Not detaching it when an element
172- // is removed will trigger a panic.
173- self . closure = Some ( closure) ;
174- // self.handler.replace(handler); // todo ?
175- }
176-
177- fn detach < T > ( & self , el_ws : & T )
178- where T : AsRef < web_sys:: EventTarget > {
179- // This and attach taken from Draco.
180- let closure = self . closure . as_ref ( ) . unwrap ( ) ;
181- ( el_ws. as_ref ( ) as & web_sys:: EventTarget )
182- . remove_event_listener_with_callback ( & self . trigger , closure. as_ref ( ) . unchecked_ref ( ) )
183- . expect ( "problem removing listener from element" ) ;
184- }
185- }
186-
187184impl < Ms : Clone + ' static > PartialEq for Listener < Ms > {
188185 fn eq ( & self , other : & Self ) -> bool {
189186 // todo we're only checking the trigger - will miss changes if
@@ -350,6 +347,14 @@ impl Attrs {
350347 result
351348 }
352349
350+ /// Create an HTML-compatible string representation
351+ pub fn to_string ( & self ) -> String {
352+ self . vals . iter ( )
353+ . map ( |( k, v) |format ! ( "{}=\" {}\" " , k, v) )
354+ . collect :: < Vec < _ > > ( )
355+ . join ( " " )
356+ }
357+
353358 /// Add a new key, value pair
354359 pub fn add ( & mut self , key : & str , val : & str ) {
355360 self . vals . insert ( key. to_string ( ) , val. to_string ( ) ) ;
@@ -372,23 +377,6 @@ impl Attrs {
372377 }
373378}
374379
375- // todo temp duplication of trait and main methods for vals
376- impl crate :: vdom:: Attrs for Attrs {
377- fn vals ( self ) -> HashMap < String , String > {
378- self . vals
379- }
380- }
381-
382- impl ToString for Attrs {
383- /// Create an HTML-compatible string representation
384- fn to_string ( & self ) -> String {
385- self . vals . iter ( )
386- . map ( |( k, v) |format ! ( "{}=\" {}\" " , k, v) )
387- . collect :: < Vec < _ > > ( )
388- . join ( " " )
389- }
390- }
391-
392380/// Handle Style separately from Attrs, since it commonly involves multiple parts,
393381/// and has a different semantic meaning.
394382#[ derive( Clone , PartialEq ) ]
@@ -416,6 +404,18 @@ impl Style {
416404 Self { vals : HashMap :: new ( ) }
417405 }
418406
407+ /// Output style as a string, as would be set in the DOM as the attribute value
408+ /// for 'style'. Eg: "display: flex; font-size: 1.5em"
409+ pub fn to_string ( & self ) -> String {
410+ if self . vals . keys ( ) . len ( ) > 0 {
411+ self . vals
412+ . iter ( )
413+ . map ( |( k, v) | format ! ( "{}:{}" , k, v) )
414+ . collect :: < Vec < _ > > ( )
415+ . join ( ";" )
416+ } else { String :: new ( ) }
417+ }
418+
419419 pub fn add ( & mut self , key : & str , val : & str ) {
420420 self . vals . insert ( key. to_string ( ) , val. to_string ( ) ) ;
421421 }
@@ -430,27 +430,6 @@ impl Style {
430430 }
431431}
432432
433- // todo temp duplication of trait and main methods for vals
434- impl crate :: vdom:: Style for Style {
435- fn vals ( self ) -> HashMap < String , String > {
436- self . vals
437- }
438- }
439-
440- impl ToString for Style {
441- /// Output style as a string, as would be set in the DOM as the attribute value
442- /// for 'style'. Eg: "display: flex; font-size: 1.5em"
443- fn to_string ( & self ) -> String {
444- if self . vals . keys ( ) . len ( ) > 0 {
445- self . vals
446- . iter ( )
447- . map ( |( k, v) | format ! ( "{}:{}" , k, v) )
448- . collect :: < Vec < _ > > ( )
449- . join ( ";" )
450- } else { String :: new ( ) }
451- }
452- }
453-
454433/// Similar to tag population.
455434macro_rules! make_events {
456435 // Create shortcut macros for any element; populate these functions in this module.
@@ -548,12 +527,12 @@ macro_rules! make_tags {
548527 ) +
549528 }
550529
551- impl ToString for Tag {
552- fn to_string ( & self ) -> String {
530+ impl Tag {
531+ pub fn as_str ( & self ) -> & str {
553532 match self {
554533 Tag :: Custom ( name) => & name,
555534 $ (
556- Tag :: $tag_camel => $tag. to_string ( ) ,
535+ Tag :: $tag_camel => $tag,
557536 ) +
558537 }
559538 }
@@ -812,7 +791,7 @@ impl<Ms: Clone + 'static> El<Ms> {
812791 fn _html ( & self ) -> String {
813792 let text = self . text . clone ( ) . unwrap_or_default ( ) ;
814793
815- let opening = String :: from ( "<" ) + & self . tag . to_string ( ) + & self . attrs . to_string ( ) +
794+ let opening = String :: from ( "<" ) + self . tag . as_str ( ) + & self . attrs . to_string ( ) +
816795 " style=\" " + & self . style . to_string ( ) + ">\n " ;
817796
818797 let inner = self . children . iter ( ) . fold ( String :: new ( ) , |result, child| result + & child. _html ( ) ) ;
@@ -881,6 +860,15 @@ impl<Ms: Clone + 'static> Clone for El<Ms> {
881860 }
882861}
883862
863+ pub type SeedEl < Ms > = crate :: vdom:: DomEl <
864+ Tag ,
865+ Attrs ,
866+ Style ,
867+ String ,
868+ Listener < Ms > ,
869+ El < Ms >
870+ > ;
871+
884872impl < Ms : Clone + ' static > PartialEq for El < Ms > {
885873 fn eq ( & self , other : & Self ) -> bool {
886874 // todo Again, note that the listeners check only checks triggers.
@@ -896,16 +884,8 @@ impl<Ms: Clone + 'static> PartialEq for El<Ms> {
896884 }
897885}
898886
899- // todo consider splitting this up into subcrates.
900-
901- // todo: Consider moving this out into an outer crate/file
902- impl < Ms : Clone + ' static > crate :: vdom:: DomEl < Ms > for El < Ms > {
903- type Tg = Tag ;
904- type At = Attrs ;
905- type St = Style ;
906- type Ls = Listener < Ms > ;
907- type Tx = String ;
908-
887+ impl < Ms : Clone + ' static > crate :: vdom:: DomEl < Tag , Attrs , Style , String ,
888+ Listener < Ms > , El < Ms > > for El < Ms > {
909889 fn tag ( self ) -> Tag {
910890 self . tag
911891 }
@@ -924,38 +904,13 @@ impl <Ms: Clone + 'static>crate::vdom::DomEl<Ms> for El<Ms> {
924904 fn children ( self ) -> Vec < Self > {
925905 self . children
926906 }
927- fn did_mount ( self ) -> Option < Box < FnMut ( & web_sys:: Element ) > > {
928- self . did_mount
929- }
930- fn did_update ( self ) -> Option < Box < FnMut ( & web_sys:: Element ) > > {
931- self . did_mount
932- }
933- fn will_unmount ( self ) -> Option < Box < FnMut ( & web_sys:: Element ) > > {
934- self . did_mount
935- }
907+
936908 fn websys_el ( self ) -> Option < web_sys:: Element > {
937909 self . el_ws
938910 }
939911 fn id ( self ) -> Option < u32 > {
940912 self . id
941913 }
942- fn raw_html ( self ) -> bool {
943- self . raw_html
944- }
945- fn namespace ( self ) -> Option < Namespace > {
946- self . namespace
947- }
948-
949- fn empty ( self ) -> Self {
950- self . empty ( )
951- }
952-
953- fn set_id ( & mut self , id : Option < u32 > ) {
954- self . id = id
955- }
956- fn set_websys_el ( & mut self , el_ws : Option < web_sys:: Element > ) {
957- self . el_ws = el_ws
958- }
959914
960915// fn make_websys_el(&mut self, document: &web_sys::Document) -> web_sys::Element {
961916// crate::websys_bridge::make_websys_el(self, document)
@@ -1018,7 +973,6 @@ pub mod tests {
1018973 let mut el: El < Msg > = div ! [ "test" ] ;
1019974 crate :: vdom:: setup_els ( & crate :: util:: document ( ) , & mut el, 0 , 0 ) ;
1020975 assert_eq ! ( expected, el. el_ws. unwrap( ) . outer_html( ) ) ;
1021- <<<<<<< HEAD
1022976 }
1023977
1024978 #[ wasm_bindgen_test]
@@ -1043,14 +997,12 @@ pub mod tests {
1043997 pub fn attrs ( ) {
1044998 let expected = "<section class=\" biochemistry\" src=\" https://seed-rs.org\" >ok</section>" ;
1045999
1046- let mut el: El < Msg > = section ! [
1000+ let mut el: El < Msg > = div ! [
10471001 attrs!{ "class" => "biochemistry" ; "src" => "https://seed-rs.org" } ,
10481002 "ok"
10491003 ] ;
10501004
10511005 crate :: vdom:: setup_els ( & crate :: util:: document ( ) , & mut el, 0 , 0 ) ;
10521006 assert_eq ! ( expected, el. el_ws. unwrap( ) . outer_html( ) ) ;
1053- =======
1054- >>>>>>> 953 d5a04f788c3c06fffc8a82a42c4d0c93ff981
10551007 }
10561008}
0 commit comments