rumtk_web/components/
mod.rs1use crate::utils::ComponentFunction;
22use rumtk_core::cache::{new_cache, LazyRUMCache};
23use rumtk_core::strings::RUMString;
24use rumtk_core::{rumtk_cache_get, rumtk_cache_push};
25
26pub mod app_body;
28pub mod app_head;
29pub mod app_shell;
30
31mod contact_button;
33mod contact_card;
34mod content_viewer;
35pub mod div;
36mod footer;
37pub mod form;
38mod formatted_label;
39mod header;
40mod info_card;
41mod label;
42mod list;
43mod logo;
44mod main;
45mod navlink;
46mod portrait_card;
47mod script;
48mod socials;
49mod spacer;
50mod text_card;
51mod title;
52
53pub type ComponentCache = LazyRUMCache<RUMString, ComponentFunction>;
54pub type UserComponentItem<'a> = (&'a str, ComponentFunction);
55pub type UserComponents<'a> = Vec<UserComponentItem<'a>>;
56pub type UserComponentCacheItem = ComponentFunction;
57
58static mut COMPONENT_CACHE: ComponentCache = new_cache();
59static DEFAULT_COMPONENT: ComponentFunction = div::div;
60
61pub fn register_component(name: &str, component_fxn: ComponentFunction) {
62 let key = RUMString::from(name);
63 let _ = rumtk_cache_push!(&raw mut COMPONENT_CACHE, &key, component_fxn);
64}
65
66pub fn get_component(name: &str) -> Option<UserComponentCacheItem> {
67 rumtk_cache_get!(
68 &raw mut COMPONENT_CACHE,
69 &RUMString::from(name)
70 )
71}
72
73pub fn init_components(user_components: Option<UserComponents>) {
74 register_component("logo", logo::logo);
76 register_component("info_card", info_card::info_card);
77 register_component("portrait_card", portrait_card::portrait_card);
78 register_component("title", title::title);
79 register_component("footer", footer::footer);
80 register_component("main", main::main);
81 register_component("header", header::header);
82 register_component("contact_card", contact_card::contact_card);
83 register_component("contact_button", contact_button::contact_button);
84 register_component("socials", socials::socials);
85 register_component("list", list::list);
86 register_component("navlink", navlink::navlink);
87 register_component("label", label::label);
88 register_component("formatted_label", formatted_label::formatted_label);
89 register_component("text_card", text_card::text_card);
90 register_component("form", form::form::form);
91 register_component("spacer", spacer::spacer);
92 register_component("script", script::script);
93 register_component("content_viewer", content_viewer::content_viewer);
94 register_component("div", div::div);
95
96 for itm in user_components.unwrap_or_default() {
98 let (name, value) = itm;
99 register_component(name, value);
100 }
101}
102
103#[macro_export]
104macro_rules! rumtk_web_register_component {
105 ( $key:expr, $fxn:expr ) => {{
106 use $crate::components::register_component;
107 register_component($key, $fxn)
108 }};
109}
110
111#[macro_export]
112macro_rules! rumtk_web_get_component {
113 ( $key:expr ) => {{
114 use $crate::components::get_component;
115 get_component($key)
116 }};
117}
118
119#[macro_export]
120macro_rules! rumtk_web_init_components {
121 ( $components:expr ) => {{
122 use $crate::components::init_components;
123 init_components($components)
124 }};
125}