diff options
Diffstat (limited to 'web/src/jsx.tsx')
| -rw-r--r-- | web/src/jsx.tsx | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/web/src/jsx.tsx b/web/src/jsx.tsx index 64a8aad..a92fc11 100644 --- a/web/src/jsx.tsx +++ b/web/src/jsx.tsx @@ -1,6 +1,7 @@ import $ from 'jquery'; const assetAttributeNames = new Set( ['data', 'srcset', 'src', 'href'] ); +let curRoute = null; export interface Route { path: string, component: Function, @@ -82,10 +83,12 @@ export function navigate( route: string ) { if( !cb ) return navigate( err404page ); - window.history.pushState( {}, null, url.href ); + if( curRoute != cb ) + window.history.pushState( {}, null, url.href ); onprenavigate(); const el = $( cb() ); + curRoute = cb; $( `#${rootId}` ).children().remove(); $( `#${rootId}` ).append( el ); @@ -104,10 +107,12 @@ export function navigateParams( route: string, params: any ) { if( !cb ) return navigate( err404page ); - window.history.pushState( {}, null, url.href ); + if( curRoute != cb ) + window.history.pushState( {}, null, url.href ); onprenavigate(); const el = $( cb() ); + curRoute = cb; $( `#${rootId}` ).children().remove(); $( `#${rootId}` ).append( el ); @@ -139,6 +144,7 @@ export function navigateParamsSilent( route: string, params: any ) { onprenavigate(); const el = $( cb() ); + curRoute = cb; $( `#${rootId}` ).children().remove(); $( `#${rootId}` ).append( el ); @@ -157,6 +163,7 @@ export function navigateSilent( route: string ) { onprenavigate(); const el = $( cb() ); + curRoute = cb; $( `#${rootId}` ).children().remove(); $( `#${rootId}` ).append( el ); @@ -174,14 +181,32 @@ export function onPopState() { if( !cb ) return navigateSilent( err404page ); + if( cb == curRoute ) + return; + onprenavigate(); const el = $( cb() ); + curRoute = cb; $( `#${rootId}` ).children().remove(); $( `#${rootId}` ).append( el ); onpostnavigate(); } +/** + * navigates to the parent directory from the current page +**/ +export function goUpDirectory() { + const url = new URL( window.location.href ); + if( url.pathname.endsWith( "/" ) ) + url.pathname = url.pathname.slice( 0, -1 ); + let idx = url.pathname.lastIndexOf( "/" ); + if( idx === -1 ) + return; + url.pathname = url.pathname.slice( 0, url.pathname.lastIndexOf( "/" ) ); + navigate( url.pathname ); +} + export function getRoutes() : Route[] { return routes; } |
