1use crate::*;
2
3impl PartialEq for AttributeValue {
10 fn eq(&self, other: &Self) -> bool {
11 match (self, other) {
12 (AttributeValue::Text(a_val), AttributeValue::Text(b_val)) => a_val == b_val,
13 (AttributeValue::Signal(a_sig), AttributeValue::Signal(b_sig)) => {
14 a_sig.get() == b_sig.get()
15 }
16 (AttributeValue::Signal(a_sig), AttributeValue::Text(b_val)) => a_sig.get() == *b_val,
17 (AttributeValue::Text(a_val), AttributeValue::Signal(b_sig)) => *a_val == b_sig.get(),
18 (AttributeValue::Event(_), AttributeValue::Event(_)) => true,
19 (AttributeValue::Css(a_css), AttributeValue::Css(b_css)) => {
20 a_css.get_name() == b_css.get_name()
21 }
22 (AttributeValue::Dynamic(a_dyn), AttributeValue::Dynamic(b_dyn)) => a_dyn == b_dyn,
23 _ => false,
24 }
25 }
26}
27
28impl PartialEq for AttributeEntry {
33 fn eq(&self, other: &Self) -> bool {
34 self.get_name() == other.get_name() && self.get_value() == other.get_value()
35 }
36}
37
38impl PartialEq for TextNode {
43 fn eq(&self, other: &Self) -> bool {
44 self.get_content() == other.get_content()
45 }
46}
47
48impl PartialEq for CssClass {
53 fn eq(&self, other: &Self) -> bool {
54 self.get_name() == other.get_name()
55 }
56}
57
58impl PartialEq for VirtualNode {
68 fn eq(&self, other: &Self) -> bool {
69 match (self, other) {
70 (VirtualNode::Text(a_text), VirtualNode::Text(b_text)) => a_text == b_text,
71 (
72 VirtualNode::Element {
73 tag: a_tag,
74 attributes: a_attrs,
75 children: a_children,
76 ..
77 },
78 VirtualNode::Element {
79 tag: b_tag,
80 attributes: b_attrs,
81 children: b_children,
82 ..
83 },
84 ) => {
85 a_tag == b_tag
86 && a_attrs.len() == b_attrs.len()
87 && a_attrs.iter().zip(b_attrs.iter()).all(|(a, b)| a == b)
88 && a_children.len() == b_children.len()
89 && a_children
90 .iter()
91 .zip(b_children.iter())
92 .all(|(a, b)| a == b)
93 }
94 (VirtualNode::Fragment(a_children), VirtualNode::Fragment(b_children)) => {
95 a_children.len() == b_children.len()
96 && a_children
97 .iter()
98 .zip(b_children.iter())
99 .all(|(a, b)| a == b)
100 }
101 (VirtualNode::Dynamic(_), VirtualNode::Dynamic(_)) => true,
102 (VirtualNode::Empty, VirtualNode::Empty) => true,
103 _ => false,
104 }
105 }
106}
107
108impl Attribute {
110 pub fn as_str(&self) -> String {
112 match self {
113 Attribute::AccessKey => "accesskey".to_string(),
114 Attribute::Action => "action".to_string(),
115 Attribute::Alt => "alt".to_string(),
116 Attribute::AriaLabel => "aria-label".to_string(),
117 Attribute::AutoComplete => "autocomplete".to_string(),
118 Attribute::AutoFocus => "autofocus".to_string(),
119 Attribute::Checked => "checked".to_string(),
120 Attribute::Class => "class".to_string(),
121 Attribute::Cols => "cols".to_string(),
122 Attribute::ContentEditable => "contenteditable".to_string(),
123 Attribute::Data(name) => format!("data-{}", name),
124 Attribute::Dir => "dir".to_string(),
125 Attribute::Disabled => "disabled".to_string(),
126 Attribute::Draggable => "draggable".to_string(),
127 Attribute::EncType => "enctype".to_string(),
128 Attribute::For => "for".to_string(),
129 Attribute::Form => "form".to_string(),
130 Attribute::Height => "height".to_string(),
131 Attribute::Hidden => "hidden".to_string(),
132 Attribute::Href => "href".to_string(),
133 Attribute::Id => "id".to_string(),
134 Attribute::Lang => "lang".to_string(),
135 Attribute::Max => "max".to_string(),
136 Attribute::MaxLength => "maxlength".to_string(),
137 Attribute::Method => "method".to_string(),
138 Attribute::Min => "min".to_string(),
139 Attribute::MinLength => "minlength".to_string(),
140 Attribute::Multiple => "multiple".to_string(),
141 Attribute::Name => "name".to_string(),
142 Attribute::Pattern => "pattern".to_string(),
143 Attribute::Placeholder => "placeholder".to_string(),
144 Attribute::ReadOnly => "readonly".to_string(),
145 Attribute::Required => "required".to_string(),
146 Attribute::Rows => "rows".to_string(),
147 Attribute::Selected => "selected".to_string(),
148 Attribute::Size => "size".to_string(),
149 Attribute::SpellCheck => "spellcheck".to_string(),
150 Attribute::Src => "src".to_string(),
151 Attribute::Step => "step".to_string(),
152 Attribute::Style => "style".to_string(),
153 Attribute::TabIndex => "tabindex".to_string(),
154 Attribute::Target => "target".to_string(),
155 Attribute::Title => "title".to_string(),
156 Attribute::Type => "type".to_string(),
157 Attribute::Value => "value".to_string(),
158 Attribute::Width => "width".to_string(),
159 Attribute::Other(name) => name.clone(),
160 }
161 }
162}
163
164impl Default for DynamicNode {
166 fn default() -> Self {
167 let node: DynamicNode = DynamicNode {
168 render_fn: Rc::new(RefCell::new(|| VirtualNode::Empty)),
169 hook_context: HookContext::default(),
170 };
171 node
172 }
173}
174
175impl Clone for DynamicNode {
177 fn clone(&self) -> Self {
178 DynamicNode {
179 render_fn: Rc::clone(self.get_render_fn()),
180 hook_context: self.hook_context,
181 }
182 }
183}
184
185impl AsNode for VirtualNode {
187 fn as_node(&self) -> Option<VirtualNode> {
188 Some(self.clone())
189 }
190}
191
192impl AsNode for &VirtualNode {
194 fn as_node(&self) -> Option<VirtualNode> {
195 Some((*self).clone())
196 }
197}
198
199impl AsNode for String {
201 fn as_node(&self) -> Option<VirtualNode> {
202 Some(VirtualNode::Text(TextNode::new(self.clone(), None)))
203 }
204}
205
206impl AsNode for &str {
208 fn as_node(&self) -> Option<VirtualNode> {
209 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
210 }
211}
212
213impl AsNode for i32 {
215 fn as_node(&self) -> Option<VirtualNode> {
216 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
217 }
218}
219
220impl AsNode for i64 {
222 fn as_node(&self) -> Option<VirtualNode> {
223 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
224 }
225}
226
227impl AsNode for usize {
229 fn as_node(&self) -> Option<VirtualNode> {
230 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
231 }
232}
233
234impl AsNode for f32 {
236 fn as_node(&self) -> Option<VirtualNode> {
237 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
238 }
239}
240
241impl AsNode for f64 {
243 fn as_node(&self) -> Option<VirtualNode> {
244 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
245 }
246}
247
248impl AsNode for bool {
250 fn as_node(&self) -> Option<VirtualNode> {
251 Some(VirtualNode::Text(TextNode::new(self.to_string(), None)))
252 }
253}
254
255impl<T> AsNode for Signal<T>
257where
258 T: Clone + PartialEq + std::fmt::Display + 'static,
259{
260 fn as_node(&self) -> Option<VirtualNode> {
261 Some(self.as_reactive_text())
262 }
263}
264
265impl IntoNode for VirtualNode {
267 fn into_node(self) -> VirtualNode {
268 self
269 }
270}
271
272impl<F> IntoNode for F
277where
278 F: FnMut() -> VirtualNode + 'static,
279{
280 fn into_node(self) -> VirtualNode {
281 VirtualNode::Dynamic(DynamicNode {
282 render_fn: Rc::new(RefCell::new(self)),
283 hook_context: crate::reactive::create_hook_context(),
284 })
285 }
286}
287
288impl IntoNode for String {
290 fn into_node(self) -> VirtualNode {
291 VirtualNode::Text(TextNode::new(self, None))
292 }
293}
294
295impl IntoNode for &str {
297 fn into_node(self) -> VirtualNode {
298 VirtualNode::Text(TextNode::new(self.to_string(), None))
299 }
300}
301
302impl IntoNode for i32 {
304 fn into_node(self) -> VirtualNode {
305 VirtualNode::Text(TextNode::new(self.to_string(), None))
306 }
307}
308
309impl IntoNode for usize {
311 fn into_node(self) -> VirtualNode {
312 VirtualNode::Text(TextNode::new(self.to_string(), None))
313 }
314}
315
316impl IntoNode for bool {
318 fn into_node(self) -> VirtualNode {
319 VirtualNode::Text(TextNode::new(self.to_string(), None))
320 }
321}
322
323impl<T> IntoNode for Signal<T>
325where
326 T: Clone + PartialEq + std::fmt::Display + 'static,
327{
328 fn into_node(self) -> VirtualNode {
329 self.as_reactive_text()
330 }
331}
332
333impl VirtualNode {
335 pub fn get_element_node(tag_name: &str) -> Self {
337 VirtualNode::Element {
338 tag: Tag::Element(tag_name.to_string()),
339 attributes: Vec::new(),
340 children: Vec::new(),
341 key: None,
342 }
343 }
344
345 pub fn get_text_node(content: &str) -> Self {
347 VirtualNode::Text(TextNode::new(content.to_string(), None))
348 }
349
350 pub fn with_attribute(mut self, name: &str, value: AttributeValue) -> Self {
352 if let VirtualNode::Element {
353 ref mut attributes, ..
354 } = self
355 {
356 attributes.push(AttributeEntry::new(name.to_string(), value));
357 }
358 self
359 }
360
361 pub fn with_child(mut self, child: VirtualNode) -> Self {
363 if let VirtualNode::Element {
364 ref mut children, ..
365 } = self
366 {
367 children.push(child);
368 }
369 self
370 }
371
372 pub fn is_component(&self) -> bool {
374 matches!(
375 self,
376 VirtualNode::Element {
377 tag: Tag::Component(_),
378 ..
379 }
380 )
381 }
382
383 pub fn tag_name(&self) -> Option<String> {
385 match self {
386 VirtualNode::Element { tag, .. } => match tag {
387 Tag::Element(name) => Some(name.clone()),
388 Tag::Component(name) => Some(name.clone()),
389 },
390 _ => None,
391 }
392 }
393
394 pub fn try_get_prop(&self, name: &Attribute) -> Option<String> {
396 let name_str: String = name.as_str();
397 if let VirtualNode::Element { attributes, .. } = self {
398 for attr in attributes {
399 if attr.get_name() == &name_str {
400 match attr.get_value() {
401 AttributeValue::Text(value) => return Some(value.clone()),
402 AttributeValue::Signal(signal) => return Some(signal.get()),
403 _ => {}
404 }
405 }
406 }
407 }
408 None
409 }
410
411 pub fn try_get_signal_prop(&self, name: &Attribute) -> Option<Signal<String>> {
416 let name_str: String = name.as_str();
417 if let VirtualNode::Element { attributes, .. } = self {
418 for attr in attributes {
419 if attr.get_name() == &name_str
420 && let AttributeValue::Signal(signal) = attr.get_value()
421 {
422 return Some(*signal);
423 }
424 }
425 }
426 None
427 }
428
429 pub fn get_children(&self) -> Vec<VirtualNode> {
431 if let VirtualNode::Element { children, .. } = self {
432 children.clone()
433 } else {
434 Vec::new()
435 }
436 }
437
438 pub fn try_get_text(&self) -> Option<String> {
440 match self {
441 VirtualNode::Text(text_node) => Some(text_node.get_content().clone()),
442 VirtualNode::Element { children, .. } => {
443 children.first().and_then(VirtualNode::try_get_text)
444 }
445 _ => None,
446 }
447 }
448
449 pub fn try_get_event(
451 &self,
452 name: &NativeEventName,
453 ) -> Option<crate::event::NativeEventHandler> {
454 let name_str: String = name.as_str();
455 if let VirtualNode::Element { attributes, .. } = self {
456 for attr in attributes {
457 if attr.get_name() == &name_str
458 && let AttributeValue::Event(handler) = attr.get_value()
459 {
460 return Some(handler.clone());
461 }
462 }
463 }
464 None
465 }
466
467 pub fn try_get_callback(&self, name: &str) -> Option<crate::event::NativeEventHandler> {
469 if let VirtualNode::Element { attributes, .. } = self {
470 for attr in attributes {
471 if attr.get_name() == name
472 && let AttributeValue::Event(handler) = attr.get_value()
473 {
474 return Some(handler.clone());
475 }
476 }
477 }
478 None
479 }
480}
481
482impl<T> AsReactiveText for Signal<T>
484where
485 T: Clone + PartialEq + std::fmt::Display + 'static,
486{
487 fn as_reactive_text(&self) -> VirtualNode {
488 let signal: Signal<T> = *self;
489 let initial: String = signal.get().to_string();
490 let string_signal: Signal<String> = {
491 let boxed: Box<SignalInner<String>> = Box::new(SignalInner::new(initial.clone()));
492 Signal::from_inner(Box::leak(boxed) as *mut SignalInner<String>)
493 };
494 let source_signal: Signal<T> = *self;
495 let string_signal_clone: Signal<String> = string_signal;
496 source_signal.subscribe({
497 let source_signal: Signal<T> = source_signal;
498 move || {
499 let new_value: String = source_signal.get().to_string();
500 string_signal_clone.set(new_value);
501 }
502 });
503 VirtualNode::Text(TextNode::new(initial, Some(string_signal)))
504 }
505}
506
507impl Style {
509 pub fn property<N, V>(mut self, name: N, value: V) -> Self
514 where
515 N: AsRef<str>,
516 V: AsRef<str>,
517 {
518 self.get_mut_properties().push(StyleProperty::new(
519 name.as_ref().replace('_', "-"),
520 value.as_ref().to_string(),
521 ));
522 self
523 }
524
525 pub fn to_css_string(&self) -> String {
527 self.get_properties()
528 .iter()
529 .map(|p| format!("{}: {};", p.get_name(), p.get_value()))
530 .collect::<Vec<String>>()
531 .join(" ")
532 }
533}
534
535impl Default for Style {
537 fn default() -> Self {
538 Self::new(Vec::new())
539 }
540}
541
542impl CssClass {
544 pub fn new(name: String, style: String) -> Self {
548 let mut css_class: CssClass = CssClass::default();
549 css_class.set_name(name);
550 css_class.set_style(style);
551 css_class.inject_style();
552 css_class
553 }
554
555 pub fn inject_style(&self) {
562 #[cfg(target_arch = "wasm32")]
563 {
564 let style_id: &str = "euv-css-injected";
565 let document: web_sys::Document = web_sys::window()
566 .expect("no global window exists")
567 .document()
568 .expect("no document exists");
569 let style_element: web_sys::HtmlStyleElement = match document
570 .get_element_by_id(style_id)
571 {
572 Some(el) => el.dyn_into::<web_sys::HtmlStyleElement>().unwrap(),
573 None => {
574 let el: web_sys::HtmlStyleElement = document
575 .create_element("style")
576 .unwrap()
577 .dyn_into::<web_sys::HtmlStyleElement>()
578 .unwrap();
579 el.set_id(style_id);
580 let keyframes: &str = "@keyframes euv-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } @keyframes euv-fade-in { from { opacity: 0; } to { opacity: 1; } } @keyframes euv-scale-in { from { transform: scale(0.9); opacity: 0; } to { transform: scale(1); opacity: 1; } } @keyframes euv-pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.2); } } @keyframes euv-slide-up { from { transform: translateY(100%); } to { transform: translateY(0); } } @keyframes euv-slide-left { from { transform: translateX(-100%); } to { transform: translateX(0); } } @keyframes euv-fade-in-up { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }";
581 let global: &str = "html, body, #app { height: 100%; margin: 0; padding: 0; overflow: hidden; } * { -webkit-tap-highlight-color: transparent; }";
582 let media_queries: &str = "@media (max-width: 767px) { .c_app_nav { display: none; } .c_app_main { padding: 20px 16px; max-width: 100%; } .c_page_title { font-size: 22px; } .c_page_subtitle { font-size: 14px; } .c_card { padding: 16px; margin: 12px 0; border-radius: 10px; } .c_card_title { font-size: 16px; } .c_form_grid { grid-template-columns: 1fr; } .c_browser_api_row { grid-template-columns: 1fr; } .c_modal_content { max-width: 100%; width: calc(100% - 32px); border-radius: 16px 16px 0 0; position: fixed; bottom: 0; left: 16px; height: 80vh; animation: euv-slide-up 0.25s ease; } .c_modal_overlay { align-items: flex-end; } .c_event_stats { gap: 12px; flex-wrap: wrap; } .c_event_section_row { gap: 12px; flex-wrap: wrap; } .c_event_section_col { min-width: 100%; } .c_counter_value { font-size: 20px; } .c_timer_value { font-size: 36px; } .c_not_found_code { font-size: 56px; } .c_not_found_container { padding: 40px 20px; } .c_list_input_row { flex-direction: column; } .c_vconsole_button { bottom: 16px; right: 16px; width: 44px; height: 44px; border-radius: 12px; } .c_tab_bar { flex-wrap: wrap; } .c_primary_button { padding: 10px 18px; font-size: 14px; } .c_badge { padding: 4px 10px; font-size: 11px; } .c_badge_outline { padding: 4px 10px; font-size: 11px; } .c_browser_info_grid { grid-template-columns: 1fr; } .c_anim_spin { font-size: 36px; } .c_anim_spin_stopped { font-size: 36px; } .c_anim_pulse { font-size: 36px; } .c_anim_pulse_stopped { font-size: 36px; } }";
583 el.set_inner_text(&format!("{} {} {}", global, keyframes, media_queries));
584 document.head().unwrap().append_child(&el).unwrap();
585 el
586 }
587 };
588 let existing_css: String = style_element.inner_text();
589 let class_rule: String = format!(".{} {{ {} }}", self.get_name(), self.get_style());
590 if !existing_css.contains(&class_rule) {
591 let new_css: String = if existing_css.is_empty() {
592 class_rule
593 } else {
594 format!("{}\n{}", existing_css, class_rule)
595 };
596 style_element.set_inner_text(&new_css);
597 }
598 }
599 }
600}
601
602impl std::fmt::Display for CssClass {
607 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
608 write!(f, "{}", self.get_name())
609 }
610}