feat: add prevent default attribute and upgrade router
This commit is contained in:
1 parent
cb2782b4bb
commit
427b126bc1
16 files changed
+220
-181
No files matched your search
@@ -1,13 +1,18 @@
|
||||
use gloo::history::{BrowserHistory, History, HistoryListener};
|
||||
use std::{cell::RefCell, collections::HashMap, rc::Rc};
|
||||
use std::{
|
||||
cell::{Cell, RefCell},
|
||||
collections::HashMap,
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use dioxus_core::ScopeId;
|
||||
|
||||
pub struct RouterService {
|
||||
pub(crate) regen_route: Rc<dyn Fn(ScopeId)>,
|
||||
history: RefCell<BrowserHistory>,
|
||||
history: Rc<RefCell<BrowserHistory>>,
|
||||
registerd_routes: RefCell<RouteSlot>,
|
||||
slots: RefCell<HashMap<ScopeId, String>>,
|
||||
slots: Rc<RefCell<Vec<(ScopeId, String)>>>,
|
||||
root_found: Rc<Cell<bool>>,
|
||||
cur_root: RefCell<String>,
|
||||
listener: HistoryListener,
|
||||
}
|
||||
@@ -31,9 +36,20 @@ impl RouterService {
|
||||
let location = history.location();
|
||||
let path = location.path();
|
||||
|
||||
let regen = Rc::clone(®en_route);
|
||||
let slots: Rc<RefCell<Vec<(ScopeId, String)>>> = Default::default();
|
||||
|
||||
let _slots = slots.clone();
|
||||
|
||||
let root_found = Rc::new(Cell::new(false));
|
||||
let regen = regen_route.clone();
|
||||
let _root_found = root_found.clone();
|
||||
let listener = history.listen(move || {
|
||||
regen(root_scope);
|
||||
_root_found.set(false);
|
||||
// checking if the route is valid is cheap, so we do it
|
||||
for (slot, _) in _slots.borrow_mut().iter().rev() {
|
||||
log::debug!("regenerating slot {:?}", slot);
|
||||
regen(*slot);
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
@@ -42,9 +58,10 @@ impl RouterService {
|
||||
total: String::from("/"),
|
||||
rest: Vec::new(),
|
||||
}),
|
||||
history: RefCell::new(history),
|
||||
root_found,
|
||||
history: Rc::new(RefCell::new(history)),
|
||||
regen_route,
|
||||
slots: Default::default(),
|
||||
slots,
|
||||
cur_root: RefCell::new(path.to_string()),
|
||||
listener,
|
||||
}
|
||||
@@ -54,20 +71,37 @@ impl RouterService {
|
||||
self.history.borrow_mut().push(route);
|
||||
}
|
||||
|
||||
pub fn register_total_route(&self, route: String, scope: ScopeId) {
|
||||
self.slots.borrow_mut().insert(scope, route);
|
||||
pub fn register_total_route(&self, route: String, scope: ScopeId, fallback: bool) {
|
||||
self.slots.borrow_mut().push((scope, route));
|
||||
}
|
||||
|
||||
pub fn should_render(&self, scope: ScopeId) -> bool {
|
||||
if self.root_found.get() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let location = self.history.borrow().location();
|
||||
let path = location.path();
|
||||
|
||||
let roots = self.slots.borrow();
|
||||
|
||||
let root = roots.get(&scope);
|
||||
let root = roots.iter().find(|(id, route)| id == &scope);
|
||||
|
||||
// fallback logic
|
||||
match root {
|
||||
Some(r) => r == path,
|
||||
Some((_id, route)) => {
|
||||
if route == path {
|
||||
self.root_found.set(true);
|
||||
true
|
||||
} else {
|
||||
if route == "" {
|
||||
self.root_found.set(true);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user