|
@@ -1,6 +1,6 @@
|
|
|
-use gloo::history::{BrowserHistory, History, HistoryListener};
|
|
|
+use gloo::history::{BrowserHistory, History, HistoryListener, Location};
|
|
|
use std::{
|
|
|
- cell::{Cell, RefCell},
|
|
|
+ cell::{Cell, Ref, RefCell},
|
|
|
collections::HashMap,
|
|
|
rc::Rc,
|
|
|
};
|
|
@@ -10,10 +10,9 @@ use dioxus_core::ScopeId;
|
|
|
pub struct RouterService {
|
|
|
pub(crate) regen_route: Rc<dyn Fn(ScopeId)>,
|
|
|
history: Rc<RefCell<BrowserHistory>>,
|
|
|
- registerd_routes: RefCell<RouteSlot>,
|
|
|
slots: Rc<RefCell<Vec<(ScopeId, String)>>>,
|
|
|
- root_found: Rc<Cell<bool>>,
|
|
|
- cur_root: RefCell<String>,
|
|
|
+ root_found: Rc<Cell<Option<ScopeId>>>,
|
|
|
+ cur_path_params: Rc<RefCell<HashMap<String, String>>>,
|
|
|
listener: HistoryListener,
|
|
|
}
|
|
|
|
|
@@ -40,48 +39,54 @@ impl RouterService {
|
|
|
|
|
|
let _slots = slots.clone();
|
|
|
|
|
|
- let root_found = Rc::new(Cell::new(false));
|
|
|
+ let root_found = Rc::new(Cell::new(None));
|
|
|
let regen = regen_route.clone();
|
|
|
let _root_found = root_found.clone();
|
|
|
let listener = history.listen(move || {
|
|
|
- _root_found.set(false);
|
|
|
+ _root_found.set(None);
|
|
|
// checking if the route is valid is cheap, so we do it
|
|
|
- for (slot, _) in _slots.borrow_mut().iter().rev() {
|
|
|
- log::trace!("regenerating slot {:?}", slot);
|
|
|
+ for (slot, root) in _slots.borrow_mut().iter().rev() {
|
|
|
+ log::trace!("regenerating slot {:?} for root '{}'", slot, root);
|
|
|
regen(*slot);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
Self {
|
|
|
- registerd_routes: RefCell::new(RouteSlot::Routes {
|
|
|
- partial: String::from("/"),
|
|
|
- total: String::from("/"),
|
|
|
- rest: Vec::new(),
|
|
|
- }),
|
|
|
root_found,
|
|
|
history: Rc::new(RefCell::new(history)),
|
|
|
regen_route,
|
|
|
slots,
|
|
|
- cur_root: RefCell::new(path.to_string()),
|
|
|
+ cur_path_params: Rc::new(RefCell::new(HashMap::new())),
|
|
|
listener,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pub fn push_route(&self, route: &str) {
|
|
|
+ log::trace!("Pushing route: {}", route);
|
|
|
self.history.borrow_mut().push(route);
|
|
|
}
|
|
|
|
|
|
pub fn register_total_route(&self, route: String, scope: ScopeId, fallback: bool) {
|
|
|
- self.slots.borrow_mut().push((scope, route));
|
|
|
+ let clean = clean_route(route);
|
|
|
+ log::trace!("Registered route '{}' with scope id {:?}", clean, scope);
|
|
|
+ self.slots.borrow_mut().push((scope, clean));
|
|
|
}
|
|
|
|
|
|
pub fn should_render(&self, scope: ScopeId) -> bool {
|
|
|
- if self.root_found.get() {
|
|
|
+ log::trace!("Should render scope id {:?}?", scope);
|
|
|
+ if let Some(root_id) = self.root_found.get() {
|
|
|
+ log::trace!(" we already found a root with scope id {:?}", root_id);
|
|
|
+ if root_id == scope {
|
|
|
+ log::trace!(" yes - it's a match");
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ log::trace!(" no - it's not a match");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
let location = self.history.borrow().location();
|
|
|
let path = location.path();
|
|
|
+ log::trace!(" current path is '{}'", path);
|
|
|
|
|
|
let roots = self.slots.borrow();
|
|
|
|
|
@@ -89,15 +94,24 @@ impl RouterService {
|
|
|
|
|
|
// fallback logic
|
|
|
match root {
|
|
|
- Some((_id, route)) => {
|
|
|
- if route == path {
|
|
|
- self.root_found.set(true);
|
|
|
+ Some((id, route)) => {
|
|
|
+ log::trace!(
|
|
|
+ " matched given scope id {:?} with route root '{}'",
|
|
|
+ scope,
|
|
|
+ route,
|
|
|
+ );
|
|
|
+ if let Some(params) = route_matches_path(route, path) {
|
|
|
+ log::trace!(" and it matches the current path '{}'", path);
|
|
|
+ self.root_found.set(Some(*id));
|
|
|
+ *self.cur_path_params.borrow_mut() = params;
|
|
|
true
|
|
|
} else {
|
|
|
if route == "" {
|
|
|
- self.root_found.set(true);
|
|
|
+ log::trace!(" and the route is the root, so we will use that without a better match");
|
|
|
+ self.root_found.set(Some(*id));
|
|
|
true
|
|
|
} else {
|
|
|
+ log::trace!(" and the route '{}' is not the root nor does it match the current path", route);
|
|
|
false
|
|
|
}
|
|
|
}
|
|
@@ -105,6 +119,70 @@ impl RouterService {
|
|
|
None => false,
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ pub fn current_location(&self) -> Location {
|
|
|
+ self.history.borrow().location().clone()
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn current_path_params(&self) -> Ref<HashMap<String, String>> {
|
|
|
+ self.cur_path_params.borrow()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+fn clean_route(route: String) -> String {
|
|
|
+ if route.as_str() == "/" {
|
|
|
+ return route;
|
|
|
+ }
|
|
|
+ route.trim_end_matches('/').to_string()
|
|
|
+}
|
|
|
+
|
|
|
+fn clean_path(path: &str) -> &str {
|
|
|
+ if path == "/" {
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+ path.trim_end_matches('/')
|
|
|
+}
|
|
|
+
|
|
|
+fn route_matches_path(route: &str, path: &str) -> Option<HashMap<String, String>> {
|
|
|
+ let route_pieces = route.split('/').collect::<Vec<_>>();
|
|
|
+ let path_pieces = clean_path(path).split('/').collect::<Vec<_>>();
|
|
|
+
|
|
|
+ log::trace!(
|
|
|
+ " checking route pieces {:?} vs path pieces {:?}",
|
|
|
+ route_pieces,
|
|
|
+ path_pieces,
|
|
|
+ );
|
|
|
+
|
|
|
+ if route_pieces.len() != path_pieces.len() {
|
|
|
+ log::trace!(" the routes are different lengths");
|
|
|
+ return None;
|
|
|
+ }
|
|
|
+
|
|
|
+ let mut matches = HashMap::new();
|
|
|
+ for (i, r) in route_pieces.iter().enumerate() {
|
|
|
+ log::trace!(" checking route piece '{}' vs path", r);
|
|
|
+ // If this is a parameter then it matches as long as there's
|
|
|
+ // _any_thing in that spot in the path.
|
|
|
+ if r.starts_with(':') {
|
|
|
+ log::trace!(
|
|
|
+ " route piece '{}' starts with a colon so it matches anything",
|
|
|
+ r,
|
|
|
+ );
|
|
|
+ let param = &r[1..];
|
|
|
+ matches.insert(param.to_string(), path_pieces[i].to_string());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ log::trace!(
|
|
|
+ " route piece '{}' must be an exact match for path piece '{}'",
|
|
|
+ r,
|
|
|
+ path_pieces[i],
|
|
|
+ );
|
|
|
+ if path_pieces[i] != *r {
|
|
|
+ return None;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Some(matches)
|
|
|
}
|
|
|
|
|
|
pub struct RouterCfg {
|