1
+ // @ts -nocheck
1
2
import "htmx.org"
2
3
import "htmx-ext-preload"
3
4
import { initTocNav } from "./toc-nav" ;
@@ -6,7 +7,6 @@ import {initTabs} from "./tabs";
6
7
import { initCopyButton } from "./copybutton" ;
7
8
import { initNav } from "./pages-nav" ;
8
9
import { $ , $$ } from "select-dom"
9
- import htmx from "htmx.org" ;
10
10
11
11
document . addEventListener ( 'htmx:load' , function ( ) {
12
12
initTocNav ( ) ;
@@ -16,33 +16,32 @@ document.addEventListener('htmx:load', function() {
16
16
initNav ( ) ;
17
17
} ) ;
18
18
19
- document . body . addEventListener ( 'htmx:oobAfterSwap' , function ( event ) {
20
- if ( event . target . id === 'markdown-content' ) {
19
+ document . body . addEventListener ( 'htmx:oobBeforeSwap' , function ( event ) {
20
+ // This is needed to scroll to the top of the page when the content is swapped
21
+ if ( event . target . id === 'markdown-content' || event . target . id === 'content-container' ) {
21
22
window . scrollTo ( 0 , 0 ) ;
22
23
}
23
24
} ) ;
24
25
25
26
document . body . addEventListener ( 'htmx:pushedIntoHistory' , function ( event ) {
26
- const currentNavItem = $$ ( '.current' ) ;
27
+ const pagesNav = $ ( '#pages-nav' ) ;
28
+ const currentNavItem = $$ ( '.current' , pagesNav ) ;
27
29
currentNavItem . forEach ( el => {
28
30
el . classList . remove ( 'current' ) ;
29
31
} )
30
- // @ts -ignore
31
- const navItems = $$ ( 'a[href="' + event . detail . path + '"]' ) ;
32
+ const navItems = $$ ( 'a[href="' + event . detail . path + '"]' , pagesNav ) ;
32
33
navItems . forEach ( navItem => {
33
34
navItem . classList . add ( 'current' ) ;
34
35
} ) ;
35
36
} ) ;
36
37
37
38
document . body . addEventListener ( 'htmx:responseError' , function ( event ) {
38
- // event.preventDefault();
39
-
39
+ // If you get a 404 error while clicking on a hx-get link, actually open the link
40
+ // This is needed because the browser doesn't update the URL when the response is a 404
41
+ // In production, cloudfront handles serving the 404 page.
42
+ // Locally, the DocumentationWebHost handles it.
43
+ // On previews, a generic 404 page is shown.
40
44
if ( event . detail . xhr . status === 404 ) {
41
45
window . location . assign ( event . detail . pathInfo . requestPath ) ;
42
46
}
43
-
44
- // const rootPath = $('body').dataset.rootPath;
45
- // window.history.pushState({ path: event.detail.pathInfo.requestPath }, '', event.detail.pathInfo.requestPath);
46
- // htmx.ajax('get', rootPath + 'not-found', { select: '#main-container', target: '#main-container' }).then(() => {
47
- // });
48
47
} ) ;
0 commit comments