4
4
5
5
$ ( function ( ) {
6
6
// FIXME: How to get the real base URL (without using Liquid and Front Matter) ?!?!
7
- const docRootName = 'doc ' ;
7
+ const docRoot = '' ;
8
8
const notFoundPageName = '404.html' ;
9
9
const contentID = 'article' ;
10
10
11
+ function trimCharFromString ( str , char ) {
12
+ // Create a regular expression to match the given character at the beginning or end of the string
13
+ const regex = new RegExp ( `^${ char } |${ char } $` , 'g' ) ;
14
+
15
+ // Use replace() to trim the character from the string
16
+ return str . replace ( regex , '' ) ;
17
+ }
18
+
19
+ function hideTocIfNotNeeded ( docObject , forceHide ) {
20
+ var shouldHide = forceHide ;
21
+ var tocElement = docObject . querySelector ( '.toc' ) ;
22
+
23
+ if ( tocElement ) {
24
+ var tocMenuElement = tocElement . querySelector ( '.toc__menu' ) ;
25
+ if ( null == tocMenuElement || false == tocMenuElement . hasChildNodes )
26
+ shouldHide = true ;
27
+ }
28
+ if ( shouldHide ) {
29
+ // TOC is autogenerated via 'include toc.html' so its size is not known prior the call of toc.html
30
+ // Signal emptiness, css will hide if needed based on it and config settings
31
+ // NOTE: Not hiding directly here to behave the same way like the left sidebar does
32
+ tocElement . classList . add ( 'empty' ) ;
33
+ }
34
+ }
35
+
11
36
function adjustSidebars ( ) {
12
37
// Identify the URL of the loaded page
13
38
var loadedPageUrl = window . location . pathname ;
@@ -56,15 +81,7 @@ $(function () {
56
81
matchingNavItem . scrollIntoView ( { behavior : 'smooth' , block : 'nearest' } ) ;
57
82
}
58
83
59
- // TOC is autogenerated via 'include toc.html' so its size is not known prior the call of toc.html
60
- // Signal emptiness, css will hide if needed based on it and config settings
61
- // NOTE: Not hiding directly here to behave the same way like the left sidebar does
62
- var tocElement = document . querySelector ( '.toc' ) ;
63
- if ( tocElement ) {
64
- var tocMenuElement = tocElement . querySelector ( '.toc__menu' ) ;
65
- if ( null == tocMenuElement || false == tocMenuElement . hasChildNodes )
66
- tocElement . classList . add ( 'empty' ) ;
67
- }
84
+ hideTocIfNotNeeded ( document , false ) ;
68
85
}
69
86
}
70
87
@@ -158,7 +175,7 @@ $(function () {
158
175
error => {
159
176
if ( error == "Error: 404" ) {
160
177
var baseURL = window . location . origin ;
161
- var notFoundURL = baseURL + '/' + docRoot + '/' + notFoundPageName ;
178
+ var notFoundURL = baseURL + '/' + ( docRoot != '' ? docRoot + '/' : '' ) + notFoundPageName ;
162
179
163
180
updateContentFromUrl ( notFoundURL ) ;
164
181
}
@@ -174,15 +191,20 @@ $(function () {
174
191
// Functions to handle link clicks
175
192
// -------------
176
193
function getCollectionFromDocPath ( url ) {
177
- var parts = url . href . split ( '/' ) ;
178
- var docIndex = parts . indexOf ( docRootName ) ;
194
+ var collection = '' ;
195
+ var parts = trimCharFromString ( url . pathname , '/' ) . split ( '/' ) ;
179
196
180
- // If 'doc' is not found or it's the last segment, return an empty string
181
- if ( docIndex === - 1 || docIndex === parts . length - 1 ) {
182
- return '' ;
197
+ if ( docRoot == '' ) {
198
+ collection = parts [ 0 ] ;
183
199
}
200
+ else {
201
+ var docIndex = parts . indexOf ( docRoot ) ;
184
202
185
- return parts [ docIndex + 1 ] ;
203
+ // If 'docRoot' is found or it is not the last segment, return the next part after it as the collection name
204
+ if ( docIndex !== - 1 && docIndex !== parts . length - 1 )
205
+ collection = parts [ docIndex + 1 ] ;
206
+ }
207
+ return collection ;
186
208
}
187
209
188
210
function sameCollection ( url1 , url2 ) {
@@ -194,6 +216,9 @@ $(function () {
194
216
195
217
function handleNavLinkClick ( event ) {
196
218
if ( ! event . shiftKey && ! event . ctrlKey && ! event . altKey && ! event . metaKey ) {
219
+ if ( tooltipTarget )
220
+ hideTooltip ( true ) ;
221
+
197
222
var updated = false ;
198
223
// Get the relative URL value and update the browser URL
199
224
// Use originalTarget or explicitTarget to get the correct one even for clicks from the tooltips
@@ -205,7 +230,7 @@ $(function () {
205
230
// Try to load into the inner content frame only if the collection has not changed
206
231
// Otherwise let the original click flow take effect, as the nav bar must be reloaded too
207
232
// for a different collection
208
- if ( url . origin == window . location . origin && sameCollection ( url , window . location ) ) {
233
+ if ( url . origin === window . location . origin && anchorElement . target !== '_blank' && sameCollection ( url , window . location ) ) {
209
234
// Prevent default navigation behavior, we will use our content load method
210
235
event . preventDefault ( ) ;
211
236
@@ -224,7 +249,7 @@ $(function () {
224
249
event . target . blur ( ) ;
225
250
}
226
251
if ( false == updated )
227
- console . debug ( "Different collection item requested, loading full page..." )
252
+ console . debug ( "Different collection item or new tab/page requested, loading full page..." )
228
253
}
229
254
}
230
255
@@ -287,10 +312,13 @@ $(function () {
287
312
} ) ;
288
313
}
289
314
290
- function alterPageTitle ( content ) {
315
+ function alterPageForTooltip ( content , fullPageContent ) {
291
316
let tempContainer = document . createElement ( 'div' ) ;
292
317
tempContainer . innerHTML = content ;
293
318
319
+ if ( fullPageContent )
320
+ hideTocIfNotNeeded ( tempContainer , true ) ;
321
+
294
322
// Remove/Override some default title style formatting to look better in the tooltip
295
323
const pageTitle = tempContainer . querySelector ( '#page-title' ) ;
296
324
if ( pageTitle )
@@ -300,7 +328,11 @@ $(function () {
300
328
if ( pageSubtitle )
301
329
pageSubtitle . style . borderBottom = '0px' ;
302
330
303
- return tempContainer . innerHTML ;
331
+ var newContent = tempContainer . innerHTML
332
+ // remove unnecessary, reqursive inner content tooltips
333
+ newContent = newContent . replace ( / \b c o n t e n t - t o o l t i p \b / g, '' ) ;
334
+
335
+ return newContent ;
304
336
}
305
337
306
338
function loadContentPartFrom ( url , onSuccess , onError ) {
@@ -319,17 +351,14 @@ $(function () {
319
351
var startHeading = newContent . querySelector ( '#' + startHeadingId ) ;
320
352
if ( startHeading ) {
321
353
var content = startHeading . outerHTML ; // Include the starting <h> element itself
322
-
323
354
var nextSibling = startHeading . nextElementSibling ;
355
+
324
356
// Collect all siblings until the next heading or the end of the document
325
357
// FIXME: This magic 6 must be maintained together now with generate_links.rb (and other places ?!)
326
358
while ( nextSibling && nextSibling . tagName !== 'H1' && nextSibling . tagName !== 'H2' && nextSibling . tagName !== 'H3' && nextSibling . tagName !== 'H4' && nextSibling . tagName !== 'H5' && nextSibling . tagName !== 'H6' ) {
327
359
content += nextSibling . outerHTML ;
328
360
nextSibling = nextSibling . nextElementSibling ;
329
361
}
330
- if ( false == hasAnchor )
331
- content = alterPageTitle ( content ) ;
332
-
333
362
onSuccess ( content ) ;
334
363
}
335
364
else
@@ -373,9 +402,14 @@ $(function () {
373
402
tooltip . style . setProperty ( posName , newPosition ) ;
374
403
}
375
404
376
- function showTooltip ( event , tooltipText ) {
405
+ function showTooltip ( event , tooltipText , fullPageContent ) {
377
406
tooltip . innerHTML = tooltipText . innerHTML ;
378
407
408
+ if ( fullPageContent )
409
+ tooltip . classList . add ( "full-content-tooltip" ) ;
410
+ else
411
+ tooltip . classList . remove ( "full-content-tooltip" ) ;
412
+
379
413
var tooltipPos = getTooltipPos ( event , tooltipTarget )
380
414
var tooltipArrowLeftShift = 2 * toolTipArrowSize ;
381
415
@@ -436,31 +470,41 @@ $(function () {
436
470
element . appendChild ( tooltipText ) ;
437
471
438
472
element . addEventListener ( 'mouseover' , function ( event ) {
473
+ var fullPageContent = element . classList . contains ( 'full-content-tooltip' ) ;
474
+
439
475
tooltipTarget = element ;
440
476
441
477
// Load only once per page load
442
478
if ( tooltipText . innerHTML === '' ) {
443
479
var url = element . href ;
444
- loadContentPartFrom (
445
- url ,
446
- newContent => {
447
- // remove unnecessary inner content tooltips
448
- newContent = newContent . replace ( / \b c o n t e n t - t o o l t i p \b / g, '' ) ;
449
- // cache for reuse
450
- tooltipText . innerHTML = newContent ;
451
- showTooltip ( event , tooltipText ) ;
452
- } ,
453
- error => {
454
- console . error ( 'Error loading the tooltip content!' + error ) ;
455
- }
456
- ) ;
480
+
481
+ function onSuccess ( newContent ) {
482
+ if ( typeof ( newContent ) === 'object' && 'innerHTML' in newContent )
483
+ newContent = newContent . innerHTML ;
484
+ newContent = alterPageForTooltip ( newContent , fullPageContent ) ;
485
+
486
+ // cache for reuse
487
+ tooltipText . innerHTML = newContent ;
488
+ showTooltip ( event , tooltipText , fullPageContent ) ;
489
+ }
490
+
491
+ function onError ( error ) {
492
+ console . error ( 'Error loading the tooltip content!' + error ) ;
493
+ }
494
+
495
+ if ( fullPageContent ) {
496
+ loadContentFromUrl ( url , newContent => onSuccess ( newContent ) , error => onError ( error ) ) ;
497
+ }
498
+ else {
499
+ loadContentPartFrom ( url , newContent => onSuccess ( newContent ) , error => onError ( error ) ) ;
500
+ }
457
501
}
458
502
else
459
- showTooltip ( event , tooltipText ) ;
503
+ showTooltip ( event , tooltipText , fullPageContent ) ;
460
504
} ) ;
461
505
} ) ;
462
506
463
- document . addEventListener ( 'mousemove' , ( event ) => {
507
+ document . addEventListener ( 'mousemove' , function ( event ) {
464
508
if ( shouldHideTooltip ( event . target ) ) {
465
509
if ( tooltipTarget )
466
510
hideTooltip ( true ) ;
@@ -471,7 +515,7 @@ $(function () {
471
515
}
472
516
} ) ;
473
517
474
- document . addEventListener ( 'scroll' , ( event ) => {
518
+ document . addEventListener ( 'scroll' , function ( event ) {
475
519
if ( elementUnderCursor == null || shouldHideTooltip ( elementUnderCursor ) ) {
476
520
if ( tooltipTarget )
477
521
hideTooltip ( true ) ;
@@ -481,6 +525,11 @@ $(function () {
481
525
document . addEventListener ( "mouseover" , function ( event ) {
482
526
elementUnderCursor = event . target ;
483
527
} ) ;
528
+
529
+ window . addEventListener ( 'blur' , function ( ) {
530
+ if ( tooltipTarget )
531
+ hideTooltip ( true ) ;
532
+ } ) ;
484
533
}
485
534
486
535
// -------------
0 commit comments