@@ -427,68 +427,53 @@ class SuggestionsList {
427
427
428
428
window . SuggestionsList = SuggestionsList ;
429
429
430
- function positionTooltip ( originRect , tooltipEl , options ) {
430
+ function positionTooltip ( targetPosition , tooltip , options ) {
431
431
options = options || { } ;
432
432
433
- const gap = options . gap || 0 ;
434
- const verticalPreference = options . preference || 'bottom' ;
435
- const containerEl = options . offsetParent || tooltipEl . offsetParent || tooltipEl . doc . documentElement ;
436
- const horizontalAlignment = options . horizontalAlignment || 'left' ;
433
+ tooltip . style . display = 'block' ;
437
434
438
- const containerScrollTop = containerEl . scrollTop + 10 ;
439
- const containerBottom = containerEl . scrollTop + containerEl . clientHeight - 10 ;
435
+ const gap = options . gap !== undefined ? options . gap : 0 ;
436
+ const preference = options . preference !== undefined ? options . preference : 'bottom' ;
437
+ const offsetParent = options . offsetParent !== undefined ? options . offsetParent : tooltip . offsetParent || tooltip . ownerDocument . documentElement ;
438
+ const horizontalAlignment = options . horizontalAlignment !== undefined ? options . horizontalAlignment : 'left' ;
440
439
441
- const tooltipTop = Math . min ( originRect . top , containerBottom ) ;
442
- const tooltipBottom = Math . max ( originRect . bottom , containerScrollTop ) ;
440
+ const topBound = Math . min ( targetPosition . top , offsetParent . scrollTop + offsetParent . clientHeight - tooltip . offsetHeight - 10 ) ;
441
+ const bottomBound = Math . max ( targetPosition . bottom , offsetParent . scrollTop + 10 ) ;
443
442
444
- const tooltipHeight = tooltipEl . offsetHeight ;
443
+ const isAboveTarget = targetPosition . top - offsetParent . scrollTop >= tooltip . offsetHeight + gap ;
444
+ const isBelowTarget = offsetParent . scrollTop + offsetParent . clientHeight - targetPosition . bottom >= tooltip . offsetHeight + gap ;
445
445
446
- let tooltipVerticalPosition = 0 ;
447
- let result ;
446
+ let top = 0 ;
447
+ let result = '' ;
448
448
449
- if ( tooltipTop - containerScrollTop >= tooltipHeight + gap ) {
450
- // Fits on top
451
- result = 'top' ;
452
- tooltipVerticalPosition = tooltipTop - gap - tooltipHeight ;
453
- } else if ( containerBottom - originRect . bottom >= tooltipHeight + gap ) {
454
- // Fits on bottom
455
- result = 'bottom' ;
456
- tooltipVerticalPosition = tooltipBottom + gap ;
457
- } else {
458
- // Overlap
459
- result = 'overlap' ;
460
-
461
- if ( verticalPreference === 'top' ) {
462
- tooltipVerticalPosition = containerScrollTop + gap ;
449
+ if ( ! isAboveTarget || ( preference === 'bottom' && isBelowTarget ) ) {
450
+ if ( offsetParent . clientHeight < tooltip . offsetHeight + gap ) {
451
+ top = offsetParent . scrollTop ;
452
+ result = 'overlap' ;
453
+ } else if ( preference === 'top' ) {
454
+ top = offsetParent . scrollTop + gap ;
455
+ result = 'overlap' ;
463
456
} else {
464
- tooltipVerticalPosition = containerBottom - tooltipHeight ;
457
+ top = bottomBound + gap ;
458
+ result = 'bottom' ;
465
459
}
466
- }
467
-
468
- let horizontalPosition
469
-
470
- const containerLeft = containerEl . scrollLeft + 10 ;
471
- const containerRight = containerEl . scrollLeft + containerEl . clientWidth - 10 ;
472
-
473
- const tooltipWidth = tooltipEl . offsetWidth ;
474
-
475
- if ( horizontalAlignment === 'left' ) {
476
- horizontalPosition = originRect . left ;
477
460
} else {
478
- horizontalPosition = originRect . right - tooltipWidth ;
461
+ top = topBound - gap ;
462
+ result = 'top' ;
479
463
}
480
464
481
- horizontalPosition = Math . max ( horizontalPosition , containerLeft ) ;
482
- horizontalPosition = Math . min ( horizontalPosition , containerRight - tooltipWidth ) ;
465
+ let left = horizontalAlignment === 'left' ? targetPosition . left : targetPosition . right - tooltip . offsetWidth ;
466
+
467
+ if ( left < offsetParent . scrollLeft + 10 ) {
468
+ left = offsetParent . scrollLeft + 10 ;
469
+ } else if ( left + tooltip . offsetWidth > offsetParent . scrollLeft + offsetParent . clientWidth - 10 ) {
470
+ left = offsetParent . scrollLeft + offsetParent . clientWidth - tooltip . offsetWidth - 10 ;
471
+ }
483
472
484
- tooltipEl . style . top = `${ tooltipVerticalPosition } px` ;
485
- tooltipEl . style . left = `${ horizontalPosition } px` ;
473
+ tooltip . style . top = `${ top } px` ;
474
+ tooltip . style . left = `${ left } px` ;
486
475
487
- return {
488
- top : tooltipVerticalPosition ,
489
- left : horizontalPosition ,
490
- result
491
- } ;
476
+ return { top, left, result } ;
492
477
}
493
478
494
479
class SearchView {
@@ -518,7 +503,7 @@ class SearchView {
518
503
document . addEventListener ( "click" , this . onDocumentClick . bind ( this ) )
519
504
}
520
505
521
- addMessage ( text ) {
506
+ addMessage ( text ) {
522
507
const messageEl = document . createElement ( 'div' ) ;
523
508
messageEl . classList . add ( "search-message" ) ;
524
509
this . resultEl . appendChild ( messageEl ) ;
0 commit comments