✨ Nuevo método writable() para componentes hijo
This commit is contained in:
parent
234ee12e78
commit
83e09c38ce
1 changed files with 28 additions and 8 deletions
|
|
@ -2,7 +2,7 @@ use crate::core::component::{ComponentTrait, Context};
|
||||||
use crate::html::{html, Markup};
|
use crate::html::{html, Markup};
|
||||||
use crate::{fn_builder, UniqueId};
|
use crate::{fn_builder, UniqueId};
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock, RwLockWriteGuard};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ChildComponent(Arc<RwLock<dyn ComponentTrait>>);
|
pub struct ChildComponent(Arc<RwLock<dyn ComponentTrait>>);
|
||||||
|
|
@ -12,6 +12,16 @@ impl ChildComponent {
|
||||||
ChildComponent(Arc::new(RwLock::new(component)))
|
ChildComponent(Arc::new(RwLock::new(component)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChildComponent GETTERS.
|
||||||
|
|
||||||
|
pub fn id(&self) -> Option<String> {
|
||||||
|
self.0.read().unwrap().id()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writable(&self) -> RwLockWriteGuard<'_, dyn ComponentTrait> {
|
||||||
|
self.0.write().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
// ChildComponent RENDER.
|
// ChildComponent RENDER.
|
||||||
|
|
||||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||||
|
|
@ -24,7 +34,7 @@ impl ChildComponent {
|
||||||
self.0.read().unwrap().type_id()
|
self.0.read().unwrap().type_id()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn id(&self) -> String {
|
fn child_id(&self) -> String {
|
||||||
self.0.read().unwrap().id().unwrap_or_default()
|
self.0.read().unwrap().id().unwrap_or_default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -44,6 +54,16 @@ impl<C: ComponentTrait> TypedComponent<C> {
|
||||||
TypedComponent(Arc::new(RwLock::new(component)))
|
TypedComponent(Arc::new(RwLock::new(component)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TypedComponent GETTERS.
|
||||||
|
|
||||||
|
pub fn id(&self) -> Option<String> {
|
||||||
|
self.0.read().unwrap().id()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn writable(&self) -> RwLockWriteGuard<'_, C> {
|
||||||
|
self.0.write().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
// TypedComponent RENDER.
|
// TypedComponent RENDER.
|
||||||
|
|
||||||
pub fn render(&self, cx: &mut Context) -> Markup {
|
pub fn render(&self, cx: &mut Context) -> Markup {
|
||||||
|
|
@ -135,7 +155,7 @@ impl Children {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn insert_after_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
|
fn insert_after_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
|
||||||
match self.0.iter().position(|c| c.id() == id) {
|
match self.0.iter().position(|c| c.child_id() == id) {
|
||||||
Some(index) => self.0.insert(index + 1, child),
|
Some(index) => self.0.insert(index + 1, child),
|
||||||
_ => self.0.push(child),
|
_ => self.0.push(child),
|
||||||
};
|
};
|
||||||
|
|
@ -144,7 +164,7 @@ impl Children {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn insert_before_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
|
fn insert_before_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
|
||||||
match self.0.iter().position(|c| c.id() == id) {
|
match self.0.iter().position(|c| c.child_id() == id) {
|
||||||
Some(index) => self.0.insert(index, child),
|
Some(index) => self.0.insert(index, child),
|
||||||
_ => self.0.insert(0, child),
|
_ => self.0.insert(0, child),
|
||||||
};
|
};
|
||||||
|
|
@ -159,7 +179,7 @@ impl Children {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn remove_by_id(&mut self, id: &str) -> &mut Self {
|
fn remove_by_id(&mut self, id: &str) -> &mut Self {
|
||||||
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
|
if let Some(index) = self.0.iter().position(|c| c.child_id() == id) {
|
||||||
self.0.remove(index);
|
self.0.remove(index);
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
|
|
@ -168,7 +188,7 @@ impl Children {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn replace_by_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
|
fn replace_by_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
|
||||||
for c in &mut self.0 {
|
for c in &mut self.0 {
|
||||||
if c.id() == id {
|
if c.child_id() == id {
|
||||||
*c = child;
|
*c = child;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -194,12 +214,12 @@ impl Children {
|
||||||
|
|
||||||
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ChildComponent> {
|
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&ChildComponent> {
|
||||||
let id = id.into();
|
let id = id.into();
|
||||||
self.0.iter().find(|c| c.id() == id)
|
self.0.iter().find(|c| c.child_id() == id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ChildComponent> {
|
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &ChildComponent> {
|
||||||
let id = id.into();
|
let id = id.into();
|
||||||
self.0.iter().filter(move |&c| c.id() == id)
|
self.0.iter().filter(move |&c| c.child_id() == id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &ChildComponent> {
|
pub fn iter_by_type_id(&self, type_id: UniqueId) -> impl Iterator<Item = &ChildComponent> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue