Changing child method to accept &mut references
This commit is contained in:
parent
8ecf715c31
commit
13c855f644
|
@ -190,8 +190,3 @@ pub(crate) fn focus(elem: &HtmlElement) {
|
|||
pub(crate) fn blur(elem: &HtmlElement) {
|
||||
elem.blur().unwrap_throw();
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn remove_all_children(node: &Node) {
|
||||
node.set_text_content(Some(intern("")));
|
||||
}
|
||||
|
|
12
src/dom.rs
12
src/dom.rs
|
@ -385,7 +385,6 @@ fn set_property<A, B, C>(element: &A, name: &B, value: C) where A: AsRef<JsValue
|
|||
pub struct DomBuilder<A> {
|
||||
element: A,
|
||||
callbacks: Callbacks,
|
||||
children: usize,
|
||||
// TODO verify this with static types instead ?
|
||||
dynamic_children: bool,
|
||||
}
|
||||
|
@ -416,7 +415,6 @@ impl<A> DomBuilder<A> {
|
|||
Self {
|
||||
element: value,
|
||||
callbacks: Callbacks::new(),
|
||||
children: 0,
|
||||
dynamic_children: false,
|
||||
}
|
||||
}
|
||||
|
@ -584,9 +582,9 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn child(mut self, mut child: Dom) -> Self {
|
||||
pub fn child<B: BorrowMut<Dom>>(mut self, mut child: B) -> Self {
|
||||
self.check_children();
|
||||
operations::insert_children_one(self.element.as_ref(), &mut self.callbacks, &mut self.children, &mut child);
|
||||
operations::insert_children_one(self.element.as_ref(), &mut self.callbacks, child.borrow_mut());
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -594,14 +592,13 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
|
|||
#[inline]
|
||||
pub fn children<B: BorrowMut<Dom>, C: IntoIterator<Item = B>>(mut self, children: C) -> Self {
|
||||
self.check_children();
|
||||
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, &mut self.children, children);
|
||||
operations::insert_children_iter(self.element.as_ref(), &mut self.callbacks, children);
|
||||
self
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn text(mut self, value: &str) -> Self {
|
||||
pub fn text(self, value: &str) -> Self {
|
||||
self.check_children();
|
||||
self.children += 1;
|
||||
// TODO should this intern ?
|
||||
bindings::append_child(self.element.as_ref(), &bindings::create_text_node(value));
|
||||
self
|
||||
|
@ -613,7 +610,6 @@ impl<A> DomBuilder<A> where A: AsRef<Node> {
|
|||
C: Signal<Item = B> + 'static {
|
||||
|
||||
self.check_children();
|
||||
self.children += 1;
|
||||
let element = make_text_signal(&mut self.callbacks, value);
|
||||
bindings::append_child(self.element.as_ref(), &element);
|
||||
self
|
||||
|
|
|
@ -53,21 +53,19 @@ fn for_each_vec<A, B>(signal: A, mut callback: B) -> CancelableFutureHandle
|
|||
}
|
||||
|
||||
|
||||
pub(crate) fn insert_children_one(element: &Node, callbacks: &mut Callbacks, children: &mut usize, dom: &mut Dom) {
|
||||
pub(crate) fn insert_children_one(element: &Node, callbacks: &mut Callbacks, dom: &mut Dom) {
|
||||
// TODO can this be made more efficient ?
|
||||
callbacks.after_insert.append(&mut dom.callbacks.after_insert);
|
||||
callbacks.after_remove.append(&mut dom.callbacks.after_remove);
|
||||
|
||||
bindings::append_child(element, &dom.element);
|
||||
|
||||
*children += 1;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn insert_children_iter<A: std::borrow::BorrowMut<Dom>, B: IntoIterator<Item = A>>(element: &Node, callbacks: &mut Callbacks, children: &mut usize, value: B) {
|
||||
pub(crate) fn insert_children_iter<A: std::borrow::BorrowMut<Dom>, B: IntoIterator<Item = A>>(element: &Node, callbacks: &mut Callbacks, value: B) {
|
||||
for mut dom in value {
|
||||
let dom = std::borrow::BorrowMut::borrow_mut(&mut dom);
|
||||
insert_children_one(element, callbacks, children, dom);
|
||||
insert_children_one(element, callbacks, dom);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -173,13 +171,9 @@ pub(crate) fn insert_children_signal_vec<A>(element: Node, callbacks: &mut Callb
|
|||
}
|
||||
|
||||
fn clear(&mut self, element: &Node) {
|
||||
// TODO is this correct ?
|
||||
if self.children.len() > 0 {
|
||||
bindings::remove_all_children(element);
|
||||
|
||||
for dom in self.children.drain(..) {
|
||||
dom.callbacks.discard();
|
||||
}
|
||||
for dom in self.children.drain(..) {
|
||||
bindings::remove_child(element, &dom.element);
|
||||
dom.callbacks.discard();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue