node.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use crate::{state::State, tree::NodeId};
  2. use dioxus_core::ElementId;
  3. use rustc_hash::{FxHashMap, FxHashSet};
  4. /// The node is stored client side and stores only basic data about the node.
  5. #[derive(Debug, Clone)]
  6. pub struct Node<S: State> {
  7. /// The transformed state of the node.
  8. pub state: S,
  9. /// The raw data for the node
  10. pub node_data: NodeData,
  11. }
  12. #[derive(Debug, Clone)]
  13. pub struct NodeData {
  14. /// The id of the node
  15. pub node_id: NodeId,
  16. /// The id of the node in the vdom.
  17. pub element_id: Option<ElementId>,
  18. /// Additional inforation specific to the node type
  19. pub node_type: NodeType,
  20. }
  21. #[derive(Debug, Clone)]
  22. pub struct ElementNode {
  23. pub tag: String,
  24. pub namespace: Option<String>,
  25. pub attributes: FxHashMap<OwnedAttributeDiscription, OwnedAttributeValue>,
  26. pub listeners: FxHashSet<String>,
  27. }
  28. /// A type of node with data specific to the node type. The types are a subset of the [VNode] types.
  29. #[derive(Debug, Clone)]
  30. pub enum NodeType {
  31. Text(String),
  32. Element(ElementNode),
  33. Placeholder,
  34. }
  35. impl<S: State> Node<S> {
  36. pub fn new(node_type: NodeType) -> Self {
  37. Node {
  38. state: S::default(),
  39. node_data: NodeData {
  40. element_id: None,
  41. node_type,
  42. node_id: NodeId(0),
  43. },
  44. }
  45. }
  46. /// get the mounted id of the node
  47. pub fn mounted_id(&self) -> Option<ElementId> {
  48. self.node_data.element_id
  49. }
  50. }
  51. #[derive(Debug, Clone, Hash, PartialEq, Eq)]
  52. pub struct OwnedAttributeDiscription {
  53. pub name: String,
  54. pub namespace: Option<String>,
  55. pub volatile: bool,
  56. }
  57. /// An attribute on a DOM node, such as `id="my-thing"` or
  58. /// `href="https://example.com"`.
  59. #[derive(Clone, Copy, Debug)]
  60. pub struct OwnedAttributeView<'a> {
  61. /// The discription of the attribute.
  62. pub attribute: &'a OwnedAttributeDiscription,
  63. /// The value of the attribute.
  64. pub value: &'a OwnedAttributeValue,
  65. }
  66. #[derive(Clone, Debug)]
  67. pub enum OwnedAttributeValue {
  68. Text(String),
  69. Float(f32),
  70. Int(i32),
  71. Bool(bool),
  72. None,
  73. }
  74. impl OwnedAttributeValue {
  75. pub fn as_text(&self) -> Option<&str> {
  76. match self {
  77. OwnedAttributeValue::Text(text) => Some(text),
  78. _ => None,
  79. }
  80. }
  81. pub fn as_float(&self) -> Option<f32> {
  82. match self {
  83. OwnedAttributeValue::Float(float) => Some(*float),
  84. _ => None,
  85. }
  86. }
  87. pub fn as_int(&self) -> Option<i32> {
  88. match self {
  89. OwnedAttributeValue::Int(int) => Some(*int),
  90. _ => None,
  91. }
  92. }
  93. pub fn as_bool(&self) -> Option<bool> {
  94. match self {
  95. OwnedAttributeValue::Bool(bool) => Some(*bool),
  96. _ => None,
  97. }
  98. }
  99. pub fn as_none(&self) -> Option<()> {
  100. match self {
  101. OwnedAttributeValue::None => Some(()),
  102. _ => None,
  103. }
  104. }
  105. }