rust-galmon-web/src/ui/main.rs

145 lines
2.9 KiB
Rust

mod observer;
mod world_geo;
use std::fmt;
use yew::prelude::*;
use crate::models;
use crate::ui::app::BaseData;
use crate::uitools::routing;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum MainPath {
Index,
Observers,
Map,
}
impl Default for MainPath {
fn default() -> Self {
Self::Index
}
}
impl fmt::Display for MainPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Index => write!(f, "Home"),
Self::Observers => write!(f, "Observer list"),
Self::Map => write!(f, "Map"),
// Self::Server(name, ServerPath::Zones(ZonesPath::Index)) => name.fmt(f),
// Self::Server(_, sp) => sp.fmt(f),
}
}
}
impl routing::Path for MainPath {
fn fmt(&self, f: &mut dyn routing::PathFormatter) -> fmt::Result {
match self {
Self::Index => Ok(()),
Self::Observers => f.append("observers"),
Self::Map => f.append("map"),
// Self::Server(name, sp) => {
// f.append(&name)?;
// sp.fmt(f)
// },
}
}
fn parse(path: &str) -> Self {
let (next, rem) = routing::next_component(path);
match (&*next, rem) {
("", _) => Self::Index,
("observers", _) => Self::Observers,
("map", _) => Self::Map,
// (name, rem) => Self::Server(Rc::new(name.into()), rem.map(ServerPath::parse).unwrap_or_default()),
_ => Self::Index,
}
}
fn crumbs<F: FnMut(Self)>(&self, mut add: F) {
add(Self::Index);
match self {
Self::Index => {
add(Self::Observers);
add(Self::Map);
},
Self::Observers => add(Self::Observers),
Self::Map => add(Self::Map),
// Self::Server(name, sp) => sp.crumbs(move |sp| add(Self::Server(name.clone(), sp))),
}
}
}
#[derive(Properties)]
pub struct ModelProperties {
#[props(required)]
pub ongoto: Callback<MainPath>,
pub path: MainPath,
#[props(required)]
pub base: BaseData,
#[props(required)]
pub world_geo: models::WorldGeo,
}
pub struct Main {
props: ModelProperties,
}
pub enum Msg {
RefreshGeo,
Goto(MainPath),
}
impl Main {
fn goto(&mut self, path: MainPath) -> bool {
if path == self.props.path {
false
} else {
self.props.path = path;
self.props.ongoto.emit(self.props.path.clone());
true
}
}
}
impl Component for Main {
type Message = Msg;
type Properties = ModelProperties;
fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
Self {
props,
}
}
fn change(&mut self, props: Self::Properties) -> ShouldRender {
self.props = props;
true
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::Goto(path) => {
self.goto(path)
},
Msg::RefreshGeo => {
self.props.world_geo.refresh();
false
}
}
}
}
impl Renderable<Main> for Main {
fn view(&self) -> Html<Self> {
match self.props.path {
MainPath::Index => html!{"Home"},
MainPath::Observers => self.view_observer_list(),
MainPath::Map => crate::uitools::loading::show("map", self.props.world_geo.get(), || Msg::RefreshGeo, |world_geo| {
self.view_map(world_geo)
}),
}
}
}