Trait domafic::dom_node::DomNode
[−]
[src]
pub trait DomNode: Sized { type Message; type Children: DomNodes<Message=Self::Message>; type Listeners: Listeners<Message=Self::Message>; type WithoutListeners: DomNode<Message=Self::Message, Children=Self::Children, Listeners=EmptyListeners<Self::Message>>; fn key(&self) -> Option<u32>; fn get_attribute(&self, _index: usize) -> Option<&KeyValue>; fn children(&self) -> &Self::Children; fn listeners(&self) -> &Self::Listeners; fn children_and_listeners(&self) -> (&Self::Children, &Self::Listeners); fn split_listeners(self) -> (Self::WithoutListeners, Self::Listeners); fn value(&self) -> DOMValue; fn with_key(self, key: usize) -> WithKey<Self> { ... } fn displayable(&self) -> HtmlDisplayable<Self> { ... } fn attributes(&self) -> AttributeIter<Self> { ... } fn with_attributes<A: AsRef<[KeyValue]>>(self,
attributes: A)
-> WithAttributes<Self, A> { ... } fn with_listeners<L: Listeners<Message=Self::Message>>(self,
listeners: L)
-> WithListeners<Self::WithoutListeners, (L, Self::Listeners)> { ... } }
A DomNode
specifies the HTML DOM (Document Object Model) representation of a type.
Note that there can be many different types that map to the same HTML. For example, both
String
and str
can be used to create HTML text nodes.
Associated Types
type Message
The type of message sent by a listener. Messages of this type should be used to update application state.
type Children: DomNodes<Message=Self::Message>
The type of the set of children contained by the DomNode
.
Examples:
Tag<...>
(Tag<...>, Tag<...>, Tag<...>)
[Tag<...>; 5]
type Listeners: Listeners<Message=Self::Message>
The type of the set of listeners watching this DomNode
for events.
Examples:
FnListener<...>
(FnListener<..>, FnListener<...>)
[Box<Listener<Message=()>>; 5]
type WithoutListeners: DomNode<Message=Self::Message, Children=Self::Children, Listeners=EmptyListeners<Self::Message>>
The type of the DomNode
with its listeners replaced by EmptyListeners
.
This is useful for splitting the DomNode
up into its listener and non-listener components
so that they can be transformed separately.
Required Methods
fn key(&self) -> Option<u32>
If present, the key will be included in the KeyStack
returned alongside a message.
This should be used to differentiate messages from peer DomNode
s.
fn get_attribute(&self, _index: usize) -> Option<&KeyValue>
Get the nth attribute for a given DomNode
.
If node.get_attribute(i)
returns None
, node.get_attribute(j)
should return None
for all j >= i
.
fn children(&self) -> &Self::Children
Returns a reference to the children of this DomNode
fn listeners(&self) -> &Self::Listeners
Returns a reference to the listeners listening for events on this DomNode
fn children_and_listeners(&self) -> (&Self::Children, &Self::Listeners)
Returns a reference to both the children and listeners of this DomNode
fn split_listeners(self) -> (Self::WithoutListeners, Self::Listeners)
Splits self
into two separate components, one with and one without listeners.
This is used to perform type-level modifications to the listeners.
fn value(&self) -> DOMValue
Returns an enum representing either the node's HTML tag or, in the case of a text node, the node's text value.
Provided Methods
fn with_key(self, key: usize) -> WithKey<Self>
Add a key to this DomNode
. This method will panic if the node already has a key.
Keys are used to differentiate between large numbers of similar components. When an event occurs in a keyed component, the keys of that component and all of its parent components will be returned to the ""
Example:
use domafic::{DomNode, KeyIter, IntoNode}; use domafic::tags::div; use domafic::listener::on; #[cfg(target_os = "emscripten")] use domafic::web_render::run; struct Clicked; type State = (); let _render = |_state: &State| div( (0..50).into_iter().map(|index| div(index.to_string().into_node()) .with_listeners(on("click", |_| Clicked)) .with_key(index) ).collect::<Vec<_>>() ); let _update = |_state: &mut State, _msg: Clicked, mut keys: KeyIter| println!("div number {} was clicked", keys.next().unwrap()); // If using in a browser: #[cfg(target_os = "emscripten")] run("body", _update, _render, ());
fn displayable(&self) -> HtmlDisplayable<Self>
Returns a wrapper that can displayed as HTML
fn attributes(&self) -> AttributeIter<Self>
Returns an iterator over a DomNode
's attributes.
fn with_attributes<A: AsRef<[KeyValue]>>(self,
attributes: A)
-> WithAttributes<Self, A>
attributes: A)
-> WithAttributes<Self, A>
Wrap the DomNode
in an additional set of attributes.
Example:
use domafic::DomNode; use domafic::empty::empty; use domafic::tags::div; use domafic::AttributeValue::Str; type MessageType = (); // Type of messages sent in response to JS events // Need to manually specify message type here since it can't be inferred let my_div = div(empty::<MessageType>()); let my_div_with_attrs = my_div.with_attributes([("key", Str("value"))]); assert_eq!(my_div_with_attrs.get_attribute(0), Some(&("key", Str("value"))));
fn with_listeners<L: Listeners<Message=Self::Message>>(self,
listeners: L)
-> WithListeners<Self::WithoutListeners, (L, Self::Listeners)>
listeners: L)
-> WithListeners<Self::WithoutListeners, (L, Self::Listeners)>
Wrap the DomNode
in an additional set of liseners.
Example:
use domafic::DomNode; use domafic::empty::empty; use domafic::listener::on; use domafic::tags::div; use domafic::AttributeValue::Str; struct Clicked; // Type of messages sent // We don't need to manually annotate the message type here since it can be inferred let my_div = div(()); let _my_div_with_listener = my_div.with_listeners(on("click", |_| Clicked));
Implementors
impl<T: DomNode> DomNode for WithKey<T>
impl<T, A> DomNode for WithAttributes<T, A> where T: DomNode,
A: AsRef<[KeyValue]>impl<T, L> DomNode for WithListeners<T, L> where T: DomNode<Message=L::Message, Listeners=EmptyListeners<L::Message>>,
L: Listenersimpl<M> DomNode for StringNode<M>
impl<M> DomNode for StringRefNode<M>
impl<C: DomNodes, A: AsRef<[KeyValue]>, L: Listeners<Message=C::Message>> DomNode for Tag<C, A, L>