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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
use crate::base;
use crate::scope::ScopeContext;
use crate::target::Target;
use gloo_history::{AnyHistory, BrowserHistory, History, HistoryListener, Location};
use std::borrow::Cow;
use std::fmt::Debug;
use std::rc::Rc;
use yew::prelude::*;
#[derive(Clone, PartialEq)]
pub struct RouterContext<T>
where
T: Target,
{
pub(crate) scope: Rc<ScopeContext<T>>,
// The active target
pub active_target: Option<T>,
}
impl<T> RouterContext<T>
where
T: Target,
{
/// Push a new state to the history. This changes the current target, but doesn't leave the page.
pub fn push(&self, target: T) {
self.scope.push(target);
}
/// Check if the provided target is the active target
pub fn is_same(&self, target: &T) -> bool {
match &self.active_target {
Some(current) => current == target,
None => false,
}
}
/// Check if the target is active.
///
/// This is intended for components to find out if their target, or part of their target
/// is active. If the function is provided with a predicate, then this will override the
/// decision process. Otherwise function will check if the provided `target` is the same as
/// the active target.
///
/// Assume you have a nested navigation tree. The active state of a leaf entry would be
/// identified by the target being "the same". While branch entries would need to provide a
/// predicate, as there is no "value" to compare to.
///
/// A component supporting this model can provide two properties: a target, and an optional
/// predicate. The user can then configure this accordingly. The component can simply pass
/// the information to this function to perform the check.
pub fn is_active(&self, target: &T, predicate: Option<&Callback<T, bool>>) -> bool {
match predicate {
Some(predicate) => self
.active_target
.clone()
.map(|target| predicate.emit(target))
.unwrap_or_default(),
None => self.is_same(target),
}
}
/// Get the active target, this may be [`None`], in the case this branch doesn't have an
/// active target.
pub fn active(&self) -> &Option<T> {
&self.active_target
}
}
/// Properties for the [`Router`] component.
#[derive(Clone, Debug, PartialEq, Properties)]
pub struct RouterProps<T>
where
T: Target,
{
/// The content to render.
#[prop_or_default]
pub children: Children,
/// The default target to use in case none matched.
#[prop_or_default]
pub default: Option<T>,
/// The application base.
///
/// Defaults to an empty string or the content of the `href` attribute of the `<base>` element.
///
/// This can be used in case the application is hosted on a sub path to adapt paths generated
/// and expected by the router.
///
/// ## Usage with `trunk`
///
/// If you are using `trunk` to build the application, you can add the following to your
/// `index.html` file:
///
/// ```html
/// <head>
/// <base data-trunk-public-url/>
/// </head>
/// ```
///
/// This will automatically populate the `<base>` element with the root provided using the
/// `--public-url` argument.
#[prop_or_default]
pub base: Option<String>,
}
#[derive(Debug)]
#[doc(hidden)]
pub enum Msg<T: Target> {
RouteChanged(Location),
ChangeTarget(T),
}
/// Top-level router component.
pub struct Router<T: Target> {
history: AnyHistory,
_listener: HistoryListener,
target: Option<T>,
scope: Rc<ScopeContext<T>>,
router: RouterContext<T>,
base: String,
}
impl<T> Component for Router<T>
where
T: Target + 'static,
{
type Message = Msg<T>;
type Properties = RouterProps<T>;
fn create(ctx: &Context<Self>) -> Self {
let history = AnyHistory::Browser(BrowserHistory::new());
let cb = ctx.link().callback(Msg::RouteChanged);
let base = ctx
.props()
.base
.clone()
.or_else(|| base::eval_base())
.unwrap_or_else(|| "".into());
let target =
Self::parse_location(&base, history.location()).or_else(|| ctx.props().default.clone());
let listener = {
let history = history.clone();
history.clone().listen(move || {
cb.emit(history.location());
})
};
let (scope, router) = Self::build_context(&target, ctx);
Self {
history,
_listener: listener,
target,
scope,
router,
base,
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
// log::debug!("update: {msg:?}");
match msg {
Msg::RouteChanged(location) => {
let target = Self::parse_location(&self.base, location)
.or_else(|| ctx.props().default.clone());
if target != self.target {
self.target = target;
self.sync_context(ctx);
return true;
}
}
Msg::ChangeTarget(target) => {
// log::debug!("Pushing state: {:?}", request.path);
let route = format!(
"{}/{}",
self.base,
target
.render_path()
.into_iter()
.map(|segment| urlencoding::encode(&segment).to_string())
.collect::<Vec<_>>()
.join("/")
);
log::debug!("Push URL: {route}");
self.history.push(route);
}
}
false
}
fn changed(&mut self, ctx: &Context<Self>, _old_props: &Self::Properties) -> bool {
self.sync_context(ctx);
true
}
fn view(&self, ctx: &Context<Self>) -> Html {
let scope = self.scope.clone();
let router = self.router.clone();
html! (
<ContextProvider<ScopeContext<T>> context={(*scope).clone()}>
<ContextProvider<RouterContext<T >> context={router}>
{ for ctx.props().children.iter() }
</ContextProvider<RouterContext<T >>>
</ContextProvider<ScopeContext<T>>>
)
}
}
impl<T: Target> Router<T> {
fn parse_location(base: &str, location: Location) -> Option<T> {
// get the current path
let path = location.path();
// if the prefix doesn't match, nothing will
if !path.starts_with(&base) {
return None;
}
// split off the prefix
let (_, path) = path.split_at(base.len());
log::debug!("Path: {path}");
// parse into path segments
let path: Result<Vec<Cow<str>>, _> = path
.split('/')
.skip(1)
// urldecode in the process
.map(|segment| urlencoding::decode(segment))
.collect();
// get a path, or return none if we had an urldecode error
let path = match &path {
Ok(path) => path.iter().map(|s| s.as_ref()).collect::<Vec<_>>(),
Err(_) => return None,
};
// parse the path into a target
log::debug!("Path: {path:?}");
let target = T::parse_path(&path);
log::debug!("New target: {target:?}");
// done
target
}
fn sync_context(&mut self, ctx: &Context<Self>) {
let (scope, router) = Self::build_context(&self.target, ctx);
self.scope = scope;
self.router = router;
}
fn build_context(
target: &Option<T>,
ctx: &Context<Self>,
) -> (Rc<ScopeContext<T>>, RouterContext<T>) {
let scope = Rc::new(ScopeContext {
upwards: ctx.link().callback(Msg::ChangeTarget),
});
let router = RouterContext {
scope: scope.clone(),
active_target: target.clone(),
};
(scope, router)
}
}
#[hook]
/// Get access to the router.
///
/// The hook requires to be called from a component which is nested into a [`Router`] component of
/// the type `T` provided here. If not, it will return [`None`].
pub fn use_router<T>() -> Option<RouterContext<T>>
where
T: Target + 'static,
{
use_context()
}