« get me outta code hell

hoverable-tooltip.js « client « js « static « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static/js/client/hoverable-tooltip.js
blob: 484f2ab0631b8a8f4dec1ff665a985fed023be9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
/* eslint-env browser */

import {empty, filterMultipleArrays} from '../../shared-util/sugar.js';

import {WikiRect} from '../rectangles.js';

import {
  cssProp,
  dispatchInternalEvent,
  getVisuallyContainingElement,
  pointIsOverAnyOf,
} from '../client-util.js';

import {info as stickyHeadingInfo} from './sticky-heading.js';

export const info = {
  id: 'hoverableTooltipInfo',

  settings: {
    // Hovering has two speed settings. The normal setting is used by default,
    // and once a tooltip is displayed as a result of hover, the entire tooltip
    // system will enter a "fast hover mode" - hovering will activate tooltips
    // sooner. "Fast hover mode" is disabled after a sustained duration of not
    // hovering over any hoverables; it's meant only to accelerate switching
    // tooltips while still deciding, or getting a quick overview across more
    // than one tooltip.
    normalHoverInfoDelay: 400,
    fastHoveringInfoDelay: 150,
    endFastHoveringDelay: 500,

    // Focusing has a single speed setting, which is how long it will take to
    // enter a functional "focus mode" (though it's not actually implemented
    // in terms of this state). As soon as "focus mode" is entered, the tooltip
    // for the current hoverable is displayed, and focusing another hoverable
    // will cause the current tooltip to be swapped for that one immediately.
    // "Focus mode" ends as soon as anything apart from a tooltip or hoverable
    // is focused, and it will be necessary to wait on this delay again.
    focusInfoDelay: 750,

    hideTooltipDelay: 500,

    // If a tooltip that's transitioning to hidden is hovered during the grace
    // period (or the corresponding hoverable is hovered at any point in the
    // transition), it'll cancel out of this animation immediately.
    transitionHiddenDuration: 300,
    inertGracePeriod: 100,
  },

  state: {
    // These maps store a record for each registered element and related state
    // and registration info, if applicable.
    registeredTooltips: new Map(),
    registeredHoverables: new Map(),

    // These are common across all tooltips, rather than stored individually,
    // based on the principles that 1) only a single tooltip can be displayed
    // at once, and 2) likewise, only a single hoverable can be hovered,
    // focused, or otherwise active at once.
    hoverTimeout: null,
    focusTimeout: null,
    touchTimeout: null,
    hideTimeout: null,
    transitionHiddenTimeout: null,
    inertGracePeriodTimeout: null,
    currentlyShownTooltip: null,
    currentlyActiveHoverable: null,
    currentlyTransitioningHiddenTooltip: null,
    previouslyActiveHoverable: null,
    tooltipWasJustHidden: false,
    hoverableWasRecentlyTouched: false,

    // Fast hovering is a global mode which is activated as soon as any tooltip
    // is displayed and turns off after a delay of no hoverables being hovered.
    // Note that fast hovering may be turned off while hovering a tooltip, but
    // it will never be turned off while idling over a hoverable.
    fastHovering: false,
    endFastHoveringTimeout: false,

    // These track the identifiers of current touches and a record of current
    // identifiers that are "banished" by scrolling - that is, touches which
    // existed while the page scrolled and were probably responsible for that
    // scrolling. This is a bit loose (we can't actually tell which touches
    // caused the page to scroll) but it's intended to keep scrolling the page
    // from causing the current tooltip to be hidden.
    currentTouchIdentifiers: new Set(),
    touchIdentifiersBanishedByScrolling: new Set(),

    // This is a two-item array that tracks the direction we've already
    // dynamically placed the current tooltip. If we *reposition* the tooltip
    // (because its dimensions changed), we'll try to follow this anchor first.
    dynamicTooltipAnchorDirection: null,
  },

  event: {
    whenTooltipShows: [],
    whenTooltipHides: [],
  },
};

// Adds DOM event listeners, so must be called during addPageListeners step.
export function registerTooltipElement(tooltip) {
  const {state} = info;

  if (!tooltip)
    throw new Error(`Expected tooltip`);

  if (state.registeredTooltips.has(tooltip))
    throw new Error(`This tooltip is already registered`);

  // No state or registration info here.
  state.registeredTooltips.set(tooltip, {});

  tooltip.addEventListener('mouseenter', () => {
    handleTooltipMouseEntered(tooltip);
  });

  tooltip.addEventListener('mouseleave', () => {
    handleTooltipMouseLeft(tooltip);
  });

  tooltip.addEventListener('focusin', event => {
    handleTooltipReceivedFocus(tooltip, event.relatedTarget);
  });

  tooltip.addEventListener('focusout', event => {
    // This event gets activated for tabbing *between* links inside the
    // tooltip, which is no good and certainly doesn't represent the focus
    // leaving the tooltip.
    if (currentlyShownTooltipHasFocus(event.relatedTarget)) return;

    handleTooltipLostFocus(tooltip, event.relatedTarget);
  });
}

// Adds DOM event listeners, so must be called during addPageListeners step.
export function registerTooltipHoverableElement(hoverable, tooltip) {
  const {state} = info;

  if (!hoverable || !tooltip)
    if (hoverable)
      throw new Error(`Expected hoverable and tooltip, got only hoverable`);
    else
      throw new Error(`Expected hoverable and tooltip, got neither`);

  if (!state.registeredTooltips.has(tooltip))
    throw new Error(`Register tooltip before registering hoverable`);

  if (state.registeredHoverables.has(hoverable))
    throw new Error(`This hoverable is already registered`);

  state.registeredHoverables.set(hoverable, {tooltip});

  hoverable.addEventListener('mouseenter', () => {
    handleTooltipHoverableMouseEntered(hoverable);
  });

  hoverable.addEventListener('mouseleave', () => {
    handleTooltipHoverableMouseLeft(hoverable);
  });

  hoverable.addEventListener('focusin', event => {
    handleTooltipHoverableReceivedFocus(hoverable, event);
  });

  hoverable.addEventListener('focusout', event => {
    handleTooltipHoverableLostFocus(hoverable, event);
  });

  hoverable.addEventListener('touchend', event => {
    handleTooltipHoverableTouchEnded(hoverable, event);
  });

  hoverable.addEventListener('click', event => {
    handleTooltipHoverableClicked(hoverable, event);
  });
}

function handleTooltipMouseEntered(tooltip) {
  const {state} = info;

  if (state.currentlyTransitioningHiddenTooltip) {
    cancelTransitioningTooltipHidden(true);
    return;
  }

  if (state.currentlyShownTooltip !== tooltip) return;

  // Don't time out the current tooltip while hovering it.

  if (state.hideTimeout) {
    clearTimeout(state.hideTimeout);
    state.hideTimeout = null;
  }
}

function handleTooltipMouseLeft(tooltip) {
  const {settings, state} = info;

  if (state.currentlyShownTooltip !== tooltip) return;

  // Start timing out the current tooltip when it's left. This could be
  // canceled by mousing over a hoverable, or back over the tooltip again.
  if (!state.hideTimeout) {
    state.hideTimeout =
      setTimeout(() => {
        state.hideTimeout = null;
        hideCurrentlyShownTooltip();
      }, settings.hideTooltipDelay);
  }
}

function handleTooltipReceivedFocus(_tooltip) {
  const {state} = info;

  // Cancel the tooltip-hiding timeout if it exists. The tooltip will never
  // be hidden while it contains the focus anyway, but this ensures the timeout
  // will be suitably reset when the tooltip loses focus.
  if (state.hideTimeout) {
    clearTimeout(state.hideTimeout);
    state.hideTimeout = null;
  }
}

function handleTooltipLostFocus(_tooltip) {
  // Hide the current tooltip right away when it loses focus. Specify intent
  // to replace - while we don't strictly know if another tooltip is going to
  // immediately replace it, the mode of navigating with tab focus (once one
  // tooltip has been activated) is a "switch focus immediately" kind of
  // interaction in its nature.
  hideCurrentlyShownTooltip(true);
}

function handleTooltipHoverableMouseEntered(hoverable) {
  const {settings, state} = info;
  const {tooltip} = state.registeredHoverables.get(hoverable);

  // If this tooltip was transitioning to hidden, hovering should cancel that
  // animation and show it immediately.

  if (tooltip === state.currentlyTransitioningHiddenTooltip) {
    cancelTransitioningTooltipHidden(true);
    return;
  }

  // Start a timer to show the corresponding tooltip, with the delay depending
  // on whether fast hovering or not. This could be canceled by mousing out of
  // the hoverable.

  const hoverTimeoutDelay =
    (state.fastHovering
      ? settings.fastHoveringInfoDelay
      : settings.normalHoverInfoDelay);

  state.hoverTimeout =
    setTimeout(() => {
      state.hoverTimeout = null;
      state.fastHovering = true;
      showTooltipFromHoverable(hoverable);
    }, hoverTimeoutDelay);

  // Don't stop fast hovering while over any hoverable.
  if (state.endFastHoveringTimeout) {
    clearTimeout(state.endFastHoveringTimeout);
    state.endFastHoveringTimeout = null;
  }

  // Don't time out the current tooltip while over any hoverable.
  if (state.hideTimeout) {
    clearTimeout(state.hideTimeout);
    state.hideTimeout = null;
  }
}

function handleTooltipHoverableMouseLeft(_hoverable) {
  const {settings, state} = info;

  // Don't show a tooltip when not over a hoverable!
  if (state.hoverTimeout) {
    clearTimeout(state.hoverTimeout);
    state.hoverTimeout = null;
  }

  // Start timing out fast hovering (if active) when not over a hoverable.
  // This will only be canceled by mousing over another hoverable.
  if (state.fastHovering && !state.endFastHoveringTimeout) {
    state.endFastHoveringTimeout =
      setTimeout(() => {
        state.endFastHoveringTimeout = null;
        state.fastHovering = false;
      }, settings.endFastHoveringDelay);
  }

  // Start timing out the current tooltip when mousing not over a hoverable.
  // This could be canceled by mousing over another hoverable, or over the
  // currently shown tooltip.
  if (state.currentlyShownTooltip && !state.hideTimeout) {
    state.hideTimeout =
      setTimeout(() => {
        state.hideTimeout = null;
        hideCurrentlyShownTooltip();
      }, settings.hideTooltipDelay);
  }
}

function handleTooltipHoverableReceivedFocus(hoverable) {
  const {settings, state} = info;

  // By default, display the corresponding tooltip after a delay.

  state.focusTimeout =
    setTimeout(() => {
      state.focusTimeout = null;
      showTooltipFromHoverable(hoverable);
    }, settings.focusInfoDelay);

  // If a tooltip was just hidden - which is almost certainly a result of the
  // focus changing - then display this tooltip immediately, canceling the
  // above timeout.

  if (state.tooltipWasJustHidden) {
    clearTimeout(state.focusTimeout);
    state.focusTimeout = null;

    showTooltipFromHoverable(hoverable);
  }
}

function handleTooltipHoverableLostFocus(hoverable, domEvent) {
  const {state} = info;

  // Don't show a tooltip from focusing a hoverable if it isn't focused
  // anymore! If another hoverable is receiving focus, that will be evaluated
  // and set its own focus timeout after we clear the previous one here.
  if (state.focusTimeout) {
    clearTimeout(state.focusTimeout);
    state.focusTimeout = null;
  }

  // Unless focus is entering the tooltip itself, hide the tooltip immediately.
  // This will set the tooltipWasJustHidden flag, which is detected by a newly
  // focused hoverable, if applicable. Always specify intent to replace when
  // navigating via tab focus. (Check `handleTooltipLostFocus` for details.)
  if (!currentlyShownTooltipHasFocus(domEvent.relatedTarget)) {
    hideCurrentlyShownTooltip(true);
  }
}

function handleTooltipHoverableTouchEnded(hoverable, domEvent) {
  const {state} = info;
  const {tooltip} = state.registeredHoverables.get(hoverable);

  // Don't proceed if this hoverable's tooltip is already visible - in that
  // case touching the hoverable again should behave just like a normal click.
  if (state.currentlyShownTooltip === tooltip) {
    // If the hoverable was *recently* touched - meaning that this is a second
    // touchend in short succession - then just letting the click come through
    // naturally would (depending on timing) not actually navigate anywhere,
    // because we've deliberately banished the *first* touch from navigation.
    // We do want the second touch to navigate, so clear that recently-touched
    // state, allowing this touch's click to behave as normal.
    if (state.hoverableWasRecentlyTouched) {
      clearTimeout(state.touchTimeout);
      state.touchTimeout = null;
      state.hoverableWasRecentlyTouched = false;
    }

    // Otherwise, this is just a second touch after enough time has passed
    // that the one which showed the tooltip is no longer "recent", and we're
    // not in any special state. The link will navigate to its page just like
    // normal.
    return;
  }

  const touches = Array.from(domEvent.changedTouches);
  const identifiers = touches.map(touch => touch.identifier);

  // Don't process touch events that were "banished" because the page was
  // scrolled while those touches were active, and most likely as a result of
  // them.
  filterMultipleArrays(touches, identifiers,
    (_touch, identifier) =>
      !state.touchIdentifiersBanishedByScrolling.has(identifier));

  if (empty(touches)) return;

  // Don't proceed if none of the (just-ended) touches ended over the
  // hoverable.

  const pointIsOverThisHoverable = pointIsOverAnyOf([hoverable]);

  const anyTouchEndedOverHoverable =
    touches.some(({clientX, clientY}) =>
      pointIsOverThisHoverable(clientX, clientY));

  if (!anyTouchEndedOverHoverable) {
    return;
  }

  if (state.touchTimeout) {
    clearTimeout(state.touchTimeout);
    state.touchTimeout = null;
  }

  // Show the tooltip right away.
  showTooltipFromHoverable(hoverable);

  // Set a state, for a brief but not instantaneous period, indicating that a
  // hoverable was recently touched. The touchend event may precede the click
  // event by some time, and we don't want to navigate away from the page as
  // a result of the click event which this touch precipitated.
  state.hoverableWasRecentlyTouched = true;
  state.touchTimeout =
    setTimeout(() => {
      state.touchTimeout = null;
      state.hoverableWasRecentlyTouched = false;
    }, 1200);
}

function handleTooltipHoverableClicked(hoverable) {
  const {state} = info;

  // Don't navigate away from the page if the this hoverable was recently
  // touched (and had its tooltip activated). That flag won't be set if its
  // tooltip was already open before the touch.
  if (
    state.currentlyActiveHoverable === hoverable &&
    state.hoverableWasRecentlyTouched
  ) {
    event.preventDefault();
  }
}

export function currentlyShownTooltipHasFocus(focusElement = document.activeElement) {
  const {state} = info;

  const {
    currentlyShownTooltip: tooltip,
    currentlyActiveHoverable: hoverable,
  } = state;

  // If there's no tooltip, it can't possibly have focus.
  if (!tooltip) return false;

  // If the tooltip literally contains (or is) the focused element, then that's
  // the principle condition we're looking for.
  if (tooltip.contains(focusElement)) return true;

  // If the hoverable *which opened the tooltip* is focused, then that also
  // represents the tooltip being focused (in its currently shown state).
  if (hoverable.contains(focusElement)) return true;

  return false;
}

export function beginTransitioningTooltipHidden(tooltip) {
  const {settings, state} = info;

  if (state.currentlyTransitioningHiddenTooltip) {
    cancelTransitioningTooltipHidden();
  }

  cssProp(tooltip, {
    'display': 'block',
    'opacity': '0',

    'transition-property': 'opacity',
    'transition-timing-function':
      `steps(${Math.ceil(settings.transitionHiddenDuration / 60)}, end)`,
    'transition-duration':
      `${settings.transitionHiddenDuration / 1000}s`,
  });

  state.currentlyTransitioningHiddenTooltip = tooltip;
  state.transitionHiddenTimeout =
    setTimeout(() => {
      endTransitioningTooltipHidden();
    }, settings.transitionHiddenDuration);
}

export function cancelTransitioningTooltipHidden(andShow = false) {
  const {state} = info;

  endTransitioningTooltipHidden();

  if (andShow) {
    showTooltipFromHoverable(state.previouslyActiveHoverable);
  }
}

export function endTransitioningTooltipHidden() {
  const {state} = info;
  const {currentlyTransitioningHiddenTooltip: tooltip} = state;

  if (!tooltip) return;

  cssProp(tooltip, {
    'display': null,
    'opacity': null,
    'transition-property': null,
    'transition-timing-function': null,
    'transition-duration': null,
  });

  state.currentlyTransitioningHiddenTooltip = null;

  if (state.inertGracePeriodTimeout) {
    clearTimeout(state.inertGracePeriodTimeout);
    state.inertGracePeriodTimeout = null;
  }

  if (state.transitionHiddenTimeout) {
    clearTimeout(state.transitionHiddenTimeout);
    state.transitionHiddenTimeout = null;
  }
}

export function hideCurrentlyShownTooltip(intendingToReplace = false) {
  const {settings, state, event} = info;
  const {currentlyShownTooltip: tooltip} = state;

  // If there was no tooltip to begin with, we're functionally in the desired
  // state already, so return true.
  if (!tooltip) return true;

  // Never hide the tooltip if it's focused.
  if (currentlyShownTooltipHasFocus()) return false;

  state.currentlyActiveHoverable.classList.remove('has-visible-tooltip');

  // If there's no intent to replace this tooltip, it's the last one currently
  // apparent in the interaction, and should be hidden with a transition.
  if (intendingToReplace) {
    cssProp(tooltip, 'display', 'none');
  } else {
    beginTransitioningTooltipHidden(state.currentlyShownTooltip);
  }

  // Wait just a moment before making the tooltip inert. You might react
  // (to the ghosting, or just to time passing) and realize you wanted
  // to look at the tooltip after all - this delay gives a little buffer
  // to second guess letting it disappear.
  state.inertGracePeriodTimeout =
    setTimeout(() => {
      tooltip.inert = true;
    }, settings.inertGracePeriod);

  state.previouslyActiveHoverable = state.currentlyActiveHoverable;

  state.currentlyShownTooltip = null;
  state.currentlyActiveHoverable = null;

  state.dynamicTooltipAnchorDirection = null;

  // Set this for one tick of the event cycle.
  state.tooltipWasJustHidden = true;
  setTimeout(() => {
    state.tooltipWasJustHidden = false;
  });

  dispatchInternalEvent(event, 'whenTooltipHides', {
    tooltip,
  });

  return true;
}

export function showTooltipFromHoverable(hoverable) {
  const {state, event} = info;
  const {tooltip} = state.registeredHoverables.get(hoverable);

  if (!hideCurrentlyShownTooltip(true)) return false;

  // Cancel out another tooltip that's transitioning hidden, if that's going
  // on - it's a distraction that this tooltip is now replacing.
  cancelTransitioningTooltipHidden();

  hoverable.classList.add('has-visible-tooltip');

  positionTooltipFromHoverableWithBrains(hoverable);

  cssProp(tooltip, 'display', 'block');
  tooltip.inert = false;

  state.currentlyShownTooltip = tooltip;
  state.currentlyActiveHoverable = hoverable;

  state.tooltipWasJustHidden = false;

  dispatchInternalEvent(event, 'whenTooltipShows', {
    tooltip,
  });

  return true;
}

export function peekTooltipClientRect(tooltip) {
  const oldDisplayStyle = cssProp(tooltip, 'display');
  cssProp(tooltip, 'display', 'block');

  // Tooltips have a bit of padding that makes the interactive
  // area wider, so that you're less likely to accidentally let
  // the tooltip disappear (by hovering outside it). But this
  // isn't visual at all, so for placement we only care about
  // the content element.
  const content =
    tooltip.querySelector('.tooltip-content');

  try {
    return WikiRect.fromElement(content);
  } finally {
    cssProp(tooltip, 'display', oldDisplayStyle);
  }
}

export function repositionCurrentTooltip() {
  const {state} = info;
  const {currentlyActiveHoverable} = state;

  if (!currentlyActiveHoverable) {
    throw new Error(`No hoverable active to reposition tooltip from`);
  }

  positionTooltipFromHoverableWithBrains(currentlyActiveHoverable);
}

export function positionTooltipFromHoverableWithBrains(hoverable) {
  const {state} = info;
  const {tooltip} = state.registeredHoverables.get(hoverable);

  const anchorDirection = state.dynamicTooltipAnchorDirection;

  // Reset before doing anything else. We're going to adapt to
  // its natural placement, adjusted by CSS, which otherwise
  // could be obscured by a placement we've previously provided.
  resetDynamicTooltipPositioning(tooltip);

  const opportunities =
    getTooltipFromHoverablePlacementOpportunityAreas(hoverable);

  const tooltipRect =
    peekTooltipClientRect(tooltip);

  // If the tooltip is already in the baseline containing area,
  // prefer to keep it positioned naturally, adjusted by CSS
  // instead of JavaScript.

  const {numBaselineRects, idealBaseline: baselineRect} = opportunities;

  if (baselineRect.contains(tooltipRect)) {
    return;
  }

  const tryDirection = (dir1, dir2, i) => {
    selectedRect = opportunities[dir1][dir2][i];
    return !!selectedRect;
  };

  let selectedRect = null;
  selectRect: {
    if (anchorDirection) {
      for (let i = 0; i < numBaselineRects; i++) {
        if (tryDirection(...anchorDirection, i)) {
          break selectRect;
        }
      }
    }

    for (let i = 0; i < numBaselineRects; i++) {
      for (const [dir1, dir2] of [
        ['right', 'down'],
        ['left', 'down'],
        ['right', 'up'],
        ['left', 'up'],
        ['down', 'right'],
        ['down', 'left'],
        ['up', 'right'],
        ['up', 'left'],
      ]) {
        if (tryDirection(dir1, dir2, i)) {
          state.dynamicTooltipAnchorDirection = [dir1, dir2];
          break selectRect;
        }
      }
    }

    selectedRect = baselineRect;
  }

  positionTooltip(tooltip, selectedRect.x, selectedRect.y);
}

export function positionTooltip(tooltip, x, y) {
  // Imagine what it'd be like if the tooltip were positioned
  // with zero left/top offset, and calculate its actual offsets
  // based on that.

  cssProp(tooltip, {
    left: `0`,
    top: `0`,
  });

  const tooltipRect =
    peekTooltipClientRect(tooltip);

  cssProp(tooltip, {
    left: `${x - tooltipRect.x}px`,
    top: `${y - tooltipRect.y}px`,
  });
}

export function resetDynamicTooltipPositioning(tooltip) {
  cssProp(tooltip, {
    left: null,
    top: null,
  });
}

export function getTooltipFromHoverablePlacementOpportunityAreas(hoverable) {
  const {state} = info;
  const {tooltip} = state.registeredHoverables.get(hoverable);

  const baselineRects =
    getTooltipBaselineOpportunityAreas(tooltip);

  const hoverableRect =
    WikiRect.fromElementUnderMouse(hoverable).toExtended(5, 10);

  const tooltipRect =
    peekTooltipClientRect(tooltip);

  // Get placements relative to the hoverable. Make these available by key,
  // allowing the caller to choose by preferred orientation. Each value is
  // an array which corresponds to the baseline areas - placement closer to
  // front of the array indicates stronger preference. Since not all relative
  // placements cooperate with all baseline areas, any of these arrays may
  // include (or be entirely made of) null.

  const keepIfFits = (rect) =>
    (rect?.fits(tooltipRect)
      ? rect
      : null);

  const prepareRegionRects = (relationalRect, direct) =>
    baselineRects
      .map(rect => rect.intersectionWith(relationalRect))
      .map(direct)
      .map(keepIfFits);

  const regionRects = {
    left:
      prepareRegionRects(
        WikiRect.leftOf(hoverableRect),
        rect => WikiRect.fromRect({
          x: rect.right,
          y: rect.y,
          width: -rect.width,
          height: rect.height,
        })),

    right:
      prepareRegionRects(
        WikiRect.rightOf(hoverableRect),
        rect => rect),

    top:
      prepareRegionRects(
        WikiRect.above(hoverableRect),
        rect => WikiRect.fromRect({
          x: rect.x,
          y: rect.bottom,
          width: rect.width,
          height: -rect.height,
        })),

    bottom:
      prepareRegionRects(
        WikiRect.beneath(hoverableRect),
        rect => rect),
  };

  const neededVerticalOverlap = 30;
  const neededHorizontalOverlap = 30;

  const upTopDown =
    WikiRect.beneath(
      hoverableRect.top + neededVerticalOverlap - tooltipRect.height);

  const downBottomUp =
    WikiRect.above(
      hoverableRect.bottom - neededVerticalOverlap + tooltipRect.height);

  // Please don't ask us to make this but horizontal?
  const prepareVerticalOrientationRects = (regionRects) => {
    const orientations = {};

    const orientHorizontally = (rect, i) => {
      if (!rect) return null;

      const regionRect = regionRects[i];
      if (regionRect.width > 0) {
        return rect;
      } else {
        return WikiRect.fromRect({
          x: regionRect.right - tooltipRect.width,
          y: rect.y,
          width: rect.width,
          height: rect.height,
        });
      }
    };

    orientations.up =
      regionRects
        .map(rect => rect?.intersectionWith(upTopDown))
        .map(orientHorizontally)
        .map(keepIfFits);

    orientations.down =
      regionRects
        .map(rect => rect?.intersectionWith(downBottomUp))
        .map(rect =>
          (rect
            ? rect.intersectionWith(WikiRect.fromRect({
                x: rect.x,
                y: rect.bottom - tooltipRect.height,
                width: rect.width,
                height: tooltipRect.height,
              }))
            : null))
        .map(orientHorizontally)
        .map(keepIfFits);

    const centerRect =
      WikiRect.fromRect({
        x: -Infinity, width: Infinity,
        y: hoverableRect.top
         + hoverableRect.height / 2
         - tooltipRect.height / 2,
        height: tooltipRect.height,
      });

    orientations.center =
      regionRects
        .map(rect => rect?.intersectionWith(centerRect))
        .map(orientHorizontally)
        .map(keepIfFits);

    return orientations;
  };

  const rightRightLeft =
    WikiRect.leftOf(
      hoverableRect.left - neededHorizontalOverlap + tooltipRect.width);

  const leftLeftRight =
    WikiRect.rightOf(
      hoverableRect.left + neededHorizontalOverlap - tooltipRect.width);

  // Oops.
  const prepareHorizontalOrientationRects = (regionRects) => {
    const orientations = {};

    const orientVertically = (rect, i) => {
      if (!rect) return null;

      const regionRect = regionRects[i];

      if (regionRect.height > 0) {
        return rect;
      } else {
        return WikiRect.fromRect({
          x: rect.x,
          y: regionRect.bottom - tooltipRect.height,
          width: rect.width,
          height: rect.height,
        });
      }
    };

    orientations.left =
      regionRects
        .map(rect => rect?.intersectionWith(leftLeftRight))
        .map(orientVertically)
        .map(keepIfFits);

    orientations.right =
      regionRects
        .map(rect => rect?.intersectionWith(rightRightLeft))
        .map(rect =>
          (rect
            ? rect.intersectionWith(WikiRect.fromRect({
                x: rect.right - tooltipRect.width,
                y: rect.y,
                width: rect.width,
                height: tooltipRect.height,
              }))
            : null))
        .map(orientVertically)
        .map(keepIfFits);

    // No analogous center because we don't actually use
    // center alignment...

    return orientations;
  };

  const orientationRects = {
    left: prepareVerticalOrientationRects(regionRects.left),
    right: prepareVerticalOrientationRects(regionRects.right),
    down: prepareHorizontalOrientationRects(regionRects.bottom),
    up: prepareHorizontalOrientationRects(regionRects.top),
  };

  return {
    numBaselineRects: baselineRects.length,
    idealBaseline: baselineRects[0],
    ...orientationRects,
  };
}

export function getTooltipBaselineOpportunityAreas(tooltip) {
  // Returns multiple basic areas in order of preference, with front of the
  // array representing greater preference.

  const {stickyContainers} = stickyHeadingInfo;
  const results = [];

  const windowRect =
    WikiRect.fromWindow().toInset(10);

  const workingRect =
    WikiRect.fromRect(windowRect);

  const tooltipRect =
    peekTooltipClientRect(tooltip);

  // As a baseline, always treat the window rect as fitting the tooltip.
  results.unshift(WikiRect.fromRect(workingRect));

  const containingParent =
    getVisuallyContainingElement(tooltip);

  if (containingParent) {
    const containingRect =
      WikiRect.fromElement(containingParent);

    // Only respect a portion of the container's padding, giving
    // the tooltip the impression of a "raised" element.
    const padding = side =>
      0.5 *
      parseFloat(cssProp(containingParent, 'padding-' + side));

    const insetContainingRect =
      containingRect.toInset({
        left: padding('left'),
        right: padding('right'),
        top: padding('top'),
        bottom: padding('bottom'),
      });

    workingRect.chopExtendingOutside(insetContainingRect);

    if (!workingRect.fits(tooltipRect)) {
      return results;
    }

    results.unshift(WikiRect.fromRect(workingRect));
  }

  // This currently assumes a maximum of one sticky container
  // per visually containing element.

  const stickyContainer =
    stickyContainers
      .find(el => el.parentElement === containingParent);

  if (stickyContainer) {
    const stickyRect =
      stickyContainer.getBoundingClientRect()

    // Add some padding so the tooltip doesn't line up exactly
    // with the edge of the sticky container.
    const beneathStickyContainer =
      WikiRect.beneath(stickyRect, 10);

    workingRect.chopExtendingOutside(beneathStickyContainer);

    if (!workingRect.fits(tooltipRect)) {
      return results;
    }

    results.unshift(WikiRect.fromRect(workingRect));
  }

  return results;
}

export function addPageListeners() {
  const {state} = info;

  const getTouchIdentifiers = domEvent =>
    Array.from(domEvent.changedTouches)
      .map(touch => touch.identifier)
      .filter(identifier => typeof identifier !== 'undefined');

  document.body.addEventListener('touchstart', domEvent => {
    for (const identifier of getTouchIdentifiers(domEvent)) {
      state.currentTouchIdentifiers.add(identifier);
    }
  });

  window.addEventListener('scroll', () => {
    for (const identifier of state.currentTouchIdentifiers) {
      state.touchIdentifiersBanishedByScrolling.add(identifier);
    }
  });

  document.body.addEventListener('touchend', domEvent => {
    setTimeout(() => {
      for (const identifier of getTouchIdentifiers(domEvent)) {
        state.currentTouchIdentifiers.delete(identifier);
        state.touchIdentifiersBanishedByScrolling.delete(identifier);
      }
    });
  });

  const getHoverablesAndTooltips = () => [
    ...Array.from(state.registeredHoverables.keys()),
    ...Array.from(state.registeredTooltips.keys()),
  ];

  document.body.addEventListener('touchend', domEvent => {
    const touches = Array.from(domEvent.changedTouches);
    const identifiers = touches.map(touch => touch.identifier);

    // Don't process touch events that were "banished" because the page was
    // scrolled while those touches were active, and most likely as a result of
    // them.
    filterMultipleArrays(touches, identifiers,
      (_touch, identifier) =>
        !state.touchIdentifiersBanishedByScrolling.has(identifier));

    if (empty(touches)) return;

    const pointIsOverHoverableOrTooltip =
      pointIsOverAnyOf(getHoverablesAndTooltips());

    const anyTouchOverAnyHoverableOrTooltip =
      touches.some(({clientX, clientY}) =>
        pointIsOverHoverableOrTooltip(clientX, clientY));

    if (!anyTouchOverAnyHoverableOrTooltip) {
      hideCurrentlyShownTooltip();
    }
  });

  document.body.addEventListener('click', domEvent => {
    const {clientX, clientY} = domEvent;

    const pointIsOverHoverableOrTooltip =
      pointIsOverAnyOf(getHoverablesAndTooltips());

    if (!pointIsOverHoverableOrTooltip(clientX, clientY)) {
      // Hide with "intent to replace" - we aren't actually going to replace
      // the tooltip with a new one, but this intent indicates that it should
      // be hidden right away, instead of showing. What we're really replacing,
      // or rather removing, is the state of interacting with tooltips at all.
      hideCurrentlyShownTooltip(true);

      // Part of that state is fast hovering, which should be canceled out.
      state.fastHovering = false;
      if (state.endFastHoveringTimeout) {
        clearTimeout(state.endFastHoveringTimeout);
        state.endFastHoveringTimeout = null;
      }

      // Also cancel out of transitioning a tooltip hidden - this isn't caught
      // by `hideCurrentlyShownTooltip` because a transitioning-hidden tooltip
      // doesn't count as "shown" anymore.
      cancelTransitioningTooltipHidden();
    }
  });
}