11use gloo:: events:: EventListener ;
2+ use route_recognizer:: Params ;
23use std:: cell:: RefCell ;
4+ use std:: collections:: HashMap ;
35use std:: rc:: Rc ;
4- use wasm_bindgen:: { JsCast , JsValue } ;
6+ use wasm_bindgen:: { prelude :: * , JsCast } ;
57use web_sys:: { Event , History } ;
68use weblog:: * ;
79use yew:: prelude:: * ;
@@ -11,9 +13,18 @@ use yew_functional::*;
1113pub struct CurrentRoute {
1214 path : String ,
1315 params : route_recognizer:: Params ,
16+ query : HashMap < String , String > ,
1417}
1518
1619impl CurrentRoute {
20+ pub ( crate ) fn new ( path : String , params : route_recognizer:: Params ) -> Self {
21+ Self {
22+ path,
23+ params,
24+ query : get_query_params ( ) ,
25+ }
26+ }
27+
1728 pub fn path ( & self ) -> & str {
1829 & self . path
1930 }
@@ -22,6 +33,10 @@ impl CurrentRoute {
2233 pub fn parmas ( & self ) -> & route_recognizer:: Params {
2334 & self . params
2435 }
36+
37+ pub fn query ( & self ) -> & HashMap < String , String > {
38+ & self . query
39+ }
2540}
2641
2742pub struct YewRouter {
@@ -79,7 +94,7 @@ pub fn router(props: &RouterProps) -> Html {
7994 let route = from_route (
8095 & pathname,
8196 & props. children ,
82- props. not_found_route . as_ref ( ) . map ( |it| it . as_str ( ) ) ,
97+ props. not_found_route . as_deref ( ) ,
8398 & * router. borrow ( ) ,
8499 ) ;
85100 let ( children, current_route) = match route {
@@ -153,10 +168,7 @@ fn from_route(
153168 . children ;
154169 selected = Some ( (
155170 children,
156- CurrentRoute {
157- path : path. handler ( ) . to_string ( ) ,
158- params : path. params ( ) . clone ( ) ,
159- } ,
171+ CurrentRoute :: new ( path. handler ( ) . to_string ( ) , path. params ( ) . clone ( ) ) ,
160172 ) ) ;
161173 }
162174
@@ -168,10 +180,7 @@ fn from_route(
168180 let route = routes. iter ( ) . find ( |it| it. props . to == not_found_route) ?;
169181 Some ( (
170182 route. props . children ,
171- CurrentRoute {
172- path : not_found_route. to_string ( ) ,
173- params : Default :: default ( ) ,
174- } ,
183+ CurrentRoute :: new ( not_found_route. to_string ( ) , Params :: default ( ) ) ,
175184 ) )
176185 }
177186 }
@@ -232,10 +241,54 @@ fn base_url() -> Option<String> {
232241}
233242
234243fn build_path_with_base ( to : & str ) -> String {
235- let to = format ! (
236- "{}{}" ,
237- base_url ( ) . as_ref ( ) . map ( |it| it . as_str ( ) ) . unwrap_or ( "" ) ,
244+ let to = format ! ( "{}{}" , base_url ( ) . as_deref ( ) . unwrap_or ( "" ) , to ) ;
245+
246+ let path = if to == "/" {
238247 to
239- ) ;
240- to. strip_suffix ( "/" ) . map ( |it| it. to_string ( ) ) . unwrap_or ( to)
248+ } else {
249+ to. strip_suffix ( "/" ) . map ( |it| it. to_string ( ) ) . unwrap_or ( to)
250+ } ;
251+ console_log ! ( format!( "path11 {}" , path) ) ;
252+
253+ path
254+ }
255+
256+ fn get_query_params ( ) -> HashMap < String , String > {
257+ #[ wasm_bindgen( inline_js = r#"
258+ export function params() {
259+ let entries = (new URL(document.URL)).searchParams.entries()
260+ let list = []
261+ for (const [key, value] of entries) {
262+ list.push({key, value})
263+ }
264+ return list
265+ }
266+ "# ) ]
267+ extern "C" {
268+ fn params ( ) -> js_sys:: Array ;
269+ }
270+
271+ #[ wasm_bindgen]
272+ extern "C" {
273+ #[ derive( Debug ) ]
274+ type Param ;
275+
276+ #[ wasm_bindgen( getter, method) ]
277+ fn key ( this : & Param ) -> String ;
278+
279+ #[ wasm_bindgen( getter, method) ]
280+ fn value ( this : & Param ) -> String ;
281+ }
282+
283+ let iter = params ( ) . to_vec ( ) . into_iter ( ) . map ( |value : JsValue | {
284+ let param = value. unchecked_into :: < Param > ( ) ;
285+ ( param. key ( ) , param. value ( ) )
286+ } ) ;
287+ let mut map = HashMap :: new ( ) ;
288+
289+ for ( k, v) in iter {
290+ map. insert ( k, v) ;
291+ }
292+
293+ map
241294}
0 commit comments