|
@@ -1,7 +1,7 @@
|
|
// todo: how does router work in multi-window contexts?
|
|
// todo: how does router work in multi-window contexts?
|
|
// does each window have its own router? probably, lol
|
|
// does each window have its own router? probably, lol
|
|
|
|
|
|
-use crate::{cfg::RouterCfg, location::ParsedRoute};
|
|
|
|
|
|
+use crate::cfg::RouterCfg;
|
|
use dioxus_core::ScopeId;
|
|
use dioxus_core::ScopeId;
|
|
use futures_channel::mpsc::UnboundedSender;
|
|
use futures_channel::mpsc::UnboundedSender;
|
|
use std::any::Any;
|
|
use std::any::Any;
|
|
@@ -41,135 +41,103 @@ use url::Url;
|
|
/// - On desktop, mobile, and SSR, this is just a Vec of Strings. Currently on
|
|
/// - On desktop, mobile, and SSR, this is just a Vec of Strings. Currently on
|
|
/// desktop, there is no way to tap into forward/back for the app unless explicitly set.
|
|
/// desktop, there is no way to tap into forward/back for the app unless explicitly set.
|
|
pub struct RouterCore {
|
|
pub struct RouterCore {
|
|
- pub root_found: Cell<Option<ScopeId>>,
|
|
|
|
|
|
+ pub(crate) route_found: Cell<Option<ScopeId>>,
|
|
|
|
|
|
- pub stack: RefCell<Vec<Arc<ParsedRoute>>>,
|
|
|
|
|
|
+ pub(crate) stack: RefCell<Vec<Arc<ParsedRoute>>>,
|
|
|
|
|
|
- pub router_needs_update: Cell<bool>,
|
|
|
|
|
|
+ pub(crate) tx: UnboundedSender<RouteEvent>,
|
|
|
|
|
|
- pub tx: UnboundedSender<RouteEvent>,
|
|
|
|
|
|
+ pub(crate) slots: Rc<RefCell<HashMap<ScopeId, String>>>,
|
|
|
|
|
|
- pub slots: Rc<RefCell<HashMap<ScopeId, String>>>,
|
|
|
|
|
|
+ pub(crate) onchange_listeners: Rc<RefCell<HashSet<ScopeId>>>,
|
|
|
|
|
|
- pub onchange_listeners: Rc<RefCell<HashSet<ScopeId>>>,
|
|
|
|
|
|
+ pub(crate) history: Box<dyn RouterProvider>,
|
|
|
|
|
|
- pub query_listeners: Rc<RefCell<HashMap<ScopeId, String>>>,
|
|
|
|
|
|
+ pub(crate) cfg: RouterCfg,
|
|
|
|
+}
|
|
|
|
|
|
- pub semgment_listeners: Rc<RefCell<HashMap<ScopeId, String>>>,
|
|
|
|
|
|
+/// A shared type for the RouterCore.
|
|
|
|
+pub type RouterService = Arc<RouterCore>;
|
|
|
|
|
|
- pub history: Box<dyn RouterProvider>,
|
|
|
|
|
|
+/// A route is a combination of window title, saved state, and a URL.
|
|
|
|
+#[derive(Debug, Clone)]
|
|
|
|
+pub struct ParsedRoute {
|
|
|
|
+ /// The URL of the route.
|
|
|
|
+ pub url: Url,
|
|
|
|
|
|
- pub cfg: RouterCfg,
|
|
|
|
-}
|
|
|
|
|
|
+ /// The title of the route.
|
|
|
|
+ pub title: Option<String>,
|
|
|
|
|
|
-pub type RouterService = Arc<RouterCore>;
|
|
|
|
|
|
+ /// The serialized state of the route.
|
|
|
|
+ pub serialized_state: Option<String>,
|
|
|
|
+}
|
|
|
|
|
|
#[derive(Debug)]
|
|
#[derive(Debug)]
|
|
-pub enum RouteEvent {
|
|
|
|
- Push(String),
|
|
|
|
|
|
+pub(crate) enum RouteEvent {
|
|
|
|
+ Push {
|
|
|
|
+ route: String,
|
|
|
|
+ title: Option<String>,
|
|
|
|
+ serialized_state: Option<String>,
|
|
|
|
+ },
|
|
|
|
+ Replace {
|
|
|
|
+ route: String,
|
|
|
|
+ title: Option<String>,
|
|
|
|
+ serialized_state: Option<String>,
|
|
|
|
+ },
|
|
Pop,
|
|
Pop,
|
|
}
|
|
}
|
|
|
|
|
|
impl RouterCore {
|
|
impl RouterCore {
|
|
- pub fn new(tx: UnboundedSender<RouteEvent>, cfg: RouterCfg) -> Arc<Self> {
|
|
|
|
|
|
+ pub(crate) fn new(tx: UnboundedSender<RouteEvent>, cfg: RouterCfg) -> Arc<Self> {
|
|
#[cfg(feature = "web")]
|
|
#[cfg(feature = "web")]
|
|
let history = Box::new(web::new(tx.clone()));
|
|
let history = Box::new(web::new(tx.clone()));
|
|
|
|
|
|
#[cfg(not(feature = "web"))]
|
|
#[cfg(not(feature = "web"))]
|
|
- let history = Box::new(hash::create_router());
|
|
|
|
|
|
+ let history = Box::new(hash::new());
|
|
|
|
|
|
- let route = Arc::new(ParsedRoute::new(history.init_location()));
|
|
|
|
|
|
+ let route = Arc::new(history.init_location());
|
|
|
|
|
|
Arc::new(Self {
|
|
Arc::new(Self {
|
|
cfg,
|
|
cfg,
|
|
tx,
|
|
tx,
|
|
- root_found: Cell::new(None),
|
|
|
|
|
|
+ route_found: Cell::new(None),
|
|
stack: RefCell::new(vec![route]),
|
|
stack: RefCell::new(vec![route]),
|
|
slots: Default::default(),
|
|
slots: Default::default(),
|
|
- semgment_listeners: Default::default(),
|
|
|
|
- query_listeners: Default::default(),
|
|
|
|
onchange_listeners: Default::default(),
|
|
onchange_listeners: Default::default(),
|
|
history,
|
|
history,
|
|
- router_needs_update: Default::default(),
|
|
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn handle_route_event(&self, msg: RouteEvent) -> Option<Arc<ParsedRoute>> {
|
|
|
|
- log::debug!("handling route event {:?}", msg);
|
|
|
|
- self.root_found.set(None);
|
|
|
|
-
|
|
|
|
- match msg {
|
|
|
|
- RouteEvent::Push(route) => {
|
|
|
|
- let cur = self.current_location();
|
|
|
|
-
|
|
|
|
- let new_url = cur.url.join(&route).ok().unwrap();
|
|
|
|
-
|
|
|
|
- self.history.push(new_url.as_str());
|
|
|
|
-
|
|
|
|
- let route = Arc::new(ParsedRoute::new(new_url));
|
|
|
|
-
|
|
|
|
- self.stack.borrow_mut().push(route.clone());
|
|
|
|
-
|
|
|
|
- Some(route)
|
|
|
|
- }
|
|
|
|
- RouteEvent::Pop => {
|
|
|
|
- let mut stack = self.stack.borrow_mut();
|
|
|
|
- if stack.len() == 1 {
|
|
|
|
- return None;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- self.history.pop();
|
|
|
|
- stack.pop()
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/// Push a new route to the history.
|
|
/// Push a new route to the history.
|
|
///
|
|
///
|
|
/// This will trigger a route change event.
|
|
/// This will trigger a route change event.
|
|
///
|
|
///
|
|
/// This does not modify the current route
|
|
/// This does not modify the current route
|
|
- pub fn push_route(&self, route: &str) {
|
|
|
|
- // convert the users route to our internal format
|
|
|
|
- self.tx
|
|
|
|
- .unbounded_send(RouteEvent::Push(route.to_string()))
|
|
|
|
- .unwrap();
|
|
|
|
|
|
+ pub fn push_route(&self, route: &str, title: Option<String>, serialized_state: Option<String>) {
|
|
|
|
+ let _ = self.tx.unbounded_send(RouteEvent::Push {
|
|
|
|
+ route: route.to_string(),
|
|
|
|
+ title,
|
|
|
|
+ serialized_state,
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
/// Pop the current route from the history.
|
|
/// Pop the current route from the history.
|
|
- ///
|
|
|
|
- ///
|
|
|
|
pub fn pop_route(&self) {
|
|
pub fn pop_route(&self) {
|
|
- self.tx.unbounded_send(RouteEvent::Pop).unwrap();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- pub(crate) fn register_total_route(&self, route: String, scope: ScopeId) {
|
|
|
|
- let clean = clean_route(route);
|
|
|
|
- self.slots.borrow_mut().insert(scope, clean);
|
|
|
|
|
|
+ let _ = self.tx.unbounded_send(RouteEvent::Pop);
|
|
}
|
|
}
|
|
|
|
|
|
- pub(crate) fn should_render(&self, scope: ScopeId) -> bool {
|
|
|
|
- log::debug!("Checking render: {:?}", scope);
|
|
|
|
-
|
|
|
|
- if let Some(root_id) = self.root_found.get() {
|
|
|
|
- return root_id == scope;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- let roots = self.slots.borrow();
|
|
|
|
-
|
|
|
|
- if let Some(route) = roots.get(&scope) {
|
|
|
|
- log::debug!("Registration found for scope {:?} {:?}", scope, route);
|
|
|
|
-
|
|
|
|
- if route_matches_path(&self.current_location(), route) || route.is_empty() {
|
|
|
|
- self.root_found.set(Some(scope));
|
|
|
|
- true
|
|
|
|
- } else {
|
|
|
|
- false
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- log::debug!("no route found for scope: {:?}", scope);
|
|
|
|
- false
|
|
|
|
- }
|
|
|
|
|
|
+ /// Instead of pushing a new route, replaces the current route.
|
|
|
|
+ pub fn replace_route(
|
|
|
|
+ &self,
|
|
|
|
+ route: &str,
|
|
|
|
+ title: Option<String>,
|
|
|
|
+ serialized_state: Option<String>,
|
|
|
|
+ ) {
|
|
|
|
+ let _ = self.tx.unbounded_send(RouteEvent::Replace {
|
|
|
|
+ route: route.to_string(),
|
|
|
|
+ title,
|
|
|
|
+ serialized_state,
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
/// Get the current location of the Router
|
|
/// Get the current location of the Router
|
|
@@ -177,12 +145,7 @@ impl RouterCore {
|
|
self.stack.borrow().last().unwrap().clone()
|
|
self.stack.borrow().last().unwrap().clone()
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn query_current_location(&self) -> HashMap<String, String> {
|
|
|
|
- todo!()
|
|
|
|
- // self.history.borrow().query()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Get the current location of the Router
|
|
|
|
|
|
+ /// Get the current native location of the Router
|
|
pub fn native_location<T: 'static>(&self) -> Option<Box<T>> {
|
|
pub fn native_location<T: 'static>(&self) -> Option<Box<T>> {
|
|
self.history.native_location().downcast::<T>().ok()
|
|
self.history.native_location().downcast::<T>().ok()
|
|
}
|
|
}
|
|
@@ -200,6 +163,35 @@ impl RouterCore {
|
|
pub fn unsubscribe_onchange(&self, id: ScopeId) {
|
|
pub fn unsubscribe_onchange(&self, id: ScopeId) {
|
|
self.onchange_listeners.borrow_mut().remove(&id);
|
|
self.onchange_listeners.borrow_mut().remove(&id);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ pub(crate) fn register_total_route(&self, route: String, scope: ScopeId) {
|
|
|
|
+ let clean = clean_route(route);
|
|
|
|
+ self.slots.borrow_mut().insert(scope, clean);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) fn should_render(&self, scope: ScopeId) -> bool {
|
|
|
|
+ if let Some(root_id) = self.route_found.get() {
|
|
|
|
+ return root_id == scope;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let roots = self.slots.borrow();
|
|
|
|
+
|
|
|
|
+ if let Some(route) = roots.get(&scope) {
|
|
|
|
+ if route_matches_path(
|
|
|
|
+ &self.current_location().url,
|
|
|
|
+ route,
|
|
|
|
+ self.cfg.base_url.as_ref(),
|
|
|
|
+ ) || route.is_empty()
|
|
|
|
+ {
|
|
|
|
+ self.route_found.set(Some(scope));
|
|
|
|
+ true
|
|
|
|
+ } else {
|
|
|
|
+ false
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
fn clean_route(route: String) -> String {
|
|
fn clean_route(route: String) -> String {
|
|
@@ -222,27 +214,26 @@ fn clean_path(path: &str) -> &str {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-fn route_matches_path(cur: &ParsedRoute, attempt: &str) -> bool {
|
|
|
|
- let cur_pieces = cur.url.path_segments().unwrap().collect::<Vec<_>>();
|
|
|
|
|
|
+fn route_matches_path(cur: &Url, attempt: &str, base_url: Option<&String>) -> bool {
|
|
|
|
+ let cur_piece_iter = cur.path_segments().unwrap();
|
|
|
|
+
|
|
|
|
+ let cur_pieces = match base_url {
|
|
|
|
+ // baseurl is naive right now and doesn't support multiple nesting levels
|
|
|
|
+ Some(_) => cur_piece_iter.skip(1).collect::<Vec<_>>(),
|
|
|
|
+ None => cur_piece_iter.collect::<Vec<_>>(),
|
|
|
|
+ };
|
|
|
|
+
|
|
let attempt_pieces = clean_path(attempt).split('/').collect::<Vec<_>>();
|
|
let attempt_pieces = clean_path(attempt).split('/').collect::<Vec<_>>();
|
|
|
|
|
|
if attempt == "/" && cur_pieces.len() == 1 && cur_pieces[0].is_empty() {
|
|
if attempt == "/" && cur_pieces.len() == 1 && cur_pieces[0].is_empty() {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- log::debug!(
|
|
|
|
- "Comparing cur {:?} to attempt {:?}",
|
|
|
|
- cur_pieces,
|
|
|
|
- attempt_pieces
|
|
|
|
- );
|
|
|
|
-
|
|
|
|
if attempt_pieces.len() != cur_pieces.len() {
|
|
if attempt_pieces.len() != cur_pieces.len() {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
for (i, r) in attempt_pieces.iter().enumerate() {
|
|
for (i, r) in attempt_pieces.iter().enumerate() {
|
|
- log::debug!("checking route: {:?}", r);
|
|
|
|
-
|
|
|
|
// If this is a parameter then it matches as long as there's
|
|
// If this is a parameter then it matches as long as there's
|
|
// _any_thing in that spot in the path.
|
|
// _any_thing in that spot in the path.
|
|
if r.starts_with(':') {
|
|
if r.starts_with(':') {
|
|
@@ -257,30 +248,37 @@ fn route_matches_path(cur: &ParsedRoute, attempt: &str) -> bool {
|
|
true
|
|
true
|
|
}
|
|
}
|
|
|
|
|
|
-pub trait RouterProvider {
|
|
|
|
- fn push(&self, path: &str);
|
|
|
|
- fn pop(&self);
|
|
|
|
|
|
+pub(crate) trait RouterProvider {
|
|
|
|
+ fn push(&self, route: &ParsedRoute);
|
|
|
|
+ fn replace(&self, route: &ParsedRoute);
|
|
fn native_location(&self) -> Box<dyn Any>;
|
|
fn native_location(&self) -> Box<dyn Any>;
|
|
- fn init_location(&self) -> Url;
|
|
|
|
|
|
+ fn init_location(&self) -> ParsedRoute;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#[cfg(not(feature = "web"))]
|
|
mod hash {
|
|
mod hash {
|
|
use super::*;
|
|
use super::*;
|
|
|
|
|
|
|
|
+ pub fn new() -> HashRouter {
|
|
|
|
+ HashRouter {}
|
|
|
|
+ }
|
|
|
|
+
|
|
/// a simple cross-platform hash-based router
|
|
/// a simple cross-platform hash-based router
|
|
pub struct HashRouter {}
|
|
pub struct HashRouter {}
|
|
|
|
|
|
impl RouterProvider for HashRouter {
|
|
impl RouterProvider for HashRouter {
|
|
- fn push(&self, _path: &str) {}
|
|
|
|
|
|
+ fn push(&self, _route: &ParsedRoute) {}
|
|
|
|
|
|
fn native_location(&self) -> Box<dyn Any> {
|
|
fn native_location(&self) -> Box<dyn Any> {
|
|
Box::new(())
|
|
Box::new(())
|
|
}
|
|
}
|
|
|
|
|
|
- fn pop(&self) {}
|
|
|
|
-
|
|
|
|
- fn init_location(&self) -> Url {
|
|
|
|
- Url::parse("app:///").unwrap()
|
|
|
|
|
|
+ fn init_location(&self) -> ParsedRoute {
|
|
|
|
+ ParsedRoute {
|
|
|
|
+ url: Url::parse("app:///").unwrap(),
|
|
|
|
+ title: None,
|
|
|
|
+ serialized_state: None,
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -288,50 +286,73 @@ mod hash {
|
|
#[cfg(feature = "web")]
|
|
#[cfg(feature = "web")]
|
|
mod web {
|
|
mod web {
|
|
use super::RouterProvider;
|
|
use super::RouterProvider;
|
|
- use crate::RouteEvent;
|
|
|
|
|
|
+ use crate::{ParsedRoute, RouteEvent};
|
|
|
|
|
|
use futures_channel::mpsc::UnboundedSender;
|
|
use futures_channel::mpsc::UnboundedSender;
|
|
- use gloo::{
|
|
|
|
- events::EventListener,
|
|
|
|
- history::{BrowserHistory, History},
|
|
|
|
- };
|
|
|
|
|
|
+ use gloo_events::EventListener;
|
|
use std::any::Any;
|
|
use std::any::Any;
|
|
- use url::Url;
|
|
|
|
|
|
+ use web_sys::History;
|
|
|
|
|
|
pub struct WebRouter {
|
|
pub struct WebRouter {
|
|
// keep it around so it drops when the router is dropped
|
|
// keep it around so it drops when the router is dropped
|
|
- _listener: gloo::events::EventListener,
|
|
|
|
|
|
+ _listener: gloo_events::EventListener,
|
|
|
|
|
|
- history: BrowserHistory,
|
|
|
|
|
|
+ window: web_sys::Window,
|
|
|
|
+ history: History,
|
|
}
|
|
}
|
|
|
|
|
|
impl RouterProvider for WebRouter {
|
|
impl RouterProvider for WebRouter {
|
|
- fn push(&self, path: &str) {
|
|
|
|
- self.history.push(path);
|
|
|
|
- // use gloo::history;
|
|
|
|
- // web_sys::window()
|
|
|
|
- // .unwrap()
|
|
|
|
- // .location()
|
|
|
|
- // .set_href(path)
|
|
|
|
- // .unwrap();
|
|
|
|
|
|
+ fn push(&self, route: &ParsedRoute) {
|
|
|
|
+ let ParsedRoute {
|
|
|
|
+ url,
|
|
|
|
+ title,
|
|
|
|
+ serialized_state,
|
|
|
|
+ } = route;
|
|
|
|
+
|
|
|
|
+ let _ = self.history.push_state_with_url(
|
|
|
|
+ &wasm_bindgen::JsValue::from_str(serialized_state.as_deref().unwrap_or("")),
|
|
|
|
+ title.as_deref().unwrap_or(""),
|
|
|
|
+ Some(url.as_str()),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
- fn native_location(&self) -> Box<dyn Any> {
|
|
|
|
- todo!()
|
|
|
|
|
|
+ fn replace(&self, route: &ParsedRoute) {
|
|
|
|
+ let ParsedRoute {
|
|
|
|
+ url,
|
|
|
|
+ title,
|
|
|
|
+ serialized_state,
|
|
|
|
+ } = route;
|
|
|
|
+
|
|
|
|
+ let _ = self.history.replace_state_with_url(
|
|
|
|
+ &wasm_bindgen::JsValue::from_str(serialized_state.as_deref().unwrap_or("")),
|
|
|
|
+ title.as_deref().unwrap_or(""),
|
|
|
|
+ Some(url.as_str()),
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
- fn pop(&self) {
|
|
|
|
- // set the title, maybe?
|
|
|
|
|
|
+ fn native_location(&self) -> Box<dyn Any> {
|
|
|
|
+ Box::new(self.window.location())
|
|
}
|
|
}
|
|
|
|
|
|
- fn init_location(&self) -> Url {
|
|
|
|
- url::Url::parse(&web_sys::window().unwrap().location().href().unwrap()).unwrap()
|
|
|
|
|
|
+ fn init_location(&self) -> ParsedRoute {
|
|
|
|
+ ParsedRoute {
|
|
|
|
+ url: url::Url::parse(&web_sys::window().unwrap().location().href().unwrap())
|
|
|
|
+ .unwrap(),
|
|
|
|
+ title: web_sys::window()
|
|
|
|
+ .unwrap()
|
|
|
|
+ .document()
|
|
|
|
+ .unwrap()
|
|
|
|
+ .title()
|
|
|
|
+ .into(),
|
|
|
|
+ serialized_state: None,
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- pub fn new(tx: UnboundedSender<RouteEvent>) -> WebRouter {
|
|
|
|
|
|
+ pub(crate) fn new(tx: UnboundedSender<RouteEvent>) -> WebRouter {
|
|
WebRouter {
|
|
WebRouter {
|
|
- history: BrowserHistory::new(),
|
|
|
|
|
|
+ history: web_sys::window().unwrap().history().unwrap(),
|
|
|
|
+ window: web_sys::window().unwrap(),
|
|
_listener: EventListener::new(&web_sys::window().unwrap(), "popstate", move |_| {
|
|
_listener: EventListener::new(&web_sys::window().unwrap(), "popstate", move |_| {
|
|
let _ = tx.unbounded_send(RouteEvent::Pop);
|
|
let _ = tx.unbounded_send(RouteEvent::Pop);
|
|
}),
|
|
}),
|