/
main.rs
171 lines (158 loc) · 5.03 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#![recursion_limit = "128"]
cfg_if::cfg_if! {
if #[cfg(feature = "std_web")] {
use counter_std_web as counter;
use inner_html_std_web as inner_html;
use mount_point_std_web as mount_point;
use node_refs_std_web as node_refs;
use npm_and_rest_std_web as npm_and_rest;
use todomvc_std_web as todomvc;
use two_apps_std_web as two_apps;
} else if #[cfg(feature = "web_sys")] {
use counter_web_sys as counter;
use inner_html_web_sys as inner_html;
use mount_point_web_sys as mount_point;
use node_refs_web_sys as node_refs;
use npm_and_rest_web_sys as npm_and_rest;
use todomvc_web_sys as todomvc;
use two_apps_web_sys as two_apps;
}
}
use counter::Model as Counter;
use crm::Model as Crm;
use custom_components::Model as CustomComponents;
use dashboard::Model as Dashboard;
use fragments::Model as Fragments;
use game_of_life::Model as GameOfLife;
use inner_html::Model as InnerHtml;
use large_table::Model as LargeTable;
use log::trace;
use mount_point::Model as MountPoint;
use node_refs::Model as NodeRefs;
use npm_and_rest::Model as NpmAndRest;
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter, EnumString};
use textarea::Model as Textarea;
use timer::Model as Timer;
use todomvc::Model as Todomvc;
use two_apps::Model as TwoApps;
use yew::components::Select;
use yew::{html, App, Component, ComponentLink, Html, ShouldRender};
#[derive(Clone, Debug, Display, EnumString, EnumIter, PartialEq)]
enum Scene {
Counter,
Crm,
CustomComponents,
Dashboard,
NodeRefs,
Fragments,
GameOfLife,
InnerHtml,
LargeTable,
MountPoint,
NpmAndRest,
Textarea,
Timer,
Todomvc,
TwoApps,
}
struct Model {
scene: Option<Scene>,
link: ComponentLink<Self>,
}
enum Msg {
SwitchTo(Scene),
Reset,
}
impl Component for Model {
type Message = Msg;
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
Self { scene: None, link }
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::SwitchTo(scene) => {
self.scene = Some(scene);
true
}
Msg::Reset => {
self.scene = None;
true
}
}
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {
<div id="fullscreen">
<style>{ self.view_style() }</style>
<div id="left_pane">
<h2>{ "Yew showcase" }</h2>
<Select<Scene>
selected=self.scene.clone()
options=Scene::iter().collect::<Vec<_>>()
onchange=self.link.callback(Msg::SwitchTo) />
<button onclick=self.link.callback(|_| Msg::Reset)>
{ "Reset" }
</button>
</div>
<div id="right_pane">
{ self.view_scene() }
</div>
</div>
}
}
}
impl Model {
fn view_scene(&self) -> Html {
if let Some(scene) = self.scene.as_ref() {
match scene {
Scene::Counter => html! { <Counter /> },
Scene::Crm => html! { <Crm /> },
Scene::CustomComponents => html! { <CustomComponents /> },
Scene::Dashboard => html! { <Dashboard /> },
Scene::NodeRefs => html! { <NodeRefs /> },
Scene::Fragments => html! { <Fragments /> },
Scene::GameOfLife => html! { <GameOfLife /> },
Scene::InnerHtml => html! { <InnerHtml /> },
Scene::LargeTable => html! { <LargeTable /> },
Scene::MountPoint => html! { <MountPoint /> },
Scene::NpmAndRest => html! { <NpmAndRest /> },
Scene::Textarea => html! { <Textarea /> },
Scene::Timer => html! { <Timer /> },
Scene::Todomvc => html! { <Todomvc /> },
Scene::TwoApps => html! { <TwoApps /> },
}
} else {
html! {
<p>{ "Select the scene, please." }</p>
}
}
}
fn view_style(&self) -> &str {
if let Some(scene) = self.scene.as_ref() {
match scene {
Scene::GameOfLife => include_str!("../../game_of_life/static/styles.css"),
Scene::LargeTable => include_str!("../../large_table/static/styles.css"),
Scene::Todomvc => include_str!("../static/todomvc.css"),
_ => "",
}
} else {
""
}
}
}
fn main() {
web_logger::init();
trace!("Initializing yew...");
yew::initialize();
trace!("Creating an application instance...");
let app: App<Model> = App::new();
trace!("Mount the App to the body of the page...");
app.mount_to_body();
trace!("Run");
yew::run_loop();
}