1
1
/* ==========================================================================
2
- Sidebar navigation and visualization helper scripts
2
+ Sidebar, page, etc. navigation and visualization helper scripts
3
3
========================================================================== */
4
4
5
5
$ ( function ( ) {
@@ -68,28 +68,9 @@ $(function () {
68
68
}
69
69
}
70
70
71
- // Function to apply all of our custom modifications on the self loaded pages
72
- function finalizeContent ( anchorId ) {
73
- // Sync the sidebar with the current page url, migth be out of sync when the page is loaded initially from an inner url
74
- adjustSidebars ( ) ;
75
- // There might be nav-links in the loaded new content as well (e.g.Next / Prev buttons
76
- // so, handle the links here as the last action
77
- updateNavLinks ( ) ;
78
- // Add page heading anchors
79
- addPageAnchors ( ) ;
80
- // Add toc to anchor scrolling
81
- addTOCScrolling ( ) ;
82
- // Add code block enhancements
83
- if ( ClipboardJS . isSupported ( ) )
84
- addCodeBlocksTitle ( ) ;
85
- // Add content tooltips
86
- addContentTooltips ( ) ;
87
- // Try to scroll to a giben anchor, if any
88
- if ( anchorId )
89
- scrollToAnchor ( anchorId ) ;
90
- }
91
-
71
+ // -------------
92
72
// Function to load content based on relative URL
73
+ // -------------
93
74
function loadContentFromUrl ( url , onSuccess , onError ) {
94
75
fetch ( url )
95
76
. then ( response => {
@@ -133,6 +114,27 @@ $(function () {
133
114
return anchorId ;
134
115
}
135
116
117
+ // Function to apply all of our custom modifications on the self loaded pages
118
+ function finalizeContent ( anchorId ) {
119
+ // Sync the sidebar with the current page url, migth be out of sync when the page is loaded initially from an inner url
120
+ adjustSidebars ( ) ;
121
+ // There might be nav-links in the loaded new content as well (e.g.Next / Prev buttons
122
+ // so, handle the links here as the last action
123
+ updateNavLinks ( ) ;
124
+ // Add page heading anchors
125
+ addPageAnchors ( ) ;
126
+ // Add toc to anchor scrolling
127
+ addTOCScrolling ( ) ;
128
+ // Add code block enhancements
129
+ if ( ClipboardJS . isSupported ( ) )
130
+ addCodeBlocksTitle ( ) ;
131
+ // Add content tooltips
132
+ addContentTooltips ( ) ;
133
+ // Try to scroll to a giben anchor, if any
134
+ if ( anchorId )
135
+ scrollToAnchor ( anchorId ) ;
136
+ }
137
+
136
138
function updateContentFromUrl ( url ) {
137
139
var currContent = document . querySelector ( contentID ) ;
138
140
@@ -168,6 +170,9 @@ $(function () {
168
170
) ;
169
171
}
170
172
173
+ // -------------
174
+ // Functions to handle link clicks
175
+ // -------------
171
176
function getCollectionFromDocPath ( url ) {
172
177
var parts = url . href . split ( '/' ) ;
173
178
var docIndex = parts . indexOf ( docRootName ) ;
@@ -187,7 +192,6 @@ $(function () {
187
192
return collection1 === collection2 ;
188
193
}
189
194
190
- // Function to handle link clicks
191
195
function handleNavLinkClick ( event ) {
192
196
if ( ! event . shiftKey && ! event . ctrlKey && ! event . altKey && ! event . metaKey ) {
193
197
var updated = false ;
@@ -232,6 +236,9 @@ $(function () {
232
236
} ) ;
233
237
}
234
238
239
+ // -------------
240
+ // TOC smooth scrolling
241
+ // -------------
235
242
const smoothScrollTopOffset = 100 ;
236
243
var smoothScroll = new SmoothScroll ( 'a[href*="#"]' , {
237
244
offset : smoothScrollTopOffset ,
@@ -240,7 +247,6 @@ $(function () {
240
247
durationMax : 500
241
248
} ) ;
242
249
243
- // TOC smooth scrolling
244
250
function addTOCScrolling ( ) {
245
251
// Gumshoe scroll spy init
246
252
if ( $ ( "nav.toc a" ) . length > 0 ) {
@@ -263,7 +269,9 @@ $(function () {
263
269
}
264
270
}
265
271
272
+ // -------------
266
273
// Add anchors for headings
274
+ // -------------
267
275
function addPageAnchors ( ) {
268
276
// FIXME: This magic 6 must be maintained together now with generate_links.rb (and other places ?!)
269
277
$ ( '.page__content' ) . find ( 'h1, h2, h3, h4, h5, h6' ) . each ( function ( ) {
@@ -333,10 +341,13 @@ $(function () {
333
341
) ;
334
342
}
335
343
344
+ // -------------
336
345
// Tooltip generation and handling
346
+ // -------------
337
347
const toolTipArrowSize = 10 ;
338
348
var tooltip = null ;
339
349
var tooltipTarget = null ;
350
+ var elementUnderCursor = null ;
340
351
var shouldShowTooltip = false ;
341
352
var showTimeoutFuncID ;
342
353
var hideTimeoutFuncID ;
@@ -452,15 +463,98 @@ $(function () {
452
463
document . addEventListener ( 'mousemove' , ( event ) => {
453
464
if ( shouldHideTooltip ( event . target ) ) {
454
465
if ( tooltipTarget )
455
- hideTooltip ( true )
466
+ hideTooltip ( true ) ;
456
467
}
457
468
else {
458
469
clearTimeout ( hideTimeoutFuncID ) ;
459
470
shouldShowTooltip = true ;
460
471
}
461
472
} ) ;
473
+
474
+ document . addEventListener ( 'scroll' , ( event ) => {
475
+ if ( elementUnderCursor == null || shouldHideTooltip ( elementUnderCursor ) ) {
476
+ if ( tooltipTarget )
477
+ hideTooltip ( true ) ;
478
+ }
479
+ } ) ;
480
+
481
+ document . addEventListener ( "mouseover" , function ( event ) {
482
+ elementUnderCursor = event . target ;
483
+ } ) ;
484
+ }
485
+
486
+ // -------------
487
+ // Sticky sidebar
488
+ // -------------
489
+
490
+ var initiallySticky = $ ( ".sidebar" ) . hasClass ( "sticky" ) ;
491
+ var stickySideBar = function ( ) {
492
+ var show =
493
+ $ ( ".author__urls-wrapper" ) . find ( "button" ) . length === 0
494
+ ? $ ( window ) . width ( ) > 1024 // width should match $large Sass variable
495
+ : ! $ ( ".author__urls-wrapper" ) . find ( "button" ) . is ( ":visible" ) ;
496
+ if ( show ) {
497
+ if ( initiallySticky ) {
498
+ $ ( ".sidebar" ) . addClass ( "sticky" ) ;
499
+ }
500
+ } else {
501
+ $ ( ".sidebar" ) . removeClass ( "sticky" ) ;
502
+ }
503
+ } ;
504
+
505
+ $ ( window ) . on ( "resize" , function ( ) {
506
+ stickySideBar ( ) ;
507
+ } ) ;
508
+
509
+ // -------------
510
+ // Search
511
+ // -------------
512
+
513
+ // Close search screen with Esc key or toggle with predefined hotKey
514
+ $ ( document ) . on ( "keyup" , function ( event ) {
515
+ // Define the desired hotkey (in this case, Ctrl + Shift + F)
516
+ var searchHotkey = { ctrlKey : true , shiftKey : true , key : 'F' } ;
517
+
518
+ if ( event . keyCode === 27 ) {
519
+ if ( $ ( ".initial-content" ) . hasClass ( "is--hidden" ) )
520
+ toggleSearch ( ) ;
521
+ }
522
+ else if ( event . ctrlKey === searchHotkey . ctrlKey &&
523
+ event . shiftKey === searchHotkey . shiftKey &&
524
+ event . key === searchHotkey . key ) {
525
+ toggleSearch ( ) ;
526
+ }
527
+ } ) ;
528
+
529
+ function toggleSearch ( ) {
530
+ $ ( ".search-content" ) . toggleClass ( "is--visible" ) ;
531
+ $ ( ".initial-content" ) . toggleClass ( "is--hidden" ) ;
532
+
533
+ if ( $ ( ".initial-content" ) . hasClass ( "is--hidden" ) ) {
534
+ // set focus on input
535
+ setTimeout ( function ( ) {
536
+ var input = $ ( ".search-content" ) . find ( "input" ) ;
537
+ input . trigger ( "focus" ) ;
538
+ input . trigger ( "select" ) ;
539
+ } , 250 ) ;
540
+ }
541
+ else {
542
+ // set focus back to the initial content otherwise the focus will not get back to the search input once again
543
+ $ ( ".initial-content" ) . find ( "input" ) . focus ( ) ;
544
+ }
545
+
546
+ if ( tooltipTarget )
547
+ hideTooltip ( true ) ;
462
548
}
463
549
550
+ $ ( ".search__toggle" ) . on ( "click" , toggleSearch ) ;
551
+
552
+ // -------------
553
+ // Startup
554
+ // -------------
555
+
556
+ stickySideBar ( ) ;
557
+
464
558
// Make sure everything is initialized correctly on an initial load as well
465
559
// (e.g. when an inner embedded page link is opened directly in a new tab, not via the internal navigational links)
466
560
finalizeContent ( ) ;
0 commit comments